fix: AppContext::new is a const fn over a &'static label (QA S-09)
A label is one fixed string per purpose; taking any runtime bytes let a consumer derive it from a peer name, a file name or a message field, and two purposes could then collide. AppContext::new now takes &'static [u8] and is a const fn, so contexts are built as consts and a bad label fails the consumer's build; there is no runtime constructor (the simplest API: one constructor, no escape hatch until a need appears). Tests: a const OK and a const EMPTY context; sig_hygiene refuses a consumer that passes a runtime Vec (E0597/E0716); the one test that needs runtime labels (flipping label bytes) leaks them on purpose. The random labels in sig_malformed and fuzz mode 5's AppContext part are dropped (the raw-ctx part of mode 5 stays). A doctest shows the const pattern, and docs/02_signatures.md says so.
This commit is contained in:
@@ -101,6 +101,11 @@ fn contexts_of_the_wrong_length_are_typed_errors() {
|
||||
Error::TooLong { input: Input::Context, maximum: MAX_APP_LABEL_LEN, actual: MAX_APP_LABEL_LEN + 1 }
|
||||
);
|
||||
assert_eq!(AppContext::new(b"").unwrap_err(), Error::TooShort { input: Input::Context, minimum: 1, actual: 0 });
|
||||
// QA S-09: the constructor is a `const fn`, so a bad label can fail the consumer's build.
|
||||
const OK: Result<AppContext, Error> = AppContext::new(b"const");
|
||||
const EMPTY: Result<AppContext, Error> = AppContext::new(b"");
|
||||
assert_eq!(OK.unwrap().as_bytes(), b"domi/arcanum/v2/sig/app/const");
|
||||
assert!(EMPTY.is_err());
|
||||
|
||||
// The raw RFC interface: 255 bytes is the most, 256 is a typed error (sign and verify).
|
||||
let sig = sign::sign_composite_raw(&sk, b"m", &[1; 255], &mut rng(64)).unwrap();
|
||||
@@ -149,8 +154,6 @@ fn random_bytes_never_panic() {
|
||||
let sig = k.sign(&buf, &c, &mut r).unwrap();
|
||||
assert_eq!(k.verifying_key().verify(&buf, &c, &sig), Ok(()));
|
||||
}
|
||||
// Random labels: typed errors or a context, never a panic.
|
||||
let _ = AppContext::new(&buf[..len.min(300)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user