Skip to content

The agent that grows with you

fix(gateway): mint forwarded tickets only for structured login subjects

liuhao1024a055077c8859
diff --git a/gateway/control_socket.py b/gateway/control_socket.py
index 385f43ace63..a12734a9fb8 100644
--- a/gateway/control_socket.py
+++ b/gateway/control_socket.py
@@ -128,6 +128,20 @@ def _detect_supervisor() -> str:
return "external" if "--external-supervisor" in sys.argv else "manual"
+def _structured_login_subject(value: str) -> bool:
+ """Whether ``value`` is ``authenticated_subject``'s namespaced login form
+ ``auth:v1:[provider, issuer, user]`` (gateway.session_identity) — the only identity
+ a same-user peer may forward a ticket for. Bare labels (``admin``, ``uid:501``) fail:
+ they can collide with another stored owner representation."""
+ if not value.startswith('auth:v1:'):
+ return False
+ try:
+ parts = json.loads(value[len('auth:v1:'):])
+ except ValueError:
+ return False
+ return isinstance(parts, list) and len(parts) == 3 and all(isinstance(p, str) for p in parts)
+
+
def launch_supervisor(identity: dict[str, Any]) -> Optional[str]:
"""What launched the gateway behind an ``identify`` answer (``manual``, ``desktop``, ``systemd``, ...).
@@ -337,10 +351,14 @@ class GatewayControlServer:
# A verified same-user peer may name the principal it acts for (a standalone dashboard
# forwarding a mutation for its authenticated user, #138915): the socket's peer gate
# stays the trust boundary — the forwarded subject only relabels who that same peer is,
- # it never admits a less-verified caller. Anything shaped wrong fails closed.
+ # it never admits a less-verified caller. Only a structured login subject is nameable
+ # (``authenticated_subject``'s ``auth:v1:[provider, issuer, user]`` form); a bare label
+ # like ``admin`` could collide with another stored owner representation, so it fails closed.
subject = params.get("subject", peer_subject)
if not isinstance(subject, str) or not subject or len(subject) > 256:
raise PermissionError("invalid bootstrap subject")
+ if subject is not peer_subject and not _structured_login_subject(subject):
+ raise PermissionError("invalid bootstrap subject")
ticket = self.ticket_store.mint(profile_id=params["profile_id"], subject=subject,
purpose=params["purpose"], scope=params.get("scope", "profile"))
return {"ticket": ticket, "expires_in_seconds": 30,
diff --git a/tests/gateway/test_control_socket_forwarded_subject.py b/tests/gateway/test_control_socket_forwarded_subject.py
index 9cc6731fe4e..bee6870693b 100644
--- a/tests/gateway/test_control_socket_forwarded_subject.py
+++ b/tests/gateway/test_control_socket_forwarded_subject.py
@@ -70,6 +70,20 @@ def test_a_malformed_forwarded_subject_fails_closed(tmp_path):
assert "invalid bootstrap subject" in response["error"]
+def test_a_bare_forwarded_label_fails_closed(tmp_path):
+ """Only a structured login subject is nameable: a bare label like ``admin`` (or a UID label)
+ could collide with another stored owner representation, the invariant
+ test_private_control_peer_mints_profile_bound_ticket guards."""
+ for bad in ("admin", "basic:wweic", "uid:501", 'auth:v1:"basic"', "auth:v1:[1,2,3]"):
+ response = json.loads(
+ _server(tmp_path).handle_request_line(
+ _mint_line(tmp_path, bad), peer_subject="uid:501"
+ )
+ )
+ assert response["ok"] is False, bad
+ assert "invalid bootstrap subject" in response["error"]
+
+
def test_a_forwarded_subject_matching_the_row_owner_takes_the_mutation(tmp_path):
"""The reviewer-repro shape: a stored ``desktop`` row owned by a dashboard account is
deletable by a forwarded actor acting as that account, and refused for any other."""