Skip to content

fix(link): refuse a prepared relay body that ends before its declared Content-Length - #8611

Merged
lidge-jun merged 1 commit into
devfrom
fix/link-relay-short-body
Nov 13, 2026
Merged

lidge-jun merged 1 commit into
devfrom
fix/link-relay-short-body

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

From the final review of #8594. Closes #8604.

A Child's link relay buffers and zstd-compresses a request body when the route allows it and the Home named link-request-zstd/1. readDeclaredBody (src/client/link-request-compression.ts) reads exactly the declared Content-Length into one buffer. When the body ended cleanly before that length, it returned the shorter slice. The relay then set Content-Length to that shorter length and forwarded the body, so the Home received a body that looked complete although the client had declared more. In practice the truncated JSON failed to parse on the Home.

readDeclaredBody now throws a RangeError when the body ends with fewer bytes than declared, as it already did for more. The relay's existing catch answers 400 link relay request body unreadable (or the tunnel-unavailable 503 when the caller aborted), releases the preparation allowance, and sends nothing to the Home.

The streamed path does not call readDeclaredBody and is unchanged. It covers a Home that did not name the capability, routes that do not compress, chunked bodies, and bodies that do not fit the preparation allowance. A streamed body keeps the caller's Content-Length, so the Home still sees a short upload there. I checked this with a scratch test outside the repository, run against both versions. A body 10 bytes short on /v1/responses to a Home without the capability, and on /v1/images/generations to a Home with it, reached the Home with the declared length (8212), the Home's read ended in AbortError, and the caller got 503 at the relay's header deadline (about 12 s), the same before and after. That check depends on the wall clock, so it is not committed.

structure/remote-link.md now states the refusal and the streamed-path difference.

What keeps this inert. Only the prepared path changes, and it runs only on a Child whose Home named link-request-zstd/1, for a POST on a compressible route with a declared length between 1 KiB and 16 MiB. A body whose bytes match its declared length, which is every well-formed request, is prepared exactly as before. No config key, default or other code path changes.

Verification

Tests written first, on dev at e85a975, in tests/clients/link-request-zstd.test.ts:

  • a body that ends cleanly before its declared length is unreadable, never a shorter complete body: readDeclaredBody on a stream 10 bytes short of its declared length must reject with a RangeError, and an exact-length stream still resolves to the same bytes. On dev it failed with "Received promise that resolved".
  • a prepared body shorter than its declared Content-Length answers 400 and reaches no Home: through the relay to a real hub-link session dispatcher that named the capability, a short body must answer 400 link relay request body unreadable, leave the Home's traffic book at the one priming request, and release the preparation allowance. On dev the truncated body reached the Home's JSON decoder ("JSON Parse error: Unterminated string") and the test failed.

Commands on this branch (exit codes):

  • TMPDIR=/tmp bun test tests/clients/link-request-zstd.test.ts: 16 pass, 0 fail (exit 0).
  • bun run typecheck: exit 0.
  • bun run privacy:scan: exit 0.
  • bun run structure:check: exit 0.
  • TMPDIR=/tmp bun scripts/test.ts --parallel=2 tests/clients tests/test-layout.test.ts tests/test-layout-tooling.test.ts tests/ci-workflows/structure-ssot.test.ts tests/ci-workflows/ci-structure-gate.test.ts tests/ci-workflows/file-size-ratchet.test.ts (203 files, including tests/clients/client-link-relay.test.ts and tests/clients/link-boundary.test.ts): 3410 pass, 67 skip, 3 fail (exit 1). The 3 failures are the Rust ocx-child tests (tests/clients/link-ocx-child-e2e.test.ts, two cases, and the Rust case of tests/clients/link-ocx-child-conformance.test.ts). They stop before any test code runs, because cargo on the build machine has no default rustup toolchain. This branch touches no Rust code.

NOT RUN: the full bun run test suite (the build machine allows focused files only; CI covers the rest); the three Rust ocx-child tests above (no toolchain); whether Bun ever ends a real request body cleanly when it is short (the issue left this open; the fix holds either way); Windows and macOS (the changed code is platform-independent).

Review: one read-only round by an independent reviewer (gpt-6.1-terra, xhigh effort) on the exact head 12894b9 found no P0–P2. It confirmed that readDeclaredBody has one production caller (the prepared-body path, reached only by eligible POSTs without Content-Encoding; HEAD, chunked and already encoded bodies never enter it), that the error path releases the preparation lease and the reader lock before contacting the Home, and that an aborted relay signal maps to 503 and a clean short end or stream error to 400. It also confirmed that both new tests fail on the parent for the stated reasons and pass on this head (link-request-zstd and client-link-relay: 61 pass, 0 fail). Not established by the tests: which stream and signal behaviour Bun produces for every real client disconnect; the tests simulate a clean short stream.

Checklist

  • Scope stays focused and avoids unrelated cleanup.
  • Docs or release notes were updated when needed.
  • Security-sensitive changes were reviewed for secrets, auth, and unsafe defaults.

Implemented by Claude Opus 5.5 (anthropic/claude-opus-5-5) in GOAT through the Codex harness; review by GPT-6.1 Terra (xhigh). Received as a git bundle from the VM that built it and pushed by the coordinator.

… Content-Length

readDeclaredBody returned the shorter slice when a compressible request body ended
cleanly before its declared length, and the relay forwarded it to the Home as a
complete body with the shorter Content-Length. A short body now throws, which the
relay already answers with 400 before anything reaches the Home. The streamed path
is unchanged.

Refs #8604
@lidge-jun lidge-jun added the bug Something isn't working label Nov 13, 2026
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@lidge-jun
lidge-jun marked this pull request as ready for review November 13, 2026 01:15
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner November 13, 2026 01:15
@lidge-jun
lidge-jun merged commit acab412 into dev Nov 13, 2026
34 of 36 checks passed
@lidge-jun
lidge-jun deleted the fix/link-relay-short-body branch November 13, 2026 01:15
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T01:17:25.057698Z 12894b9 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Link relay: a prepared body shorter than its declared Content-Length is forwarded as complete

1 participant