fix(server): port upstream PTY connect route authorization (anomalyco/opencode#58258)
Issue for this PR
Closes #1614
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
With server auth enabled, a ticketed PTY WebSocket connect skips Basic/token auth so the handler can validate the ticket. That bypass was decided by hasPtyConnectTicketURL(url), a regex over the raw request URL, not by the route Effect actually dispatched, so authorization and routing could disagree about the path. The connect handler also looked the PTY up before checking origin and ticket, so an unauthenticated caller with a junk ticket or foreign Origin got 404 vs 403 and could probe which PTY IDs exist.
Ported from upstream:
- anomalyco/opencode#58258: both authorization layers (
packages/opencodeinstance HttpApi andpackages/server) readHttpRouter.RouteContextand only bypass whenroute.route.path === PTY_CONNECT_PATHand aticketquery is present. The regex helpers are replaced by a sharedPTY_CONNECT_PATHroute constant used by the endpoint definitions. - anomalyco/opencode@7b39a338a0: both connect handlers check origin and consume the ticket before the PTY existence check.
All changes wrapped in altimate_change markers tagged upstream_fix:.
How did you verify your code works?
- New test in
test/server/httpapi-pty.test.ts: a junk ticket and a cross-origin connect to a missing PTY both return 403. It fails onmain(404) and passes here. test/upstream/adversarial/upi-server.test.tsupdated for the route constant;httpapi-pty,upi-serverandhttpapi-listentests pass (22 pass, 8 existing todo).bun run typecheckclean inpackages/opencodeandpackages/server;bun run script/upstream/analyze.ts --markers --base origin/main --strictclean.
Cost side effects
None. No model calls, queries or resources added.
Screenshots / recordings
N/A, no UI change.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
- I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"
Summary by cubic
Fixes PTY WebSocket connect authorization so the ticket bypass is keyed to the matched route rather than a raw-URL regex, and bad-origin or bad-ticket requests now return 403 before revealing whether a PTY exists.
Bug Fixes
- Both authorization layers (
packages/opencodeandpackages/server) now readHttpRouter.RouteContextand only bypass credential checks when the matched route is the PTY connect path with aticketquery, so authorization cannot disagree with dispatch. - The connect handlers now validate origin and consume the ticket before the PTY existence check, so junk tickets and cross-origin requests to missing PTYs get 403 instead of 404.
- Replaced the regex path helpers with a shared
PTY_CONNECT_PATHroute constant used by both endpoint definitions.