ratchet: reject frames under MIN_FRAME_LEN before indexing the version byte (fixes peer-reachable panic) #90

Merged
sirius merged 2 commits from wt/t_c0566260 into main 2026-09-17 02:46:17 +00:00
Owner

Bug

A 4-byte rat frame (prefix, nothing else) panicked instead of being rejected, reachable from any authenticated peer: dispatch::classify matches rat and returns Kind::Ratchet, the receive path calls ingest_chat_frame -> RatchetState::decrypt -> Frame::parse. Frame::parse checked the prefix, then indexed frame[4] for the version byte, and only then checked the length:

thread 'short_frames_are_rejected_without_panicking' panicked at src/ratchet.rs:693:15:
index out of bounds: the len is 4 but the index is 4
test result: FAILED. 0 passed; 1 failed

That is the RED for this PR: cargo test --locked --test ratchet short_frames against the unfixed tree (1c998e9), 0 passed / 1 failed with the panic above. Every other short length already returned BadFrame("truncated"), so exactly the prefix-only case escaped the length check.

Fix

One reorder in src/ratchet.rs Frame::parse (wire format untouched): the frame.len() < MIN_FRAME_LEN check 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 as BadFrame("truncated") whatever its version byte says; a frame at or above MIN_FRAME_LEN (73) with a foreign version byte is still BadFrame("version"). rat exactly is therefore a length error, not a prefix or version error - that is the honest reason, and it is what docs/PROTOCOL.md already requires (MUST reject anything under 73 bytes).

Tests

tests/ratchet.rs (13 -> 15 tests):

  • short_frames_are_rejected_without_panicking - bare rat , empty slice, partial prefix rat, rat plus a five-byte tail (9 bytes), MIN_FRAME_LEN - 1 (72) bytes. Each must be Err(BadFrame(_)) and must leave to_bytes() byte-identical.
  • a_wrong_frame_version_is_rejected - a 73-byte frame with version byte 2 is BadFrame("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)

$ cargo test --locked
... test result: ok. N passed; 0 failed; ... (every suite, 290 tests passed, 3 ignored live-Tor)
$ cargo clippy --locked --all-targets -- -D warnings
Finished `dev` profile [unoptimized + debuginfo] target(s) in 56.51s
$ rustfmt --edition 2024 --check src/ratchet.rs tests/ratchet.rs
(exit 0; cargo fmt --check lists neither file - the repo-wide churn in src/store.rs, src/tui.rs, tests/tui_spec.rs is inherited)
$ git diff --stat
 src/ratchet.rs   |  9 ++++--
 tests/ratchet.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--

Only the two allowed files changed; no change to crates/onionwire-sdk and nothing it re-exports (both new tests use the public RatchetState/RatchetKeyPair API only), so the SDK workspace is untouched by this PR.

## Bug A 4-byte `rat ` frame (prefix, nothing else) panicked instead of being rejected, reachable from any authenticated peer: `dispatch::classify` matches `rat ` and returns `Kind::Ratchet`, the receive path calls `ingest_chat_frame` -> `RatchetState::decrypt` -> `Frame::parse`. `Frame::parse` checked the prefix, then indexed `frame[4]` for the version byte, and only then checked the length: ``` thread 'short_frames_are_rejected_without_panicking' panicked at src/ratchet.rs:693:15: index out of bounds: the len is 4 but the index is 4 test result: FAILED. 0 passed; 1 failed ``` That is the RED for this PR: `cargo test --locked --test ratchet short_frames` against the unfixed tree (`1c998e9`), 0 passed / 1 failed with the panic above. Every other short length already returned `BadFrame("truncated")`, so exactly the prefix-only case escaped the length check. ## Fix One reorder in `src/ratchet.rs` `Frame::parse` (wire format untouched): the `frame.len() < MIN_FRAME_LEN` check 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 as `BadFrame("truncated")` whatever its version byte says; a frame at or above `MIN_FRAME_LEN` (73) with a foreign version byte is still `BadFrame("version")`. `rat ` exactly is therefore a length error, not a prefix or version error - that is the honest reason, and it is what `docs/PROTOCOL.md` already requires (MUST reject anything under 73 bytes). ## Tests `tests/ratchet.rs` (13 -> 15 tests): - `short_frames_are_rejected_without_panicking` - bare `rat `, empty slice, partial prefix `rat`, `rat ` plus a five-byte tail (9 bytes), `MIN_FRAME_LEN - 1` (72) bytes. Each must be `Err(BadFrame(_))` and must leave `to_bytes()` byte-identical. - `a_wrong_frame_version_is_rejected` - a 73-byte frame with version byte 2 is `BadFrame("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`) ``` $ cargo test --locked ... test result: ok. N passed; 0 failed; ... (every suite, 290 tests passed, 3 ignored live-Tor) $ cargo clippy --locked --all-targets -- -D warnings Finished `dev` profile [unoptimized + debuginfo] target(s) in 56.51s $ rustfmt --edition 2024 --check src/ratchet.rs tests/ratchet.rs (exit 0; cargo fmt --check lists neither file - the repo-wide churn in src/store.rs, src/tui.rs, tests/tui_spec.rs is inherited) $ git diff --stat src/ratchet.rs | 9 ++++-- tests/ratchet.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- ``` Only the two allowed files changed; no change to `crates/onionwire-sdk` and nothing it re-exports (both new tests use the public `RatchetState`/`RatchetKeyPair` API only), so the SDK workspace is untouched by this PR.
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
3c78cd3e39
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.
Merge branch 'main' into wt/t_c0566260
All checks were successful
ci / test (pull_request) Successful in 5m10s
ci / sdk (pull_request) Successful in 1m14s
android-ci / android (pull_request) Successful in 8m5s
8bedf45e53
sirius merged commit 598270b519 into main 2026-09-17 02:46:17 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
sirius/onionwire!90
No description provided.