Repository navigation
feat(gateway): open pgweb in a browser with a signed link - #785
andrewpark-scaleai wants to merge 1 commit into
Conversation
9459d49 to
7985a79
Compare
fc919dc to
d8a14a1
Compare
| SESSION_SECONDS = 3600 | ||
| _KEY_LABEL = b"pgweb-console-v1" | ||
| _SIGNED = re.compile(r"([0-9]{1,12})\.([A-Za-z0-9_-]{43})") | ||
| _LINK_IN_LOG = re.compile(rf"({LINK_PARAM}=)[^&\s\"']+") |
There was a problem hiding this comment.
Encoded links leak into logs.
opening_link accepts percent-encoded parameter names, but this filter only matches literal pgweb_link=. A valid request using ?%70gweb_link=<link> opens the console while leaving the usable link in access logs. Someone with log access can replay it before expiry to get an hour-long session. Redact the same parameter spellings that opening_link accepts and test encoded names.
How this was verified: A minted link with %70gweb_link passed opening_link and opens, while its value remained in an access-log record after LinkRedaction ran.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/env/gateway/console_session.py
Line: 34
Comment:
**Encoded links leak into logs.** `opening_link` accepts percent-encoded parameter names, but this filter only matches literal `pgweb_link=`. A valid request using `?%70gweb_link=<link>` opens the console while leaving the usable link in access logs. Someone with log access can replay it before expiry to get an hour-long session. Redact the same parameter spellings that `opening_link` accepts and test encoded names.
**How this was verified:** A minted link with `%70gweb_link` passed `opening_link` and `opens`, while its value remained in an access-log record after `LinkRedaction` ran.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| pytest.param(lambda: _mint("another deployment's token").link(), id="other-deployment"), | ||
| pytest.param(lambda: _mint(shift=-200).link(120), id="expired"), | ||
| pytest.param(lambda: _mint(shift=1000).link(120), id="too-far-ahead"), | ||
| pytest.param(lambda: _mint().link()[:-1] + "A", id="forged"), |
There was a problem hiding this comment.
Forged-link test fails randomly. Replacing the last character with
A leaves the link unchanged when its signature already ends in A. The gateway then correctly returns 303, but the test expects 401. The link changes with the current second, so this causes intermittent test failures. Change a character to a guaranteed different value or use a fixed clock and a known invalid signature.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tst/unit/env/gateway/console_routes_test.py
Line: 195
Comment:
**Forged-link test fails randomly.** Replacing the last character with `A` leaves the link unchanged when its signature already ends in `A`. The gateway then correctly returns 303, but the test expects 401. The link changes with the current second, so this causes intermittent test failures. Change a character to a guaranteed different value or use a fixed clock and a known invalid signature.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.ConsoleSession derives one key per deployment from its control token and mints short links and cookies under it. The gateway trades a valid ?pgweb_link= on the pgweb index for a pgweb-only __Host- cookie, which counts only on same-origin requests or a navigation to the console index, and strips the cookie from every request it forwards. The access and httpx logs redact the link.
d8a14a1 to
a3d9e25
Compare
7985a79 to
5d14668
Compare
| fetch = {name: value for name, value in headers if name in (b"sec-fetch-site", b"sec-fetch-mode")} | ||
| if fetch.get(b"sec-fetch-site") == b"same-origin": | ||
| return True |
There was a problem hiding this comment.
Website scripts inherit database access.
same-origin includes the environment’s websites, not just pgweb. The provider publishes /website/<site>/ and /svc/pgweb/ on the same gateway host. After an engineer opens a signed link, scripts on a website they visit there can call /svc/pgweb/api/query with the browser’s cookie and read the response. Malicious website content can therefore use the engineer’s database access without knowing the control token. HttpOnly and Partitioned do not separate pages on the same origin.
Keep the console on a separate origin from website content, or otherwise isolate that content before allowing cookies to replace the control token.
How this was verified: Website and console URLs share gateway_url, website responses pass through without added isolation, and admits accepts cookie-bearing requests from any page on that origin.
Knowledge Base Used: Environment gateway and remote control
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/env/gateway/console_session.py
Line: 70-72
Comment:
**Website scripts inherit database access.** `same-origin` includes the environment’s websites, not just pgweb. The provider publishes `/website/<site>/` and `/svc/pgweb/` on the same gateway host. After an engineer opens a signed link, scripts on a website they visit there can call `/svc/pgweb/api/query` with the browser’s cookie and read the response. Malicious website content can therefore use the engineer’s database access without knowing the control token. `HttpOnly` and `Partitioned` do not separate pages on the same origin.
Keep the console on a separate origin from website content, or otherwise isolate that content before allowing cookies to replace the control token.
**How this was verified:** Website and console URLs share `gateway_url`, website responses pass through without added isolation, and `admits` accepts cookie-bearing requests from any page on that origin.
**Knowledge Base Used:** [Environment gateway and remote control](https://app.greptile.com/-/switch-tenant?tenant=scale-ai&next=%2Fcustom-context%2Fknowledge-base%2Fscaleapi%2Fagentenv-framework%2F-%2Fdocs%2Fenvironment-gateway.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Why
Engineers open pgweb in a browser. After #751 pgweb sits behind the gateway's control token, which a browser should never hold. This PR lets a server mint a short signed link that the gateway trades for a pgweb-only cookie.
What changed
console_session.pyis new.ConsoleSessionderives one key per deployment from its control token. A link and a cookie are both<unix expiry>.<HMAC>, with a purpose label in the MAC.GET /svc/pgweb/?pgweb_link=<link>for__Host-agentenv-pgweb(Secure; HttpOnly; SameSite=None; Partitioned; Max-Age=3600) and a 303 to./. Any other link gets 401.pgweb_link.PGWEB_CONSOLEmoves to the gateway's constants.Blast Radius
Sec-Fetch-Siteheader can't use the cookie on pgweb's API. Chromium, Firefox and Safari 16.4+ all send it.Verification
tst/unit/env/gateway/console_routes_test.pygains 43 cases, 126 in all.tst/unit/env,tst/unit/providersandtst/unit/clipass.ruff check src tstpasses.modal_vm: a minted link opens pgweb in a browser.This PR needs changes before merging because website content can use the browser’s database access and signed links can still leak into logs.
Fix with agent prompt
Summary
Adds signed browser links that open pgweb without exposing the deployment’s control token.
Diagram
sequenceDiagram participant Browser participant Gateway participant Pgweb Browser->>Gateway: GET pgweb with signed link Gateway-->>Browser: Cookie and redirect Browser->>Gateway: pgweb request with cookie Gateway->>Gateway: Check cookie and request origin Gateway->>Pgweb: Forward without cookie Pgweb-->>Browser: Console responseReviews (2) · Last reviewed commit: "feat(gateway): open pgweb in a browser w..." · Reviewed by Greptile