Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Add the selector engines Skyvern's producers emit to skycdp (SKY-14071) (#8217)
Shuchang Zheng6a6b6383eb6a
diff --git a/skyvern/core/script_generations/generate_script.py b/skyvern/core/script_generations/generate_script.pyindex 0b6b58e2a..4bd12de94 100644--- a/skyvern/core/script_generations/generate_script.py+++ b/skyvern/core/script_generations/generate_script.py@@ -338,6 +338,7 @@ ACTION_MAP = {"upload_file": "upload_file","select_option": "select_option","goto": "goto",+ "goto_url": "goto","scroll": "scroll","keypress": "keypress","type": "type",@@ -406,7 +407,7 @@ def _build_semantic_selector(act: dict[str, Any]) -> str | None:return None-ACTIONS_OPT_OUT_INTENTION_FOR_PROMPT = ["extract"]+ACTIONS_OPT_OUT_INTENTION_FOR_PROMPT = ["extract", "goto", "magic_link"]INDENT = " " * 4DOUBLE_INDENT = " " * 8@@ -1239,6 +1240,59 @@ def _action_to_stmt(),))+ elif method == "goto":+ if act.get("is_magic_link"):+ # The recorded URL is single-use and already spent by replay time, so the cached+ # script re-requests a link instead of navigating to the captured value.+ method = "magic_link"+ magic_link_identifier: cst.BaseExpression | None = None+ if task.get("totp_identifier"):+ magic_link_identifier = _value(task.get("totp_identifier"))+ elif credential_key := pick_credential_root_for_block(+ goal_template=goal_template,+ credential_param_keys=credential_param_keys,+ ):+ magic_link_identifier = cst.Call(+ func=cst.Attribute(+ value=cst.Name("context"),+ attr=cst.Name("credential_totp_identifier"),+ ),+ args=[cst.Arg(value=_value(credential_key))],+ )+ if magic_link_identifier is not None:+ args.append(+ cst.Arg(+ keyword=cst.Name("totp_identifier"),+ value=magic_link_identifier,+ whitespace_after_arg=cst.ParenthesizedWhitespace(+ indent=True,+ last_line=cst.SimpleWhitespace(INDENT),+ ),+ )+ )+ if task.get("totp_verification_url"):+ args.append(+ cst.Arg(+ keyword=cst.Name("totp_url"),+ value=_value(task.get("totp_verification_url")),+ whitespace_after_arg=cst.ParenthesizedWhitespace(+ indent=True,+ last_line=cst.SimpleWhitespace(INDENT),+ ),+ )+ )+ elif act.get("url"):+ args.append(+ cst.Arg(+ keyword=cst.Name("url"),+ value=_value(act.get("url")),+ whitespace_after_arg=cst.ParenthesizedWhitespace(+ indent=True,+ last_line=cst.SimpleWhitespace(INDENT),+ ),+ )+ )+elif method == "extract":args.append(cst.Arg(diff --git a/skyvern/core/script_generations/script_skyvern_page.py b/skyvern/core/script_generations/script_skyvern_page.pyindex 593ceb99f..8c7635dad 100644--- a/skyvern/core/script_generations/script_skyvern_page.py+++ b/skyvern/core/script_generations/script_skyvern_page.py@@ -46,7 +46,7 @@ from skyvern.forge.sdk.db.utils import ACTION_TYPE_TO_CLASSfrom skyvern.forge.sdk.schemas.totp_codes import OTPTypefrom skyvern.forge.sdk.services.credentials import generate_totp_codefrom skyvern.schemas.steps import AgentStepOutput-from skyvern.services.otp_service import poll_otp_value+from skyvern.services.otp_service import MAGIC_LINK_ANCHOR_GRACE, poll_otp_valuefrom skyvern.utils.url_validators import validate_fetch_urlfrom skyvern.webeye.actions.action_types import ActionTypefrom skyvern.webeye.actions.actions import (@@ -69,6 +69,7 @@ from skyvern.webeye.actions.handler import (from skyvern.webeye.actions.responses import ActionFailure, ActionResult, ActionSuccessfrom skyvern.webeye.browser_engine import BrowserEngineSelectionfrom skyvern.webeye.browser_state import BrowserState+from skyvern.webeye.navigation import default_navigation_settle, navigate_with_retry, redact_url_secretsfrom skyvern.webeye.scraper.scraped_page import ScrapedPagefrom skyvern.webeye.utils.page import SkyvernFrame@@ -1076,6 +1077,57 @@ class ScriptSkyvernPage(SkyvernPage):raise RuntimeError("Navigation failed but no error was captured")raise last_error+ async def magic_link(+ self,+ totp_identifier: str | None = None,+ totp_url: str | None = None,+ ) -> None:+ """Poll for an emailed sign-in link and open it on this page.++ The polled URL is never passed through ``render_template``: it is single-use, opaque, and+ may contain sequences the renderer would treat as markup.+ """+ context = skyvern_context.current()+ organization_id = context.organization_id if context else None+ if not organization_id:+ raise RuntimeError("A sign-in link is unavailable: no organization is associated with this run.")++ if totp_identifier:+ totp_identifier = render_template(totp_identifier)+ if totp_url:+ totp_url = render_template(totp_url)+ if not totp_identifier and not totp_url:+ raise RuntimeError("A sign-in link is unavailable: this step has no email/SMS identifier to receive it on.")++ polled = await poll_otp_value(+ organization_id=organization_id,+ task_id=context.task_id if context else None,+ workflow_run_id=context.workflow_run_id if context else None,+ totp_verification_url=totp_url,+ totp_identifier=totp_identifier,+ created_after=naive_utc_now() - MAGIC_LINK_ANCHOR_GRACE,+ expected_otp_type=OTPType.MAGIC_LINK,+ )+ if polled is None:+ raise RuntimeError("A sign-in link could not be retrieved for this step.")+ # A configured webhook answers without honouring expected_otp_type, so a one-time code can+ # come back here; navigating to it would report a confusing blocked-destination instead.+ if polled.get_otp_type() is not OTPType.MAGIC_LINK:+ raise RuntimeError(+ f"Expected a sign-in link but this step received {polled.get_otp_type().value}. "+ "Use the one-time code verb for this site instead of magic_link."+ )++ await navigate_with_retry(+ navigate=lambda strategy: self.page.goto(+ polled.value, timeout=settings.BROWSER_LOADING_TIMEOUT_MS, wait_until=strategy+ ),+ url=polled.value,+ retry_times=NAVIGATION_MAX_RETRY_TIME,+ settle=default_navigation_settle,+ log_url=redact_url_secrets(polled.value),+ )+@action_wrap(ActionType.SOLVE_CAPTCHA)async def solve_captcha(self, prompt: str | None = None, data: str | dict[str, Any] | None = None, intention: str | None = Nonediff --git a/skyvern/core/script_generations/skyvern_page.py b/skyvern/core/script_generations/skyvern_page.pyindex 26288cc82..f8d66b4c8 100644--- a/skyvern/core/script_generations/skyvern_page.py+++ b/skyvern/core/script_generations/skyvern_page.py@@ -192,6 +192,15 @@ class SkyvernPage(Page):) -> str:return value+ async def magic_link(+ self,+ totp_identifier: str | None = None,+ totp_url: str | None = None,+ ) -> None:+ # Raises rather than no-opping like the value helpers above: a silent return would let a+ # script continue past a sign-in that never happened.+ raise NotImplementedError("Magic link sign-in is not supported outside server context")+def _is_secret_reference(self, value: str) -> bool:return Falsediff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex 52ebab0ca..e453d99ca 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -1810,7 +1810,12 @@ class AgentFunction:if "@" not in totp_identifier:return None- from skyvern.services.otp_service import InsufficientCreditsForOTPParse, parse_otp_login+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.services.otp_service import (+ InsufficientCreditsForOTPParse,+ looks_like_magic_link,+ parse_otp_login,+ )lookup_context = context or EmailOTPVerificationContext()sources = build_email_otp_sources(self)@@ -1893,11 +1898,17 @@ class AgentFunction:)continuesource_context.remember_message(credential_id, candidate.message_id)+ if otp_value is None and expected_otp_type is OTPType.TOTP:+ # An enforced parse reports "not found" rather than the type it did see,+ # so a link arriving for a code-verb call is only visible by its shape.+ if looks_like_magic_link(candidate.content):+ lookup_context.observed_otp_types.add(OTPType.MAGIC_LINK)if (otp_valueand expected_otp_type is not Noneand otp_value.get_otp_type() != expected_otp_type):+ lookup_context.observed_otp_types.add(otp_value.get_otp_type())continueif otp_value:try:diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.pyindex 8ead73b9d..ad02b0e73 100644--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py@@ -2930,5 +2930,10 @@ def credential_otp_authoring_guidance(credential_key: str) -> str:"use a broad text locator as the authenticated anchor, including `page.get_by_text(...)` even with "'`exact=True`, `page.locator("text=...")`, or an unscoped `.first`; hidden or duplicate text can turn a '"successful sign-in into a timeout or strict-mode failure. If the page shows an invalid or rejected code, "- "raise so the run reports failure."+ "raise so the run reports failure. "+ f"If the scouted page offers an emailed sign-in link rather than a one-time-code field, author "+ f"`await {credential_key}.magic_link(page)` instead of `await {credential_key}.otp()`. It polls for the "+ "link and navigates the page itself, so there is nothing to fill or open. Choose one verb from the "+ "scouted page; a wrong choice fails at test-run time, so switch verbs then rather than authoring a "+ "fallback that tries both.")diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 03b7ad856..a67789a8c 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -57,6 +57,7 @@ from skyvern.constants import (MAX_FILE_PARSE_INPUT_TOKENS,MAX_PDF_OCR_PAGES,MAX_UPLOAD_FILE_COUNT,+ NAVIGATION_MAX_RETRY_TIME,PDF_OCR_PAGE_CONCURRENCY,SAVE_DOWNLOADED_FILES_TIMEOUT,)@@ -126,6 +127,7 @@ from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.aiohttp_helper import aiohttp_requestfrom skyvern.forge.sdk.core.hashing import diagnostic_fingerprintfrom skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.db.datetime_utils import naive_utc_nowfrom skyvern.forge.sdk.db.enums import TaskTypefrom skyvern.forge.sdk.db.exceptions import NotFoundErrorfrom skyvern.forge.sdk.db.id import generate_action_id@@ -213,7 +215,7 @@ from skyvern.schemas.workflows import (_validate_code_block_error_code_calls,_validate_code_block_error_code_mapping,)-from skyvern.services import otp_service, planner_levers+from skyvern.services import otp_email, otp_service, planner_leversfrom skyvern.services.error_detection_service import detect_user_defined_errors_for_taskfrom skyvern.services.self_heal_cap import check_and_increment_self_heal_capfrom skyvern.utils.prompt_engine import PROMPT_HARD_CEILING_TOKENS@@ -227,8 +229,10 @@ from skyvern.webeye.actions.action_types import ActionTypefrom skyvern.webeye.actions.actions import Action, ActionStatusfrom skyvern.webeye.browser_artifacts import DownloadBindingfrom skyvern.webeye.browser_factory import rebind_download_dir+from skyvern.webeye.browser_object_predicates import is_page_likefrom skyvern.webeye.browser_state import BrowserStatefrom skyvern.webeye.cdp_download_interceptor import normalize_download_filename, settle_browser_downloads_for_context+from skyvern.webeye.navigation import default_navigation_settle, navigate_with_retry, redact_url_secretsfrom skyvern.webeye.real_browser_state import RealBrowserStatefrom skyvern.webeye.utils.page import SkyvernFrame@@ -4131,6 +4135,36 @@ class OTPResult(str):return self.otp_type is OTPType.MAGIC_LINK+_OTP_VERB_BY_TYPE = {+ OTPType.TOTP: "await <credential>.otp()",+ OTPType.MAGIC_LINK: "await <credential>.magic_link(page)",+}+++def _otp_wait_failure(+ expected_otp_type: OTPType,+ observed_otp_types: set[OTPType],+ budget_seconds: int,+) -> CodeBlockOTPError:+ """Turn a wait that produced nothing into a mismatch report when the other kind of OTP arrived.++ A wrong verb otherwise looks exactly like a mailbox that stayed empty, which leaves the+ authoring retry with nothing to act on.+ """+ mismatched = sorted(observed for observed in observed_otp_types if observed is not expected_otp_type)+ if not mismatched:+ return CodeBlockOTPError(f"OTP was not received within {budget_seconds} seconds.")+ found = ", ".join(observed.value for observed in mismatched)+ # .get, not [], so a future OTPType cannot turn this error path into a KeyError.+ suggested = _OTP_VERB_BY_TYPE.get(mismatched[0], "the matching verb")+ attempted = _OTP_VERB_BY_TYPE.get(expected_otp_type, "this verb")+ return CodeBlockOTPError(+ f"Waited {budget_seconds}s for an OTP of type {expected_otp_type.value}, "+ f"but this mailbox delivered {found}. This sign-in uses the other flow: "+ f"author {suggested} instead of {attempted}."+ )++async def _poll_code_block_otp(totp_identifier: str,organization_id: str | None,@@ -4139,6 +4173,7 @@ async def _poll_code_block_otp(*,budget_seconds: int,subject: str,+ expected_otp_type: OTPType,) -> otp_service.OTPValue:if not organization_id:raise CodeBlockOTPError("OTP is unavailable: no organization is associated with this code block.")@@ -4148,6 +4183,19 @@ async def _poll_code_block_otp(if workflow_run is None:raise CodeBlockOTPError("OTP is unavailable: the workflow run could not be loaded.")+ # A magic link is single-use, so one minted earlier in this run may already be spent. Anchoring+ # near call time narrows that window; it does not close it (SKY-13417 owns consumed-entry marking).+ # The grace interval keeps a link that landed while the triggering click was still settling+ # visible, and absorbs clock skew against the inbox's timestamps. Naive UTC, matching+ # workflow_runs.started_at and both downstream filters: a local-clock anchor would filter+ # every delivery east of UTC and widen the window west.+ if expected_otp_type is OTPType.MAGIC_LINK:+ created_after = naive_utc_now() - otp_service.MAGIC_LINK_ANCHOR_GRACE+ else:+ created_after = workflow_run.started_at++ email_context = otp_email.EmailOTPVerificationContext()+ raw_context = otp_service.RawOTPVerificationContext()try:polled = await asyncio.wait_for(otp_service.poll_otp_value(@@ -4156,13 +4204,19 @@ async def _poll_code_block_otp(workflow_run_id=workflow_run_id,workflow_permanent_id=workflow_run.workflow_permanent_id,totp_identifier=totp_identifier,- created_after=workflow_run.started_at,- expected_otp_type=OTPType.TOTP,+ created_after=created_after,+ expected_otp_type=expected_otp_type,+ email_context=email_context,+ raw_context=raw_context,),timeout=budget_seconds,)except asyncio.TimeoutError:- raise CodeBlockOTPError(f"OTP was not received within {budget_seconds} seconds.")+ raise _otp_wait_failure(+ expected_otp_type,+ email_context.observed_otp_types | raw_context.observed_otp_types,+ budget_seconds,+ )except (NoTOTPVerificationCodeFound, FailedToGetTOTPVerificationCode):raise CodeBlockOTPError(f"OTP could not be retrieved for {subject}.")@@ -4216,6 +4270,43 @@ async def _resolve_code_block_otp(workflow_run_context,budget_seconds=budget_seconds,subject="this credential",+ expected_otp_type=OTPType.TOTP,+ )+ return polled.value+++async def _resolve_code_block_magic_link(+ credential_parameter_key: str,+ organization_id: str | None,+ workflow_run_id: str | None,+ *,+ budget_seconds: int,+) -> str:+ """Resolve a fresh sign-in link for one credential, for the caller to navigate to.++ The authenticator-seed branch that ``_resolve_code_block_otp`` tries first is deliberately+ skipped: a seed can only mint a 6-digit code, and returning one here would navigate to it.+ """+ if not workflow_run_id:+ raise CodeBlockOTPError("A sign-in link is unavailable: no workflow run is associated with this code block.")+ workflow_run_context = _code_block_workflow_run_context(workflow_run_id)++ totp_identifier = workflow_run_context.get_credential_totp_identifier(credential_parameter_key)+ if not totp_identifier:+ raise CodeBlockOTPError(+ "No email or SMS identifier is configured for this credential, so no sign-in link can be "+ "received. Add an email/SMS identifier to the credential; an authenticator secret cannot "+ "produce a sign-in link."+ )++ polled = await _poll_code_block_otp(+ totp_identifier,+ organization_id,+ workflow_run_id,+ workflow_run_context,+ budget_seconds=budget_seconds,+ subject="this credential",+ expected_otp_type=OTPType.MAGIC_LINK,)return polled.value@@ -4239,6 +4330,7 @@ async def _resolve_code_block_otp_for_identifier(workflow_run_context,budget_seconds=budget_seconds,subject="this address",+ expected_otp_type=OTPType.TOTP,)return OTPResult(polled.value, polled.get_otp_type())@@ -4262,6 +4354,47 @@ def _bind_code_block_otp(return otp+def _bind_code_block_magic_link(+ credential_parameter_key: str,+ organization_id: str | None,+ workflow_run_id: str | None,+ expected_page: Page | RecordingPage | None = None,+) -> Callable[[Page | RecordingPage], Awaitable[None]]:+ """Build the awaitable ``magic_link`` method bound onto a code block's Credential.++ Takes the page as an argument rather than closing over it, mirroring ``solve_captcha(page)``.+ ``expected_page`` is the page the block will actually run on; the caller rebinds with it once+ that page exists, so the argument can be checked by identity rather than by capability.+ """++ async def magic_link(page: Page | RecordingPage) -> None:+ # Authored code hands the page in, and it receives the link through whatever goto() it+ # carries. Only identity is unforgeable here: a structural capability check is satisfiable+ # by a class the block defines itself. The structural fallback covers the bind-time window+ # before the page exists, which no authored code can reach.+ candidate: object = page+ if expected_page is not None:+ if candidate is not expected_page:+ raise CodeBlockOTPError("magic_link requires the code block's page.")+ elif not is_page_like(candidate):+ raise CodeBlockOTPError("magic_link requires the code block's page.")+ link = await _resolve_code_block_magic_link(+ credential_parameter_key,+ organization_id,+ workflow_run_id,+ budget_seconds=settings.CODE_BLOCK_OTP_POLL_TIMEOUT_SECONDS,+ )+ await navigate_with_retry(+ navigate=lambda strategy: page.goto(link, timeout=settings.BROWSER_LOADING_TIMEOUT_MS, wait_until=strategy),+ url=link,+ retry_times=NAVIGATION_MAX_RETRY_TIME,+ settle=default_navigation_settle,+ log_url=redact_url_secrets(link),+ )++ return magic_link++async def _code_block_otp_builtin(credential: object,*,@@ -4269,8 +4402,8 @@ async def _code_block_otp_builtin(workflow_run_id: str | None = None,) -> str:"""Top-level ``await otp(...)`` sugar. Given a credential it forwards to that credential's- bound otp(); given a bare email/SMS address it resolves against the address alone and- returns an OTPResult that still knows whether it is a code or a link."""+ bound otp(), which returns a bare code string; given a bare email/SMS address it resolves+ against the address alone and returns an OTPResult that knows whether it is a code or a link."""if isinstance(credential, str):return await _resolve_code_block_otp_for_identifier(credential,@@ -4616,6 +4749,14 @@ class CodeBlock(Block):if parameters:for key, value in parameters.items():if key not in safe_vars:+ # Rebind against the page this block actually runs on. The bind above happens+ # before the page exists, and only object identity is unforgeable: authored code+ # can define a class carrying any capability a structural check looks for, and+ # would then receive the sign-in link through its own goto().+ if isinstance(value, Credential):+ value.magic_link = _bind_code_block_magic_link(+ key, organization_id, workflow_run_id, expected_page=page+ )safe_vars[key] = valueif key.isidentifier() and not keyword.iskeyword(key) and not key.startswith("__"):parameter_defaults[key] = value@@ -6446,6 +6587,9 @@ async def wrapper({default_args}):real_secret_values[credential_field] = real_secret_valuecredential_namespace = Credential(**real_secret_values)credential_namespace.otp = _bind_code_block_otp(parameter.key, organization_id, workflow_run_id)+ credential_namespace.magic_link = _bind_code_block_magic_link(+ parameter.key, organization_id, workflow_run_id+ )parameter_values[parameter.key] = credential_namespaceelse:secret_value = workflow_run_context.get_original_secret_value_or_none(value)diff --git a/skyvern/services/otp_email.py b/skyvern/services/otp_email.pyindex 33beca52a..a5109acb3 100644--- a/skyvern/services/otp_email.py+++ b/skyvern/services/otp_email.py@@ -8,6 +8,7 @@ from typing import TYPE_CHECKING, Protocolimport httpxfrom google.oauth2.credentials import Credentials+from skyvern.forge.sdk.schemas.totp_codes import OTPTypefrom skyvern.forge.sdk.services import google_gmail_service, google_oauth_service, microsoft_oauth_servicefrom skyvern.services.email import outlook@@ -76,6 +77,10 @@ class EmailOTPVerificationContext:"""Whole-poll cache: one EmailOTPSourceContext per source name."""per_source: dict[str, EmailOTPSourceContext] = field(default_factory=dict)+ # Types seen but rejected for not matching the caller's expected_otp_type. A rejected+ # message is never persisted, so this is the only surviving record that a wrong-verb+ # caller waited out its budget while the other kind of OTP was sitting in the inbox.+ observed_otp_types: set[OTPType] = field(default_factory=set)def for_source(self, source_name: str) -> EmailOTPSourceContext:return self.per_source.setdefault(source_name, EmailOTPSourceContext())diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.pyindex ba51bdff6..a161a88ed 100644--- a/skyvern/services/otp_service.py+++ b/skyvern/services/otp_service.py@@ -31,10 +31,18 @@ LOG = structlog.get_logger()_RAW_OTP_REPARSE_LIMIT = 3+# How far before its call a magic_link() poll may look. A link's email is often sent while the+# triggering click is still settling, so a strict call-time anchor filters out the very link the+# poll awaits; a spent link from earlier in the run only re-surfaces inside this interval.+MAGIC_LINK_ANCHOR_GRACE = timedelta(seconds=60)+@dataclassclass RawOTPVerificationContext:misses: set[tuple[str, OTPType]] = field(default_factory=set)+ # Types that were present but rejected for not matching expected_otp_type; lets a caller+ # that times out say which kind of OTP did arrive instead of only that none matched.+ observed_otp_types: set[OTPType] = field(default_factory=set)_MFA_PARAMETER_KEY_HINTS = ("mfa", "otp", "verification")@@ -170,6 +178,15 @@ def _verbatim_otp_value(content: str, otp_type: OTPType | None, llm_value: str |return scored[0][1]+def looks_like_magic_link(content: str) -> bool:+ """Whether a message body is a bare sign-in link, decided without an LLM call.++ Used only to explain a wrong-verb timeout: an enforced parse returns None rather than the+ other OTP type, so the type that did arrive is otherwise unrecoverable.+ """+ return bool(_BARE_URL_PATTERN.match(content.strip()))++async def parse_otp_login(content: str,organization_id: str,@@ -561,7 +578,14 @@ async def poll_otp_value(totp_identifier: str | None = None,created_after: datetime | None = None,expected_otp_type: OTPType | None = None,+ email_context: EmailOTPVerificationContext | None = None,+ raw_context: RawOTPVerificationContext | None = None,) -> OTPValue | None:+ """Poll until an OTP of ``expected_otp_type`` arrives or the wall-clock budget expires.++ A caller that cancels this early (``asyncio.wait_for``) can pass its own contexts and then+ read ``observed_otp_types`` off them to report which kind of OTP did arrive.+ """timeout = timedelta(minutes=settings.VERIFICATION_CODE_POLLING_TIMEOUT_MINS)start_datetime = datetime.utcnow()timeout_datetime = start_datetime + timeout@@ -576,8 +600,8 @@ async def poll_otp_value(consecutive_failures = 0last_error_reason: str | None = Noneorg_token: OrganizationAuthToken | None = None- email_otp_context = EmailOTPVerificationContext()- raw_otp_context = RawOTPVerificationContext()+ email_otp_context = email_context if email_context is not None else EmailOTPVerificationContext()+ raw_otp_context = raw_context if raw_context is not None else RawOTPVerificationContext()while True:await asyncio.sleep(10)if datetime.utcnow() > timeout_datetime:@@ -856,6 +880,7 @@ async def _get_otp_value_from_db(stored_otp_value = OTPValue(value=parsed_row.code, type=parsed_row.otp_type)if expected_otp_type is None or stored_otp_value.get_otp_type() == expected_otp_type:return stored_otp_value+ context.observed_otp_types.add(stored_otp_value.get_otp_type())if not parsed_row.content:continueif expected_otp_type is None:@@ -885,6 +910,8 @@ async def _get_otp_value_from_db(return Noneif otp_value is None or otp_value.get_otp_type() != expected_otp_type:context.misses.add(cache_key)+ if otp_value is not None:+ context.observed_otp_types.add(otp_value.get_otp_type())continueif is_raw:await app.DATABASE.otp.promote_raw_otp_code(diff --git a/skyvern/webeye/navigation.py b/skyvern/webeye/navigation.pyindex 7eaf4b35b..d742a0e49 100644--- a/skyvern/webeye/navigation.py+++ b/skyvern/webeye/navigation.py@@ -4,9 +4,11 @@ import asyncioimport timefrom collections.abc import Awaitable, Callablefrom typing import Literal+from urllib.parse import urlsplitimport structlog+from skyvern.config import settingsfrom skyvern.constants import PERMANENT_NAV_ERRORS, SKIP_INNER_NAV_RETRY_ERRORSfrom skyvern.exceptions import BlockedHost, BlockedNavigationDestination, FailedToNavigateToUrl, InvalidUrlfrom skyvern.utils.url_validators import canonical_navigation_host, is_blocked_host@@ -109,6 +111,25 @@ def is_permanent_navigation_error(error_message: str) -> bool:return any(pattern in error_message for pattern in PERMANENT_NAV_ERRORS)+def redact_url_secrets(url: str) -> str:+ """Reduce a URL to scheme and host for display.++ A single-use sign-in link carries its bearer token in the path, query, or fragment, and the+ display value below reaches both the logs and the run's user-visible failure_reason.+ """+ split = urlsplit(url)+ # hostname, not netloc: netloc carries any user:password@ prefix.+ host = split.hostname+ if not split.scheme or not host:+ return "<redacted>"+ port = f":{split.port}" if split.port else ""+ return f"{split.scheme}://{host}{port}/<redacted>"+++async def default_navigation_settle() -> None:+ await asyncio.sleep(settings.BROWSER_ACTION_TIMEOUT_MS / 1000)++async def navigate_with_retry(navigate: NavigateFunc,url: str,@@ -116,18 +137,33 @@ async def navigate_with_retry(settle: SettleFunc,wait_until: Literal["load", "domcontentloaded", "commit"] = "load",sleep: SleepFunc | None = None,+ log_url: str | None = None,) -> None:# Late-bound so a test patching ``asyncio.sleep`` reaches the retry backoff.if sleep is None:sleep = asyncio.sleepdegradation = _DEGRADATION_MAP.get(wait_until, [wait_until])+ # Callers navigating to a secret URL pass a redacted display value; validation and the+ # navigation itself still use the real one.+ display_url = log_url if log_url is not None else url++ # A redacting caller also loses the exception chain and traceback: both carry the original+ # message, and a secret URL must not survive in either.+ redacting = log_url is not None# Fail closed before any request is dispatched so a blocked target never reaches the browser.- await asyncio.to_thread(validate_navigation_destination, url)+ try:+ await asyncio.to_thread(validate_navigation_destination, url)+ except BlockedNavigationDestination as error:+ # A self-hosted portal's sign-in link can legitimately resolve to a private host, so the+ # refused URL is as much a secret as an accepted one.+ if not redacting:+ raise+ raise BlockedNavigationDestination(url=display_url, reason=error.reason) from Nonefor attempt in range(retry_times):strategy = degradation[min(attempt, len(degradation) - 1)]- LOG.info("Trying to navigate to url", url=url, retry_time=attempt, wait_until=strategy)+ LOG.info("Trying to navigate to url", url=display_url, retry_time=attempt, wait_until=strategy)try:start_time = time.monotonic()response = await navigate(strategy)@@ -135,24 +171,33 @@ async def navigate_with_retry(# layer, so a public entry point can still land on an internal host (SKY-13112).await _revalidate_navigation_response(response)elapsed = time.monotonic() - start_time- LOG.info("Page loading time", loading_time=elapsed, url=url, wait_until=strategy)+ LOG.info("Page loading time", loading_time=elapsed, url=display_url, wait_until=strategy)await settle()- LOG.info("Successfully navigated to url", url=url, retry_time=attempt, wait_until=strategy)+ LOG.info("Successfully navigated to url", url=display_url, retry_time=attempt, wait_until=strategy)return- except BlockedNavigationDestination:+ except BlockedNavigationDestination as error:# Blocked destinations are permanent; retrying re-issues the same request.- raise+ if not redacting:+ raise+ # This one names a refused redirect hop, not the requested url, so redact that hop+ # rather than substituting the caller's display value for it.+ raise BlockedNavigationDestination(url=redact_url_secrets(error.url), reason=error.reason) from Noneexcept Exception as error:error_str = str(error)+ # Playwright names the destination in its own message, so the raw text carries the+ # secret even though the url field is redacted. Match on the raw text, report the safe one.+ safe_error_str = error_str.replace(url, display_url) if log_url is not None else error_strif is_skip_inner_retry_error(error_str):LOG.warning("Non-retriable navigation error, failing immediately",- url=url,- error=error_str,+ url=display_url,+ error=safe_error_str,+ )+ raise FailedToNavigateToUrl(url=display_url, error_message=safe_error_str) from (+ None if redacting else error)- raise FailedToNavigateToUrl(url=url, error_message=error_str) from errorif attempt >= retry_times - 1:# Terminal navigation failure is re-raised as FailedToNavigateToUrl and surfaced@@ -160,19 +205,21 @@ async def navigate_with_retry(# (site-unreachable) outcome. Keep the traceback, drop the error level.LOG.warning("Failed to navigate after retries",- url=url,+ url=display_url,retry_times=retry_times,- error=error_str,- exc_info=True,+ error=safe_error_str,+ exc_info=not redacting,+ )+ raise FailedToNavigateToUrl(url=display_url, error_message=safe_error_str) from (+ None if redacting else error)- raise FailedToNavigateToUrl(url=url, error_message=error_str) from errorLOG.warning("Error while navigating to url, retrying",- exc_info=True,- url=url,+ exc_info=not redacting,+ url=display_url,retry_time=attempt,wait_until=strategy,- error=error_str,+ error=safe_error_str,)await sleep(1)diff --git a/skyvern/webeye/skycdp/facade/locator.py b/skyvern/webeye/skycdp/facade/locator.pyindex 3fc874a02..af5aace6b 100644--- a/skyvern/webeye/skycdp/facade/locator.py+++ b/skyvern/webeye/skycdp/facade/locator.py@@ -56,22 +56,49 @@ class FrameLocator:return frame-# Selector resolution mirrors the xpath-vs-css split of Playwright's engine for the shapes-# Skyvern's DOM layer emits: `xpath=`-, `//`-, and `..`-prefixed steps are XPath, everything else-# is CSS. (Playwright's other engines -- `text=`, `:visible`, `>>` chains -- still fail loudly as-# invalid CSS here.) Three Playwright behaviors are deliberately copied: a leading `/` is-# rewritten relative when the root is an element, so chained absolute paths stay inside their-# scope instead of silently escaping to the document; XPath results keep only element nodes, so a-# `..` hop onto an attribute/text/shadow-root node vanishes instead of impersonating an element;-# and the CSS branch pierces open shadow roots -- including when the root itself is the host,-# which dom.py's interactive-descendant guards rely on because they fail open on count 0. Shadow-# matches are appended after their host root's own matches, a document-order approximation that is-# exact for the unique-match selectors (`[unique_id=...]`, ids) the DOM layer emits.+# Selector resolution mirrors Playwright's engine for the shapes Skyvern's producers emit.+# Engine dispatch: `xpath=` / `css=` / `id=` / `text=` prefixes, bare XPath auto-detection+# (`//`, `..`, and parenthesised `(//x)[n]`), and the css engine's `:has-text()` / `:visible`+# tail pseudo-extensions. Anything else still fails loudly as invalid CSS. Copied Playwright+# behaviors that matter for correctness: a leading `/` is rewritten relative when the root is an+# element (chained absolute paths stay inside their scope); XPath results keep only element nodes+# (a `..` hop onto an attribute/text/shadow-root node vanishes); CSS pierces open shadow roots,+# including when the root itself is the host, with a root's light-DOM matches emitted before its+# shadow matches; `text=` matches the smallest containing element, case-insensitively when+# unquoted, exactly when quoted. `:has-text()`/`:visible` resolve+# anywhere Playwright allows them -- comma lists, composed with native pseudos, intermediate+# compounds (via a :scope recursion per candidate). Text containment across nested shadow+# boundaries follows textContent, which does not cross shadow roots -- same as per-root matching._QUERY_ALL_JS = """const __queryAll = (root, selector) => {- const explicit = selector.startsWith('xpath=');- if (explicit || selector.startsWith('//') || selector.startsWith('..')) {- let xp = explicit ? selector.slice('xpath='.length) : selector;+ const normText = (s) => s.replace(/\\s+/g, ' ').trim();+ const elementText = (el) => {+ if (el.tagName === 'INPUT' && (el.type === 'button' || el.type === 'submit')) return el.value || '';+ if (/^(SCRIPT|STYLE|NOSCRIPT)$/.test(el.tagName)) return '';+ let text = '';+ for (const child of el.childNodes) {+ if (child.nodeType === 3) text += child.nodeValue;+ else if (child.nodeType === 1) text += elementText(child);+ }+ return text;+ };+ const isVisible = (el) => {+ const rect = el.getBoundingClientRect();+ return rect.width > 0 && rect.height > 0 && getComputedStyle(el).visibility !== 'hidden';+ };+ const queryCss = (from, css) => {+ const out = [];+ const visit = (r) => {+ for (const el of r.querySelectorAll(css)) out.push(el);+ if (r.nodeType === 1 && r.shadowRoot) visit(r.shadowRoot);+ for (const el of r.querySelectorAll('*')) {+ if (el.shadowRoot) visit(el.shadowRoot);+ }+ };+ visit(from);+ return out;+ };+ const queryXPath = (xp) => {if (xp.startsWith('/') && root.nodeType !== 9) xp = '.' + xp;const doc = root.nodeType === 9 ? root : root.ownerDocument;const it = doc.evaluate(xp, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);@@ -81,20 +108,198 @@ const __queryAll = (root, selector) => {if (n && n.nodeType === 1) out.push(n);}return out;+ };++ let engine = 'css';+ let body = selector;+ const prefixed = /^([a-zA-Z][a-zA-Z0-9-]*)=([\\s\\S]*)$/.exec(selector);+ if (prefixed && ['css', 'xpath', 'id', 'text'].includes(prefixed[1])) {+ engine = prefixed[1];+ body = prefixed[2];+ } else if (selector.startsWith('//') || selector.startsWith('..') || /^\\(+\\s*\\.?\\//.test(selector)) {+ engine = 'xpath';}- const out = [];- const visit = (r) => {- for (const el of r.querySelectorAll(selector)) out.push(el);- if (r.nodeType === 1 && r.shadowRoot) visit(r.shadowRoot);- for (const el of r.querySelectorAll('*')) {- if (el.shadowRoot) visit(el.shadowRoot);++ if (engine === 'xpath') return queryXPath(body);+ if (engine === 'id') return queryCss(root, '[id=' + JSON.stringify(body) + ']');+ if (engine === 'text') {+ let matchesText;+ const re = /^\\/(.*)\\/([a-z]*)$/s.exec(body);+ const quoted =+ body.length > 1 &&+ ((body.startsWith('"') && body.endsWith('"')) || (body.startsWith("'") && body.endsWith("'")));+ if (re) {+ matchesText = (t) => new RegExp(re[1], re[2]).test(t);+ } else if (quoted) {+ const exact = normText(body.slice(1, -1));+ matchesText = (t) => t === exact;+ } else {+ const needle = normText(body).toLowerCase();+ matchesText = (t) => t.toLowerCase().includes(needle);+ }+ // Playwright's text engine never matches inside head/script/style content.+ const matches = queryCss(root, '*').filter(+ (el) => !/^(HEAD|SCRIPT|STYLE|NOSCRIPT)$/.test(el.tagName) && matchesText(normText(elementText(el)))+ );+ return matches.filter(+ (el) => !matches.some((m) => m !== el && (el.contains(m) || (el.shadowRoot && el.shadowRoot.contains(m))))+ );+ }++ // CSS with the :visible / :has-text() extensions anywhere Playwright allows them: in+ // comma-separated lists, composed with native pseudos, and on intermediate compounds.+ // scanCss marks indices outside quotes, brackets, and parens so splitting and pseudo+ // detection never trip on selector-internal punctuation.+ const scanCss = (s) => {+ const top = new Array(s.length).fill(false);+ let quote = null;+ let depth = 0;+ for (let i = 0; i < s.length; i++) {+ const ch = s[i];+ if (quote) {+ if (ch === '\\\\') { i++; continue; }+ if (ch === quote) quote = null;+ continue;+ }+ if (ch === '"' || ch === "'") { quote = ch; continue; }+ if (ch === '[' || ch === '(') { depth++; continue; }+ if (ch === ']' || ch === ')') { depth--; continue; }+ if (depth === 0) top[i] = true;+ }+ return top;+ };+ const findPseudo = (s) => {+ const top = scanCss(s);+ for (let i = 0; i < s.length; i++) {+ if (!top[i] || s[i] !== ':') continue;+ if (s.startsWith(':visible', i) && !/[a-zA-Z-]/.test(s[i + 8] || '')) {+ return { start: i, end: i + 8, kind: 'visible' };+ }+ if (s.startsWith(':has-text(', i)) {+ let j = i + 10;+ let quote = null;+ for (; j < s.length; j++) {+ const ch = s[j];+ if (quote) {+ if (ch === '\\\\') { j++; continue; }+ if (ch === quote) quote = null;+ continue;+ }+ if (ch === '"' || ch === "'") { quote = ch; continue; }+ if (ch === ')') break;+ }+ return { start: i, end: j + 1, kind: 'has-text', raw: s.slice(i + 10, j).trim() };+ }+ }+ return null;+ };+ const hasTextPredicate = (raw) => {+ if (raw.length > 1 && (raw[0] === '"' || raw[0] === "'") && raw[raw.length - 1] === raw[0]) {+ raw = raw.slice(1, -1).replace(/\\\\(.)/g, '$1');}+ const needle = normText(raw).toLowerCase();+ return (el) => normText(elementText(el)).toLowerCase().includes(needle);};- visit(root);- return out;+ const resolveComplex = (r, sel) => {+ const found = findPseudo(sel);+ if (!found) return queryCss(r, sel);+ const top = scanCss(sel);+ let cStart = 0;+ for (let i = found.start - 1; i >= 0; i--) {+ if (top[i] && ' >+~'.includes(sel[i])) { cStart = i + 1; break; }+ }+ let cEnd = sel.length;+ for (let i = found.end; i < sel.length; i++) {+ if (top[i] && ' >+~'.includes(sel[i])) { cEnd = i; break; }+ }+ let compound = sel.slice(cStart, cEnd);+ const predicates = [];+ for (;;) {+ const p = findPseudo(compound);+ if (!p) break;+ predicates.push(p.kind === 'visible' ? isVisible : hasTextPredicate(p.raw));+ compound = compound.slice(0, p.start) + compound.slice(p.end);+ }+ const base = sel.slice(0, cStart) + (compound.trim() === '' ? '*' : compound);+ let candidates = queryCss(r, base);+ for (const pred of predicates) candidates = candidates.filter(pred);+ const rest = sel.slice(cEnd);+ if (rest.trim() === '') return candidates;+ if (rest.trimStart()[0] === '+' || rest.trimStart()[0] === '~') {+ // Loud, not silently empty: :scope queries only reach descendants, so a sibling+ // combinator after a custom pseudo would always resolve to nothing.+ throw new Error('sibling combinator after :visible/:has-text() is not supported: ' + sel);+ }+ const seen = new Set();+ const out = [];+ for (const c of candidates) {+ for (const el of resolveSelector(c, ':scope' + rest)) {+ if (!seen.has(el)) { seen.add(el); out.push(el); }+ }+ }+ return out;+ };+ const resolveSelector = (r, sel) => {+ const top = scanCss(sel);+ const parts = [];+ let start = 0;+ for (let i = 0; i < sel.length; i++) {+ if (top[i] && sel[i] === ',') { parts.push(sel.slice(start, i)); start = i + 1; }+ }+ parts.push(sel.slice(start));+ const trimmed = parts.map((p) => p.trim()).filter(Boolean);+ if (trimmed.length <= 1) return resolveComplex(r, trimmed[0] || sel);+ // A pseudo-free comma list is native CSS: one query keeps document order exactly.+ if (trimmed.every((part) => !findPseudo(part))) return queryCss(r, sel);+ const seen = new Set();+ const out = [];+ for (const part of trimmed) {+ for (const el of resolveComplex(r, part)) {+ if (!seen.has(el)) { seen.add(el); out.push(el); }+ }+ }+ // Merged branches must come back in document order -- .first/.nth() and capped consumers+ // depend on it. Stable sort leaves cross-shadow (disconnected) pairs in insertion order.+ out.sort((a, b) => {+ const pos = a.compareDocumentPosition(b);+ if (pos & Node.DOCUMENT_POSITION_FOLLOWING) return -1;+ if (pos & Node.DOCUMENT_POSITION_PRECEDING) return 1;+ return 0;+ });+ return out;+ };+ return resolveSelector(root, body);};"""++def _split_selector_chain(selector: str) -> list[str]:+ """Split a Playwright `a >> b` chain into steps at the top level, leaving quoted `>>` alone."""+ parts: list[str] = []+ current: list[str] = []+ quote: str | None = None+ i = 0+ while i < len(selector):+ ch = selector[i]+ if quote is not None:+ if ch == quote:+ quote = None+ current.append(ch)+ elif ch in "\"'":+ quote = ch+ current.append(ch)+ elif selector.startswith(">>", i):+ parts.append("".join(current).strip())+ current = []+ i += 2+ continue+ else:+ current.append(ch)+ i += 1+ parts.append("".join(current).strip())+ return [part for part in parts if part] or [selector.strip()]++# Walks a locator chain inside the page: each step queries within the previous step's matches, and a# step carrying an index narrows to exactly that match (negative counts from the end) before the next# step runs. Returning either the count or one element from the same walk keeps the two consistent.@@ -143,7 +348,8 @@ class Locator:# caller builds the chain synchronously.self._frame_source = frameif isinstance(selector, str):- self._steps: list[tuple[str, int | None]] = [(selector, index)]+ chain = _split_selector_chain(selector)+ self._steps: list[tuple[str, int | None]] = [(part, None) for part in chain[:-1]] + [(chain[-1], index)]else:self._steps = list(selector)if index is not None:@@ -165,7 +371,8 @@ class Locator:# -- narrowing ----------------------------------------------------------def locator(self, selector: str) -> Locator:- return Locator(self._frame_source, [*self._steps, (selector, None)])+ chained = [(part, None) for part in _split_selector_chain(selector)]+ return Locator(self._frame_source, [*self._steps, *chained])def nth(self, index: int) -> Locator:return Locator(self._frame_source, self._steps, index=index)diff --git a/tests/unit/test_action_to_stmt_totp.py b/tests/unit/test_action_to_stmt_totp.pyindex 483a19844..02073f44a 100644--- a/tests/unit/test_action_to_stmt_totp.py+++ b/tests/unit/test_action_to_stmt_totp.py@@ -8,16 +8,21 @@ instead of reusing the stale literal value from the original run."""from typing import Any+from unittest.mock import MagicMock, patchimport libcst as cst-from skyvern.core.script_generations.generate_script import _action_to_stmt+from skyvern.core.script_generations.generate_script import _action_to_stmt, _build_block_fndef _render(stmt: cst.BaseStatement) -> str:return cst.Module(body=[stmt]).code+def _render_fn(fn: cst.FunctionDef) -> str:+ return cst.Module(body=[fn]).code++class TestActionToStmtTotpVerificationUrl:"""Ensure totp_verification_url is emitted into fill() calls."""@@ -82,3 +87,110 @@ class TestActionToStmtTotpVerificationUrl:"totp_url kwarg missing — bug regression: condition was checking 'totp_url' "f"instead of 'totp_verification_url':\n{code}")+++class TestActionToStmtGotoUrl:+ """A recorded GOTO_URL action must emit a call, not crash script generation (SKY-14056)."""++ def test_goto_url_action_does_not_raise_key_error(self) -> None:+ recorded_url = "https://example.com/dashboard"+ act: dict[str, Any] = {"action_type": "goto_url", "url": recorded_url}++ stmt = _action_to_stmt(act, {})+ code = _render(stmt)++ assert "page.goto(" in code, code+ # nosemgrep: incomplete-url-substring-sanitization # searching generated source, not a URL+ assert recorded_url in code, code++ def test_magic_link_goto_emits_magic_link_and_drops_the_single_use_url(self) -> None:+ act: dict[str, Any] = {+ "action_type": "goto_url",+ "url": "https://mail.example.com/signin?token=one-time-secret-value",+ "is_magic_link": True,+ }+ task: dict[str, Any] = {"totp_identifier": "[email protected]"}++ stmt = _action_to_stmt(act, task)+ code = _render(stmt)++ assert "page.magic_link(" in code, code+ assert "totp_identifier" in code, code+ assert "[email protected]" in code, code+ # The recorded link is single-use: replaying it would navigate to a spent token.+ assert "one-time-secret-value" not in code, code+ assert "url=" not in code, code++ def test_magic_link_falls_back_to_the_runtime_credential_identifier(self) -> None:+ act: dict[str, Any] = {+ "action_type": "goto_url",+ "url": "https://mail.example.com/signin?token=abc",+ "is_magic_link": True,+ }++ stmt = _action_to_stmt(+ act,+ {},+ goal_template="log in with {{ portal_credential }}",+ credential_param_keys=frozenset({"portal_credential"}),+ )+ code = _render(stmt)++ assert "page.magic_link(" in code, code+ assert "context.credential_totp_identifier(" in code, code+ assert "portal_credential" in code, code+++class TestMagicLinkBlockGeneration:+ """Whole-block generation, not one statement: a recorded magic-link login must yield a+ cached script, since on main the same actions raised KeyError and the script was dropped."""++ @staticmethod+ def _build(actions: list[dict[str, Any]]) -> str:+ block = {+ "label": "sign_in",+ "block_type": "task",+ "navigation_goal": "Sign in to the member portal.",+ "parameters": [{"key": "portal_credential"}],+ }+ with patch("skyvern.core.script_generations.generate_script.app") as mock_app:+ mock_app.AGENT_FUNCTION.build_ats_pipeline_block_fn = MagicMock(return_value=None)+ fn = _build_block_fn(+ block=block,+ actions=actions,+ use_semantic_selectors=True,+ credential_param_keys=frozenset({"portal_credential"}),+ )+ return _render_fn(fn)++ def test_recorded_magic_link_login_generates_a_block_that_re_requests_the_link(self) -> None:+ code = self._build(+ [+ {+ "action_type": "input_text",+ "xpath": "//input[@id='email']",+ "text": "demo_business_user",+ "css_selector": "#email",+ "attributes": {"id": "email"},+ },+ {+ "action_type": "goto_url",+ "url": "https://portal.example.com/verify?token=single-use-token-value",+ "is_magic_link": True,+ },+ ]+ )++ assert "page.magic_link(" in code, code+ assert "context.credential_totp_identifier(" in code, code+ # The captured link is spent by replay time; emitting it would navigate to a dead token.+ assert "single-use-token-value" not in code, code++ def test_a_plain_recorded_navigation_still_generates_a_goto(self) -> None:+ recorded_url = "https://portal.example.com/dashboard"+ code = self._build([{"action_type": "goto_url", "url": recorded_url}])++ assert "page.goto(" in code, code+ # nosemgrep: incomplete-url-substring-sanitization # searching generated source, not a URL+ assert recorded_url in code, code+ assert "page.magic_link(" not in code, codediff --git a/tests/unit/test_code_block_sandbox.py b/tests/unit/test_code_block_sandbox.pyindex 79c691d6f..87ca8e814 100644--- a/tests/unit/test_code_block_sandbox.py+++ b/tests/unit/test_code_block_sandbox.py@@ -1158,7 +1158,31 @@ class _FakeWorkflowRun:def __init__(self) -> None:self.workflow_id = "w"self.workflow_permanent_id = "wpid"- self.started_at = datetime(2026, 6, 14, 0, 0, 0, tzinfo=timezone.utc)+ # Naive, like workflow_runs.started_at (Column(DateTime)). An aware value here would let a+ # local-clock anchor pass the call-time assertion below in every timezone.+ self.started_at = datetime(2026, 6, 14, 0, 0, 0)+++def _fake_code_block_page(navigations: list | None = None, fills: list | None = None):+ """A page the production binder will accept: it type-checks as a real Playwright Page."""+ from unittest.mock import MagicMock++ from playwright.async_api import Page++ page = MagicMock(spec=Page)++ async def goto(url: str, **kwargs: object) -> None:+ if navigations is not None:+ navigations.append(url)+ return None++ async def fill(*args: object, **kwargs: object) -> None:+ if fills is not None:+ fills.append(args)++ page.goto = goto+ page.fill = fill+ return pagedef _patch_context_resolution(monkeypatch: "pytest.MonkeyPatch", wrc) -> None:@@ -1906,6 +1930,275 @@ class TestIsSafeCodeClassStatements:is_safe_script_code("class Weird(metaclass=type):\n pass")+class TestCodeBlockMagicLink:+ """SKY-14056: a credential can request an emailed sign-in link and open it."""++ @staticmethod+ def _patch_poll(monkeypatch: pytest.MonkeyPatch, wrc, polled, *, raises: BaseException | None = None):+ from skyvern.forge.sdk.workflow.models import block as block_module++ _patch_context_resolution(monkeypatch, wrc)++ async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:+ return _FakeWorkflowRun()++ captured: dict[str, object] = {}++ async def fake_poll(**kwargs: object):+ captured.update(kwargs)+ if raises is not None:+ raise raises+ return polled++ monkeypatch.setattr(+ block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False+ )+ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)+ return captured++ @pytest.mark.asyncio+ async def test_polls_for_a_link_and_navigates_once_without_filling(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_link+ from skyvern.services.otp_service import OTPValue++ link = "https://portal.example.com/signin?token=" + "z" * 900+ wrc = _build_wrc_with_identifier()+ captured = self._patch_poll(monkeypatch, wrc, OTPValue(value=link, type=OTPType.MAGIC_LINK))++ navigations: list[str] = []+ fills: list[object] = []++ page = _fake_code_block_page(navigations=navigations, fills=fills)++ magic_link = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)+ await magic_link(page)++ assert captured["expected_otp_type"] is OTPType.MAGIC_LINK+ assert navigations == [link]+ assert fills == []++ @pytest.mark.asyncio+ async def test_anchors_created_after_at_call_time_not_run_start(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_link+ from skyvern.services.otp_service import OTPValue++ wrc = _build_wrc_with_identifier()+ captured = self._patch_poll(+ monkeypatch, wrc, OTPValue(value="https://example.com/go?t=1", type=OTPType.MAGIC_LINK)+ )++ page = _fake_code_block_page()++ await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)++ from skyvern.services.otp_service import MAGIC_LINK_ANCHOR_GRACE++ # A link minted earlier in the run may already be spent, so the run-start anchor+ # the code path uses would re-serve it.+ anchor = captured["created_after"]+ assert anchor > _FakeWorkflowRun().started_at+ # Naive UTC, which is what the DB column and the inbox cutoff are both compared against.+ # A local-clock anchor drifts by the host's offset: east of UTC it filters out every+ # delivery, west of UTC it re-opens the window this anchor exists to close.+ assert anchor.tzinfo is None+ expected_anchor = datetime.now(timezone.utc).replace(tzinfo=None) - MAGIC_LINK_ANCHOR_GRACE+ assert abs((anchor - expected_anchor).total_seconds()) < 30++ @pytest.mark.asyncio+ async def test_a_seed_only_credential_fails_closed_naming_the_missing_identifier(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """An authenticator seed can only mint a 6-digit code; navigating to one is nonsense."""+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link++ wrc = _build_wrc_with_totp_seed()+ _patch_context_resolution(monkeypatch, wrc)++ navigations: list[str] = []++ page = _fake_code_block_page(navigations=navigations)++ magic_link = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)+ with pytest.raises(CodeBlockOTPError) as excinfo:+ await magic_link(page)++ message = str(excinfo.value)+ assert "email" in message.lower()+ assert "authenticator" in message.lower()+ assert navigations == []++ @pytest.mark.asyncio+ async def test_wrong_verb_reports_the_type_that_actually_arrived(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """The repair signal: a link-verb call against a code mailbox must not read as an empty inbox."""+ import asyncio++ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.forge.sdk.workflow.models import block as block_module+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link++ wrc = _build_wrc_with_identifier()+ _patch_context_resolution(monkeypatch, wrc)++ async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:+ return _FakeWorkflowRun()++ async def fake_poll(**kwargs: object):+ # Stand in for the real poll: a code was seen and rejected for the requested type.+ kwargs["raw_context"].observed_otp_types.add(OTPType.TOTP)+ raise asyncio.TimeoutError++ monkeypatch.setattr(+ block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False+ )+ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)++ page = _fake_code_block_page()++ with pytest.raises(CodeBlockOTPError) as excinfo:+ await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)++ message = str(excinfo.value)+ assert "magic_link" in message+ assert "totp" in message+ assert "otp()" in message++ @pytest.mark.asyncio+ async def test_code_verb_against_a_link_mailbox_reports_the_mismatch_too(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ import asyncio++ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.forge.sdk.workflow.models import block as block_module+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _resolve_code_block_otp++ wrc = _build_wrc_with_identifier()+ _patch_context_resolution(monkeypatch, wrc)++ async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:+ return _FakeWorkflowRun()++ async def fake_poll(**kwargs: object):+ kwargs["email_context"].observed_otp_types.add(OTPType.MAGIC_LINK)+ raise asyncio.TimeoutError++ monkeypatch.setattr(+ block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False+ )+ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)++ with pytest.raises(CodeBlockOTPError) as excinfo:+ await _resolve_code_block_otp(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, budget_seconds=120)++ message = str(excinfo.value)+ assert "magic_link" in message+ assert "totp" in message++ @pytest.mark.asyncio+ async def test_a_silent_mailbox_still_reports_a_plain_timeout(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """No mismatch was observed, so the message must not invent one."""+ import asyncio++ from skyvern.forge.sdk.workflow.models import block as block_module+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link++ wrc = _build_wrc_with_identifier()+ _patch_context_resolution(monkeypatch, wrc)++ async def fake_get_workflow_run(*args: object, **kwargs: object) -> _FakeWorkflowRun:+ return _FakeWorkflowRun()++ async def fake_poll(**kwargs: object):+ raise asyncio.TimeoutError++ monkeypatch.setattr(+ block_module.app.DATABASE.workflow_runs, "get_workflow_run", fake_get_workflow_run, raising=False+ )+ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)++ page = _fake_code_block_page()++ with pytest.raises(CodeBlockOTPError, match="was not received within"):+ await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(page)++ @pytest.mark.asyncio+ async def test_a_page_of_the_blocks_own_making_is_refused_before_polling(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """Authored code passes the page in, so an impostor would otherwise receive the link."""+ from skyvern.forge.sdk.workflow.models import block as block_module+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link++ wrc = _build_wrc_with_identifier()+ _patch_context_resolution(monkeypatch, wrc)++ polls: list[object] = []++ async def fake_poll(**kwargs: object):+ polls.append(kwargs)+ return None++ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)++ captured: list[str] = []++ class _Impostor:+ async def goto(self, url: str, **kwargs: object) -> None:+ captured.append(url)++ with pytest.raises(CodeBlockOTPError, match="requires the code block's page"):+ await _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID)(_Impostor())++ # Refused before the link is even resolved, so nothing was there to capture.+ assert captured == []+ assert polls == []++ @pytest.mark.asyncio+ async def test_a_forgery_carrying_page_capabilities_is_still_refused(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """Authored code can define a class, so a capability check is satisfiable; identity is not."""+ from skyvern.forge.sdk.workflow.models import block as block_module+ from skyvern.forge.sdk.workflow.models.block import CodeBlockOTPError, _bind_code_block_magic_link++ wrc = _build_wrc_with_identifier()+ _patch_context_resolution(monkeypatch, wrc)++ polls: list[object] = []++ async def fake_poll(**kwargs: object):+ polls.append(kwargs)+ return None++ monkeypatch.setattr(block_module.otp_service, "poll_otp_value", fake_poll)++ captured: list[str] = []++ class _Forgery:+ """Carries every capability is_page_like requires, plus a goto that keeps the link."""++ main_frame = None+ context = None++ def bring_to_front(self) -> None:+ return None++ def evaluate(self, *args: object, **kwargs: object) -> None:+ return None++ async def goto(self, url: str, **kwargs: object) -> None:+ captured.append(url)++ real_page = _fake_code_block_page()+ bound = _bind_code_block_magic_link(_CREDENTIAL_KEY, _ORG_ID, _WORKFLOW_RUN_ID, expected_page=real_page)++ with pytest.raises(CodeBlockOTPError, match="requires the code block's page"):+ await bound(_Forgery())++ assert captured == []+ assert polls == []++class TestCodeBlockTemplateSecretScoping:"""Template data carries secrets only for credential parameters the block declares (SKY-14047),mirroring the execution namespace, so a block cannot render another block's credential."""diff --git a/tests/unit/test_script_skyvern_page.py b/tests/unit/test_script_skyvern_page.pyindex 91adf9ef3..15746f2d8 100644--- a/tests/unit/test_script_skyvern_page.py+++ b/tests/unit/test_script_skyvern_page.py@@ -2954,6 +2954,125 @@ async def test_get_or_create_browser_state_does_not_record_session_on_acquire_faassert ctx.browser_session_id is None # requested session not recorded because the attach failed+class TestScriptSkyvernPageMagicLink:+ """SKY-14056: the generated-script sign-in-link verb."""++ @staticmethod+ def _install(monkeypatch, polled):+ """Wire a ScriptSkyvernPage whose page records navigations and fills."""+ from skyvern.core.script_generations import script_skyvern_page as module++ state: dict = {"navigations": [], "fills": [], "rendered": [], "polled_with": {}}++ async def fake_goto(url, **kwargs):+ state["navigations"].append(url)+ return None++ async def fake_fill(*args, **kwargs):+ state["fills"].append(args)++ async def fake_poll(**kwargs):+ state["polled_with"].update(kwargs)+ return polled++ def fake_render(value):+ state["rendered"].append(value)+ return value++ monkeypatch.setattr(module, "poll_otp_value", fake_poll)+ monkeypatch.setattr(module, "render_template", fake_render)+ monkeypatch.setattr(module.settings, "ALLOWED_HOSTS", ["portal.example.com"])++ page = SimpleNamespace(goto=fake_goto, fill=fake_fill)+ script_page = ScriptSkyvernPage.__new__(ScriptSkyvernPage)+ script_page.page = page+ return script_page, state++ @staticmethod+ def _with_context():+ from skyvern.forge.sdk.core import skyvern_context++ skyvern_context.set(skyvern_context.SkyvernContext(organization_id="org_1", workflow_run_id="wr_1"))+ return skyvern_context++ @pytest.mark.asyncio+ async def test_navigates_to_the_polled_link_without_filling_it(self, monkeypatch) -> None:+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.services.otp_service import OTPValue++ link = "https://portal.example.com/verify?token=" + "s" * 400+ script_page, state = self._install(monkeypatch, OTPValue(value=link, type=OTPType.MAGIC_LINK))++ ctx = self._with_context()+ try:+ await script_page.magic_link(totp_identifier="[email protected]")+ finally:+ ctx.reset()++ assert state["polled_with"]["expected_otp_type"] is OTPType.MAGIC_LINK+ assert state["navigations"] == [link]+ assert state["fills"] == []+ # The anchor sits one grace interval behind now (naive UTC), so an email that landed+ # while the triggering click was still settling stays visible to the poll.+ from datetime import datetime, timezone++ from skyvern.services.otp_service import MAGIC_LINK_ANCHOR_GRACE++ anchor = state["polled_with"]["created_after"]+ assert anchor.tzinfo is None+ expected_anchor = datetime.now(timezone.utc).replace(tzinfo=None) - MAGIC_LINK_ANCHOR_GRACE+ assert abs((anchor - expected_anchor).total_seconds()) < 30++ @pytest.mark.asyncio+ async def test_a_returned_one_time_code_is_reported_not_navigated_to(self, monkeypatch) -> None:+ """A configured webhook answers without honouring expected_otp_type."""+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.services.otp_service import OTPValue++ script_page, state = self._install(monkeypatch, OTPValue(value="123456", type=OTPType.TOTP))++ ctx = self._with_context()+ try:+ with pytest.raises(RuntimeError, match="totp"):+ await script_page.magic_link(totp_identifier="[email protected]")+ finally:+ ctx.reset()++ assert state["navigations"] == []++ @pytest.mark.asyncio+ async def test_the_link_never_goes_through_render_template(self, monkeypatch) -> None:+ """The polled URL is opaque; rendering it would corrupt or expand its token."""+ from skyvern.forge.sdk.schemas.totp_codes import OTPType+ from skyvern.services.otp_service import OTPValue++ link = "https://portal.example.com/verify?token={{ not_a_template }}"+ script_page, state = self._install(monkeypatch, OTPValue(value=link, type=OTPType.MAGIC_LINK))++ ctx = self._with_context()+ try:+ await script_page.magic_link(totp_identifier="[email protected]")+ finally:+ ctx.reset()++ assert state["navigations"] == [link]+ assert link not in state["rendered"]+ assert "[email protected]" in state["rendered"]++ @pytest.mark.asyncio+ async def test_no_identifier_fails_closed_before_polling(self, monkeypatch) -> None:+ script_page, state = self._install(monkeypatch, None)++ ctx = self._with_context()+ try:+ with pytest.raises(RuntimeError, match="identifier"):+ await script_page.magic_link()+ finally:+ ctx.reset()++ assert state["polled_with"] == {}++class TestAiClickRunsRegisteredClickSetup:"""SKY-13916: the cached-script AI click path must run the registeredActionHandler setup for the click action before dispatching the physicaldiff --git a/tests/unit_tests/test_url_validators_extended.py b/tests/unit_tests/test_url_validators_extended.pyindex 77e867ef6..d41a128fd 100644--- a/tests/unit_tests/test_url_validators_extended.py+++ b/tests/unit_tests/test_url_validators_extended.py@@ -1,5 +1,7 @@"""Extended tests for URL validators module."""+import re+import pytestfrom skyvern.exceptions import InvalidUrl@@ -60,7 +62,9 @@ class TestEncodeUrl:result = encode_url(url)# Unicode should be percent-encoded, original characters should not appearassert "路径" not in result- assert "example.com/" in result+ # re.search rather than ``in``: CodeQL's py/incomplete-url-substring-sanitization reads a+ # hostname-literal ``in`` check as broken sanitization (OSS alert #89); this is an assertion.+ assert re.search(r"example\.com/", result)# "路径" in UTF-8 is encoded as %E8%B7%AF%E5%BE%84assert "%E8%B7%AF%E5%BE%84" in resultdiff --git a/tests/unit_tests/webeye/test_navigation_retry.py b/tests/unit_tests/webeye/test_navigation_retry.pyindex 5fe982a0d..415c9c189 100644--- a/tests/unit_tests/webeye/test_navigation_retry.py+++ b/tests/unit_tests/webeye/test_navigation_retry.py@@ -1,14 +1,17 @@from __future__ import annotations+import reimport socketfrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMockimport pytest+import structlog.testingfrom skyvern.exceptions import BlockedHost, BlockedNavigationDestination, FailedToNavigateToUrl, UnresolvableHostfrom skyvern.webeye.navigation import (navigate_with_retry,+ redact_url_secrets,revalidate_redirect_chain,validate_navigation_destination,)@@ -483,3 +486,139 @@ async def test_revalidate_redirect_chain_tolerates_a_response_without_a_request(await revalidate_redirect_chain(SimpleNamespace(request=None), calls.append)assert calls == []+++def test_redact_url_secrets_keeps_only_scheme_and_host() -> None:+ assert redact_url_secrets("https://portal.example.com/verify/abc?token=xyz#frag") == (+ "https://portal.example.com/<redacted>"+ )+ assert redact_url_secrets("https://portal.example.com:8443/verify?token=xyz") == (+ "https://portal.example.com:8443/<redacted>"+ )+ assert redact_url_secrets("not a url") == "<redacted>"+++def test_redact_url_secrets_drops_basic_auth_credentials() -> None:+ """netloc carries user:password@; a redactor must not republish it."""+ redacted = redact_url_secrets("https://tok:[email protected]/verify?token=xyz")++ assert "s3cret" not in redacted+ assert "tok" not in redacted+ assert redacted == "https://portal.example.com/<redacted>"+++async def test_a_refused_secret_destination_does_not_report_the_real_url() -> None:+ """A self-hosted portal's link can legitimately resolve to a private host."""+ secret = "http://127.0.0.1/verify?token=super-secret-token-value"++ async def navigate(strategy: str) -> object:+ raise AssertionError("must not navigate to a blocked destination")++ async def settle() -> None:+ return None++ with structlog.testing.capture_logs() as logs:+ with pytest.raises(BlockedNavigationDestination) as excinfo:+ await navigate_with_retry(+ navigate=navigate,+ url=secret,+ retry_times=2,+ settle=settle,+ log_url=redact_url_secrets(secret),+ )++ assert "super-secret-token-value" not in str(excinfo.value)+ assert "super-secret-token-value" not in repr(logs)+ # The chained cause would carry the original message into any rendered traceback.+ assert excinfo.value.__cause__ is None+++async def test_log_url_keeps_the_real_url_out_of_logs_and_the_failure_reason() -> None:+ """A sign-in link is a bearer credential: navigate to it, but never log or report it."""+ secret = "https://portal.example.com/verify?token=super-secret-token-value"++ async def navigate(strategy: str) -> object:+ # Playwright names the destination in its own message, so the secret arrives via the+ # error text as well as the url field.+ raise RuntimeError(f"Page.goto: net::ERR_CONNECTION_REFUSED at {secret}")++ async def settle() -> None:+ return None++ async def no_sleep(_seconds: float) -> None:+ return None++ with structlog.testing.capture_logs() as logs:+ with pytest.raises(FailedToNavigateToUrl) as excinfo:+ await navigate_with_retry(+ navigate=navigate,+ url=secret,+ retry_times=2,+ settle=settle,+ sleep=no_sleep,+ log_url=redact_url_secrets(secret),+ )++ assert "super-secret-token-value" not in str(excinfo.value)+ assert "super-secret-token-value" not in repr(logs)+ # The host still reaches the operator, so a failure stays diagnosable. re.search rather+ # than ``in``: CodeQL's py/incomplete-url-substring-sanitization reads a hostname-literal+ # ``in`` check as broken sanitization; this is an assertion, not a sanitizer.+ assert any(re.search(r"portal\.example\.com", repr(entry)) for entry in logs)+++async def test_a_refused_redirect_hop_is_redacted_for_a_secret_caller() -> None:+ """The hop that gets refused is a different URL than the one requested; redact that one."""+ secret = "https://portal.example.com/verify?token=super-secret-token-value"+ # A literal loopback address is refused before DNS, unlike the hostnames the suite's+ # autouse fixture resolves to a public address.+ hop = "http://127.0.0.1/r?dest=internal&session=hop-secret-value"++ async def navigate(strategy: str) -> object:+ return SimpleNamespace(request=SimpleNamespace(url=hop, redirected_from=None))++ async def settle() -> None:+ return None++ with structlog.testing.capture_logs() as logs:+ with pytest.raises(BlockedNavigationDestination) as excinfo:+ await navigate_with_retry(+ navigate=navigate,+ url=secret,+ retry_times=2,+ settle=settle,+ log_url=redact_url_secrets(secret),+ )++ assert "hop-secret-value" not in str(excinfo.value)+ assert "hop-secret-value" not in repr(logs)+ assert "super-secret-token-value" not in str(excinfo.value)+ # The refused hop's host still identifies what was blocked.+ assert "127.0.0.1" in str(excinfo.value)+ assert excinfo.value.__cause__ is None+++async def test_navigation_still_targets_the_real_url_when_a_display_url_is_given() -> None:+ secret = "https://portal.example.com/verify?token=super-secret-token-value"+ navigated: list[str] = []++ async def navigate(strategy: str) -> object:+ navigated.append(secret)+ return None++ async def settle() -> None:+ return None++ await navigate_with_retry(+ navigate=navigate,+ url=secret,+ retry_times=1,+ settle=settle,+ log_url=redact_url_secrets(secret),+ )++ assert navigated == [secret]