Skip to content

Fix sandboxed HEAD responses reporting Content-Length: 0 - #150

Merged
jimhoyd merged 1 commit into
mainfrom
fix/sandboxed-head-content-length
Sep 19, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
fix/sandboxed-head-content-length

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #144. Verified the bug is real: src/guest-api.ts's guest bootstrap unconditionally serialized body: '' for a HEAD request (both the function-only __invoke path and the middleware-chain __invokePipeline path) without ever measuring response._text. src/function-worker.ts's outgoing FunctionResult carried no separate length, so src/http-response.ts's prepareResponse fell back to result.body?.length ?? 0 and advertised Content-Length: 0 for a sandboxed function/middleware route's HEAD response even when the equivalent GET had a real body — the same pattern #143 fixed for the trusted execution path.

  • src/guest-api.ts: adds a small in-sandbox UTF-8 byte counter (the guest has no TextEncoder/Buffer) and sends the real length as an optional contentLength field alongside the existing empty body: '' for HEAD, in both the function-only and middleware-chain paths. _text is already a fully materialized string at construction time, so no extra read or stream consumption is needed to get the length.
  • src/function-worker.ts: validates the guest's contentLength (integer, ≥0, same trust-boundary check as the rest of the response shape) and forwards it in the outgoing FunctionWorkerMessage.
  • src/functions.ts: threads the optional contentLength through FunctionWorkerMessage's type (FunctionResult already had it via HandlerResult).
  • src/http-response.ts is untouched — prepareResponse already picks up contentLength exactly as it does for the trusted/native paths after Fix trusted-execution body buffering, timer leaks and HEAD length; reconcile docs #143.

No sandboxing guarantee changes: the module allowlist, byte budgets, worker termination, and guest capability surface (no Node, no new host functions) are exactly as before. Only an integer crosses the existing JSON boundary between guest and host.

Test plan

  • Added a regression test in test/sandbox.test.ts mirroring [Bug]: Trusted HEAD responses advertise Content-Length 0 for nonempty GET representations #139's trusted-path test: a plain sandboxed function and a middleware-transformed one, each returning a nonempty Response('hello')/'HELLO' body.
    • Confirmed it fails on unmodified guest-api.ts (HEAD reports Content-Length: 0 against GET's real Content-Length: 5).
    • Confirmed it passes after the fix.
  • npm run typecheck — green.
  • npm run lint — green.
  • npm run verify (lint, typecheck, check, build, full test suite) — green: 491 passed, 0 failed, 2 skipped (expected, unrelated to this change — a TLS-target skip and an interchange skip pre-existing in the suite). One earlier run showed a single unrelated transient failure that did not reproduce on immediate rerun (not connected to this change; the full suite reran clean twice in a row afterward).

🤖 Generated with Claude Code

https://claude.ai/code/session_018neC2tYKPxMPbzZf1wNctr


Generated by Claude Code

The sandboxed (QuickJS/WASM) execution path had the same bug #143 fixed
in the trusted-execution path: guest-api.ts unconditionally serialized
body: '' for a HEAD request without ever measuring response._text, so
function-worker.ts's outgoing FunctionResult carried no length and
http-response.ts's prepareResponse advertised Content-Length: 0 for a
HEAD reply even when the equivalent GET had a real body.

The guest already holds _text as a fully materialized string, so its
byte length is known without any stream read or extra host capability.
guest-api.ts now sends that length as an optional contentLength field
alongside the empty HEAD body (computed by a small in-sandbox UTF-8
byte counter, since the guest has no TextEncoder/Buffer), threaded
through function-worker.ts's validated response and functions.ts's
FunctionWorkerMessage, mirroring how native.contentLength already
crosses the same boundary. prepareResponse is unchanged; it already
picks this up exactly as it does for the trusted/native paths.

No sandboxing guarantee changes: the module allowlist, byte budgets,
worker termination and guest capability surface are untouched — only
a length integer crosses the existing JSON boundary.

Added a regression test (test/sandbox.test.ts) mirroring #139's test
for the trusted path: confirmed it fails against unmodified guest-api.ts
(Content-Length: 0 for a nonempty GET body) and passes after the fix,
for both a plain sandboxed function and a middleware-transformed one.

Fixes #144

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018neC2tYKPxMPbzZf1wNctr
@jimhoyd
jimhoyd merged commit 6bc21d8 into main Sep 19, 2026
10 checks passed
@jimhoyd
jimhoyd deleted the fix/sandboxed-head-content-length branch September 19, 2026 13:05
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.

[Bug]: Sandboxed HEAD responses likely advertise Content-Length 0 too, same pattern as #139

2 participants