Skip to content

feat(gateway): open pgweb in a browser with a signed link - #785

Closed
andrewpark-scaleai wants to merge 1 commit into
andrewpark/vuln-3253-sidecars-behind-gatewayfrom
andrewpark/vuln-3253-pgweb-gateway-session
Closed

andrewpark-scaleai wants to merge 1 commit into
andrewpark/vuln-3253-sidecars-behind-gatewayfrom
andrewpark/vuln-3253-pgweb-gateway-session

Conversation

@andrewpark-scaleai

@andrewpark-scaleai andrewpark-scaleai commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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.py is new. ConsoleSession derives 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.
  • The gateway trades a valid 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.
  • The cookie stands in for the token only on pgweb's routes, and only on same-origin requests or a GET navigation to the console index.
  • The gateway strips the cookie from every request it forwards.
  • uvicorn's access log and httpx's log redact pgweb_link.
  • PGWEB_CONSOLE moves to the gateway's constants.

Blast Radius

  • A gateway with no token, or with no pgweb console, behaves as before.
  • A browser that sends no Sec-Fetch-Site header 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.py gains 43 cases, 126 in all.
  • Unit trees tst/unit/env, tst/unit/providers and tst/unit/cli pass. ruff check src tst passes.
  • Local Docker and a live modal_vm: a minted link opens pgweb in a browser.

RetriggerView in GreptileConfidence Score: 1/5

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 All in CursorFindings

  1. P1 Security Website scripts inherit database access ▶
  2. P1 Security Encoded links leak into logs. ▶
  3. P2 Forged-link test fails randomly. ▶
Fix with agent prompt
### Issue 1
src/agent_env/env/gateway/console_session.py:70-72
**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.

### Issue 2
src/agent_env/env/gateway/console_session.py:undefined-32
`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.

### Issue 3
tst/unit/env/gateway/console_routes_test.py:undefined-195
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.

Summary

Adds signed browser links that open pgweb without exposing the deployment’s control token.

  • Engineers can open pgweb with a short signed link.

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 response
Loading

Reviews (2) · Last reviewed commit: "feat(gateway): open pgweb in a browser w..." · Reviewed by Greptile

@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrewpark/vuln-3253-sidecars-behind-gateway branch from 9459d49 to 7985a79 Compare November 18, 2026 23:54
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrewpark/vuln-3253-pgweb-gateway-session branch from fc919dc to d8a14a1 Compare November 18, 2026 23:55
@andrewpark-scaleai andrewpark-scaleai changed the title feat(gateway): open pgweb in a browser with a signed link and a pgweb-only cookie (VULN-3253) feat(gateway): open pgweb in a browser with a signed link Nov 19, 2026
@andrewpark-scaleai
andrewpark-scaleai marked this pull request as ready for review November 19, 2026 18:21
@andrewpark-scaleai
andrewpark-scaleai requested a review from a team as a code owner November 19, 2026 18:21
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\"']+")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security 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.

Fix in Cursor Fix in Claude Code Fix in Codex

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"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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.

Fix in Cursor Fix in Claude Code Fix in Codex

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.
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrewpark/vuln-3253-pgweb-gateway-session branch from d8a14a1 to a3d9e25 Compare November 19, 2026 18:48
@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrewpark/vuln-3253-sidecars-behind-gateway branch from 7985a79 to 5d14668 Compare November 19, 2026 18:48
@andrewpark-scaleai
andrewpark-scaleai deleted the andrewpark/vuln-3253-pgweb-gateway-session branch November 19, 2026 18:52
Comment on lines +70 to +72
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security 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.

Fix in Cursor Fix in Claude Code Fix in Codex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant