fix(sandbox): bound daemon config-PUT error body reads - #5334
Merged
Conversation
apps/api/src/tools/sandbox/{patch-sandbox-operator,resolve-env,sync-git-credentials}.ts
each PUT to the daemon's /_sandbox/config and, on a non-ok response, read the
error body with a plain res.text() with no size cap. sandbox-proxy.ts already
bounds every proxied response to the same endpoint via readBoundedText()
(#5316/#5319/#5330) — these three call sites hit /_sandbox/config directly
(during publish, rebase, and sandbox start) and were missed, so a
compromised or misbehaving daemon can still make the API process buffer an
unbounded error body into memory on that path.
Extracted readBoundedText()/UpstreamPayloadTooLargeError out of
sandbox-proxy.ts into apps/api/src/lib/bounded-text.ts (no behavior change)
so the tools/sandbox call sites can reuse it without an api-routes ->
tools-layer dependency, and switched all three sites to it with the same
10MB cap sandbox-proxy.ts already applies to this endpoint.
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.
Follows #5316/#5319/#5330, which capped every upstream response read in
sandbox-proxy.ts'sproxyDaemonhelper against the sandbox daemon's/_sandbox/configendpoint (and others) to prevent an unbounded body from being buffered fully into the API process's memory.Three call sites that PUT directly to
/_sandbox/configoutside that proxy path were missed:patch-sandbox-operator.ts(runs on every publish/rebase),sync-git-credentials.ts(runs on every publish that pushes to GitHub), andresolve-env.ts(runs on sandbox start). Each reads the daemon's error body via a plainres.text()with no size cap on the non-ok path, so a compromised or misbehaving daemon can still make the Studio API process buffer an unbounded body into memory on this path — the exact concern the prior three PRs addressed for every other route.Failure scenario: a sandbox daemon (attacker-controlled or just buggy) returns a huge body with a non-2xx status from
/_sandbox/config; any of the three call sites (hit on essentially every publish, rebase, or sandbox start) reads it fully into memory with no ceiling, unlike every other route to the same endpoint.Fix: extracted the existing
readBoundedText/UpstreamPayloadTooLargeErrorhelper out ofsandbox-proxy.tsintoapps/api/src/lib/bounded-text.ts(pure move, no behavior change — avoids an api-routes -> tools-layer import), and switched all three call sites to it with the same 10MB capsandbox-proxy.tsalready applies to this endpoint.To verify:
cd apps/api && bunx tsc --noEmit(clean) andbun test apps/api/src/api/routes/sandbox-proxy.test.ts apps/api/src/tools/sandbox/sync-git-credentials.test.ts(14 pass, 0 fail) — both cover the moved/consuming code.Locally ran:
bun run fmt, the targeted typecheck above, and the two targeted test files above. Full CI covers the rest.Summary by cubic
Bounds error-body reads on direct PUTs to the sandbox daemon
/_sandbox/configto prevent unbounded memory usage in the API process. Applies the same 10 MB cap used by the proxy and centralizes the helper.Bug Fixes
/_sandbox/configinpatch-sandbox-operator.ts,sync-git-credentials.ts, andresolve-env.tsviareadBoundedText(10 MB).Refactors
readBoundedTextandUpstreamPayloadTooLargeErrortoapps/api/src/lib/bounded-text.ts(no behavior change).sandbox-proxy.test.tsimports to use the new lib.Written for commit 4816327. Summary will update on new commits.