ratchet: reject frames under MIN_FRAME_LEN before indexing the version byte (fixes peer-reachable panic) #90
Loading…
Reference in a new issue
No description provided.
Delete branch "wt/t_c0566260"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Bug
A 4-byte
ratframe (prefix, nothing else) panicked instead of being rejected, reachable from any authenticated peer:dispatch::classifymatchesratand returnsKind::Ratchet, the receive path callsingest_chat_frame->RatchetState::decrypt->Frame::parse.Frame::parsechecked the prefix, then indexedframe[4]for the version byte, and only then checked the length:That is the RED for this PR:
cargo test --locked --test ratchet short_framesagainst the unfixed tree (1c998e9), 0 passed / 1 failed with the panic above. Every other short length already returnedBadFrame("truncated"), so exactly the prefix-only case escaped the length check.Fix
One reorder in
src/ratchet.rsFrame::parse(wire format untouched): theframe.len() < MIN_FRAME_LENcheck now runs before the version match, so nothing past the prefix is indexed until the length is known good. A frame that cannot hold a header, nonce and tag is reported asBadFrame("truncated")whatever its version byte says; a frame at or aboveMIN_FRAME_LEN(73) with a foreign version byte is stillBadFrame("version").ratexactly is therefore a length error, not a prefix or version error - that is the honest reason, and it is whatdocs/PROTOCOL.mdalready requires (MUST reject anything under 73 bytes).Tests
tests/ratchet.rs(13 -> 15 tests):short_frames_are_rejected_without_panicking- barerat, empty slice, partial prefixrat,ratplus a five-byte tail (9 bytes),MIN_FRAME_LEN - 1(72) bytes. Each must beErr(BadFrame(_))and must leaveto_bytes()byte-identical.a_wrong_frame_version_is_rejected- a 73-byte frame with version byte 2 isBadFrame("version"); a sub-minimum frame with the same wrong version is a length error, and the state is untouched either way.Verification (at
3c78cd3,origin/main=1c998e9)Only the two allowed files changed; no change to
crates/onionwire-sdkand nothing it re-exports (both new tests use the publicRatchetState/RatchetKeyPairAPI only), so the SDK workspace is untouched by this PR.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.