Repository navigation
Description
Context
While fixing #139 (trusted-path HEAD responses reporting Content-Length: 0 for a nonempty GET representation) in #143, I noticed src/guest-api.ts appears to have the exact same pattern in the sandboxed (sandbox: true) execution path. I did not fix or touch this — it's out of scope for #139 (which names src/trusted-functions.ts:145 specifically) and for the trusted-execution PR, and src/trusted-functions.ts's own header comment says the sandboxed path is deliberately untouched by that file. This issue is to track verifying and, if confirmed, fixing it separately.
What I observed (not yet reproduced end-to-end through a live sandboxed route)
In src/guest-api.ts, the guest bootstrap that serializes a handler's Response back to the host does this for both the function-only path and the middleware-chain path:
// function path
globalThis.__output = stringify({status:response.status,headers,body: input.request.method === 'HEAD' ? '' : response._text});
// middleware/native-passthrough path
globalThis.__output = stringify({status:response.status,headers:response.headers._pairs,
body:nativeBody || input.request.method === 'HEAD' ? '' : response._text,nativeBody});
Both unconditionally serialize body: '' for a HEAD request, without ever reading response._text's length. Unlike the trusted path (which lost the length because it skipped an async stream read), the guest's Response/Response-like classes already hold _text as a fully materialized string at construction time (this._text = body || ''), so the length is already known "for free" — no additional read or stream consumption is needed to get it.
Downstream, src/function-worker.ts's message handler builds the outgoing FunctionResult purely from the guest's JSON payload (body: Buffer.from(value.body,'utf8')) — there is no separate contentLength field sent for the HEAD case, so a HEAD answer's body buffer is empty, and (by the same mechanism #139 described) src/http-response.ts's prepareResponse would then advertise Content-Length: 0 for a sandboxed function/middleware route's HEAD response, even when the equivalent GET has a real body.
Suggested reproduction (untested by me — I did not have the sandbox toolchain warmed up before running out of scope for this PR)
version: "1"
routes:
/:
sandbox: true
function: {source: f.mjs}f.mjs: export default () => new Response('hello');
Compare GET (Content-Length: 5, expected) against HEAD (Content-Length: 0, suspected) through createRuntime/prepareResponse, same shape as #139's own repro.
Expected fix, if confirmed
Send the real byte length (e.g. Buffer.byteLength(response._text)) alongside body: '' for the HEAD case in guest-api.ts's serialization, thread it through function-worker.ts's FunctionWorkerMessage/GuestResponsePayload as an optional contentLength (mirroring how native.contentLength is already threaded through FunctionPool.execute's native-passthrough branch), and let http-response.ts's existing prepareResponse logic pick it up exactly as it does for the trusted/native paths after #143. This is the same fix pattern as #139, just for the sandboxed transport.
Scope note
Per AGENTS.md, the sandboxed path's isolation guarantees must be preserved exactly — this fix, if confirmed, should only add length information to the existing JSON/string boundary, not change what the guest can access or how isolation works.
Activity