Repository navigation
shell: throw on Response/Request stream body used as redirect instead of delivering zero bytes - #35314
shell: throw on Response/Request stream body used as redirect instead of delivering zero bytes#35314robobun wants to merge 4 commits into
Conversation
… of silently delivering zero bytes When a Response (or Request) whose body is a JS-driven ReadableStream (start/pull source, TransformStream, async generator, a piped CompressionStream) is used as a shell stdin redirect, the command previously ran on an empty stdin and reported success. Body::use_as_any_blob() returns an empty Blob for a Locked body it cannot synchronously convert, and Stdio::extract_blob turns an empty blob into Stdio::Ignore. The same substitution happened for an already-consumed body (Value::Used) and an errored body (Value::Error). The shell redirect setup now checks the body state after to_blob_if_possible(): a Locked body throws ERR_INVALID_ARG_TYPE with a hint to buffer it first; a Used body throws ERR_BODY_ALREADY_USED; an Error body rethrows its stored error. Blob, string, InternalBlob and Bun.file/native-stream bodies (which to_blob_if_possible converts) continue to work. Both the spawned-subprocess path (Cmd::init_subproc_redirections, used for cat on POSIX) and the builtin path (Builtin::init_redirections, used for cat on Windows) are covered, and Request is now accepted alongside Response for parity with the builtin path and Bun.spawn.
WalkthroughRequest and Response bodies are now supported as shell redirect sources through generic body extraction and validation. Invalid stream or consumed bodies fail redirects, while string, Blob, and Request bodies remain usable. Tests cover these cases. ChangesRequest/Response shell redirects
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
Updated 4:24 PM PT - Jul 23rd, 2026
✅ @robobun, your commit c91149d099342f6f2230630a70ac87fd3cf786ca passed in 🧪 To try this PR locally: bunx bun-pr 35314That installs a local version of the PR into your bun-35314 --bun |
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
|
Not a duplicate of either. This PR does not touch the bare-
Neither #33996 nor #30550 changes the |
Addresses review: the Locked/Used/Error body check was duplicated between Cmd::init_subproc_redirections (POSIX spawned-command path) and Builtin::init_redirections (Windows builtin path).
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/runtime/shell/util.rs`:
- Around line 33-38: Update the BODY_ALREADY_USED error constructed in the
Value::Used branch to identify the consumed Request/Response body, state that it
cannot be redirected, and instruct callers to create a new body or redirect the
buffered bytes. Preserve the existing error code and throw behavior.
In `@test/js/bun/shell/bunshell.test.ts`:
- Around line 1670-1731: Add forced-subprocess coverage in the “stdin from
Request/Response” tests so Request inputs exercise
Cmd::init_subproc_redirections rather than builtin cat. Add rejection cases for
a Request backed by a ReadableStream and for an already-consumed Request body,
while preserving the existing successful Request stdin coverage.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: c21ecb31-bf6f-4224-8464-bfacc671f130
📒 Files selected for processing (5)
src/runtime/shell/Builtin.rssrc/runtime/shell/shell_body.rssrc/runtime/shell/states/Cmd.rssrc/runtime/shell/util.rstest/js/bun/shell/bunshell.test.ts
…dy stdin rejection
… redirect
The remedy 'Read it first: $`cmd < ${await response.bytes()}`' only
makes sense for an input redirect; a Response wrapping a ReadableStream
can never be a write target. check_body_for_redirect now takes the
redirect direction and drops the hint for stdout/stderr.
Also notes that the Value::Error arm is reached via fetch-driven bodies,
not user-constructed new Response().
|
Closing as part of a cleanup of stale pull requests. This PR has had no new commits since 2026-07-23 and it conflicts with main. This is not a judgment on the fix itself. If the problem still reproduces on a current build, reopen this PR after a rebase or open a new one against main. |
What does this PR do?
A
Responsewhose body is a JS-drivenReadableStream(astart/pullsource,TransformStream#readable, an async generator,pipeThrough(new CompressionStream(...))) used as a shell stdin redirect runs the command on zero bytes and reports success:The same
Response's.text()returns the full payload. "Response as stdin" is the docs' headline shell example; the breakage is only the composed-stream case (string /Blob/blob.stream()/Bun.file/ realfetch()bodies work).Cause
Cmd::init_subproc_redirections(src/runtime/shell/states/Cmd.rs) andBuiltin::init_redirections(src/runtime/shell/Builtin.rs) callBody::use_as_any_blob()/Body::use_()on the wrapped body. For aValue::Lockedbody whose stream is not backed by a synchronously-readable native source,to_any_blob_allow_promise()returnsNoneanduse_as_any_blob()falls back to.unwrap_or(Blob::default())(src/runtime/webcore/Body.rs:1249-1251).Stdio::extract_blobthen maps the empty blob toStdio::Ignore.Value::UsedandValue::Errortook the same empty-blob fallback.Fix
A new
shell::util::check_body_for_redirect(body, global, stdin)runsto_blob_if_possible()(which converts native-backed streams to aBlob) and then throws instead of letting the caller substitute an empty blob:Locked:ERR_INVALID_ARG_TYPEwithRequest/Response body is a ReadableStream, which cannot be redirected in Bun Shell yet. A stdin redirect appendsRead it first: $`cmd < ${await response.bytes()}`; stdout/stderr get the bare sentence since a stream body can never be a write target.Used:ERR_BODY_ALREADY_USED(Body already used), matchingBun.spawn({ stdin: response })and the body mixin.Error: the body's stored error is rethrown.Both the spawned-subprocess path (used for
caton POSIX) and the builtin path (used forcaton Windows) call the helper. The branch now usesBody::from_request_or_response()so aRequestis accepted alongsideResponse, matching whatBuiltin.rsandBun.spawnalready did (previouslycat < ${request}threwInvalid JS object used in shell: [object Request]).Body::use_as_any_blob()itself is unchanged; its other callers (fetch request bodies, wasm streaming,Responseinit) all sit after an explicitLocked/stream check of their own.Known trade-off:
$`echo ${request}`(aRequestin argument position, not a redirect) now emits the parser'sexpected a command or assignment but got: "JSObjRef"instead ofInvalid JS object used in shell: [object Request].handle_template_valueruns before parsing and cannot see the position, so registeringRequestas a redirect-capable value necessarily routes it there.Response,Blob,ReadableStreamandArrayBufferalready produce that parser message on main; a follow-up can improve the parser'sJSObjRef-in-argument-position error for all of them at once.Related
ReadableStreampumping into shell stdin; once it lands this branch can route theLockedcase through the sameStdio::ReadableStreampath instead of throwing.ReadableStreamredirect placeholderpanic!into a throw and fixes theYield::Failedtrampoline so the builtin-path throw does not leave the interpreter keepalive pinned in a standalone script. (The test runner exits cleanly regardless.)ReadableStreamstdin case.How did you verify your code works?
Added to
test/js/bun/shell/bunshell.test.tsunderdeno_task > redirects > stdin from Request/Response, parameterized overResponseandRequest:ReadableStream start(),TransformStream#readable, async-generator bodies as stdin each reject with/body is a ReadableStream//already used/Blobbody controls still deliver the full payloadecho hi > ${new Response(stream)}rejects with the stream-body message and without the stdin hintno test proof · iteration 0 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/bun/shell/bunshell.test.ts