Repository navigation
Fix sandboxed HEAD responses reporting Content-Length: 0 - #150
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #144. Verified the bug is real:
src/guest-api.ts's guest bootstrap unconditionally serializedbody: ''for a HEAD request (both the function-only__invokepath and the middleware-chain__invokePipelinepath) without ever measuringresponse._text.src/function-worker.ts's outgoingFunctionResultcarried no separate length, sosrc/http-response.ts'sprepareResponsefell back toresult.body?.length ?? 0and advertisedContent-Length: 0for 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 noTextEncoder/Buffer) and sends the real length as an optionalcontentLengthfield alongside the existing emptybody: ''for HEAD, in both the function-only and middleware-chain paths._textis 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'scontentLength(integer, ≥0, same trust-boundary check as the rest of the response shape) and forwards it in the outgoingFunctionWorkerMessage.src/functions.ts: threads the optionalcontentLengththroughFunctionWorkerMessage's type (FunctionResultalready had it viaHandlerResult).src/http-response.tsis untouched —prepareResponsealready picks upcontentLengthexactly 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
test/sandbox.test.tsmirroring [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 nonemptyResponse('hello')/'HELLO'body.guest-api.ts(HEAD reportsContent-Length: 0against GET's realContent-Length: 5).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