lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit 976ced53a9d041e4bca84e260334a59f39cf5e59
parent dbab7a62eed99f153c9b2c32e278a6c9d7b482c2
Author: triesap <tyson@radroots.org>
Date:   Fri, 23 Oct 2026 03:06:19 +0000

protocol: Require canonical public keys at profile boundaries

- Compare decoded profile keys with the identity-owned canonical encoder.
- Reject uppercase aliases in actor, chat, listing and private Terms input.
- Preserve general identity parsing and all valid canonical bytes and digests.
- Verify the reproduced regression, shared runtime, APIs and unchanged coverage gates.

Diffstat:
Mcontracts/protocol/terms.v1.json | 1+
Mcrates/domain/src/context.rs | 27++++++++++++++++++++++++++-
Mcrates/domain/tests/context.rs | 24++++++++++++++++++++++++
Mcrates/protocol/README.md | 4++++
Mcrates/protocol/src/command.rs | 28++++++++++++++++++++++++++++
Mcrates/protocol/src/listing_values.rs | 2+-
Mcrates/protocol/src/terms/decode.rs | 3+++
Mcrates/protocol/tests/command.rs | 47+++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/protocol/tests/public_command.rs | 23+++++++++++++++++++++++
Mcrates/protocol/tests/terms_decode.rs | 28++++++++++++++++++++++++++++
10 files changed, 185 insertions(+), 2 deletions(-)

