Repository navigation
gateway: an unset token is no token; refuse browsers from other sites - #629
Conversation
Two reports, both confirmed and reproduced.
An unset ${PASCLAW_GATEWAY_TOKEN} was the token. The DigitalOcean
template sets "token": "${PASCLAW_GATEWAY_TOKEN}", the config loader
keeps a marker whose variable is unset as its literal text, and the
gateway compared bearers against it -- so anyone who sent that public
string had the API, shell and files included (the startup log said "no
client token will match"). GetEffectiveGatewayToken now returns '' for
an unset marker: no token. Only the gateway token; other ${VAR} markers
are unchanged.
With no token, a web page could drive the agent. Nothing checked Origin
or Host, and any body is read as JSON, so a page on another site could
POST text/plain (no preflight) and run tools, and a page that rebound its
name to 127.0.0.1 could read the answers too. A token-less gateway now
refuses, with 403, a Host that is not localhost / *.localhost / 127.x /
::1 (or gateway.allowed_hosts, or a specific bind address), an Origin
that is not its own -- the --apps-port origin included -- and
Sec-Fetch-Site: cross-site. Local clients, SDKs, MCP clients and the
gateway's own pages send none of that. Exempt: the token-exempt routes,
and the relay surface when it presents the relay token. With a token
set nothing changes: the token is the gate.
Together: a Docker or DO deploy without the token answers only localhost
names, so requests through the app's domain get 403 rather than an open
agent. The Docker and local DO examples publish on 127.0.0.1, and
.env.example no longer carries a placeholder token everyone can read.
make test-gateway-local-only drives it through two live gateways.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: dbe48bb0b0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (GetEffectiveGatewayToken(FCfg) = '') and | ||
| not IsExemptRoute(ARequest.Command, Doc) and | ||
| not RelayTokenAuthorises(Doc, | ||
| ARequest.RawHeaders.Values['Authorization'], | ||
| ARequest.Params.Values['token']) then |
There was a problem hiding this comment.
Allow sandboxed app assets through the browser guard
With the default token-less gateway, this guard also rejects legitimate assets requested by desktop app frames. desktop.html creates those frames with sandbox="allow-scripts allow-forms", giving them opaque origins; their subresource requests consequently carry Sec-Fetch-Site: cross-site under the Fetch Metadata algorithm. /apps/<project>/pasclaw.js is neither an exempt route nor relay-authorized, so it now receives 403. Built-in apps in PasClaw.Suite.pas, including Notes, explicitly load that script and depend on its pasclaw API, so they lose their read/write functionality even though the iframe document loads successfully. Preserve access to the app asset surface without exempting the privileged API.
Useful? React with 👍 / 👎.
The token-less guard now checks exactly the two attacks and nothing else: an Origin from another site, and a Host that is a domain name other than localhost or a gateway.allowed_hosts entry (DNS rebinding). Any IP Host passes, since an IP address cannot be rebound. That covers 0.0.0.0, LAN addresses, and relay workers by IP. - Drop the general Sec-Fetch-Site rule; it refused app iframes when the desktop was opened under another local name. Only /v1/relay/* without the relay token still refuses cross-site, so an <img> cannot drain relay jobs. - Accept an Origin under an allowed domain name, so a reverse proxy that rewrites Host to 127.0.0.1 works. Other ports of localhost or an IP stay refused (the apps origin). - The startup log and the serve banner say who a token-less gateway is open to, by bind address: on 0.0.0.0 or a LAN address, any client that reaches it, shell included. - attach and the gateway run file use the effective token (env or config), not the raw config field. - Refusals log one warning per 10 seconds; the rest go to debug. - Remove ListenerAllowedHosts and the 127.x / ::1 branches of the localhost check, which did nothing once IP Hosts pass. - The live test has curl timeouts and checks that its own gateways answer. Unit and live cases are updated, plus docs (gateway, security, configuration, providers-relay). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
The relay rule refused only Sec-Fetch-Site: cross-site, but app code on the --apps-port origin is same-site (same host, another port). It could still claim relay jobs with a no-cors GET of /v1/relay/poll. Without the relay token, the relay surface now answers only the gateway's own pages (same-origin) and non-browsers. Real workers present the relay token or send no Sec-Fetch-Site. The live test covers the same-site drain and the gateway's own page. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
Found in a live browser run: Chromium sends Sec-Fetch-Site only to trustworthy URLs (localhost, 127.x, https). An <img> aimed at http://0.0.0.0 or a LAN address carries neither Origin nor Sec-Fetch-Site, so the relay rule covers the default loopback gateway only. A token-less gateway on another address is open to its network anyway. The comment and docs now say so. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
With --apps-port, both the apps origin the desktop frames and the one frame parent the apps listener allows were fixed at startup from --addr. A desktop opened under any other name showed no apps. For example, localhost against --addr 127.0.0.1, or anything against --addr 0.0.0.0. The browser blocked the frame by CSP frame-ancestors. From another machine, --addr 0.0.0.0 also sent the iframe to the viewer's own 0.0.0.0. Both now follow the request's Host: - /v1/desktop/config answers with the apps port under the name the desktop used. - The apps listener names the desktop's port under the name the app was asked for. - The --addr forms remain the fallback for a request with no usable Host. - SiblingOrigin accepts only host[:port] characters, so a Host cannot inject a CSP directive. - A page on any other origin still cannot frame apps. Tests cover desktop config by name, the CSP parent by Host with the no-Host fallback, and SiblingOrigin's edge cases. Four revert checks each fail a test. Live in Chromium, a desktop opened as localhost, 127.0.0.1, 0.0.0.0 and a LAN IP shows the app with its saved state, and a foreign page that frames it is refused. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U
Fixes two security reports. Both were confirmed in source and reproduced against a running gateway.
1. An unset
${PASCLAW_GATEWAY_TOKEN}was the tokenThe bug:
digitalocean/config.template.jsonsets"token": "${PASCLAW_GATEWAY_TOKEN}". When the variable is unset, the config loader keeps the marker as literal text, and the gateway compared bearers against that text. The template is public, so the "password" was too.Authorization: Bearer ${PASCLAW_GATEWAY_TOKEN}?token=%24%7BPASCLAW_GATEWAY_TOKEN%7Dread_file /etc/hostnamewith the literalThe startup log said the opposite: "no client token will match".
The fix:
GetEffectiveGatewayTokenreturns''for a marker whose variable is unset, which means no token.${VAR}markers (provider keys and so on) behave as before.bearer-token auth DISABLED -- gateway.token is ${PASCLAW_GATEWAY_TOKEN} and that env var is not set; ....pasclaw attachand the gateway run file now use the same effective token as the gateway, so an env-only token works there too.2. With no token, any web page could drive the local agent
The bug: with default settings (
127.0.0.1, no token), nothing checkedOriginorHost, and any request body was read as JSON.text/plainwith no CORS preflight. A planted memory fact and an MCPread_fileboth ran. The page can't read the answers, but/v1/chatdrives the agent and its shell, and default permission rules don't ask before running it.127.0.0.1is same-origin with the gateway./v1/configwas served toHost: attacker.example, so such a page can read every answer.The fix: a token-less gateway refuses, with 403, the two things only such a page does, and nothing else:
Originfrom another site (another site, another port,null). The gateway's own pages, and pages under a domain name ingateway.allowed_hosts, are fine. This also refuses the--apps-portorigin, so app code can't call the main API.Hostthat is a domain name other thanlocalhost,*.localhostor a name in the newgateway.allowed_hosts. An IP address can't be rebound, so any IPHostis fine:127.x,0.0.0.0,::, a LAN address.One narrow extra rule covers the relay surface.
GET /v1/relay/pollhands out a job, and an<img>or no-cors fetch carries noOrigin. So without the relay token,/v1/relay/*answers only the gateway's own pages (Sec-Fetch-Site: same-origin) and non-browsers. On the default loopback gateway, that keeps other sites, and app code on the apps port, from draining the queue (see the live run for why only there).What's unaffected: CLI tools, SDKs, MCP clients, relay workers by IP, the web UI, the desktop and app iframes. The same goes for the exempt routes (
/,/desktop, health, version,/webhooks/*), the relay surface when it presents its token, and any gateway with a token (the token is the gate).The check:
LocalRequestRefusalinPasClaw.Gateway.Auth(pure). PUT and DELETE reach it too, throughOnCommandOther. Refusals log one warning per 10 seconds; the rest go to the debug log with a count.Breaking for one setup: a token-less gateway reached by a domain name (a reverse proxy, a LAN name, a remote
pasclaw relay --gateway-url http://name:8088) now gets 403 until that name is added togateway.allowed_hosts, or a token is set. The 403 message and the docs say which. Reaching it by IP works as before.Together
A Docker or DigitalOcean deploy without the token now has no token. Requests through the app's domain get 403 with a message saying to set
PASCLAW_GATEWAY_TOKEN; health checks still pass. Before, the same deploy was an agent open to anyone who sent the template's text.What remains: a client that reaches the port directly can send any
Host. A token-less gateway reachable over the network is therefore open to whoever reaches it.servebanner now say so for a non-loopback bind:the gateway listens on 0.0.0.0, so /v1/* and /mcp -- shell and files included -- answer ANY client that can reach it.docker/README.mdandmake -C digitalocean runpublish on127.0.0.1..env.exampleno longer carries a placeholder token that everyone can read.Review fixes (second and third commits)
The first version also refused every
Sec-Fetch-Site: cross-siterequest and anyHostthat was an IP other than loopback. Both broke real setups (desktop iframes,0.0.0.0, relay workers by IP) and added little, so they're gone. These commits also fix:Host;attachand the run file using the raw config token;Tests
gateway_token_tests:LocalRequestRefusalcases: IPs,0.0.0.0, LAN, IPv6,*.localhost, look-alike names, opaque and other-port origins, the proxy rewrite, default ports;IsIPLiteral, and the startup wording by bind address.make test-gateway-local-only: two live gateways, 23 checks end to end.Origin: null403, apps port 403, rebound name 403, relay drain from another site or the apps port 403. Iframe navigation 200,Host: 0.0.0.0200, LAN IP 200, proxy rewrite 200, allowed name 200, relay from its own page or with its token 200, preflight 204. Nothing planted, and one warning for the burst.127.x/0.0.0.0names in the loopback list and the bind address as an allowed name. Both were removed. It also showed the default-port case had no test, so it got one.0.0.0.0bind prints the "ANY client" line, and the run file records the env token's hash.make testpasses on FPC 3.2.2 at the second commit and again with the fifth (head).Live run (relay, real browser)
The gateway ran on
0.0.0.0with--apps-port, a relay provider, andpermissionsrules that ask beforeshell_exec(withecho safe*allowed). The model was played through the relay by a worker that connected by LAN IP. Headless Chromium drove the desktop and the web UI.pasclaw agent --attach: ask answered in the desktopbuild_app→ app opened in the desktop (at0.0.0.0)<img>relay poll)rebind.testto 127.0.0.1)/v1/*call 403, including a same-origin read of/v1/configPASCLAW_GATEWAY_TOKENattachwith an env-only token works and the run file records its hash; the attack page gets 401servebanner0.0.0.0says ANY client;127.0.0.1says this machineTwo things the run turned up:
Fixed, a doc gap: Chromium sends
Sec-Fetch-Siteonly tolocalhost,127.xand https. So the relay rule covers the default loopback gateway, but an<img>aimed athttp://0.0.0.0or a LAN address carries no header. Such a gateway is open to its network anyway. The comment and docs now say so (fourth commit).Fixed, an older bug (fifth commit): with
--apps-port, the apps origin and the one allowed frame parent were fixed at startup from--addr. A desktop opened under any other name (for examplelocalhostwhen--addris0.0.0.0) showed no app windows, because the browser blocked the frame by CSP. Both now follow the request'sHost:/v1/desktop/configanswers with the apps port under the desktop's own name;frame-ancestorsnames the desktop's port under the app's name;--addrforms remain the fallback when there is noHost, andSiblingOriginadmits only host[:port] characters, so aHostcan't inject a CSP directive.Live in Chromium, the desktop opened as
localhost,127.0.0.1,0.0.0.0and the LAN IP shows the app with its saved state, and a page on another origin still can't frame it. Newdesktop_routes_testscases cover this; four revert checks each fail a test.Docs:
docs/gateway.md,docs/security.md,docs/configuration.md(gateway.allowed_hosts),docs/providers-relay.md,docs/desktop.md(apps origin),docker/README.md,digitalocean/.do/app.yaml.🤖 Generated with Claude Code
https://claude.ai/code/session_01KYD1zZPD7GuM8pF1btwi2U