Repository navigation
id(directory_auth): refuse out-of-domain nonces in sign_request (#6443) #6539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # sign_request nonce domain — fallible signing contract (2026-11-08) | ||
|
|
||
| Issue: developing-today/code#6443 · PR: developing-today/code#6539 | ||
|
|
||
| ## Problem | ||
|
|
||
| `verify_signed` refuses any signed request whose nonce is empty, longer | ||
| than 64 characters, or contains a character other than an ASCII letter, | ||
| digit or '-'. `sign_request` used to sign any nonce unconditionally, so a | ||
| client could produce a well-formed signature that verification would | ||
| always reject as "signature does not verify" — a failure indistinguishable | ||
| from tampering, caused entirely by the signer's own output. | ||
|
|
||
| ## Change | ||
|
|
||
| `sign_request` in `pkgs/id/src/directory_auth.rs` now validates the nonce | ||
| against the same domain before signing and returns | ||
| `Result<Signed>` instead of `Signed`: | ||
|
|
||
| - Allowed nonce domain: 1–64 characters, each an ASCII letter, digit | ||
| or '-'. This is exactly the domain `verify_signed` accepts; the check | ||
| lives in one helper (`nonce_in_domain`) used by both sides, so signer | ||
| and verifier cannot drift apart. | ||
| - Rejection behaviour: an out-of-domain nonce fails with an error naming | ||
| the nonce domain requirement; no signature is produced. | ||
| - Caller effect: this is a breaking change to a public API. Callers must | ||
| handle the `Err` (in-repo callers were updated to `.unwrap()` only in | ||
| tests, where the nonces are fixed in-domain literals). A caller passing | ||
| a generated nonce should generate inside the domain (for example hex | ||
| or a UUID) rather than recover from the error after the fact. | ||
|
|
||
| ## Tests | ||
|
|
||
| - Example tests cover the boundary and bad-character cases (empty, | ||
| 65 characters, space, underscore, plus, dot, non-ASCII; valid: short, | ||
| mixed-case with '-', exactly 64 characters) and round-trip the valid | ||
| ones through `verify_signed`. | ||
| - A `proptest` property checks, for generated nonces, that | ||
| `sign_request` succeeds exactly when an independent oracle predicate | ||
| for the domain accepts, and that accepted nonces verify. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -299,23 +299,40 @@ pub fn signed_message(method: &str, target: &str, at: u64, nonce: &str, body: &[ | |
| ) | ||
| } | ||
|
|
||
| /// Whether `nonce` is one `verify_signed` can accept: 1-64 ASCII | ||
| /// alphanumeric characters or '-'. | ||
| fn nonce_in_domain(nonce: &str) -> bool { | ||
| !nonce.is_empty() | ||
| && nonce.len() <= 64 | ||
| && nonce.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') | ||
| } | ||
|
|
||
| /// Sign a request as a client holding `key` would. | ||
| #[must_use] | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Fails if `nonce` is empty, longer than 64 characters, or has a character | ||
| /// other than an ASCII letter, digit or '-': `verify_signed` refuses every | ||
| /// such nonce, so no signature is produced for one. | ||
| pub fn sign_request( | ||
| key: &SigningKey, | ||
| method: &str, | ||
| target: &str, | ||
| body: &[u8], | ||
| at: u64, | ||
| nonce: &str, | ||
| ) -> Signed { | ||
| ) -> Result<Signed> { | ||
| anyhow::ensure!( | ||
| nonce_in_domain(nonce), | ||
| "nonce must be 1-64 ASCII letters, digits or '-'" | ||
| ); | ||
| let message = signed_message(method, target, at, nonce, body); | ||
| Signed { | ||
| Ok(Signed { | ||
| key: hex_encode(&key.verifying_key().to_bytes()), | ||
| at, | ||
| nonce: nonce.to_owned(), | ||
| signature: hex_encode(&key.sign(message.as_bytes()).to_bytes()), | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// Check a signed request and return the key that made it. | ||
|
|
@@ -345,13 +362,7 @@ pub fn verify_signed( | |
| ) | ||
| .into()); | ||
| } | ||
| if signed.nonce.is_empty() | ||
| || signed.nonce.len() > 64 | ||
| || !signed | ||
| .nonce | ||
| .chars() | ||
| .all(|c| c.is_ascii_alphanumeric() || c == '-') | ||
| { | ||
| if !nonce_in_domain(&signed.nonce) { | ||
| return Err(refused()); | ||
| } | ||
| let key = normalize_key(&signed.key).ok().ok_or_else(refused)?; | ||
|
|
@@ -525,7 +536,8 @@ mod tests { | |
| b"", | ||
| now(), | ||
| "n1", | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
| let mut replays = ReplayGuard::default(); | ||
| let who = verify_signed( | ||
| &mut replays, | ||
|
|
@@ -555,7 +567,7 @@ mod tests { | |
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let at = now(); | ||
| let target = "/api/world/directory?world=x"; | ||
| let signed = sign_request(&key, "GET", target, b"", at, "edge"); | ||
| let signed = sign_request(&key, "GET", target, b"", at, "edge").unwrap(); | ||
| let mut replays = ReplayGuard::default(); | ||
|
|
||
| assert!(verify_signed(&mut replays, &signed, "GET", target, b"", at).is_ok()); | ||
|
|
@@ -587,7 +599,8 @@ mod tests { | |
| #[test] | ||
| fn a_signed_request_is_bound_to_its_method_target_body_and_time() { | ||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let signed = sign_request(&key, "POST", "/api/world/directory", b"{}", now(), "n2"); | ||
| let signed = | ||
| sign_request(&key, "POST", "/api/world/directory", b"{}", now(), "n2").unwrap(); | ||
| for (method, target, body, at) in [ | ||
| ("GET", "/api/world/directory", b"{}".as_slice(), now()), | ||
| ("POST", "/api/world/other", b"{}".as_slice(), now()), | ||
|
|
@@ -621,20 +634,20 @@ mod tests { | |
| let mut replays = ReplayGuard::after_boot(boot, window); | ||
| let target = "/api/world/directory"; | ||
|
|
||
| let early = sign_request(&key, "GET", target, b"", boot + 599_000, "b1"); | ||
| let early = sign_request(&key, "GET", target, b"", boot + 599_000, "b1").unwrap(); | ||
| let error = | ||
| verify_signed(&mut replays, &early, "GET", target, b"", boot + 599_000).unwrap_err(); | ||
| assert!(boot_refused(&error), "{error:#}"); | ||
|
|
||
| let late = sign_request(&key, "GET", target, b"", boot + 600_000, "b2"); | ||
| let late = sign_request(&key, "GET", target, b"", boot + 600_000, "b2").unwrap(); | ||
| assert!(verify_signed(&mut replays, &late, "GET", target, b"", boot + 600_000).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_request_captured_before_a_restart_is_refused_during_the_boot_window() { | ||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let boot = now(); | ||
| let captured = sign_request(&key, "GET", "/x", b"", boot - 60_000, "old"); | ||
| let captured = sign_request(&key, "GET", "/x", b"", boot - 60_000, "old").unwrap(); | ||
| let mut replays = ReplayGuard::after_boot(boot, Duration::from_mins(10)); | ||
| let error = | ||
| verify_signed(&mut replays, &captured, "GET", "/x", b"", boot + 60_000).unwrap_err(); | ||
|
|
@@ -645,10 +658,65 @@ mod tests { | |
| fn a_zero_boot_window_refuses_nothing() { | ||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let mut replays = ReplayGuard::after_boot(now(), Duration::ZERO); | ||
| let signed = sign_request(&key, "GET", "/x", b"", now(), "z1"); | ||
| let signed = sign_request(&key, "GET", "/x", b"", now(), "z1").unwrap(); | ||
| assert!(verify_signed(&mut replays, &signed, "GET", "/x", b"", now()).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sign_request_refuses_nonces_verify_signed_would_reject() { | ||
|
Comment on lines
+665
to
+666
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new signer validation establishes a crypto-format invariant, but its coverage consists only of seven invalid and three valid examples. Root AGENTS.md reference: AGENTS.md:L41-L45 Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 6a6678d (task 13316, lane code-e): added proptest |
||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let long = "a".repeat(65); | ||
| for bad in [ | ||
| "", | ||
| long.as_str(), | ||
| "has space", | ||
| "under_score", | ||
| "plus+sign", | ||
| "dot.name", | ||
| "n\u{e9}", | ||
| ] { | ||
| let error = sign_request(&key, "GET", "/x", b"", now(), bad).unwrap_err(); | ||
| assert!(format!("{error:#}").contains("nonce"), "{bad:?}: {error:#}"); | ||
| } | ||
| let max = "a".repeat(64); | ||
| for good in ["n1", "abc-DEF-123", max.as_str()] { | ||
| let signed = sign_request(&key, "GET", "/x", b"", now(), good).unwrap(); | ||
| let mut replays = ReplayGuard::default(); | ||
| assert!( | ||
| verify_signed(&mut replays, &signed, "GET", "/x", b"", now()).is_ok(), | ||
| "{good:?}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Property coverage for the nonce-domain invariant (Codex finding on | ||
| // PR #6539): for every generated nonce, `sign_request` succeeds exactly | ||
| // when an independent oracle — written from the `verify_signed` contract, | ||
| // not from `nonce_in_domain` — accepts the nonce, and every accepted | ||
| // nonce round-trips through `verify_signed`. | ||
| proptest::proptest! { | ||
| #[test] | ||
| fn sign_request_accepts_exactly_the_verify_signed_nonce_domain( | ||
| nonce in proptest::string::string_regex(".{0,70}").unwrap() | ||
| ) { | ||
| // Independent oracle: 1-64 chars, each an ASCII letter, digit | ||
| // or '-', measured in bytes to mirror the wire contract. | ||
| let oracle = !nonce.is_empty() | ||
| && nonce.len() <= 64 | ||
| && nonce.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-'); | ||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let result = sign_request(&key, "GET", "/x", b"", now(), &nonce); | ||
| proptest::prop_assert_eq!(result.is_ok(), oracle, "nonce {:?}", nonce); | ||
| if let Ok(signed) = result { | ||
| let mut replays = ReplayGuard::default(); | ||
| proptest::prop_assert!( | ||
| verify_signed(&mut replays, &signed, "GET", "/x", b"", now()).is_ok(), | ||
| "oracle-accepted nonce {:?} must verify", nonce | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn hex_bytes_refuses_plus_encoded_pairs() { | ||
| // #2517: '+' pairs must not decode at either auth shape. | ||
|
|
@@ -688,7 +756,7 @@ mod tests { | |
| let mut signed = None; | ||
| for seed in 0_u8..=255 { | ||
| let key = SigningKey::from_bytes(&[seed; 32]); | ||
| let candidate = sign_request(&key, "GET", "/x", b"", now(), "n1"); | ||
| let candidate = sign_request(&key, "GET", "/x", b"", now(), "n1").unwrap(); | ||
| if plus_encode(&candidate.key) != candidate.key { | ||
| signed = Some(candidate); | ||
| break; | ||
|
|
@@ -706,7 +774,7 @@ mod tests { | |
| #[test] | ||
| fn a_signed_request_with_a_non_canonical_s_is_refused() { | ||
| let key = SigningKey::from_bytes(&[7; 32]); | ||
| let mut signed = sign_request(&key, "GET", "/x", b"", now(), "z2"); | ||
| let mut signed = sign_request(&key, "GET", "/x", b"", now(), "z2").unwrap(); | ||
| let raw = hex_bytes::<64>(&signed.signature).unwrap(); | ||
| signed.signature = hex_encode(&with_non_canonical_s(raw)); | ||
| let mut replays = ReplayGuard::default(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing
sign_requestfromSignedtoResult<Signed>changes an existing public API and its failure semantics, triggering the dated documentation requirement inpkgs/id/AGENTS.md. This commit changes only Rust files, and a repository-wide Markdown search forsign_requestand nonce-domain references found no design record covering the new contract. Add the required dated document explaining the allowed nonce domain, rejection behavior, and caller error-handling implications; the function docstring does not replace that required record.AGENTS.md reference: pkgs/id/AGENTS.md:L161-L168
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 6a6678d (task 13316, lane code-e): added the dated design record
doc/2026-11-08T15-20-00Z_design_sign-request-nonce-domain.md(pkgs/id/docs/ symlinks there) explaining the allowed nonce domain, the rejection behaviour, and the caller effect of sign_request becoming Result. The rustdoc on sign_request also carries the Errors section.