Repository navigation
feat(gateway): open pgweb in a browser with a signed link - #824
andrewpark-scaleai wants to merge 1 commit into
Conversation
a3d9e25 to
9c8eef0
Compare
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.
9c8eef0 to
34506bd
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 gain database access.
same-origin includes pages served through /website/{service_name}/, not just pgweb. After an engineer opens a signed link, a script on one of those pages can fetch /svc/pgweb/api/query with the browser's cookie and run SQL. The website proxy streams pages without adding a sandbox, so HttpOnly and cookie stripping do not prevent this. Serve the console on a separate origin from the websites it inspects.
How this was verified: Website pages are forwarded unchanged on the gateway origin, and admits accepts their same-origin requests using the pgweb cookie.
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 gain database access.** `same-origin` includes pages served through `/website/{service_name}/`, not just pgweb. After an engineer opens a signed link, a script on one of those pages can fetch `/svc/pgweb/api/query` with the browser's cookie and run SQL. The website proxy streams pages without adding a sandbox, so `HttpOnly` and cookie stripping do not prevent this. Serve the console on a separate origin from the websites it inspects.
**How this was verified:** Website pages are forwarded unchanged on the gateway origin, and `admits` accepts their same-origin requests using the pgweb cookie.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| def filter(self, record: logging.LogRecord) -> bool: | ||
| if isinstance(record.msg, str) and LINK_PARAM in record.msg: | ||
| record.msg = _redacted(record.msg) | ||
| if isinstance(record.args, tuple): | ||
| record.args = tuple(_redacted(str(arg)) if LINK_PARAM in str(arg) else arg for arg in record.args) |
There was a problem hiding this comment.
Encoded links escape log redaction.
opening_link accepts pgweb%5flink=<link> because parse_qsl decodes the parameter name. The log filter only recognizes literal pgweb_link=, so the encoded spelling leaves a working credential in the access log. Anyone who can read it can exchange it for a session before it expires. Make redaction cover every accepted spelling, or reject noncanonical names.
How this was verified: An encoded parameter name produced an accepted link, while filtering a request line containing that same query left the full credential unchanged.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/env/gateway/console_session.py
Line: 114-118
Comment:
**Encoded links escape log redaction.** `opening_link` accepts `pgweb%5flink=<link>` because `parse_qsl` decodes the parameter name. The log filter only recognizes literal `pgweb_link=`, so the encoded spelling leaves a working credential in the access log. Anyone who can read it can exchange it for a session before it expires. Make redaction cover every accepted spelling, or reject noncanonical names.
**How this was verified:** An encoded parameter name produced an accepted link, while filtering a request line containing that same query left the full credential unchanged.
---
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 sometimes accepts. Replacing the last character with
A does not change a link that already ends in A. Because _mint uses the current time, this case intermittently sends a valid link and gets 303 instead of the expected 401. Change a character to a value guaranteed to differ, and use a fixed clock.
Prompt To Fix With AI
This is a comment left during a code review.
Path: tst/unit/env/gateway/console_routes_test.py
Line: 225
Comment:
**Forged-link test sometimes accepts.** Replacing the last character with `A` does not change a link that already ends in `A`. Because `_mint` uses the current time, this case intermittently sends a valid link and gets 303 instead of the expected 401. Change a character to a value guaranteed to differ, and use a fixed clock.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| LINK_MAX_SECONDS = 300 | ||
| SESSION_SECONDS = 3600 | ||
| _KEY_LABEL = b"pgweb-console-v1" | ||
| _SIGNED = re.compile(r"([0-9]{1,12})\.([A-Za-z0-9_-]{43})") |
There was a problem hiding this comment.
Signature limits lack names.
_SIGNED embeds 12 and 43 without naming the expiry and signature limits. The repository rule requires magic numbers to be stored in descriptively named class or instance variables. Name these limits so readers do not have to infer their meaning from the expression.
Rule Used: Store magic numbers as class or instance variables with descriptive names rather than using them inline in the code. (source)
Learned From
scaleapi/scaleapi#126388
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/env/gateway/console_session.py
Line: 31
Comment:
**Signature limits lack names.** `_SIGNED` embeds `12` and `43` without naming the expiry and signature limits. The repository rule requires magic numbers to be stored in descriptively named class or instance variables. Name these limits so readers do not have to infer their meaning from the expression.
**Rule Used:** Store magic numbers as class or instance variables with descriptive names rather than using them inline in the code. ([source](https://app.greptile.com/-/switch-tenant?tenant=scale-ai&next=%2Fcustom-context%3Fmemory%3D002e0051-41ad-46c1-9098-47433c580150))
**Learned From**
[scaleapi/scaleapi#126388](https://github.com/scaleapi/scaleapi/pull/126388)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Why
Engineers open pgweb in a browser. After #821 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.Do not merge until website pages are separated from console access, links are fully hidden from logs, and the naming requirement is satisfied.
Fix with agent prompt
Summary
Adds signed browser links that the gateway trades for a one-hour pgweb cookie, without exposing the control token.
Diagram
sequenceDiagram participant Owner as Deployment owner participant Browser participant Gateway participant Pgweb as pgweb Owner->>Owner: Sign short-lived link with control token Owner->>Browser: Share signed link Browser->>Gateway: GET console with signed link Gateway->>Gateway: Check signature and expiry Gateway-->>Browser: Set session cookie and redirect Browser->>Gateway: Request pgweb with cookie Gateway->>Gateway: Check cookie and browser request origin Gateway->>Pgweb: Forward request without session cookie Pgweb-->>Browser: Console response through gatewayReviews (1) · Last reviewed commit: "feat(gateway): open pgweb in a browser w..." · Reviewed by Greptile