Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/agent_env/env/envs/service_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from agent_env.artifact import Artifact, DockerImageArtifact
from agent_env.env.env import Env
from agent_env.env.gateway.constants import PGWEB_CONSOLE

SERVICE_DB_PORT = 5432
DB_USER = "agentenv"
Expand All @@ -15,7 +16,7 @@

# The service db's containers: compose service names and ports.
DATABASE_SERVICE_NAME = "servicedb"
PGWEB_SERVICE_NAME = "pgweb"
PGWEB_SERVICE_NAME = PGWEB_CONSOLE
DB_WEB_PORT = 8081 # pgweb: same port host-side and in-container (8081:8081)
DB_MCP_SERVICE_NAME = "db-mcp"
DB_MCP_PORT = 18767 # db-mcp host/tunnel port
Expand Down
123 changes: 123 additions & 0 deletions src/agent_env/env/gateway/console_session.py
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})")

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

_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

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

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

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

return True


def _redacted(text: str) -> str:
return _LINK_IN_LOG.sub(r"\1<redacted>", text)
2 changes: 2 additions & 0 deletions src/agent_env/env/gateway/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def data_plane_load_timeout_s(size_bytes: int | None) -> int:
# The env's SQL consoles (pgweb, db-mcp), each as `key=url` on the env's network, comma-separated. The gateway serves
# each at /svc/<key>/, every path of it behind its control token, and none of them when it has no token.
CONSOLE_URLS_ENV = "CONSOLE_URLS"
# The console a browser may open with a link instead of the control token (`console_session`): pgweb's key.
PGWEB_CONSOLE = "pgweb"
# The route a clock follower long-polls, with its read token.
CLOCK_WATCH_PATH = "/clock/watch"
CLOCK_FOLLOWERS_PATH = "/clock/followers"
Expand Down
31 changes: 28 additions & 3 deletions src/agent_env/env/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
WEBSITE_BROWSE_URL_PARAM,
WELL_KNOWN_PATH,
WILDCARD,
PGWEB_CONSOLE,
GatewayMode,
tool_name_clashes_text,
)
Expand All @@ -81,6 +82,7 @@
from .child_sessions import ChildSessions, is_dead_session
from .clock import Clock, ClockError, _iso
from .clock_followers import ClockFollowers, parse_wait
from .console_session import ConsoleSession, LinkRedaction, without_session_cookie
from .get_time import GET_TIME_TOOL_NAME, build_get_time_tool
from .rest_proxy import (
ROLE_HEADER_BYTES,
Expand Down Expand Up @@ -212,11 +214,13 @@ def __init__(
log = logging.getLogger(name)
if not any(isinstance(f, TokenRedaction) for f in log.filters):
log.addFilter(TokenRedaction())
log.addFilter(LinkRedaction())
self.gateway_mode = gateway_mode
self._service_db_url = service_db_url
self._control_bearer = f"Bearer {control_token}".encode() if control_token else None
self._control_token_children = frozenset(control_token_children)
self._consoles = dict(console_urls or {}) if control_token else {}
self._console_session = ConsoleSession(control_token) if PGWEB_CONSOLE in self._consoles else None
if clashes := sorted(self._consoles.keys() & self.rest_proxy_urls.keys()):
raise ValueError(f"console keys {clashes} are also backing servers' /svc keys")
self._computers = frozenset(computers)
Expand Down Expand Up @@ -620,17 +624,23 @@ def _asgi_app(self):
async def app(scope, receive, send):
if self._control_bearer and scope["type"] in ("http", "websocket"):
headers = scope.get("headers", ())
if scope["type"] == "http" and self._console_session and (
link := self._console_session.opening_link(scope)) is not None:
await self._open_console(link)(scope, receive, send)
return
if scope["type"] == "http" and self._takes_control_token(scope) and not hmac.compare_digest(
dict(headers).get(b"authorization", b""), self._control_bearer):
dict(headers).get(b"authorization", b""), self._control_bearer) and not (
self._console_session and self._is_pgweb(scope) and self._console_session.admits(scope)):
logger.warning("refused %s %s: it takes the env's control token", scope["method"], scope["path"])
response = self._json_response({"ok": False, "error": "this route takes the env's control token"}, 401)
await response(scope, receive, send)
return
# Only the clock watch reads the token, which the computers the gateway fronts send it, and only they
# receive it, through their /svc routes; every other request goes on without it.
if not self._keeps_control_token(scope["path"]):
scope = {**scope, "headers": [(name, value) for name, value in headers if not (
name == b"authorization" and hmac.compare_digest(value, self._control_bearer))]}
headers = [(name, value) for name, value in headers if not (
name == b"authorization" and hmac.compare_digest(value, self._control_bearer))]
scope = {**scope, "headers": without_session_cookie(headers)}
if scope["type"] != "http":
await inner(scope, receive, send)
return
Expand All @@ -647,6 +657,21 @@ async def app(scope, receive, send):

return app

def _open_console(self, link: str) -> Response:
"""The answer to a link to the pgweb console: its session cookie and the console, or 401 for a link this
deployment did not mint or that has expired."""
if not self._console_session.opens(link):
logger.warning("refused a pgweb link: not this deployment's, or expired")
return self._json_response({"ok": False, "error": "this link is not valid for this env"}, 401)
return Response(status_code=303, headers={"location": "./", "set-cookie": self._console_session.set_cookie(),
"cache-control": "no-store", "referrer-policy": "no-referrer"})

@staticmethod
def _is_pgweb(scope: dict) -> bool:
"""Whether ``scope`` is routed to the pgweb console: the service its path names, as the /svc route reads it."""
prefix, _, rest = scope["path"].lstrip("/").partition("/")
return prefix == "svc" and rest.partition("/")[0] == PGWEB_CONSOLE

def _takes_control_token(self, scope: dict) -> bool:
"""Whether a request takes the deploy's control token: a control route, the env's data plane, any path of a SQL
console behind /svc, or one of ``token_paths`` behind /svc, by the path as routed or by the one the backing
Expand Down
Loading
Loading