Skip to content

The agent that grows with you

fix(vault): bind one-time-code fills to the saved login's origin

Teknium6a8b46626377
diff --git a/tests/tools/test_browser_vault_origin_guard.py b/tests/tools/test_browser_vault_origin_guard.py
new file mode 100644
index 00000000000..77bbca76e65
--- /dev/null
+++ b/tests/tools/test_browser_vault_origin_guard.py
@@ -0,0 +1,210 @@
+"""Every secret-bearing browser_vault fill is bound to the item's saved origins.
+
+One behaviour per fill path (password, one-time code, payment card, address, save-then-fill): on a page
+whose origin is not one the item was saved for, the tool refuses BEFORE the backend resolves any secret
+value and before anything is written into the page; on a saved origin the fill goes ahead and the
+in-page write is pinned to that origin.
+
+The page is a stand-in for the CDP supervisor (current URL + inspected controls; it records every
+secret-bearing evaluation). Secrets are dummy values in a temporary local vault.
+"""
+
+from __future__ import annotations
+
+import json
+from unittest.mock import patch
+
+import pytest
+
+from agent.vault_backends.local import LocalLoginBackend
+from agent.vault_store import VaultStore, normalize_origin
+
+_PASSWORD = "dummy-password-7"
+_OTP_SEED = "JBSWY3DPEHPK3PXP" # base32 test seed, not a real account
+_CARD = {"card_number": "4111111111111111", "cardholder_name": "Test User", "exp_month": "07",
+ "exp_year": "2029", "cvc": "123", "billing_postal_code": "94107"}
+_ADDRESS = {"address_line1": "1 Test Way", "city": "Testville", "state": "CA", "postal_code": "94107",
+ "country": "US"}
+
+_LOGIN_CONTROLS = [
+ {"autocomplete": "username", "formIndex": 0, "index": 0, "label": "", "name": "user", "type": "text"},
+ {"autocomplete": "current-password", "formIndex": 0, "index": 1, "label": "", "name": "pw", "type": "password"},
+]
+_OTP_CONTROLS = [{"autocomplete": "one-time-code", "formIndex": 0, "index": 0, "label": "Authentication code",
+ "name": "otp", "type": "text"}]
+_CARD_CONTROLS = [
+ {"autocomplete": "cc-number", "index": 0, "type": "text"},
+ {"autocomplete": "cc-exp", "index": 1, "type": "text"},
+ {"autocomplete": "cc-csc", "index": 2, "type": "text"},
+]
+_ADDRESS_CONTROLS = [
+ {"autocomplete": "address-line1", "index": 0, "type": "text"},
+ {"autocomplete": "postal-code", "index": 1, "type": "text"},
+]
+
+
+class _FakePage:
+ """The supervisor's view of one open tab: answers origin reads and input inspection, and records every
+ secret-bearing fill script as a write."""
+
+ def __init__(self, href: str, controls: list[dict]):
+ self.href, self.controls, self.writes = href, controls, []
+
+ def focus_page(self, origin: str, accept=None) -> dict:
+ return {"ok": bool(origin) and normalize_origin(self.href) == origin, "url": self.href}
+
+ def evaluate_runtime(self, expression: str) -> dict:
+ if expression.strip() == "window.location.href":
+ return {"ok": True, "result": self.href}
+ if "expectedOrigin" in expression: # the fill script (build_fill_js)
+ self.writes.append(expression)
+ return {"ok": True, "result": json.dumps({"filled": 1})}
+ return {"ok": True, "result": json.dumps(self.controls)} # input inspection
+
+
+class _RecordingBackend(LocalLoginBackend):
+ """The real local backend, recording which secret kinds were resolved."""
+
+ def __init__(self):
+ self.resolved: list[str] = []
+
+ def resolve_password(self, handle):
+ self.resolved.append("password")
+ return super().resolve_password(handle)
+
+ def resolve_otp(self, handle):
+ self.resolved.append("otp")
+ return super().resolve_otp(handle)
+
+ def resolve_secret(self, handle):
+ self.resolved.append("secret")
+ return super().resolve_secret(handle)
+
+
+def vault(tmp_path):
+ from agent import redact
+ from agent.vault_backends import unlock as unlock_mod
+
+ store = VaultStore(base_dir=tmp_path / "vault")
+ backend = _RecordingBackend()
+ asked: list[str] = []
+ unlock_mod.set_code_prompt_callback(lambda site, hint: asked.append(site) or "000000")
+ try:
+ with patch("agent.vault_store.get_vault_store", return_value=store), \
+ patch("agent.vault_backends.backend_for_handle", return_value=backend), \
+ patch("agent.vault_backends.unlock.can_prompt_here", return_value=True):
+ yield store, backend, asked
+ finally:
+ unlock_mod.set_code_prompt_callback(None)
+ unlock_mod.set_save_login_prompt_callback(None)
+ redact.clear_vault_redaction_values()
+
+
+def _on_page(page: _FakePage):
+ from tools import browser_vault_tool
+
+ return patch.object(browser_vault_tool, "_ensure_supervisor", return_value=page)
+
+
+def _login(store, origin="https://bank.example", otp=True):
+ secret = {"identifier_type": "username", "identifier": "tester", "password": _PASSWORD}
+ if otp:
+ secret["otp_secret"] = _OTP_SEED
+ return store.add_item("login", "Bank", secret, origin=origin)
+
+
+def _refused_before_resolution(out: dict, backend: _RecordingBackend, page: _FakePage):
+ assert out["success"] is False
+ assert out["error_type"] == "origin_mismatch"
+ assert backend.resolved == []
+ assert page.writes == []
+
+
+class TestOneTimeCode:
+ def test_saved_code_is_not_minted_for_a_page_on_another_origin(self, vault):
+ from tools import browser_vault_tool
+
+ store, backend, asked = vault
+ meta = _login(store)
+ page = _FakePage("https://elsewhere.example/verify", _OTP_CONTROLS)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_enter_code(meta.id, task_id="t"))
+ _refused_before_resolution(out, backend, page)
+ assert asked == [] # a refusal, not a silent switch to asking the user
+
+ def test_saved_code_is_minted_on_the_saved_origin_and_pinned_to_it(self, vault):
+ from tools import browser_vault_tool
+
+ store, backend, asked = vault
+ meta = _login(store)
+ page = _FakePage("https://bank.example/2fa", _OTP_CONTROLS)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_enter_code(meta.id, task_id="t"))
+ assert out["success"] is True and out["source"] == "local" and out["origin"] == "https://bank.example"
+ assert backend.resolved == ["otp"] and asked == []
+ [write] = page.writes
+ assert '"https://bank.example"' in write # the in-page origin assert names the saved origin
+
+ def test_a_code_without_a_saved_item_is_still_asked_of_the_user(self, vault):
+ from tools import browser_vault_tool
+
+ _, backend, asked = vault
+ page = _FakePage("https://elsewhere.example/verify", _OTP_CONTROLS)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_enter_code(task_id="t"))
+ assert out["success"] is True and out["source"] == "user"
+ assert asked == ["elsewhere.example"] and backend.resolved == []
+
+
+class TestFill:
+ def test_password_is_not_resolved_for_a_page_on_another_origin(self, vault):
+ from tools import browser_vault_tool
+
+ store, backend, _ = vault
+ meta = _login(store, otp=False)
+ page = _FakePage("https://elsewhere.example/login", _LOGIN_CONTROLS)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_fill(meta.id, task_id="t"))
+ _refused_before_resolution(out, backend, page)
+
+ def test_card_is_refused_before_the_user_is_asked_to_confirm(self, vault):
+ from tools import browser_vault_tool
+
+ store, backend, _ = vault
+ meta = store.add_item(kind="payment", label="Card", origin="https://shop.example", secret=_CARD)
+ page = _FakePage("https://elsewhere.example/checkout", _CARD_CONTROLS)
+ with _on_page(page), \
+ patch("tools.approval_prompt.request_elicitation_consent", return_value="accept") as consent:
+ out = json.loads(browser_vault_tool.browser_vault_fill(meta.id, task_id="t"))
+ _refused_before_resolution(out, backend, page)
+ consent.assert_not_called()
+
+ def test_address_is_not_resolved_for_a_page_on_another_origin(self, vault):
+ from tools import browser_vault_tool
+
+ store, backend, _ = vault
+ meta = store.add_item(kind="address", label="Home", origin="https://shop.example", secret=_ADDRESS)
+ page = _FakePage("https://elsewhere.example/shipping", _ADDRESS_CONTROLS)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_fill(meta.id, task_id="t"))
+ _refused_before_resolution(out, backend, page)
+
+ def test_save_login_fill_is_refused_when_the_page_left_the_saved_origin(self, vault):
+ """save_login stores the login for the page it read, then fills through the same guard: a page that
+ navigated away while the user was typing gets nothing."""
+ from agent.vault_backends import unlock as unlock_mod
+ from tools import browser_vault_tool
+
+ store, backend, _ = vault
+ page = _FakePage("https://bank.example/login", _LOGIN_CONTROLS)
+
+ def prompt(origin, host):
+ page.href = "https://elsewhere.example/login"
+ return {"identifier": "tester", "password": _PASSWORD}
+
+ unlock_mod.set_save_login_prompt_callback(prompt)
+ with _on_page(page):
+ out = json.loads(browser_vault_tool.browser_vault_save_login(task_id="t"))
+ assert [m.origin for m in store.list_items()] == ["https://bank.example"]
+ _refused_before_resolution(out["fill"], backend, page)
diff --git a/tools/browser_vault_tool.py b/tools/browser_vault_tool.py
index 7e8e0d09726..53403c0524a 100644
--- a/tools/browser_vault_tool.py
+++ b/tools/browser_vault_tool.py
@@ -18,6 +18,9 @@ paid per request) and available wherever a browser is. Its actions:
password never appears in tool results, logs, or the session DB, and its
exact bytes are registered with the browser-result redaction boundary so
no later browser tool call can echo them back to the model.
+- ``enter_code`` → a one-time code for the current page: minted from a saved
+ login's authenticator key (same origin binding as ``fill``, checked before
+ the code is generated) or typed by the user into their surface's prompt.
Ported design from Merit-Systems/OpenInstinct (MIT): opaque-handle vault
autofill (kernel-login-autofill.ts / fill_from_vault.ts).
@@ -182,6 +185,30 @@ def _focus_bound_origin(task_id: str, origin: str, kind: str) -> Optional[str]:
return (origin or focused.get("url")) if focused.get("ok") else None
+def _bound_page_origin(task_id: str, meta: Any, probe_kind: str) -> str | dict[str, Any]:
+ """The origin guard every fill of a stored secret (password, one-time code, card, address) passes
+ BEFORE the backend resolves any value: the page origin when it is one of the item's saved origins,
+ else the refusal to return. Manager items may bind several websites; matching is exact and
+ nothing wildcard or parent-domain is inferred. The caller hands the returned origin to ``build_fill_js``,
+ which re-asserts it inside the page immediately before the write (navigation between check and fill)."""
+ allowed = list(meta.allowed_origins) or ([str(meta.origin)] if meta.origin else [])
+ if not allowed:
+ return {"success": False, "error_type": "no_origin",
+ "error": f"Vault item {meta.id!r} has no bound origin; {meta.kind} items are filled only on "
+ "the site they were saved for."}
+ page_origin = next((o for o in (_focus_bound_origin(task_id, c, probe_kind) for c in allowed) if o), None)
+ page_origin = page_origin or _current_page_origin(task_id)
+ if not page_origin:
+ return {"success": False,
+ "error": "Could not determine the current page origin. Navigate to the login page first."}
+ if page_origin not in allowed:
+ return {"success": False, "error_type": "origin_mismatch",
+ "error": (f"Refused: current page origin ({page_origin}) does not match the vault item's bound "
+ f"origin(s) ({', '.join(allowed)}). Vault fills only run on the exact origin(s) the "
+ "credential was saved for.")}
+ return page_origin
+
+
# ---------------------------------------------------------------------------
# Handlers
# ---------------------------------------------------------------------------
@@ -301,21 +328,44 @@ _TAB_PROBES["otp"] = ("!!document.querySelector('input[autocomplete=one-time-cod
"input[id*=otp i], input[id*=code i], input[name*=totp i], input[aria-label*=code i]')")
+def _saved_login(handle: str) -> tuple[Any, Any]:
+ """(backend, meta) for a vault handle, or (None, None) when nothing is saved under it (or its manager is
+ locked): enter_code then asks the user instead of minting a code."""
+ from agent.vault_backends import UnlockRequired, backend_for_handle
+
+ backend = backend_for_handle(handle) if handle else None
+ if backend is None:
+ return None, None
+ try:
+ return backend, backend.get_meta(handle)
+ except UnlockRequired:
+ return backend, None
+
+
def browser_vault_enter_code(handle: str = "", task_id: Optional[str] = None) -> str:
"""Second factor: fill the one-time code the CURRENT page asks for. If the saved login (``handle``) has an
authenticator seed, the code is minted server-side and nobody is asked; otherwise the user is prompted on
their surface for the code their phone/email/app shows. The code goes into the page over the supervisor
- socket and never enters the conversation."""
+ socket and never enters the conversation. A saved login's code is only minted on its saved origin(s)."""
from agent.redact import register_vault_redaction_value
- from agent.vault_backends import backend_for_handle
from agent.vault_backends.unlock import can_prompt_here, get_code_prompt_callback
from agent.vault_login_classifier import LoginControl, build_fill_js, build_inspection_js, build_otp_fills, classify_otp_controls
effective_task_id = task_id or "default"
- _focus_bound_origin(effective_task_id, "", "otp")
- origin = _current_page_origin(effective_task_id)
- if not origin:
- return json.dumps({"success": False, "error": "No page with a code field is open."})
+ backend, meta = _saved_login(handle)
+ if meta is not None:
+ bound = _bound_page_origin(effective_task_id, meta, "otp")
+ if isinstance(bound, dict):
+ if bound.get("error_type") == "origin_mismatch":
+ bound["next"] = ("If this page asks for the code of a different account, call enter_code without a "
+ "handle so the user is asked.")
+ return json.dumps(bound)
+ origin = bound
+ else:
+ _focus_bound_origin(effective_task_id, "", "otp")
+ origin = _current_page_origin(effective_task_id)
+ if not origin:
+ return json.dumps({"success": False, "error": "No page with a code field is open."})
site = origin.split("://", 1)[-1]
nonce = secrets.token_hex(8)
@@ -331,8 +381,7 @@ def browser_vault_enter_code(handle: str = "", task_id: Optional[str] = None) ->
code: Optional[str] = None
source = "user"
- backend = backend_for_handle(handle) if handle else None
- if backend is not None:
+ if meta is not None:
try:
code = backend.resolve_otp(handle)
except Exception:
@@ -411,42 +460,15 @@ def browser_vault_fill(handle: str, task_id: Optional[str] = None) -> str:
),
}
)
- if meta.kind != "login" and not meta.origin:
- return json.dumps({"success": False, "error_type": "no_origin",
- "error": f"Vault item {handle!r} has no bound origin; {meta.kind} items are filled only on the site they were saved for."})
- if meta.kind == "payment" and not _confirm_payment_fill(meta.label, str(meta.origin)):
+ # Origin binding pre-check (the authoritative re-check runs inside the fill script), before the user is
+ # asked to confirm a card and before any value is resolved.
+ page_origin = _bound_page_origin(effective_task_id, meta, meta.kind)
+ if isinstance(page_origin, dict):
+ return json.dumps(page_origin)
+ if meta.kind == "payment" and not _confirm_payment_fill(meta.label, page_origin):
return json.dumps({"success": False, "error_type": "payment_declined",
"error": "The user did not confirm filling this payment card. Do not retry; ask them instead."})
- # ── Origin binding pre-check (cheap early exit; the authoritative check
- # runs synchronously inside the fill script itself) ──────────────────────
- # Manager items can bind several websites (e.g. amazon.co.uk + www.amazon.co.uk);
- # every saved origin is a valid fill target. Matching stays exact-origin —
- # nothing wildcard/parent-domain is ever inferred.
- allowed = list(meta.allowed_origins) or ([str(meta.origin)] if meta.origin else [])
- page_origin = None
- for candidate in allowed:
- page_origin = _focus_bound_origin(effective_task_id, candidate, meta.kind)
- if page_origin:
- break
- page_origin = page_origin or _current_page_origin(effective_task_id)
- if not page_origin:
- return json.dumps(
- {"success": False, "error": "Could not determine the current page origin. Navigate to the login page first."}
- )
- if page_origin not in allowed:
- return json.dumps(
- {
- "success": False,
- "error_type": "origin_mismatch",
- "error": (
- f"Refused: current page origin ({page_origin}) does not match "
- f"the vault item's bound origin(s) ({', '.join(allowed)}). Vault fills "
- "only run on the exact origin(s) the credential was saved for."
- ),
- }
- )
-
# ── Inspect + classify page controls ────────────────────────────────────
nonce = secrets.token_hex(8) # binds this fill to THIS inspection's stamps
inspect = _eval_js(effective_task_id, build_inspection_js(nonce))
diff --git a/website/docs/user-guide/features/credential-vault.md b/website/docs/user-guide/features/credential-vault.md
index dc3abb33ea7..4802f698d2e 100644
--- a/website/docs/user-guide/features/credential-vault.md
+++ b/website/docs/user-guide/features/credential-vault.md
@@ -42,7 +42,8 @@ Sites that ask for a code after the password are handled the same way:
link a site shows when you enable 2FA; 1Password and Bitwarden items that
hold a TOTP seed count too): Hermes generates the current code and enters
it. Nobody is asked. Add the key in **Settings → Passwords & Logins → Add**
- or `hermes vault add`; the item shows a *2FA auto* badge.
+ or `hermes vault add`; the item shows a *2FA auto* badge. Like the
+ password, the code is only generated for a page on the login's saved site.
- **Code sent to your phone or email**: a small prompt appears in your
surface ("Verification code for github.com"), you type the code, Hermes
enters it into the page. The code never enters the conversation either.