Skip to content

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

Open
andrewpark-scaleai wants to merge 1 commit into
mainfrom
andrewpark/pgweb-signed-link
Open

andrewpark-scaleai wants to merge 1 commit into
mainfrom
andrewpark/pgweb-signed-link

Conversation

@andrewpark-scaleai

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

Copy link
Copy Markdown
Contributor

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

Do not merge until website pages are separated from console access, links are fully hidden from logs, and the naming requirement is satisfied.

Fix All in CursorFindings

  1. P1 Security Website scripts gain database access ▶
  2. P1 Security Encoded links escape log redaction ▶
  3. P2 Forged-link test sometimes accepts ▶
  4. P2 Signature limits lack names ▶
Fix with agent prompt
### Issue 1
src/agent_env/env/gateway/console_session.py:70-72
**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.

### Issue 2
src/agent_env/env/gateway/console_session.py:114-118
**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.

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

### Issue 4
src/agent_env/env/gateway/console_session.py:31
**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.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds signed browser links that the gateway trades for a one-hour pgweb cookie, without exposing the control token.

  • Browsers can open pgweb with a short signed link.

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

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

@andrewpark-scaleai
andrewpark-scaleai force-pushed the andrewpark/pgweb-signed-link branch from a3d9e25 to 9c8eef0 Compare November 19, 2026 20:22
Base automatically changed from andrewpark/gateway-console-routes to main November 20, 2026 01:07
@earakely-scale
earakely-scale marked this pull request as ready for review November 20, 2026 01:07
@earakely-scale
earakely-scale requested a review from a team as a code owner November 20, 2026 01:07
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.
@earakely-scale
earakely-scale force-pushed the andrewpark/pgweb-signed-link branch from 9c8eef0 to 34506bd Compare November 20, 2026 01:09
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 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.

Fix in Cursor Fix in Claude Code Fix in Codex

Comment on lines +114 to +118
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)

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

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

Fix in Cursor Fix in Claude Code Fix in Codex

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

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 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!

Fix in Cursor Fix in Claude Code Fix in Codex

This branch has not been deployed

No deployments
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