diff --git a/contracts/protocol/terms.v1.json b/contracts/protocol/terms.v1.json @@ -41,6 +41,7 @@ "objects_only": true, "unknown_duplicate_and_missing_fields_rejected": true, "nullable_fields_must_be_present": true, + "public_keys_require_identity_owned_canonical_lowercase_spelling": true, "private_fields_borrow_original_raw_json_until_protected_decoding": true, "private_text_decoded_scratch": "Two-pass count then zeroizing UTF-8 output; explicit JSON escapes and paired UTF-16 surrogates, with no normalization.", "domain_validation": "Every complete line, group, money, price, window, supply commitment and Terms constructor runs; no missing field or snapshot is synthesized.", diff --git a/crates/domain/src/context.rs b/crates/domain/src/context.rs @@ -473,6 +473,30 @@ mod serialization { use super::*; use serde::{Deserialize, Deserializer, Serializer, de::Error}; + fn canonical_key<'de, D: Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> { + struct KeyVisitor; + impl serde::de::Visitor<'_> for KeyVisitor { + type Value = PublicKey; + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("a canonical public key") + } + fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<PublicKey, E> { + let key = + PublicKey::from_hex(value).map_err(|_| E::custom("invalid public key"))?; + if key.to_hex() != value { + return Err(E::custom("noncanonical public key")); + } + Ok(key) + } + } + d.deserialize_str(KeyVisitor) + } + fn nullable_key<'de, D: Deserializer<'de>>(d: D) -> Result<Option<PublicKey>, D::Error> { + #[derive(Deserialize)] + struct Key(#[serde(deserialize_with = "canonical_key")] PublicKey); + Option::<Key>::deserialize(d).map(|value| value.map(|key| key.0)) + } + fn object<'de, D: Deserializer<'de>, T: Deserialize<'de>>(d: D) -> Result<T, D::Error> { struct ObjectVisitor<T>(core::marker::PhantomData<T>); impl<'de, T: Deserialize<'de>> serde::de::Visitor<'de> for ObjectVisitor<T> { @@ -519,9 +543,10 @@ mod serialization { #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct Raw { + #[serde(deserialize_with = "canonical_key")] participant: PublicKey, role: ActorRole, - #[serde(deserialize_with = "nullable")] + #[serde(deserialize_with = "nullable_key")] organization: Option<PublicKey>, #[serde(deserialize_with = "nullable")] grant_id: Option<GrantId>, diff --git a/crates/domain/tests/context.rs b/crates/domain/tests/context.rs @@ -42,6 +42,30 @@ fn context() -> CommandContextData { #[cfg(feature = "serde")] #[test] +fn profile_keys_require_canonical_spelling_in_direct_actor_and_context_decoding() { + let uppercase = KEY.to_ascii_uppercase(); + assert!(PublicKey::from_hex(&uppercase).is_ok()); + let mut descriptor = serde_json::to_value(actor()).unwrap(); + descriptor["organization"] = json!(KEY); + assert!(serde_json::from_value::<ActorRef>(descriptor.clone()).is_ok()); + for field in ["participant", "organization"] { + let mut altered = descriptor.clone(); + altered[field] = json!(uppercase); + assert!( + serde_json::from_value::<ActorRef>(altered.clone()).is_err(), + "{field}" + ); + let mut command = serde_json::to_value(context()).unwrap(); + command["actor"] = altered; + assert!( + serde_json::from_value::<CommandContextData>(command).is_err(), + "context {field}" + ); + } +} + +#[cfg(feature = "serde")] +#[test] fn context_descriptors_require_objects_when_decoded_directly() { fn check<T>(value: T, fields: &[&str]) where diff --git a/crates/protocol/README.md b/crates/protocol/README.md @@ -96,6 +96,10 @@ Private text is borrowed from the caller's original frame until explicitly decoded into zeroizing buffers; only public snapshots enter temporary JSON trees. Decoding a claim establishes no signature, consent or current source availability. Callers retain responsibility for protecting original bytes. +Profile public keys must match the identity library's canonical lowercase +spelling. The general identity parser remains case insensitive; profile input +does not silently normalize an uppercase participant, organization, peer, +listing author or product producer. The listing producer emits one canonical `radroots.listing` version-1 profile, explicit default units and no fabricated price. Paused/withdrawn projections diff --git a/crates/protocol/src/command.rs b/crates/protocol/src/command.rs @@ -453,6 +453,31 @@ pub(crate) fn required_nullable<'de, D: serde::Deserializer<'de>, T: Deserialize ) -> Result<Option<T>, D::Error> { Option::deserialize(d) } +pub(crate) fn canonical_key<'de, D: serde::Deserializer<'de>>(d: D) -> Result<PublicKey, D::Error> { + struct KeyVisitor; + impl serde::de::Visitor<'_> for KeyVisitor { + type Value = PublicKey; + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("a canonical public key") + } + fn visit_str<E: serde::de::Error>(self, value: &str) -> Result<PublicKey, E> { + let key = PublicKey::from_hex(value).map_err(|_| E::custom("invalid public key"))?; + if key.to_hex() != value { + return Err(E::custom("noncanonical public key")); + } + Ok(key) + } + } + d.deserialize_str(KeyVisitor) +} +#[cfg(any(feature = "listing", feature = "terms"))] +pub(crate) fn nullable_canonical_key<'de, D: serde::Deserializer<'de>>( + d: D, +) -> Result<Option<PublicKey>, D::Error> { + #[derive(Deserialize)] + struct Key(#[serde(deserialize_with = "canonical_key")] PublicKey); + Option::<Key>::deserialize(d).map(|value| value.map(|key| key.0)) +} fn private_text<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Zeroizing<String>, D::Error> { String::deserialize(d).map(Zeroizing::new) } @@ -460,6 +485,7 @@ fn private_text<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Zeroizing<Stri #[serde(deny_unknown_fields)] struct RawInquiry { thread_id: ThreadId, + #[serde(deserialize_with = "canonical_key")] peer: PublicKey, #[serde(deserialize_with = "required_nullable")] source: Option<MapOnly<RawSource>>, @@ -470,6 +496,7 @@ struct RawInquiry { #[serde(deny_unknown_fields)] struct RawReply { thread_id: ThreadId, + #[serde(deserialize_with = "canonical_key")] peer: PublicKey, parent: MessageId, #[serde(deserialize_with = "private_text")] @@ -486,6 +513,7 @@ struct RawSource { #[serde(deny_unknown_fields)] struct RawCoordinate { kind: u32, + #[serde(deserialize_with = "canonical_key")] author: PublicKey, d: String, } diff --git a/crates/protocol/src/listing_values.rs b/crates/protocol/src/listing_values.rs @@ -19,7 +19,7 @@ fn invalid<T, E>(value: Result<T, E>) -> Result<T, CommandError> { pub(crate) struct RawProduct { name: String, description: String, - #[serde(deserialize_with = "required_nullable")] + #[serde(deserialize_with = "crate::command::nullable_canonical_key")] producer: Option<PublicKey>, #[serde(deserialize_with = "required_nullable")] origin_summary: Option<String>, diff --git a/crates/protocol/src/terms/decode.rs b/crates/protocol/src/terms/decode.rs @@ -101,7 +101,9 @@ impl<'de: 'a, 'a, const N: usize> Deserialize<'de> for RawArray<'a, N> { #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct RawTerms<'a> { + #[serde(deserialize_with = "crate::command::canonical_key")] buyer: PublicKey, + #[serde(deserialize_with = "crate::command::canonical_key")] seller: PublicKey, #[serde(borrow)] lines: RawArray<'a, 32>, @@ -166,6 +168,7 @@ struct RawSource { #[serde(deny_unknown_fields)] struct RawCoordinate { kind: u16, + #[serde(deserialize_with = "crate::command::canonical_key")] author: PublicKey, d: ListingId, } diff --git a/crates/protocol/tests/command.rs b/crates/protocol/tests/command.rs @@ -53,6 +53,53 @@ fn inquiry( } #[test] +fn profile_keys_require_canonical_spelling_at_all_chat_command_entry_points() { + use radroots_protocol::command::CapturedCommand; + let mut base: Value = + serde_json::from_slice(inquiry(None, None, "private").protected_wire()).unwrap(); + base["context"]["actor"]["organization"] = json!(alice().to_hex()); + base["input"]["source"] = json!({"coordinate":{"kind":30402,"author":alice().to_hex(),"d":"01".repeat(16)},"event_id":"03".repeat(32)}); + assert!(CapturedChatCommand::parse(&serde_json::to_vec(&base).unwrap()).is_ok()); + for path in [ + "/context/actor/participant", + "/context/actor/organization", + "/input/peer", + "/input/source/coordinate/author", + ] { + let mut altered = base.clone(); + let uppercase = altered + .pointer(path) + .unwrap() + .as_str() + .unwrap() + .to_ascii_uppercase(); + *altered.pointer_mut(path).unwrap() = json!(uppercase); + let wire = serde_json::to_vec(&altered).unwrap(); + assert!(CapturedChatCommand::parse(&wire).is_err(), "{path}"); + assert!(CapturedCommand::parse(&wire).is_err(), "dispatcher {path}"); + } + let mut reply: Value = serde_json::from_slice( + CapturedChatCommand::reply( + context(None, None), + ReplyInput::new( + ThreadId::from_bytes([6; 32]), + bob(), + MessageId::from_bytes([7; 32]), + "private", + ) + .unwrap(), + ) + .unwrap() + .protected_wire(), + ) + .unwrap(); + reply["input"]["peer"] = json!(bob().to_hex().to_ascii_uppercase()); + let wire = serde_json::to_vec(&reply).unwrap(); + assert!(CapturedChatCommand::parse(&wire).is_err()); + assert!(CapturedCommand::parse(&wire).is_err()); +} + +#[test] fn closed_commands_require_objects_at_every_context_and_inquiry_boundary() { use radroots_protocol::command::CapturedCommand; diff --git a/crates/protocol/tests/public_command.rs b/crates/protocol/tests/public_command.rs @@ -35,6 +35,29 @@ fn input() -> Value { } #[test] +fn profile_keys_require_canonical_spelling_in_public_input_and_command_dispatch() { + let mut public = input(); + public["profile"]["product"]["producer"] = json!(key().to_hex()); + let parsed = PublishInput::parse(&serde_json::to_vec(&public).unwrap()).unwrap(); + let captured = CapturedListingCommand::capture(context(), parsed).unwrap(); + let mut base: Value = serde_json::from_slice(captured.protected_wire()).unwrap(); + base["context"]["actor"]["organization"] = json!(key().to_hex()); + for path in [ + "/context/actor/participant", + "/context/actor/organization", + "/input/profile/product/producer", + ] { + let mut altered = base.clone(); + *altered.pointer_mut(path).unwrap() = json!(key().to_hex().to_ascii_uppercase()); + let wire = serde_json::to_vec(&altered).unwrap(); + assert!(CapturedListingCommand::parse(&wire).is_err(), "{path}"); + assert!(CapturedCommand::parse(&wire).is_err(), "dispatcher {path}"); + } + public["profile"]["product"]["producer"] = json!(key().to_hex().to_ascii_uppercase()); + assert!(PublishInput::parse(&serde_json::to_vec(&public).unwrap()).is_err()); +} + +#[test] fn public_command_root_and_context_reject_positional_arrays() { let command = CapturedListingCommand::capture( context(), diff --git a/crates/protocol/tests/terms_decode.rs b/crates/protocol/tests/terms_decode.rs @@ -5,6 +5,34 @@ use serde_json::{Value, json}; fn fixtures() -> Value { serde_json::from_str(include_str!("fixtures/terms_wire.v1.json")).unwrap() } + +#[test] +fn profile_keys_require_the_approved_lowercase_spelling() { + let original = example(0); + let mut accepted = Vec::new(); + for path in [ + "/buyer", + "/seller", + "/lines/0/source/coordinate/author", + "/lines/0/product/producer", + ] { + let mut altered = original.clone(); + let key = altered + .pointer(path) + .unwrap() + .as_str() + .unwrap() + .to_ascii_uppercase(); + *altered.pointer_mut(path).unwrap() = json!(key); + if decode(&bytes(&altered)).is_ok() { + accepted.push(path); + } + } + assert!( + accepted.is_empty(), + "noncanonical profile keys accepted: {accepted:?}" + ); +} fn example(index: usize) -> Value { serde_json::from_str(fixtures()["vectors"][index]["wire"].as_str().unwrap()).unwrap() }