Repository navigation
fix: warn when viewer allows non-loopback hosts - #192
Conversation
abogartz
left a comment
There was a problem hiding this comment.
Review: CHANGES_REQUESTED
Reviewed at head 2978b4f on macOS / Bun 1.4.3. The warning itself is right and the tests are real — but the classifier you added throws on a plausible operator value, and because it runs before serve(), the viewer no longer starts at all.
BLOCKING — an IPv6 zone id in OPENCODE_VIEWER_ALLOWED_HOSTS kills viewer startup
isLoopbackHostname (src/viewer/server.ts:62-67) canonicalises the IPv6 arm with new URL(\http://[${host}]/`). Bun's isIP()accepts a zone id, WHATWG URL does not, so the check throws andstartViewer()rejects beforeserve()` is ever reached. Fresh process, real startup:
OPENCODE_VIEWER_ALLOWED_HOSTS=fe80::1%en0 bun run src/viewer/server.ts
66 | // URL canonicalization also recognizes the expanded IPv6 loopback spelling.
67 | return isIP(host) === 6 && new URL(`http://[${host}]/`).hostname === "[::1]";
^
TypeError: Invalid URL
input: "http://[fe80::1%en0]/",
code: "ERR_INVALID_URL"
at isLoopbackHostname (src/viewer/server.ts:67:30)
at <anonymous> (src/viewer/server.ts:142:47) ← the .some() in startViewer
No banner, and nothing listens: /info → no response. Unchanged 525ed89 with the same env prints the banner and answers /info. Same result for fe80::1%25en0 and 2001:db8::%eth0.
This is not exotic: a link-local address with %zone is exactly how someone binds a service to one LAN interface, and the value only ever had to match a Host header before this PR — it was inert. Two asks:
- Drop the URL parse. The loopback set is closed — compare spellings (
::1and the expanded0:0:0:0:0:0:0:1, lower-cased) instead of canonicalising arbitrary input. - If any input can still reach a throwing parse, wrap it so an unparseable entry counts as non-loopback (warn, fail-visible) rather than aborting startup, and consider evaluating the exposure check after
serve()so a warning can never prevent the viewer from coming up.
Please add the zone-id case to test/viewer-startup.test.ts — the current test.each rows all use values that parse.
What I verified is good
- Red on base exactly as claimed: your file on a clean
525ed89gives 5 fail / 2 pass — the five warning cases (LAN address, external hostname mixed with loopback, IPv6 non-loopback, localhost lookalike, wildcard bind), the two quiet cases passing; at head 7 pass / 0 fail. Unit 729 / 12 skip / 0 fail, typecheck clean, biome 409 warnings — flat versus base, so no new lint debt. - These are real startups.
Bun.spawnSyncof the actual server with a fresh temp store, asserting on captured stderr rather than a stubbedconsole.warn, and a wildcard-bind case whose store is an empty temp dir. The suppression half is not vacuous either: the "loopback aliases" row (LOCALHOST, 127.0.0.2, ::1, [::1], 0:0:0:0:0:0:0:1, ,) would fail if the warning fired unconditionally. - My own classification matrix (fresh process per input, 21 cases): quiet for unset,
127.0.0.1,localhost,LOCALHOST(case is normalised inallowedHostnames()before the classifier),[::1],0:0:0:0:0:0:0:1, and a trailing empty entry; warning forlocalhost.evil.com,127.0.0.1.evil.com,10.0.0.5,2606:4700:4700::1111,::2,*, a padded10.0.0.5, and a URL-shapedhttp://127.0.0.1. Every ambiguous input fails toward warning, which is the correct default. - No log injection. The message is a constant string, so nothing can be forged into it:
OPENCODE_VIEWER_ALLOWED_HOSTS=$'10.0.0.5\n[Fake] viewer bound to 127.0.0.1 only'produced exactly one warning line, one banner, no injected line and no raw escapes on the stream. Same for an ANSI-clear payload. - The guards are untouched. The diff is the
isLoopbackHostnamehelper, the oneconsole.warninstartViewer, and the banner sentence;rejectRequest's Host/port and Content-Type/Origin checks are not in the diff, and the existing guard suites still pass. - Banner truthfulness holds. With
OPENCODE_MEMORY_VIEWER_PORToverriding the default, the printed URL carries the real bound port (http://${HOST}:${server.port ?? port}atsrc/viewer/server.ts:186-187), and the(127.0.0.1 only; …)clause is gone, so the CLI no longer claims something the process did not do.
Nits
127.1warns. Bun'sisIPdoes not accept the abbreviated form, so it is not classified as IPv4. That is the safe direction (a quiet false-negative would be the real bug) — noting it only so the behaviour is a decision rather than a surprise.- The classifier's IPv4 arm is
isIP(host) === 4 && host.startsWith("127."), which is right, but the loopback test now lives in two concepts (allowedHostnamesnormalising case/brackets,isLoopbackHostnameclassifying). One comment saying "input arrives already trimmed and lower-cased" would stop the next reader re-guarding it defensively — and that re-guarding is where the URL parse crept in. - Consider naming the constant and reusing it:
0.0.0.0and::are the two "reachable from anywhere" binds, and they are only special here because they are non-loopback. If a future change ever allows binding withoutHostenforcement, this warning's wording ("anyone who can reach it with an allowed Host") is the sentence that has to stay accurate.
Not verified here: Windows / Bun 1.4.4 (CI's pin, where all four legs are green at this head), and whether any Bun build other than 1.4.3 accepts a zone id in isIP — the crash reproduces on this host regardless of which side of that split is responsible, which is why I'd take the closed-set comparison over the parse.
|
Fixed in The IPv6 arm now uses closed spelling comparisons, with no URL parse. Unrecognized entries warn conservatively. The exposure check runs after All 12 startup cases now pass. Every subprocess performs an actual HTTP request to the running viewer and requires status 200, including the zone-ID cases; stderr still requires exactly one constant warning for exposed configurations and silence for the quiet cases. URL-shaped input also has a conservative-warning regression. Full Windows / Bun 1.4.4 gate passed: typecheck, lint, 734 unit tests (12 skips), and 11 real-host E2E tests. After a test-string-only lint cleanup, all 12 startup tests and lint passed again; the final lint count is the unchanged baseline of 409 advisory warnings. Ready for re-review when convenient. Please feel free to decline or defer this if you prefer a different warning policy. |
abogartz
left a comment
There was a problem hiding this comment.
Round 2: APPROVED
Re-verified at c591437. The throw is gone and you took the better of the two fixes I suggested.
The crash, re-run. Same probe as round 1 — fresh process per value, real startup, then an HTTP request:
ALLOWED_HOSTS=fe80::1%en0 /info=404 warned=true banner=true
ALLOWED_HOSTS=fe80::1%25en0 /info=404 warned=true banner=true
ALLOWED_HOSTS=2001:db8::%eth0 /info=404 warned=true banner=true
ALLOWED_HOSTS=::1 /info=404 warned=false banner=true
Round 1 on these three values: no banner, no-response, TypeError: Invalid URL. The classifier is now a closed set (::1 and the expanded spelling), which is the right call — it cannot throw on input, and it does not need a parser to answer a three-value question.
The second half of my ask is in too: the warning moved after serve() (src/viewer/server.ts:183-188), so a future defect in the classifier degrades into a missing warning rather than a viewer that never comes up. Worth noting that this ordering also means the reported bound port is available when the message prints, and the wildcard-bind comment sits where the reader needs it.
Pinning: test/viewer-startup.test.ts is 12 pass at head, and on the pre-fix tree exactly the three zone-id rows fail (9 pass / 3 fail) — the regression is attributed to the fix, not to the case list having grown. The cases now also cover the two conservative warnings I flagged as judgement calls (127.1, URL-shaped entry), which turns my nit into documented behaviour: the comment says abbreviated IPv4 and ambiguous aliases deliberately fall toward warning.
I also like the drive-by hardening of the harness itself: the startup child now issues a real fetch to the bound port before stopping and throws on a non-200. That closes the class of green-but-dead startup, which is precisely how the round-1 defect would otherwise have hidden — the old test only needed stderr to match.
Gates: typecheck clean, biome exit 0 at 409 warnings — flat versus base, unit 734 pass / 12 skip / 0 fail, real-host e2e 11 pass / 0 fail.
Remaining from my list, unchanged and non-blocking: 0177.0.0.1-style octal and * still warn (safe direction), and the loopback test still lives across two places — the builder normalises case/brackets, the classifier classifies — so the new comment about arriving trimmed and lower-cased is what keeps the next reader from re-adding a defensive parse and reintroducing a throw.
|
🎉 This PR is included in version 0.14.28 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Fixes #13.
Starting the viewer with a non-loopback allowed hostname emits one stderr warning explaining that reachable clients can read or modify the private store over unauthenticated plain HTTP. The check includes the bind hostname, which is already implicitly allowed. It recognizes localhost, full IPv4 loopback addresses, and the IPv6 spellings
::1and0:0:0:0:0:0:0:1. Other spellings and ambiguous entries conservatively warn. Host/Origin enforcement is unchanged.The warning runs after the server binds and uses a constant message without echoing environment-provided host strings. Classification does not URL-parse operator input: IPv6 zone IDs in the allowed-host list warn without preventing startup. The CLI banner no longer claims it always binds to 127.0.0.1 when OPENCODE_VIEWER_HOST overrides that default.
Validation on Windows/Bun 1.4.4: 12 real-startup subprocess cases cover default/loopback silence, non-loopback names and addresses, mixed lists, a localhost lookalike, wildcard binding, three IPv6 zone-ID forms, abbreviated IPv4, and URL-shaped entries. Each subprocess also fetches the running viewer over HTTP and requires status 200. All three new zone-ID cases fail with the previous classifier at
2978b4fand pass with the fix. The original five warning cases failed on base525ed89, with the two quiet cases passing.Full Windows / Bun 1.4.4 gate passed: typecheck, lint, 734 unit tests (12 skips), and 11 real-host E2E tests. After a test-string-only lint cleanup, all 12 startup tests and lint passed again; the final lint count is the unchanged baseline of 409 advisory warnings.
Please feel free to decline or close this if you prefer different warning wording or behavior for your project.