Skip to content

Bound logged peer text and reject a non-array active_roles - #244

Merged
kahrendt merged 4 commits into
mainfrom
peer-text-bounds
Nov 4, 2026
Merged

kahrendt merged 4 commits into
mainfrom
peer-text-bounds

Conversation

@kahrendt

@kahrendt kahrendt commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Summary

From the second coherence pass on the allocation follow-ups (Sendspin/backlog#92). The handshake logs peer text only up to 32 bytes, but after it three sites a connection keyed with the published Sentinel PSK reaches logged peer text at full length: up to 16 KiB per message, enough to stall the protocol task on a UART console. determine_message_type() also compared type as a const char*, so a type with an embedded NUL ("server/hello\u0000x") dispatched as the known one. Separately, a present non-array server/activate active_roles was silently taken as omitted, keeping the prior roles, while its sibling stream/* roles rejects.

Change

  • Log bound: PEER_TEXT_LOG_BYTES (32) and peer_text_for_log() (a UTF-8-safe cut) move from noise_handshake.cpp to inline_text.h. They bound the unhandled message type, an unrecognized enum value from read_enum_field() (server/activate's pairing.method/pairing.format), and the pair/abort reason. Every other peer string is bounded where it is parsed.
  • Message type: determine_message_type() reads type once as a string_view and compares with ==.
  • active_roles: a present non-array value logs and rejects the activate, like read_stream_roles(); absent or null still keeps the prior set. A rejected activate leaves a nursery connection to the 30 s establish reap and an admitted one with its prior state.
  • Docs: conventions.md says peer text no field bounds is logged only up to the cap.
  • Test port: the inbound-off lifecycle test moves off 19105, which Add an option to turn off the inbound WebSocket server #238 and Hold the stream/* messages' bounded fields inline #241 both picked, so a parallel ctest run no longer fails one of them.

Tests

  • Protocol.DetermineMessageType gains an embedded-NUL row; a strcmp comparison fails it.
  • Protocol.ServerActivateReadsActiveRoles gains null, string and object rows; restoring the nullopt fallback fails the string and object rows, dropping the null branch fails the null row.
  • Protocol.UnrecognizedPeerTextIsLoggedOnlyUpToTheBound (pairing method and pair/abort reason) and EncryptedLifecycle.AnUnhandledTypeIsLoggedOnlyUpToTheBound capture stderr and check 32 bytes are logged and 33 are not; dropping each cut fails its test.

Not in this PR

  • A non-string entry in activities, active_roles or stream/* roles is still skipped, so active_roles:[7] on a connection that is not playback-capable passes the "MUST be empty" rule.
  • Because the pairing index is bumped only after a successful parse, a server that counts a parse-rejected pairing activate falls one index ahead until the next handshake.

- PEER_TEXT_LOG_BYTES and peer_text_for_log() move to inline_text.h and
  bound every log of peer text no field bounds: the unhandled message
  type, an unrecognized enum value (server/activate's pairing method
  and format) and the pair/abort reason, each reachable after the
  handshake from a connection keyed with the Sentinel PSK.
- determine_message_type() reads type as a string_view, so a type with
  an embedded NUL no longer matches a known one.
- A present server/activate active_roles that is not an array logs and
  rejects the activate, like stream/end and stream/clear roles; null
  still keeps the prior set.
- conventions.md says unbounded peer text is logged only up to the cap.
It shared 19105 with the stream-roles test, so a parallel ctest run
failed whichever bound second.
@kahrendt
kahrendt marked this pull request as ready for review November 4, 2026 18:35
@kahrendt
kahrendt requested a max review from Copilot November 4, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unbounded group IDs are still printed in debug logs, contradicting the documented logging guarantee and requiring either bounded output or an explicit exception.

3 open issues
What changed in this PR

This PR hardens the library’s protocol handling by bounding peer text in logs, matching complete message types, and rejecting malformed activation data.

Changes:

  • Shares a UTF-8-safe, 32-byte logging cap across handshake, parser, and dispatch paths.
  • Rejects message types containing extra bytes and non-array active_roles, while preserving absent/null behavior.
  • Adds regression coverage, documents the logging bound, and resolves a lifecycle-test port collision.
File Description
tests/​test_protocol.cpp Tests embedded null bytes, active_roles validation, and parser log bounds.
tests/​test_encrypted_lifecycle.cpp Tests the log bound for unhandled message types.
tests/​test_client_lifecycle.cpp Assigns the inbound-disabled test a separate port.
src/​protocol.cpp Bounds peer-value logs, compares complete types, and validates active_roles.
src/​noise_handshake.cpp Uses the shared truncation helper for handshake logs.
src/​inline_text.h Defines the shared logging cap and truncation helper.
src/​connection_manager.cpp Updates comment wording about unrecognized pairing-method logging.
src/​client_dispatch.cpp Bounds unhandled message-type logs.
docs/​conventions.md Documents the peer-text logging limit.

Review effort: Max

Comment thread docs/conventions.md Resolved
Comment thread src/noise_handshake.cpp Resolved
Comment thread src/protocol.cpp Resolved
- The main loop's group/update debug log cuts group_id to
  PEER_TEXT_LOG_BYTES; group_name is already cut at parse.
- NoiseHandshakeDriver.UnexpectedPeerTextIsLoggedOnlyUpToTheBound
  covers the handshake's two log sites, the unexpected type and the
  server/error reason.
- The pairing-index bump comment says a server/activate the parser
  rejects is not counted.
@kahrendt
kahrendt merged commit 5d03e5f into main Nov 4, 2026
16 checks passed
@kahrendt
kahrendt deleted the peer-text-bounds branch November 4, 2026 19:07
kahrendt added a commit that referenced this pull request Nov 4, 2026
## Summary

A follow-up from #244. messaging.md "server/activate" requires a client
to close with `client/goodbye` reason `'unauthorized'` when
`active_roles` is non-empty on a connection that is not
playback-capable. `process_server_activate_message()` skipped a
non-string entry, so `active_roles:[7]` read as empty and such a
connection was admitted where a strict client closes.

## Change

- **active_roles:** a non-string entry goes to `ActiveRoles::add()` as
an empty name. It sets no role bit but makes the set non-empty, like a
role version this library does not implement, so the existing admission
check closes the connection with `'unauthorized'`. A `null` entry reads
the same way.
- **Siblings unchanged:** `activities` and `stream/end`/`stream/clear`
`roles` keep skipping such entries, since there one only narrows what
the message applies to; a comment at the `active_roles` site says why it
differs.

## Tests

- `Protocol.ServerActivateReadsActiveRoles`: the `[7]` row expects a
non-empty set with mask 0; restoring the skip fails it. Admission takes
only whether the set is empty, which
`Admissible.RolesRequirePlaybackCapability` already covers both ways, so
no admission test changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants