Skip to content

fix(core): normalise raw-socket bytes to Uint8Array before CBOR decode - #88

Merged
Mearman merged 1 commit into
mainfrom
fix/cbor-buffer-decode-reencode
Sep 12, 2026
Merged

fix(core): normalise raw-socket bytes to Uint8Array before CBOR decode#88
Mearman merged 1 commit into
mainfrom
fix/cbor-buffer-decode-reencode

Conversation

@Mearman

@Mearman Mearman commented Sep 12, 2026

Copy link
Copy Markdown
Member

Fixes #89.

createTcpTransport and createTlsTransport's own frame readers slice the raw accumulator Buffer directly and hand that slice to cbor2's decode() as-is. cbor2 mirrors the input's own class for nested byte-string values, so a Buffer input produces Buffer-typed byte strings in the decoded structure rather than plain Uint8Array. cbor2's encode() then doesn't recognise a Buffer as a byte string at all -- it falls through to serialising it as a generic object -- so any later re-encode of a decoded byte string silently produces the wrong bytes.

A capability token's own signature verification does exactly that re-encode: COSE's Sig_structure is built by re-encoding the token's own protected-header and payload byte strings. Any capability token received over a real TCP or TLS connection therefore failed signature verification with bad_signature, despite the token's bytes crossing the wire perfectly intact -- confirmed by tracing the exact byte arrays on both ends and finding them identical, then finding the corruption only appeared once the received bytes were re-encoded.

Fix: wrap the socket's extracted body in a plain Uint8Array view (same range, same backing memory, just a class cbor2's encoder actually recognises) before calling decode(), matching frame-codec.ts's own existing new Uint8Array(data) normalisation for its message-based transports.

Added a regression test (tcp-transport-token-bytes.test.ts) that sends a manage-request frame carrying a multi-field byte-string token over a real TCP connection and asserts that re-encoding a received byte string reproduces the pre-send bytes exactly -- the actual property that was broken. Confirmed RED against the pre-fix code and GREEN after.

Full core test suite (160 tests) and the wider ts workspace build/lint/test all pass.

createTcpTransport and createTlsTransport fed decode() a Buffer sliced
straight off the accumulator, so cbor2 sliced nested byte-string values
as Buffer too. Its own encode() doesn't recognise a Buffer as a byte
string and silently serialises it as a generic object instead, so any
later re-encode of a decoded byte string produces the wrong bytes.

A capability token's signature verification does exactly that re-encode
(COSE's Sig_structure, built from the token's own protected-header and
payload byte strings), so every capability token received over a real
TCP/TLS connection failed signature verification despite the token's
bytes crossing the wire perfectly intact. Wrapping the socket's body in
a plain Uint8Array view before decode -- the same normalisation
frame-codec.ts already applies for its own message-based transports --
avoids the whole class of bug.
@Mearman
Mearman force-pushed the fix/cbor-buffer-decode-reencode branch from 851ae64 to 6a66123 Compare September 12, 2026 08:31
@Mearman
Mearman marked this pull request as ready for review September 12, 2026 08:32
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review Completed 2026-09-12T08:37:28.569518Z 6a66123 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Mearman
Mearman merged commit af0c646 into main Sep 12, 2026
8 checks passed
@Mearman
Mearman deleted the fix/cbor-buffer-decode-reencode branch September 12, 2026 08:38
Mearman added a commit to ExaDev/agent-comms that referenced this pull request Sep 12, 2026
Every capability token received over a genuine TCP/TLS connection
failed signature verification despite its bytes crossing the wire
perfectly intact -- a cbor2 Buffer-vs-Uint8Array mismatch, fixed
upstream in ExaDev/wire-mesh#88. This dependency's own project-level
.npmrc gains a matching minimum-release-age exclusion, since it's a
first-party package this repo depends on.
Mearman added a commit to ExaDev/agent-comms that referenced this pull request Sep 12, 2026
Every capability token received over a genuine TCP/TLS connection
failed signature verification despite its bytes crossing the wire
perfectly intact -- a cbor2 Buffer-vs-Uint8Array mismatch, fixed
upstream in ExaDev/wire-mesh#88. This dependency's own project-level
.npmrc gains a matching minimum-release-age exclusion, since it's a
first-party package this repo depends on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Capability tokens fail signature verification over real TCP/TLS connections

1 participant