Skip to content

fix(batch): add missing early return in nodeHttpBatchRpcResponse on non-POST#195

Open
aleister1102 wants to merge 2 commits into
cloudflare:mainfrom
aleister1102:fix/node-batch-405-early-return
Open

fix(batch): add missing early return in nodeHttpBatchRpcResponse on non-POST#195
aleister1102 wants to merge 2 commits into
cloudflare:mainfrom
aleister1102:fix/node-batch-405-early-return

Conversation

@aleister1102

Copy link
Copy Markdown

Summary

nodeHttpBatchRpcResponse wrote a 405 status for non-POST requests via response.writeHead(405, ...) but was missing response.end() and return. Execution fell through into the full RPC pipeline unconditionally, then crashed with ERR_HTTP_HEADERS_SENT when the function later tried to write the 200 response — leaving the TCP connection open until timeout.

The Fetch API counterpart (newHttpBatchRpcResponse) was unaffected; it already returned a 405 Response immediately.

Changes

  • src/batch.ts: add response.end() and return after the 405 writeHead call in nodeHttpBatchRpcResponse

Test plan

  • Verify a non-POST request to a nodeHttpBatchRpcResponse endpoint now receives a 405 with a closed connection and no RPC handlers are invoked
  • Verify a valid POST request continues to work normally

…on-POST

Without response.end() and return, a non-POST request would write the
405 status line then fall through into the full RPC pipeline. This caused
the subsequent writeHead(200) to throw ERR_HTTP_HEADERS_SENT, left the
TCP connection open (response.end never called), and — when the caller
uses the documented try/catch pattern — caused a second writeHead(500)
to throw uncaught, terminating the Node.js process.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@aleister1102

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Jun 11, 2026
@aleister1102

aleister1102 commented Jun 11, 2026

Copy link
Copy Markdown
Author

Hi @teamchong @kentonv, could you take a look when you get a chance? This is the fix for the missing early return discussed in the HackerOne report. The change is minimal - two lines in src/batch.ts. Thanks!

@pkg-pr-new

pkg-pr-new Bot commented Jun 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@195

commit: 14fa2b1

@teamchong

Copy link
Copy Markdown
Collaborator

verified the fix locally. on main a GET to this endpoint never gets a response, the socket just stays open until timeout, and there's an unhandled ERR_HTTP_HEADERS_SENT rejection later when the pipeline runs anyway.

with your branch it returns the 405 immediately. So the change is correct.

two things to add before this can merge (snippets below are just a starting point, adjust as you see fit):

a changeset (the release automation needs it):

.changeset/non-post-early-return.md

---
"capnweb": patch
---

Fix nodeHttpBatchRpcResponse leaving the connection open and crashing with
ERR_HTTP_HEADERS_SENT on non-POST requests. It now returns 405 immediately.

and a regression test, since this path had no coverage. In __tests__/index.test.ts, inside the existing describe("HTTP requests") block:

  it("rejects non-POST requests with 405", async () => {
    let response = await fetch(`http://${inject("testServerHost")}`, { method: "GET" });
    expect(response.status).toBe(405);
    await response.text();
  });

this hangs on main and passes with your fix, so it actually catches the regression.

Thanks!

@changeset-bot

changeset-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f684df8

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Add changeset for nodeHttpBatchRpcResponse early-return fix.
@aleister1102 aleister1102 force-pushed the fix/node-batch-405-early-return branch from 4883dd7 to f684df8 Compare June 11, 2026 16:50
@aleister1102

aleister1102 commented Jun 11, 2026

Copy link
Copy Markdown
Author

Done - thanks for verifying the fix locally, @teamchong.

Added both items:
• Changeset: .changeset/non-post-early-return.md
• Regression test: rejects non-POST requests with 405 in the existing describe ("HTTP requests") block

Confirmed locally that the new test passes on this branch. Should be ready for another look.

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.

2 participants