ratchet: reject frames under MIN_FRAME_LEN before indexing the version byte
All checks were successful
ci / test (pull_request) Successful in 4m46s
ci / sdk (pull_request) Successful in 56s
android-ci / android (pull_request) Successful in 9m54s

Frame::parse checked the prefix, then indexed frame[4] for the version byte,
then checked the length. A 4-byte `rat ` frame is a prefix hit, so it
panicked with 'index out of bounds: the len is 4 but the index is 4' instead
of being rejected. dispatch::classify routes any `rat `-prefixed message to
Kind::Ratchet, so this is reachable from any authenticated peer.

Move the MIN_FRAME_LEN check ahead of the version match: a frame that cannot
hold a header, nonce and tag is 'truncated' whatever its version byte says,
and nothing past the prefix is indexed until the length is known good. Wire
format unchanged.

Tests: tests/ratchet.rs short_frames_are_rejected_without_panicking (bare
prefix, empty slice, partial prefix, prefix + 5 bytes, MIN_FRAME_LEN - 1) and
a_wrong_frame_version_is_rejected (73-byte frame with version 2 is
BadFrame("version"); a short one is a length error). Both assert the state
stays byte-identical after every rejection.
This commit is contained in:
agent 2026-09-16 19:50:36 -04:00
commit 3c78cd3e39
No known key found for this signature in database
2 changed files with 89 additions and 5 deletions

View file

@ -690,13 +690,16 @@ impl<'a> Frame<'a> {
if !frame.starts_with(FRAME_PREFIX) {
return Err(Error::BadFrame("prefix"));
}
// Length first: a frame too short to hold a header, nonce and tag is
// truncated whatever its version byte claims, and indexing anything
// past the prefix on it would be out of bounds.
if frame.len() < MIN_FRAME_LEN {
return Err(Error::BadFrame("truncated"));
}
match frame[FRAME_PREFIX.len()] {
FRAME_VERSION => {}
_ => return Err(Error::BadFrame("version")),
}
if frame.len() < MIN_FRAME_LEN {
return Err(Error::BadFrame("truncated"));
}
let mut header = [0u8; HEADER_LEN];
header.copy_from_slice(&frame[HEADER_OFFSET..HEADER_OFFSET + HEADER_LEN]);
let mut dh_pub = [0u8; 32];