Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Use the saved login someone picked, instead of asking for it again (SKY-13483) (#8052)
Andrew Neilson6077d6f4ddd1
diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 192bd93bc..d021555f0 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -624,6 +624,14 @@ class RequestPolicy:# The server-held login page URLs this ask was formed against; the only origin source the# connected resume may bind from.credential_ask_login_page_urls: list[str] = field(default_factory=list)+ # Credentials this turn resolved from an explicit user reference — an exact saved name or a+ # cred_ id — as distinct from approvals carried in from earlier turns. Naming a credential+ # answers which one to use, which is what lets the login page answer where it may be typed.+ current_turn_named_credential_ids: set[str] = field(default_factory=set)+ # Login targets the classifier named that the user also wrote themselves. The classifier alone+ # decides which URL is a sign-in page but is not authority for the origin a password reaches, so+ # only the intersection may vouch for a fill.+ grounded_login_target_urls: list[str] = field(default_factory=list)credential_ask_candidate_ids: list[str] = field(default_factory=list)existing_workflow_credential_ids: list[str] = field(default_factory=list)# Sorted at the trace/JSON boundary; YAML traversal uses sets.@@ -4867,6 +4875,29 @@ def _prior_approved_credential_ids_from_context(global_llm_context: str) -> set[}+def _ground_login_target_urls(policy: RequestPolicy, user_message: str) -> None:+ """Keep the classifier's login targets this message also carries, so neither a URL the model authored+ nor one the user pasted in an older turn for another purpose becomes an origin a password reaches.+ """+ typed_origins = {+ parts[2]+ for candidate in URL_CANDIDATE_RE.findall(user_message or "")+ if (parts := _url_parts(candidate.rstrip(".,;:!?")))+ }+ policy.grounded_login_target_urls = [+ url for url in policy.login_page_urls if (parts := _url_parts(url)) and parts[2] in typed_origins+ ]+++def _record_current_turn_named_credentials(policy: RequestPolicy, credentials: Sequence[Credential]) -> None:+ """Mark credentials the user picked out by name or id in the message being handled, kept apart from+ ``resolved_credentials`` because a turn-old approval is not evidence about the page in front of us.+ """+ policy.current_turn_named_credential_ids.update(+ credential.credential_id for credential in credentials if credential.credential_id+ )++async def _seed_prior_approved_credentials(policy: RequestPolicy,*,@@ -4965,6 +4996,7 @@ async def _resolve_credentials(existing = await app.DATABASE.credentials.get_credentials_by_ids(ids, organization_id=organization_id)found = {credential.credential_id for credential in existing}policy.resolved_credentials = existing+ _record_current_turn_named_credentials(policy, existing)policy.invalid_credential_ids = [credential_id for credential_id in ids if credential_id not in found]if policy.invalid_credential_ids and policy.testing_intent != "skip_test":formatted = ", ".join(f"`{credential_id}`" for credential_id in policy.invalid_credential_ids)@@ -5002,6 +5034,7 @@ async def _resolve_credentials()if len(named_matches) == 1:policy.resolved_credentials = named_matches+ _record_current_turn_named_credentials(policy, named_matches)if (policy.classifier_status == "fallback"and policy.credential_input_kind == "none"@@ -5686,6 +5719,7 @@ async def build_request_policy(# conservative clarification flag; _resolve_credentials will block# again if the lookup is missing or ambiguous.policy.requires_user_clarification = False+ _ground_login_target_urls(policy, user_message)await _resolve_credentials(policy,organization_id,diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex 340d834e5..961e388a9 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -533,6 +533,7 @@ class AgentContext:scouted_credential_field_inventory_by_credential_id: dict[str, frozenset[str]] = field(default_factory=dict)# Read once per turn: repeated fill attempts must not re-scan the org's credentials.org_credentials_for_turn: list[Credential] | None = None+ vault_login_uris_by_credential_id: dict[str, list[str]] = field(default_factory=dict)# Highest trajectory_index visible at the latest parsed evaluate observation and whether that page# showed a password-type control; orders page evidence against post-fill submits across evictions.last_scout_observation_trajectory_index: int | None = Nonediff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.pyindex a2a8140c4..1a417d0a4 100644--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py@@ -1,24 +1,35 @@from __future__ import annotationsfrom dataclasses import dataclass-from typing import Any, Callable+from typing import Any, Awaitable, Callableimport structlog+import tldextractfrom skyvern.cli.core.session_manager import get_pagefrom skyvern.forge import app-from skyvern.forge.sdk.browser_action_policy import canonicalize_origin+from skyvern.forge.sdk.browser_action_policy import BrowserOrigin, canonicalize_originfrom skyvern.forge.sdk.copilot.config import BlockAuthoringPolicyfrom skyvern.forge.sdk.copilot.credential_resolution import load_credentials, url_partsfrom skyvern.forge.sdk.copilot.loop_detection import record_tool_step_result_for_ctx-from skyvern.forge.sdk.copilot.request_policy import RequestPolicy, admit_credential_for_live_page+from skyvern.forge.sdk.copilot.request_policy import (+ RequestPolicy,+ admit_credential_for_live_page,+ loggable_origin,+)from skyvern.forge.sdk.copilot.runtime import AgentContext, ensure_browser_session, mcp_browser_contextfrom skyvern.forge.sdk.copilot.secret_scrub import (REDACTED_SECRET_PLACEHOLDER,register_secret_scrub_value,scrub_secrets_from_text,)-from skyvern.forge.sdk.schemas.credentials import Credential, CredentialVaultType, PasswordCredential, TotpType+from skyvern.forge.sdk.schemas.credentials import (+ Credential,+ CredentialType,+ CredentialVaultType,+ PasswordCredential,+ TotpType,+)from skyvern.forge.sdk.services.credentials import generate_totp_code, normalize_totp_configfrom .banned_blocks import _copilot_block_authoring_policy@@ -49,6 +60,9 @@ _CREDENTIAL_FILL_TIMEOUT_MS = 15000@dataclass(frozen=True)class _CredentialFillOriginGrant:intended_url: str+ # A vault entry names a site, not a page, so a grant it produced travels that whole site. One+ # earned from a tested URL or a page match stays on the origin that proved it.+ whole_site: bool = Falseasync def _normalize_totp_config_for_organization(totp_secret: str, organization_id: str) -> str:@@ -105,6 +119,42 @@ def _still_on_admitted_site(current_url: str | None, admitted_url: str) -> bool:return bool(current_origin and admitted_origin and current_origin == admitted_origin)+# Private PSL entries are included so hosts that hand strangers a subdomain each -- github.io,+# vercel.app, s3.amazonaws.com -- stay separate sites rather than collapsing into one.+_SITE_EXTRACT = tldextract.TLDExtract(include_psl_private_domains=True)+++def _site_of(url: str | None) -> tuple[str, int | None, str] | None:+ """A URL's site: scheme and port alongside its domain under the public suffix.++ Scheme and port ride along so a site-wide grant moves between a site's hosts without also+ reaching it over plaintext or on another port, which the origin comparison would refuse.+ """+ parts = url_parts(url or "")+ origin = canonicalize_origin(parts[2]) if parts else None+ if origin is None:+ return None+ extracted = _SITE_EXTRACT(origin.host)+ # tldextract renamed this property; read the new name where the installed version has it.+ site = (+ extracted.top_domain_under_public_suffix+ if hasattr(extracted, "top_domain_under_public_suffix")+ else extracted.registered_domain+ )+ return (origin.scheme, origin.port, site) if site else None+++def _same_site(current_url: str | None, granted_url: str) -> bool:+ current, granted = _site_of(current_url), _site_of(granted_url)+ return bool(current and granted and current == granted)+++def _within_grant(current_url: str | None, grant: _CredentialFillOriginGrant) -> bool:+ if grant.whole_site:+ return _same_site(current_url, grant.intended_url)+ return _still_on_admitted_site(current_url, grant.intended_url)++class _CredentialFillOriginMismatchError(Exception):pass@@ -116,11 +166,11 @@ def _credential_fill_origin_mismatch_error() -> str:)-def _credential_fill_release_guard(intended_url: str) -> Callable[[str | None], None]:+def _credential_fill_release_guard(grant: _CredentialFillOriginGrant) -> Callable[[str | None], None]:"""Bind the fill grant to the resolved element's document at the release seam."""def guard(target_url: str | None) -> None:- if not _still_on_admitted_site(target_url, intended_url):+ if not _within_grant(target_url, grant):raise _CredentialFillOriginMismatchErrorreturn guard@@ -156,6 +206,34 @@ def _resolved_credential_intended_url(policy: RequestPolicy, credential_id: str))+async def _vault_named_sites(copilot_ctx: AgentContext, credential_id: str) -> list[str]:+ """The sites this credential's own vault entry names, as the user saved them.++ Read straight from the vault rather than the DB row, so a credential nobody has run the test flow+ against still knows where it belongs. Only Bitwarden items carry these; the other vaults store no+ URL for an item, so their credentials fall through to the request-grounded route below.+ """+ cached = copilot_ctx.vault_login_uris_by_credential_id.get(credential_id)+ if cached is not None:+ return cached+ uris: list[str] = []+ try:+ db_credential = await app.DATABASE.credentials.get_credential(+ credential_id, organization_id=copilot_ctx.organization_id+ )+ if db_credential is not None:+ service = app.CREDENTIAL_VAULT_SERVICES.get(db_credential.vault_type or CredentialVaultType.BITWARDEN)+ if service is not None:+ uris = list((await service.get_credential_item(db_credential)).login_uris)+ except Exception:+ # Not cached: a vault read that failed once says nothing about where the credential belongs,+ # and caching the empty answer would refuse every later fill this turn for a transient fault.+ LOG.info("copilot could not read the vault entry's sites", credential_id=credential_id, exc_info=True)+ return []+ copilot_ctx.vault_login_uris_by_credential_id[credential_id] = uris+ return uris++def _missing_credential_origin_error(credential_id: str) -> str:return (f"Credential `{credential_id}` has no intended login origin, so it cannot be filled into the live browser. "@@ -163,38 +241,89 @@ def _missing_credential_origin_error(credential_id: str) -> str:)+def _sole_grounded_login_target(policy: RequestPolicy) -> str | None:+ """The one sign-in page this request grounds in the user's own words, if there is exactly one; a+ request naming several grounds none, since nothing here can tell which of them a password belongs to.+ """+ by_origin: dict[BrowserOrigin, str] = {}+ for url in policy.grounded_login_target_urls:+ parts = url_parts(url)+ origin = canonicalize_origin(parts[2]) if parts else None+ if origin is not None:+ by_origin[origin] = url+ if len(by_origin) != 1:+ return None+ return next(iter(by_origin.values()))+++async def _sole_org_password_credential_id(+ load_org_credentials: Callable[[], Awaitable[list[Credential]]],+) -> str | None:+ """The one saved password credential carrying no login URL, where elimination answers the+ which-credential question a name would otherwise have to. Counts the pool turn start resolves from+ (``allow_urlless_sole``), so a credential resolved there is never re-asked about here.+ """+ unbound = [+ credential+ for credential in await load_org_credentials()+ if credential.credential_type == CredentialType.PASSWORD and not credential.tested_url+ ]+ return unbound[0].credential_id if len(unbound) == 1 else None+++def _ambiguous_unbound_credential_steer(credential_id: str, page_url: str) -> str:+ origin = loggable_origin(page_url)+ return (+ f"`{credential_id}` has no saved login page, so it is not established that it belongs to {origin}. "+ "Ask the user to say which saved credential to use *and* to name the sign-in page, for example "+ f'"use <exact name or cred_ id> at {origin}". A reply carrying only the credential grounds no '+ "origin and refuses again, and a bare yes authorizes nothing."+ )++async def _credential_fill_origin_grant(copilot_ctx: AgentContext, credential_id: str) -> tuple[_CredentialFillOriginGrant | None, str | None]:- """Authorize a fill only when it has a concrete intended origin.-- The returned grant is the capability consumed at the release seam. A credential that reached- ``resolved_credentials`` through a path without either a tested URL or a live-page admission- receives no grant, so a missing origin cannot silently disable the final comparison.+ """Authorize a fill only when something other than the model binds this credential to an origin; the+ grant is consumed at the release seam, so a credential nothing vouches for gets none and cannot release."""prerequisite_error = _credential_fill_prerequisite_error(copilot_ctx, credential_id)if prerequisite_error:return None, prerequisite_errorpolicy = copilot_ctx.request_policyauthority_error = _credential_fill_authority_error(copilot_ctx, credential_id)++ async def load_once() -> list[Credential]:+ if copilot_ctx.org_credentials_for_turn is None:+ copilot_ctx.org_credentials_for_turn = await load_credentials(copilot_ctx.organization_id)+ return copilot_ctx.org_credentials_for_turn+if not authority_error:if not isinstance(policy, RequestPolicy):return None, _missing_credential_origin_error(credential_id)intended_url = _resolved_credential_intended_url(policy, credential_id)- if not intended_url:- # API/import-created credentials remain unbound until tested against a login page.- # Denying that onboarding state is intentional: there is no origin safe to release to.+ if intended_url:+ return _CredentialFillOriginGrant(intended_url), None+ # A credential saved without a login URL has no record of where it belongs, but that is an+ # absent answer rather than a contrary one. The sign-in page the user themselves named can+ # supply the origin, provided something other than the model settled which credential to use.+ page_url = await _live_working_page_url(copilot_ctx) or ""+ # The vault entry names the site the user filed this credential under, so it answers where+ # the secret belongs without anyone having to run the test flow first.+ if page_url and any(_same_site(page_url, uri) for uri in await _vault_named_sites(copilot_ctx, credential_id)):+ return _CredentialFillOriginGrant(page_url, whole_site=True), None+ grounded_target = _sole_grounded_login_target(policy)+ if not page_url or grounded_target is None or not _still_on_admitted_site(page_url, grounded_target):return None, _missing_credential_origin_error(credential_id)- return _CredentialFillOriginGrant(intended_url), None+ if policy.current_turn_named_credential_ids == {credential_id}:+ return _CredentialFillOriginGrant(page_url), None+ if credential_id == await _sole_org_password_credential_id(load_once):+ return _CredentialFillOriginGrant(page_url), None+ return None, _ambiguous_unbound_credential_steer(credential_id, page_url)if not isinstance(policy, RequestPolicy):return None, authority_error- async def load_once() -> list[Credential]:- if copilot_ctx.org_credentials_for_turn is None:- copilot_ctx.org_credentials_for_turn = await load_credentials(copilot_ctx.organization_id)- return copilot_ctx.org_credentials_for_turn-admission = await admit_credential_for_live_page(policy,organization_id=copilot_ctx.organization_id,@@ -341,7 +470,7 @@ async def _fill_credential_field_impl(value,mode="direct",timeout=_CREDENTIAL_FILL_TIMEOUT_MS,- _direct_fill_release_guard=_credential_fill_release_guard(origin_grant.intended_url),+ _direct_fill_release_guard=_credential_fill_release_guard(origin_grant),)except _CredentialFillOriginMismatchError:return finish({"ok": False, "error": _credential_fill_origin_mismatch_error()})diff --git a/skyvern/forge/sdk/schemas/credentials.py b/skyvern/forge/sdk/schemas/credentials.pyindex e9d28ae82..5630b6feb 100644--- a/skyvern/forge/sdk/schemas/credentials.py+++ b/skyvern/forge/sdk/schemas/credentials.py@@ -216,6 +216,10 @@ class CredentialItem(BaseModel):credential: PasswordCredential | CreditCardCredential | SecretCredential = Field(..., description="The actual credential data")+ login_uris: list[str] = Field(+ default_factory=list,+ description="Sites the vault item itself names, as saved by the user who stored it",+ )class CreateCredentialRequest(BaseModel):diff --git a/skyvern/forge/sdk/services/bitwarden.py b/skyvern/forge/sdk/services/bitwarden.pyindex 017cc09be..b71583da8 100644--- a/skyvern/forge/sdk/services/bitwarden.py+++ b/skyvern/forge/sdk/services/bitwarden.py@@ -4,7 +4,7 @@ import osimport randomimport refrom enum import IntEnum, StrEnum-from typing import Tuple+from typing import Any, Tupleimport structlogimport tldextract@@ -261,6 +261,22 @@ def get_bitwarden_item_overview_from_bitwarden_item()+# Bitwarden's per-URI match mode, as the vault stores it. A user who narrowed a URI below its default+# is saying the credential does not belong to the whole site, and `never` says not to offer it at all.+_BITWARDEN_URI_MATCH_DOMAIN = 0+++def _whole_site_login_uris(login_item: dict[str, Any]) -> list[str]:+ """URIs whose match mode lets the credential stand for its whole site."""+ return [+ uri["uri"]+ for uri in login_item.get("uris") or []+ if isinstance(uri, dict)+ and uri.get("uri")+ and (uri.get("match") is None or uri.get("match") == _BITWARDEN_URI_MATCH_DOMAIN)+ ]++def is_valid_email(email: str | None) -> bool:if not email:return False@@ -1399,6 +1415,7 @@ class BitwardenService:totp=login_item["totp"],metadata=_password_metadata_from_bitwarden_item(response["data"]),),+ login_uris=_whole_site_login_uris(login_item),)elif response["data"]["type"] == BitwardenItemType.CREDIT_CARD:name = response["data"]["name"]diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.pyindex 63fb4b685..431470848 100644--- a/tests/unit/test_copilot_fill_credential_field.py+++ b/tests/unit/test_copilot_fill_credential_field.py@@ -61,6 +61,7 @@ def _ctx(**overrides: Any) -> SimpleNamespace:secret_scrub_values=[],scouted_credential_field_inventory_by_credential_id={},org_credentials_for_turn=None,+ vault_login_uris_by_credential_id={},)for key, value in overrides.items():setattr(ns, key, value)@@ -693,6 +694,195 @@ class TestCredentialFillLivePageAdmission:assert "intended login origin" in result["error"]assert page.fill_calls == []+ async def _unbound_grant(+ self,+ *,+ page_url: str | None,+ login_page_urls: list[str],+ named: bool,+ org_credentials: list[SimpleNamespace],+ ) -> tuple[Any, str | None]:+ """Grant for a resolved credential carrying no saved login URL."""+ policy = RequestPolicy(+ resolved_credentials=[_resolved_credential(tested_url=None)],+ login_page_urls=login_page_urls,+ grounded_login_target_urls=login_page_urls,+ )+ if named:+ policy.current_turn_named_credential_ids = {"cred_123"}+ ctx = _ctx(request_policy=policy)+ with (+ patch(+ "skyvern.forge.app.DATABASE.credentials.get_credentials",+ new=AsyncMock(return_value=org_credentials),+ ),+ patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=page_url)),+ ):+ return await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")++ @pytest.mark.asyncio+ async def test_a_named_credential_is_bound_by_the_login_page_the_request_asked_for(self) -> None:+ """Naming a credential answers *which* one; the login page the request named answers *where*.++ A credential saved without a login URL is unbound rather than disowned, so the request's own+ login target can supply the origin once something outside the model has settled which+ credential is meant.+ """+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=True,+ org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ )++ assert error is None+ assert grant is not None+ assert credential_fill_module._still_on_admitted_site(_FIXTURE_LOGIN_URL, grant.intended_url)++ @pytest.mark.asyncio+ async def test_the_only_saved_credential_needs_no_naming(self) -> None:+ """An org with one login has nothing to disambiguate, so elimination answers for the user."""+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=False,+ org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ )++ assert error is None+ assert grant is not None++ @pytest.mark.asyncio+ async def test_a_card_or_secret_does_not_count_against_the_only_saved_login(self) -> None:+ """Elimination counts logins, so a saved card leaves the one password still unambiguous."""+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=False,+ org_credentials=[+ _org_credential("cred_123", "authtest simple", None),+ _org_credential("cred_card", "company card", None, CredentialType.CREDIT_CARD),+ ],+ )++ assert error is None+ assert grant is not None++ @pytest.mark.asyncio+ async def test_naming_several_credentials_settles_which_one_no_better_than_naming_none(self) -> None:+ """Two names leave the model choosing whose password reaches the page, which is not an answer."""+ policy = RequestPolicy(+ resolved_credentials=[_resolved_credential(tested_url=None)],+ login_page_urls=[_FIXTURE_LOGIN_URL],+ grounded_login_target_urls=[_FIXTURE_LOGIN_URL],+ )+ policy.current_turn_named_credential_ids = {"cred_123", "cred_other"}+ ctx = _ctx(request_policy=policy)+ with (+ patch(+ "skyvern.forge.app.DATABASE.credentials.get_credentials",+ new=AsyncMock(+ return_value=[+ _org_credential("cred_123", "authtest simple", None),+ _org_credential("cred_other", "billing", None),+ ]+ ),+ ),+ patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=_FIXTURE_LOGIN_URL)),+ ):+ grant, error = await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")++ assert grant is None+ assert error is not None++ @pytest.mark.asyncio+ async def test_a_credential_already_bound_elsewhere_leaves_the_unbound_one_sole(self) -> None:+ """Turn start resolves the sole URL-less credential; counting bound ones too would re-ask here."""+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=False,+ org_credentials=[+ _org_credential("cred_123", "authtest simple", None),+ _org_credential("cred_bound", "billing", "https://billing.example.com/login"),+ ],+ )++ assert error is None+ assert grant is not None++ @pytest.mark.asyncio+ async def test_one_unnamed_credential_among_several_asks_rather_than_refusing(self) -> None:+ """Ambiguity is a question. Only an absence of any non-model evidence is a refusal."""+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=False,+ org_credentials=[+ _org_credential("cred_123", "authtest simple", None),+ _org_credential("cred_other", "billing", None),+ ],+ )++ assert grant is None+ assert error is not None+ # Only an exact name or a cred_ id is read back off the next message, so an ask that would+ # settle for "yes" re-asks forever — the loop this seam exists to end.+ assert "exact name" in error and "cred_" in error++ @pytest.mark.asyncio+ async def test_an_unbound_credential_is_refused_on_a_page_the_request_never_asked_for(self) -> None:+ """The lookalike case: naming a credential does not license the page the browser drifted to."""+ grant, error = await self._unbound_grant(+ page_url="https://authenticationtest.com.example.net/simpleFormAuth/",+ login_page_urls=[_FIXTURE_LOGIN_URL],+ named=True,+ org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ )++ assert grant is None+ assert error is not None+ assert "intended login origin" in error++ @pytest.mark.asyncio+ async def test_a_request_naming_two_sign_in_sites_grounds_neither(self) -> None:+ """One request routinely spans two logins — read a total here, file it there.++ Neither site can vouch for the other's password, and nothing at this seam knows which of them+ the credential belongs to, so a second sign-in target withdraws the grant instead of widening it.+ """+ for page_url in (_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"):+ grant, error = await self._unbound_grant(+ page_url=page_url,+ login_page_urls=[_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"],+ named=True,+ org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ )++ assert grant is None, f"a two-site request must not vouch for {page_url}"+ assert error is not None+ assert "intended login origin" in error++ @pytest.mark.asyncio+ async def test_a_login_target_the_user_never_wrote_cannot_vouch(self) -> None:+ """The classifier says which URL is a sign-in page; it does not get to author the URL."""+ policy = RequestPolicy(+ resolved_credentials=[_resolved_credential(tested_url=None)],+ login_page_urls=[_FIXTURE_LOGIN_URL],+ grounded_login_target_urls=[],+ )+ policy.current_turn_named_credential_ids = {"cred_123"}+ ctx = _ctx(request_policy=policy)+ with (+ patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=AsyncMock(return_value=[])),+ patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=_FIXTURE_LOGIN_URL)),+ ):+ grant, error = await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")++ assert grant is None+ assert error is not None+ assert "intended login origin" in error+@pytest.mark.asyncioasync def test_live_page_admitted_credential_short_circuits_without_an_org_lookup(self) -> None:error, _, load_mock = await self._gate(@@ -1058,3 +1248,79 @@ class TestObservationSeamCredentialBinding:assert "resolved_login_credential_id" not in resultload_mock.assert_not_awaited()+++class TestVaultNamedSiteGrant:+ """A credential goes where its own vault entry says it belongs, without a test run first."""++ async def _grant(self, *, vault_uris: list[str], page_url: str) -> tuple[Any, str | None]:+ policy = RequestPolicy(resolved_credentials=[_resolved_credential(tested_url=None)])+ ctx = _ctx(request_policy=policy, vault_login_uris_by_credential_id={"cred_123": vault_uris})+ with patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=page_url)):+ return await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "page_url",+ [+ "https://example.com/login",+ "https://eu.example.com/login",+ "https://usercontent.example.com/uploads/x",+ ],+ )+ async def test_the_vault_site_covers_the_whole_site(self, page_url: str) -> None:+ grant, error = await self._grant(vault_uris=["https://example.com"], page_url=page_url)++ assert error is None+ assert grant is not None and grant.whole_site is True++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "page_url",+ [+ "https://example.com.evil.test/login",+ "https://example-login.test/login",+ "https://bob.github.io/login",+ ],+ )+ async def test_another_site_is_refused(self, page_url: str) -> None:+ grant, error = await self._grant(+ vault_uris=["https://example.com", "https://alice.github.io"], page_url=page_url+ )++ assert grant is None+ assert error is not None++ @pytest.mark.asyncio+ async def test_a_whole_site_grant_still_cannot_leave_that_site(self) -> None:+ grant, _ = await self._grant(vault_uris=["https://example.com"], page_url="https://eu.example.com/login")++ assert grant is not None+ assert credential_fill_module._within_grant("https://other.example.com/step2", grant)+ assert not credential_fill_module._within_grant("https://example.com.evil.test/step2", grant)++ @pytest.mark.parametrize(+ "page_url,reachable",+ [+ ("https://eu.example.com/login", True),+ ("https://usercontent.example.com/x", True),+ ("http://example.com/login", False),+ ("http://eu.example.com/login", False),+ ("https://example.com:8443/login", False),+ ("https://example.com.evil.test/login", False),+ ],+ )+ def test_a_site_wide_grant_moves_between_hosts_but_not_schemes_or_ports(+ self, page_url: str, reachable: bool+ ) -> None:+ """Host mobility is the point; reaching the same site in cleartext or on another port is not."""+ grant = credential_fill_module._CredentialFillOriginGrant("https://example.com/login", whole_site=True)++ assert credential_fill_module._within_grant(page_url, grant) is reachable++ def test_a_tested_credential_keeps_the_tighter_scope(self) -> None:+ """Evidence naming one page grants one origin; only a site-level entry grants a site."""+ page_grant = credential_fill_module._CredentialFillOriginGrant("https://eu.example.com/login")++ assert not credential_fill_module._within_grant("https://other.example.com/login", page_grant)+ assert credential_fill_module._within_grant("https://eu.example.com/step2", page_grant)diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.pyindex 3a2b64dd7..c4f39a878 100644--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py@@ -33,6 +33,7 @@ from skyvern.forge.sdk.copilot.request_policy import (_classification_from_raw,_clean_email_list,_exact_credential_name_candidates,+ _ground_login_target_urls,_resolve_credentials,_saved_credential_names_mentioned,_seed_prior_approved_credentials,@@ -3408,3 +3409,40 @@ async def test_basic_auth_in_a_login_url_never_reaches_the_admission_log() -> Noassert "hunter2" not in loggedassert "admin:" not in loggedassert "code=abc" not in logged+++class TestGroundLoginTargetUrls:+ """The half that enforces "the user wrote it" — the whole basis of the release-origin claim."""++ @staticmethod+ def _ground(login_page_urls: list[str], user_message: str) -> list[str]:+ policy = RequestPolicy(login_page_urls=login_page_urls)+ _ground_login_target_urls(policy, user_message)+ return policy.grounded_login_target_urls++ def test_a_url_only_the_classifier_named_is_dropped(self) -> None:+ assert self._ground(["https://portal.example/login"], "log in with my saved credential") == []++ def test_a_url_written_in_this_message_survives(self) -> None:+ assert self._ground(+ ["https://portal.example/login"],+ "sign in at https://portal.example/login with my saved credential",+ ) == ["https://portal.example/login"]++ def test_another_path_on_the_same_written_origin_survives(self) -> None:+ assert self._ground(+ ["https://portal.example/login"],+ "read https://portal.example/reports/2026 for me",+ ) == ["https://portal.example/login"]++ def test_a_second_written_origin_is_kept_so_the_fill_seam_can_refuse_both(self) -> None:+ grounded = self._ground(+ ["https://portal.example/login", "https://tracker.example/login"],+ "sign in at https://portal.example/login then file it at https://tracker.example/login",+ )+ assert len(grounded) == 2++ def test_an_origin_written_only_in_an_earlier_turn_is_dropped(self) -> None:+ # Grounding reads the message asking for the fill, never the conversation: a link pasted in an+ # earlier turn for another purpose must not license that origin for a later credential request.+ assert self._ground(["https://evil.example/login"], "sign in with my bank credential") == []