Repository navigation
feat(gateway): open pgweb in a browser with a signed link #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| """A browser's session on the env's pgweb console, in place of the control token, which never reaches a browser. | ||
|
|
||
| The deployment's owner mints a short link from the control token (``ConsoleSession.link``); the gateway trades it for a | ||
| cookie that opens ``/svc/pgweb/`` only. Both are ``<expiry>.<signature>``: unix seconds, and the HMAC-SHA256 of | ||
| ``link:<expiry>`` or ``cookie:<expiry>``, base64url without padding, under ``HMAC-SHA256(control token, | ||
| "pgweb-console-v1")``. A link or cookie minted for another deployment fails its signature. | ||
|
|
||
| Every Modal tunnel is same-site with every other (``modal.host`` is not a public suffix), so SameSite alone would | ||
| not keep the cookie to its own deployment's pages. The cookie therefore counts only on a request from a page of the | ||
| gateway's own origin, or on a navigation to the console's index. | ||
| """ | ||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| import hashlib | ||
| import hmac | ||
| import logging | ||
| import re | ||
| import time | ||
| from collections.abc import Callable, Iterable | ||
| from urllib.parse import parse_qsl | ||
|
|
||
| from .constants import PGWEB_CONSOLE | ||
|
|
||
| LINK_PARAM = "pgweb_link" | ||
| COOKIE_NAME = "__Host-agentenv-pgweb" | ||
| CONSOLE_PREFIX = f"/svc/{PGWEB_CONSOLE}/" | ||
| 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})") | ||
| _LINK_IN_LOG = re.compile(rf"({LINK_PARAM}=)[^&\s\"']+") | ||
|
|
||
|
|
||
| class ConsoleSession: | ||
| """Mints and checks a deployment's pgweb links and cookies. ``now`` is the clock, in unix seconds.""" | ||
|
|
||
| def __init__(self, control_token: str, now: Callable[[], float] = time.time): | ||
| self._key = hmac.new(control_token.encode(), _KEY_LABEL, hashlib.sha256).digest() | ||
| self._now = now | ||
|
|
||
| def _sign(self, purpose: str, expires_at: int) -> str: | ||
| digest = hmac.new(self._key, f"{purpose}:{expires_at}".encode(), hashlib.sha256).digest() | ||
| return f"{expires_at}.{base64.urlsafe_b64encode(digest).rstrip(b'=').decode()}" | ||
|
|
||
| def _valid(self, purpose: str, value: str, longest: int) -> bool: | ||
| match = _SIGNED.fullmatch(value) | ||
| if not match: | ||
| return False | ||
| expires_at = int(match[1]) | ||
| now = int(self._now()) | ||
| return now < expires_at <= now + longest and hmac.compare_digest(self._sign(purpose, expires_at), value) | ||
|
|
||
| def link(self, seconds: int = 120) -> str: | ||
| return self._sign("link", int(self._now()) + seconds) | ||
|
|
||
| def opens(self, link: str) -> bool: | ||
| return self._valid("link", link, LINK_MAX_SECONDS) | ||
|
|
||
| def set_cookie(self) -> str: | ||
| value = self._sign("cookie", int(self._now()) + SESSION_SECONDS) | ||
| return (f"{COOKIE_NAME}={value}; Max-Age={SESSION_SECONDS}; Path=/; Secure; HttpOnly; SameSite=None; " | ||
| "Partitioned") | ||
|
|
||
| def admits(self, scope: dict) -> bool: | ||
| """Whether ``scope``, a request to the pgweb console, carries a live cookie and comes from where one counts.""" | ||
| headers = scope.get("headers", ()) | ||
| if not any(self._valid("cookie", value, SESSION_SECONDS) for value in cookie_values(headers)): | ||
| return False | ||
| 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 | ||
|
Comment on lines
+70
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How this was verified: Website pages are forwarded unchanged on the gateway origin, and Prompt To Fix With AIThis 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. |
||
| return (fetch.get(b"sec-fetch-mode") == b"navigate" and scope["method"] == "GET" | ||
| and scope["path"] == CONSOLE_PREFIX) | ||
|
|
||
| def opening_link(self, scope: dict) -> str | None: | ||
| """The link a GET of the console's index carries, or None.""" | ||
| if scope["method"] != "GET" or scope["path"] != CONSOLE_PREFIX: | ||
| return None | ||
| return dict(parse_qsl(scope.get("query_string", b"").decode("latin-1"))).get(LINK_PARAM) | ||
|
|
||
|
|
||
| def cookie_values(headers: Iterable[tuple[bytes, bytes]]) -> list[str]: | ||
| """Every value the request's Cookie headers give the session cookie.""" | ||
| found = [] | ||
| for name, value in headers: | ||
| if name != b"cookie": | ||
| continue | ||
| for pair in value.decode("latin-1").split(";"): | ||
| key, _, cookie = pair.strip().partition("=") | ||
| if key == COOKIE_NAME: | ||
| found.append(cookie) | ||
| return found | ||
|
|
||
|
|
||
| def without_session_cookie(headers: Iterable[tuple[bytes, bytes]]) -> list[tuple[bytes, bytes]]: | ||
| """``headers`` with the session cookie taken out of each Cookie header, and a header left empty dropped.""" | ||
| kept = [] | ||
| for name, value in headers: | ||
| if name == b"cookie": | ||
| pairs = [pair for pair in value.decode("latin-1").split(";") | ||
| if pair.strip().partition("=")[0] != COOKIE_NAME] | ||
| if not any(pair.strip() for pair in pairs): | ||
| continue | ||
| value = ";".join(pairs).strip().encode("latin-1") | ||
| kept.append((name, value)) | ||
| return kept | ||
|
|
||
|
|
||
| class LinkRedaction(logging.Filter): | ||
| """Takes the link out of each log line that names one. It rewrites the argument that holds it, since uvicorn's | ||
| access log formatter reads its request line from the record's arguments.""" | ||
|
|
||
| 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) | ||
|
Comment on lines
+114
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 AIThis 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. |
||
| return True | ||
|
|
||
|
|
||
| def _redacted(text: str) -> str: | ||
| return _LINK_IN_LOG.sub(r"\1<redacted>", text) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_SIGNEDembeds12and43without 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
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!