Skip to content

fix(viewer): answer a malformed Host with 400 instead of Bun's error page - #18

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/viewer-malformed-host
Oct 21, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/viewer-malformed-host

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Closes #12.

Bun builds req.url from the request's Host header, so a Host it can't parse (127.0.0.1.8787, 127.0.0.1:99999, [::1) made new URL(req.url) throw before rejectRequest ran. Bun then answered with its development error page: about 67 KB of HTML with the stack trace and local file paths.

Change (src/viewer/server.ts)

  • Parse the URL inside a try and answer 400 malformed request when it fails. This is the fix from the issue.
  • Add an error handler to serve() that returns a plain 500 internal error, so any other uncaught throw also never shows the debug page. It logs the error with console.error, because once a handler is set, Bun stops printing uncaught errors itself.

Test (test/viewer.test.ts)

  • fetch() won't send a Host it can't parse, so the new test writes a raw HTTP request over a socket. For three malformed Hosts it checks for a short 400 Bad Request that contains no store data.
  • With the change reverted, the test fails.

Checks: bun test 195 tests, 0 fail, 4 skip. tsc --noEmit is clean. biome lint reports no new findings in the touched files. Only standard Bun and node:net APIs are used, so it behaves the same on macOS.

🤖 Generated with Claude Code

WhollyMindless and others added 2 commits October 21, 2026 13:56
…page

Bun builds req.url from the Host header, so a Host such as
`127.0.0.1.8787` or `127.0.0.1:99999` made `new URL(req.url)` throw
before the request checks ran, and Bun answered with its development
error page (stack trace and local file paths). Parse defensively and
refuse with 400, and add a server error handler so any other uncaught
throw gets a plain 500.

Closes abogartz#12

Co-Authored-By: Claude Opus 5.5 <[email protected]>
main merged abogartz#27 (guarded URL parse + nosniff/CSP) while this PR was
open, resolving the same throw. Kept:
- main's 400 message and comment in the try/catch (this PR's raw-socket
  test asserts toContain("malformed request"), which main's message satisfies);
- this PR's serve() error handler — defense-in-depth beyond abogartz#27: ANY
  uncaught handler throw now answers plain JSON 500 instead of Bun's
  dev error page, stack to the terminal only;
- this PR's raw-socket test (Hosts fetch() refuses to send) ALONGSIDE
  main's fetch-based repro and the CSP/header tests.

Combined suite: 206 pass / 4 skip / 0 fail; tsc clean; biome lint exit 0.
@abogartz

Copy link
Copy Markdown
Owner

Merge conflict resolved — merged current main into the branch (4b5aa20). Context: while this PR was open, #27 landed with the same guarded-parse fix (issue #12), which is where the server.ts/viewer.test.ts conflicts came from. Rather than reduce this PR to nothing, I kept the parts that are yours and still add value:

  • Your serve({ error }) handler — strictly more than fix(viewer): guarded URL parse + nosniff/CSP (closes #12) #27: any uncaught throw from any handler now answers plain JSON internal error with the stack to the terminal only, instead of Bun's dev error page. The 500 goes through the same json() helper, so it inherits the nosniff + CSP headers too.
  • Your raw-socket test — kept alongside main's fetch-based repro, because it covers Host forms fetch() refuses to send (127.0.0.1.$port, port 99999, unclosed [::1). Compatible with main's 400 text unchanged (toContain("malformed request")).
  • In the try/catch itself, main's message and comment won since both tests pass against it.

Verified on the merged head: bun test 206 pass / 4 skip / 0 fail, tsc --noEmit clean, biome lint exit 0. Thanks — the error-handler layer is the piece #27 should have had.

@abogartz
abogartz merged commit 77d8fe2 into abogartz:main Oct 21, 2026
2 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.3.6 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless
WhollyMindless deleted the fix/viewer-malformed-host branch October 26, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewer: malformed Host header throws before the guard — Bun dev error page discloses stack/paths

2 participants