Skip to content

Automate browser based workflows with AI

Restrict credential-TOTP to the originating block's linked credentials — explicit threading, no schema change (SKY-15181) (#8346)

Shuchang Zhengf6c431fb194b
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index bcba2056a..412d5914f 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -13,7 +13,7 @@ from asyncio.exceptions import CancelledError
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
-from typing import Any, Awaitable, Callable, Iterable, Literal, Tuple, cast
+from typing import Any, Awaitable, Callable, Iterable, Literal, Sequence, Tuple, cast
import structlog
from openai.types.responses.response import Response as OpenAIResponse
@@ -497,6 +497,30 @@ def _has_multi_field_totp_shape(observations: Iterable[tuple[str, Any]]) -> bool
return False
+def block_credential_parameter_keys(task_block: BaseTaskBlock | None, workflow_run_id: str | None) -> list[str] | None:
+ """The originating block's linked credential parameter keys — the only credentials a task
+ created from that block may draw an OTP from (SKY-15181). None (no block, no run context, or a
+ script-mode run) means unrestricted: bare tasks and cached scripts keep the run-scoped legacy
+ behavior."""
+ if task_block is None or not workflow_run_id:
+ return None
+ context = skyvern_context.current()
+ if context is not None and context.script_mode:
+ # Script-built blocks are constructed without parameters=, so deriving from them would
+ # yield an empty scope that silently disables credential-TOTP for cached-script logins.
+ return None
+ if not app.WORKFLOW_CONTEXT_MANAGER.has_workflow_run_context(workflow_run_id):
+ # A block-originated task always executes inside a registered run context; if it is ever
+ # missing, fail closed (no credential-TOTP) rather than open to the whole run's credentials.
+ return []
+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(workflow_run_id)
+ return [
+ parameter.key
+ for parameter in task_block.parameters
+ if workflow_run_context.is_registered_credential_parameter_key(parameter.key)
+ ]
+
+
def _first_plan_carries_consumable_totp(actions: list[Any], multi_field_secret_ready: bool) -> bool:
"""Whether the first-pass plan already carries a shape the existing runtime materializes into a
credential TOTP at DOM-write time: a multi-field single-digit sequence backed by a runtime secret
@@ -518,9 +542,18 @@ def _first_plan_carries_consumable_totp(actions: list[Any], multi_field_secret_r
def _first_plan_carries_single_field_credential_totp(
- actions: list[Any], workflow_run_context: WorkflowRunContext | None, active_credential_parameter_key: str | None
+ actions: list[Any],
+ workflow_run_context: WorkflowRunContext | None,
+ active_credential_parameter_key: str | None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> bool:
- """Recognize the single-field credential placeholder consumed by the input handler at runtime."""
+ """Recognize the single-field credential placeholder consumed by the input handler at runtime.
+
+ ``allowed_credential_parameter_keys`` closes the SKY-15181 seam here too: the placeholder is
+ resolved against the whole run context, and the active-key gate alone is exactly the stale
+ cross-block key this restriction exists to ignore — so a plan whose credential is not linked
+ to the originating block must fall through to the deterministic resolver, which is scoped.
+ """
if not workflow_run_context or not actions:
return False
if any(not isinstance(action, dict) for action in actions):
@@ -537,6 +570,8 @@ def _first_plan_carries_single_field_credential_totp(
key = workflow_run_context.find_credential_parameter_key_for_secret(text)
if not key or workflow_run_context.values.get(key, {}).get("totp") != text:
return False
+ if allowed_credential_parameter_keys is not None and key not in allowed_credential_parameter_keys:
+ return False
if active_credential_parameter_key is not None and active_credential_parameter_key != key:
return False
resolved = workflow_run_context.get_original_secret_value_or_none(text)
@@ -1885,7 +1920,10 @@ class ForgeAgent:
# structurally never touches the live DOM must not be offered it.
verification_state = VerificationState()
auth_tools, auth_guidance = build_auth_tools(
- task, None if page_free_validation else _page_provider, state=verification_state
+ task,
+ None if page_free_validation else _page_provider,
+ state=verification_state,
+ allowed_credential_parameter_keys=block_credential_parameter_keys(task_block, task.workflow_run_id),
)
# Offered on any page-aware run (a captcha can appear mid-run, so there is no build-time
# source to gate on); solving routes through the AGENT_FUNCTION seam (OSS no-op, cloud solves).
@@ -3077,6 +3115,7 @@ class ForgeAgent:
reuse_speculative_llm_response=reuse_speculative_llm_response,
speculative_llm_metadata=speculative_llm_metadata,
context=context,
+ allowed_credential_parameter_keys=block_credential_parameter_keys(task_block, task.workflow_run_id),
)
detailed_agent_step_output.actions = actions
@@ -3634,6 +3673,7 @@ class ForgeAgent:
reuse_speculative_llm_response: bool,
speculative_llm_metadata: SpeculativeLLMMetadata | None,
context: SkyvernContext | None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> tuple[list[Action], str | None, bool]:
pdf_auto_download_src: str | None = None
pdf_auto_download_used_bytes = False
@@ -3660,6 +3700,7 @@ class ForgeAgent:
scraped_page=scraped_page,
previous_response=cua_response,
engine=engine,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
)
detailed_agent_step_output.cua_response = new_cua_response
elif engine == RunEngine.anthropic_cua:
@@ -3670,6 +3711,7 @@ class ForgeAgent:
step=step,
scraped_page=scraped_page,
llm_caller=llm_caller,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
)
elif engine == RunEngine.ui_tars and not await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(
"DISABLE_UI_TARS_CUA",
@@ -3810,7 +3852,12 @@ class ForgeAgent:
pdf_auto_download_used_bytes = pdf_bytes is not None
else:
otp_json_response, otp_actions = await self.handle_potential_OTP_actions(
- task, step, scraped_page, browser_state, json_response
+ task,
+ step,
+ scraped_page,
+ browser_state,
+ json_response,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
)
if otp_actions:
detailed_agent_step_output.llm_response = otp_json_response
@@ -3887,6 +3934,7 @@ class ForgeAgent:
scraped_page: ScrapedPage,
previous_response: OpenAIResponse | None = None,
engine: RunEngine = RunEngine.openai_cua,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> tuple[list[Action], OpenAIResponse | None]:
cua_model = app.OPENAI_CUA_MODEL
# The CUA tool must declare the browser's real viewport so returned coordinates land in the
@@ -4078,7 +4126,9 @@ class ForgeAgent:
incremental_cached_tokens=cached_tokens if cached_tokens > 0 else None,
)
- return await parse_cua_actions(task, step, current_response), current_response
+ return await parse_cua_actions(
+ task, step, current_response, allowed_credential_parameter_keys
+ ), current_response
async def _generate_anthropic_actions(
self,
@@ -4086,6 +4136,7 @@ class ForgeAgent:
step: Step,
scraped_page: ScrapedPage,
llm_caller: LLMCaller,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> list[Action]:
LOG.info(
"Anthropic CU call starts",
@@ -4180,6 +4231,7 @@ class ForgeAgent:
assistant_content,
window_dimension or llm_caller.browser_window_dimension,
llm_caller.get_screenshot_resize_target_dimension(window_dimension),
+ allowed_credential_parameter_keys,
)
return actions
@@ -8134,6 +8186,7 @@ class ForgeAgent:
scraped_page: ScrapedPage,
browser_state: BrowserState,
json_response: dict[str, Any],
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> tuple[dict[str, Any], list[Action]]:
if not task.organization_id:
return json_response, []
@@ -8181,6 +8234,10 @@ class ForgeAgent:
workflow_run_context = None
if workflow_run_id and app.WORKFLOW_CONTEXT_MANAGER.has_workflow_run_context(workflow_run_id):
workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(workflow_run_id)
+ # The {task_id}_secret stash is populated only from this task's navigation payload, which is
+ # built from the originating block's own parameters — so the multi-field skip below cannot
+ # carry an out-of-scope credential's secret (SKY-15181 provenance; single write site is
+ # _process_totp over final_navigation_payload).
multi_field_secret_ready = bool(runtime_context and runtime_context.totp_codes.get(f"{task.task_id}_secret"))
first_plan_actions = json_response.get("actions")
if (
@@ -8204,6 +8261,7 @@ class ForgeAgent:
first_plan_actions,
workflow_run_context,
getattr(runtime_context, "active_credential_parameter_key", None),
+ allowed_credential_parameter_keys,
)
and extract_totp_from_navigation_inputs(task.navigation_payload) is None
):
@@ -8212,7 +8270,12 @@ class ForgeAgent:
if should_resolve_verification_code:
json_response = await self.handle_potential_verification_code(
- task, step, scraped_page, browser_state, json_response
+ task,
+ step,
+ scraped_page,
+ browser_state,
+ json_response,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
)
actions = parse_actions(
task, step.step_id, step.order, scraped_page, _require_actions_payload(json_response)
@@ -8280,6 +8343,7 @@ class ForgeAgent:
scraped_page: ScrapedPage,
browser_state: BrowserState,
json_response: dict[str, Any],
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> dict[str, Any]:
# The caller decides when to resolve; place_to_enter_verification_code alone is sufficient.
place_to_enter_verification_code = json_response.get("place_to_enter_verification_code")
@@ -8287,7 +8351,11 @@ class ForgeAgent:
return json_response
LOG.info("Need verification code")
- otp_value = await resolve_otp_value(task, expected_otp_type=OTPType.TOTP)
+ otp_value = await resolve_otp_value(
+ task,
+ expected_otp_type=OTPType.TOTP,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
+ )
if not otp_value or otp_value.get_otp_type() != OTPType.TOTP:
return json_response
diff --git a/skyvern/forge/taskv3/auth_tools.py b/skyvern/forge/taskv3/auth_tools.py
index 230a58dd6..39d5efac1 100644
--- a/skyvern/forge/taskv3/auth_tools.py
+++ b/skyvern/forge/taskv3/auth_tools.py
@@ -17,7 +17,7 @@ import re
import time
from dataclasses import dataclass
from datetime import datetime
-from typing import Any
+from typing import Any, Sequence
from urllib.parse import unquote_plus, urlsplit
import structlog
@@ -269,7 +269,10 @@ class VerificationState:
def build_auth_tools(
- task: Task, page_provider: PageProvider | None = None, state: VerificationState | None = None
+ task: Task,
+ page_provider: PageProvider | None = None,
+ state: VerificationState | None = None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> tuple[list[ToolSpec], str]:
"""Return (tools, system-prompt guidance) for verification handling, or ([], "") when the task has
no verification source configured (so the tools aren't offered needlessly). The link tool also needs
@@ -278,8 +281,12 @@ def build_auth_tools(
verdict on it. A caller with no use for that gate can omit `state` entirely."""
if state is None:
state = VerificationState()
- offer_code_tool = has_otp_source(task, expected_otp_type=OTPType.TOTP)
- offer_link_tool = page_provider is not None and has_otp_source(task, expected_otp_type=OTPType.MAGIC_LINK)
+ offer_code_tool = has_otp_source(
+ task, expected_otp_type=OTPType.TOTP, allowed_credential_parameter_keys=allowed_credential_parameter_keys
+ )
+ offer_link_tool = page_provider is not None and has_otp_source(
+ task, expected_otp_type=OTPType.MAGIC_LINK, allowed_credential_parameter_keys=allowed_credential_parameter_keys
+ )
if not offer_code_tool and not offer_link_tool:
return [], ""
@@ -351,6 +358,7 @@ def build_auth_tools(
expected_otp_type=expected_otp_type,
max_wait_seconds=min(remaining, _PER_CALL_WAIT_SECONDS),
poll_started_at=first_poll_started_at,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
)
if otp_value is None:
if expected_otp_type == OTPType.MAGIC_LINK:
diff --git a/skyvern/services/otp_service.py b/skyvern/services/otp_service.py
index f63d10660..712fcf644 100644
--- a/skyvern/services/otp_service.py
+++ b/skyvern/services/otp_service.py
@@ -5,7 +5,7 @@ import re
from collections import Counter
from dataclasses import dataclass, field
from datetime import datetime, timedelta
-from typing import TYPE_CHECKING, Any
+from typing import TYPE_CHECKING, Any, Sequence
import structlog
from pydantic import BaseModel, Field, ValidationError
@@ -503,13 +503,17 @@ def try_generate_totp_for_credential(
return OTPValue(value=code, type=OTPType.TOTP)
-def has_credential_totp_candidate(workflow_run_id: str | None) -> bool:
+def has_credential_totp_candidate(
+ workflow_run_id: str | None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
+) -> bool:
"""Return True when try_generate_totp_from_credential would have a credential to consult.
Mirrors try_generate_totp_from_credential's selection: active-with-TOTP if an
active credential is recorded, else exactly one TOTP-bearing candidate.
Used to drive prompt gating and classifier branches without actually
- generating a code.
+ generating a code. ``allowed_credential_parameter_keys`` restricts the candidates
+ to the originating block's linked credentials (SKY-15181); None means unrestricted.
"""
if not workflow_run_id:
return False
@@ -522,6 +526,14 @@ def has_credential_totp_candidate(workflow_run_id: str | None) -> bool:
current_context = skyvern_context.current()
active_credential_key = current_context.active_credential_parameter_key if current_context else None
+ if (
+ active_credential_key
+ and allowed_credential_parameter_keys is not None
+ and active_credential_key not in allowed_credential_parameter_keys
+ ):
+ # A credential typed by an earlier block must not answer this block's OTP prompt
+ # (SKY-15181); the scan below may still find a credential the block itself links.
+ active_credential_key = None
if active_credential_key:
if not workflow_run_context.is_registered_credential_parameter_key(active_credential_key):
return False
@@ -534,14 +546,20 @@ def has_credential_totp_candidate(workflow_run_id: str | None) -> bool:
if isinstance(value, dict)
and isinstance(value.get("totp"), str)
and workflow_run_context.is_registered_credential_parameter_key(key)
+ and (allowed_credential_parameter_keys is None or key in allowed_credential_parameter_keys)
]
return len(candidate_keys) == 1
-def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue | None:
+def try_generate_totp_from_credential(
+ workflow_run_id: str | None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
+) -> OTPValue | None:
"""Generate a TOTP only for the credential the agent is currently typing into.
Falls back to single-credential heuristic when no active credential is recorded.
+ ``allowed_credential_parameter_keys`` restricts both paths to the originating block's
+ linked credentials (SKY-15181); None means unrestricted (bare tasks, cached scripts).
"""
if not workflow_run_id:
return None
@@ -555,6 +573,21 @@ def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue |
current_context = skyvern_context.current()
active_credential_key = current_context.active_credential_parameter_key if current_context else None
+ if (
+ active_credential_key
+ and allowed_credential_parameter_keys is not None
+ and active_credential_key not in allowed_credential_parameter_keys
+ ):
+ # A credential typed by an earlier block must not answer this block's OTP prompt
+ # (SKY-15181); the scan below may still find a credential the block itself links.
+ LOG.info(
+ "Ignoring credential not linked to this task's block for OTP resolution",
+ workflow_run_id=workflow_run_id,
+ active_credential_parameter_key=active_credential_key,
+ allowed_credential_parameter_keys=list(allowed_credential_parameter_keys),
+ )
+ active_credential_key = None
+
if active_credential_key:
return try_generate_totp_for_credential(workflow_run_context, active_credential_key, workflow_run_id)
@@ -565,6 +598,16 @@ def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue |
and isinstance(value.get("totp"), str)
and workflow_run_context.is_registered_credential_parameter_key(key)
]
+ if allowed_credential_parameter_keys is not None:
+ excluded = [key for key in candidate_keys if key not in allowed_credential_parameter_keys]
+ if excluded:
+ LOG.info(
+ "Skipping credential-TOTP not linked to this task's block",
+ workflow_run_id=workflow_run_id,
+ excluded_credential_keys=excluded,
+ allowed_credential_parameter_keys=list(allowed_credential_parameter_keys),
+ )
+ candidate_keys = [key for key in candidate_keys if key in allowed_credential_parameter_keys]
if len(candidate_keys) != 1:
if len(candidate_keys) > 1:
LOG.info(
@@ -576,7 +619,43 @@ def try_generate_totp_from_credential(workflow_run_id: str | None) -> OTPValue |
return try_generate_totp_for_credential(workflow_run_context, candidate_keys[0], workflow_run_id)
-def has_otp_source(task: "Task", expected_otp_type: OTPType | None = None) -> bool:
+# Facetable rollout sentinel for SKY-15181: fires only when block scoping flips a run from
+# worked-by-accident to no-OTP-source at all. Datadog queries key on the literal event string.
+SCOPE_SUPPRESSED_LEGACY_CREDENTIAL_EVENT = (
+ "Block-scoped credential-TOTP suppressed a cross-block credential with no fallback OTP source"
+)
+
+
+def _warn_if_scope_suppresses_legacy_credential(
+ task: "Task",
+ expected_otp_type: OTPType | None,
+ allowed_credential_parameter_keys: Sequence[str] | None,
+) -> None:
+ """Detect the SKY-15181 regression shape: the legacy run-scoped selection would have answered
+ this prompt with a cross-block credential, the block scope excludes it, and the task has no
+ polling fallback — so a run that used to pass by accident will now fail. Detection only; the
+ workflow's fix is linking the credential to the OTP-handling block."""
+ if allowed_credential_parameter_keys is None or expected_otp_type not in (None, OTPType.TOTP):
+ return
+ if task.totp_verification_url or task.totp_identifier:
+ return
+ if has_credential_totp_candidate(task.workflow_run_id, allowed_credential_parameter_keys):
+ return
+ if not has_credential_totp_candidate(task.workflow_run_id):
+ return
+ LOG.warning(
+ SCOPE_SUPPRESSED_LEGACY_CREDENTIAL_EVENT,
+ task_id=task.task_id,
+ workflow_run_id=task.workflow_run_id,
+ allowed_credential_parameter_keys=list(allowed_credential_parameter_keys),
+ )
+
+
+def has_otp_source(
+ task: "Task",
+ expected_otp_type: OTPType | None = None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
+) -> bool:
"""Return True when resolve_otp_value would consult at least one source for this task.
Mirrors resolve_otp_value's waterfall (payload of the expected type -> credential TOTP ->
@@ -590,9 +669,15 @@ def has_otp_source(task: "Task", expected_otp_type: OTPType | None = None) -> bo
return True
# A credential-TOTP candidate can only ever yield a TOTP code, so it's a source only when the
# caller isn't expecting some other OTP type (e.g. a magic link).
- if expected_otp_type in (None, OTPType.TOTP) and has_credential_totp_candidate(task.workflow_run_id):
+ if expected_otp_type in (None, OTPType.TOTP) and has_credential_totp_candidate(
+ task.workflow_run_id, allowed_credential_parameter_keys
+ ):
return True
- return bool((task.totp_verification_url or task.totp_identifier) and task.organization_id)
+ has_polling_source = bool((task.totp_verification_url or task.totp_identifier) and task.organization_id)
+ if not has_polling_source:
+ # This False withholds the v3 code tool entirely, which is otherwise invisible in logs.
+ _warn_if_scope_suppresses_legacy_credential(task, expected_otp_type, allowed_credential_parameter_keys)
+ return has_polling_source
async def resolve_otp_value(
@@ -600,6 +685,7 @@ async def resolve_otp_value(
expected_otp_type: OTPType | None = None,
max_wait_seconds: float | None = None,
poll_started_at: datetime | None = None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> OTPValue | None:
"""Resolve the OTP value to use for a verification step.
@@ -616,7 +702,7 @@ async def resolve_otp_value(
if otp_value and (expected_otp_type is None or otp_value.get_otp_type() == expected_otp_type):
return otp_value
- otp_value = try_generate_totp_from_credential(task.workflow_run_id)
+ otp_value = try_generate_totp_from_credential(task.workflow_run_id, allowed_credential_parameter_keys)
# A credential TOTP can only ever be a TOTP, so it's a match only when the caller isn't
# expecting some other OTP type (e.g. a magic link) -- mirrors has_otp_source's gate.
if otp_value and expected_otp_type in (None, OTPType.TOTP):
@@ -649,6 +735,7 @@ async def resolve_otp_value(
poll_started_at=poll_started_at,
)
+ _warn_if_scope_suppresses_legacy_credential(task, expected_otp_type, allowed_credential_parameter_keys)
return None
diff --git a/skyvern/webeye/actions/parse_actions.py b/skyvern/webeye/actions/parse_actions.py
index d605036cb..5fcfe7e26 100644
--- a/skyvern/webeye/actions/parse_actions.py
+++ b/skyvern/webeye/actions/parse_actions.py
@@ -1,6 +1,6 @@
import ast
import re
-from typing import Any, Dict, Match
+from typing import Any, Dict, Match, Sequence
from urllib.parse import urlparse
import structlog
@@ -453,6 +453,7 @@ async def parse_cua_actions(
task: Task,
step: Step,
response: OpenAIResponse,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> list[Action]:
computer_calls = [item for item in response.output if item.type == "computer_call"]
reasonings = [item for item in response.output if item.type == "reasoning"]
@@ -598,7 +599,9 @@ async def parse_cua_actions(
)
reasoning = reasonings[0].summary[0].text if reasonings and reasonings[0].summary else None
assistant_message = assistant_messages[0].content[0].text if assistant_messages else None
- actions = await generate_cua_fallback_actions(task, step, assistant_message, reasoning)
+ actions = await generate_cua_fallback_actions(
+ task, step, assistant_message, reasoning, allowed_credential_parameter_keys
+ )
return actions
@@ -608,6 +611,7 @@ async def parse_anthropic_actions(
assistant_content: list[dict[str, Any]],
browser_window_dimension: Resolution,
screenshot_resize_target_dimension: Resolution,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> list[Action]:
tool_calls = [block for block in assistant_content if block["type"] == "tool_use" and block["name"] == "computer"]
reasonings = [block for block in assistant_content if block["type"] == "thinking"]
@@ -932,7 +936,9 @@ async def parse_anthropic_actions(
reasoning = reasonings[0]["thinking"] if reasonings else None
assistant_messages = [block for block in assistant_content if block["type"] == "text"]
assistant_message = assistant_messages[0]["text"] if assistant_messages else None
- actions = await generate_cua_fallback_actions(task, step, assistant_message, reasoning)
+ actions = await generate_cua_fallback_actions(
+ task, step, assistant_message, reasoning, allowed_credential_parameter_keys
+ )
return actions
@@ -956,6 +962,7 @@ async def generate_cua_fallback_actions(
step: Step,
assistant_message: str | None,
reasoning: str | None,
+ allowed_credential_parameter_keys: Sequence[str] | None = None,
) -> list[Action]:
fallback_action_prompt = prompt_engine.load_prompt(
"cua-fallback-action",
@@ -1078,7 +1085,11 @@ async def generate_cua_fallback_actions(
elif skyvern_action_type == "get_verification_code":
try:
- otp_value = await resolve_otp_value(task, expected_otp_type=OTPType.TOTP)
+ otp_value = await resolve_otp_value(
+ task,
+ expected_otp_type=OTPType.TOTP,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
+ )
except NoTOTPVerificationCodeFound as e:
otp_value = None
reasoning_suffix = f"No verification code found.{describe_webhook_contract_failure(e.webhook_diagnostics)}"
@@ -1101,7 +1112,7 @@ async def generate_cua_fallback_actions(
# Three-way classification: missing config, configured-but-empty (any of
# URL / identifier / credential), or wrong-type otp_value. Each gets a
# distinct customer-visible signal so webhook consumers can branch.
- has_credential = has_credential_totp_candidate(task.workflow_run_id)
+ has_credential = has_credential_totp_candidate(task.workflow_run_id, allowed_credential_parameter_keys)
no_source_configured = (
otp_value is None and not task.totp_verification_url and not task.totp_identifier and not has_credential
)
diff --git a/tests/unit/test_agent_otp_routing.py b/tests/unit/test_agent_otp_routing.py
index 8234f527d..3cc562724 100644
--- a/tests/unit/test_agent_otp_routing.py
+++ b/tests/unit/test_agent_otp_routing.py
@@ -120,6 +120,7 @@ async def _run_otp_actions(
monkeypatch: pytest.MonkeyPatch,
task: SimpleNamespace,
json_response: dict,
+ allowed_credential_parameter_keys: list[str] | None = None,
) -> tuple[AsyncMock, MagicMock, tuple]:
"""Drive the real handle_potential_OTP_actions post-plan seam. Returns (hpvc mock, parse mock,
result). hpvc awaited ⇒ the polling verification re-plan fired; not awaited ⇒ the first plan was
@@ -137,7 +138,14 @@ async def _run_otp_actions(
monkeypatch.setattr("skyvern.forge.agent.stamp_parsed_actions", MagicMock())
agent = ForgeAgent.__new__(ForgeAgent)
- result = await agent.handle_potential_OTP_actions(task, step, scraped_page, browser_state, json_response)
+ result = await agent.handle_potential_OTP_actions(
+ task,
+ step,
+ scraped_page,
+ browser_state,
+ json_response,
+ allowed_credential_parameter_keys=allowed_credential_parameter_keys,
+ )
return hpvc, parse_mock, result
@@ -183,6 +191,29 @@ async def test_single_field_registered_credential_placeholder_skips_and_preserve
assert result[0] is response and result[0]["actions"] is actions and result[1] == []
+async def test_single_field_skip_rejected_when_credential_not_linked_to_block(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """SKY-15181: a first-pass plan carrying a credential the originating block does not link must
+ not be kept on the stale-active-key gate alone — it falls through to the scoped resolver."""
+ context = _real_credential_context()
+ manager = SimpleNamespace(
+ has_workflow_run_context=lambda _id: True,
+ get_workflow_run_context=lambda _id: context,
+ )
+ monkeypatch.setattr(otp_service.app, "WORKFLOW_CONTEXT_MANAGER", manager)
+ monkeypatch.setattr("skyvern.forge.agent.app.WORKFLOW_CONTEXT_MANAGER", manager)
+ task = _make_task()
+ actions = [{"action_type": "INPUT_TEXT", "id": "AAAA", "text": "cred_totp"}, {"action_type": "CLICK", "id": "BBBB"}]
+ response = _otp_json_response(actions)
+ with skyvern_context.scoped(SkyvernContext(task_id=task.task_id, active_credential_parameter_key="credentials")):
+ hpvc, _parse, _result = await _run_otp_actions(
+ monkeypatch, task, response, allowed_credential_parameter_keys=[]
+ )
+ hpvc.assert_awaited_once()
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
"case",
@@ -593,7 +624,7 @@ async def test_handle_potential_verification_code_uses_resolver_without_db_looku
agent = ForgeAgent.__new__(ForgeAgent)
await agent.handle_potential_verification_code(task, step, scraped_page, browser_state, json_response)
- resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP)
+ resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP, allowed_credential_parameter_keys=None)
db_get.assert_not_awaited()
@@ -646,7 +677,7 @@ async def test_handle_potential_verification_code_resolves_with_should_enter_fal
finally:
skyvern_context.reset()
- resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP)
+ resolver.assert_awaited_once_with(task, expected_otp_type=OTPType.TOTP, allowed_credential_parameter_keys=None)
rescrape.assert_awaited_once()
assert result == {"actions": [{"action_type": "INPUT_TEXT", "text": "123456"}]}
diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.py
index 8521a4df9..befce37a7 100644
--- a/tests/unit/test_agent_task_v3.py
+++ b/tests/unit/test_agent_task_v3.py
@@ -2508,7 +2508,9 @@ async def test_execute_task_v3_hands_the_loop_the_live_verification_gate(monkeyp
# a fresh or detached state would let a refused source false-complete despite the blocker.
seen_states: list[Any] = []
- def capturing_build(task: Any, page_provider: Any = None, state: Any = None) -> tuple[list[Any], str]:
+ def capturing_build(
+ task: Any, page_provider: Any = None, state: Any = None, allowed_credential_parameter_keys: Any = None
+ ) -> tuple[list[Any], str]:
seen_states.append(state)
return [], ""
@@ -2535,7 +2537,9 @@ async def test_execute_task_v3_pins_credential_before_building_auth_tools(
seen_keys: list[str | None] = []
seen_providers: list[Any] = []
- def capturing_build(task: Any, page_provider: Any = None, state: Any = None) -> tuple[list[Any], str]:
+ def capturing_build(
+ task: Any, page_provider: Any = None, state: Any = None, allowed_credential_parameter_keys: Any = None
+ ) -> tuple[list[Any], str]:
ctx = skyvern_context.current()
seen_keys.append(ctx.active_credential_parameter_key if ctx else None)
seen_providers.append(page_provider)
@@ -2564,7 +2568,9 @@ async def test_execute_task_v3_withholds_the_page_provider_from_auth_tools_when_
# navigate; a page-aware run must be, or the sign-in-link tool is silently never offered.
seen_providers: list[Any] = []
- def capturing_build(task: Any, page_provider: Any = None, state: Any = None) -> tuple[list[Any], str]:
+ def capturing_build(
+ task: Any, page_provider: Any = None, state: Any = None, allowed_credential_parameter_keys: Any = None
+ ) -> tuple[list[Any], str]:
seen_providers.append(page_provider)
return [], ""
diff --git a/tests/unit/test_otp_service.py b/tests/unit/test_otp_service.py
index 1e6939b54..a36f7a258 100644
--- a/tests/unit/test_otp_service.py
+++ b/tests/unit/test_otp_service.py
@@ -28,6 +28,7 @@ from skyvern.services.otp_service import (
_get_otp_value_from_url,
_is_mfa_like_parameter_key,
extract_totp_from_navigation_inputs,
+ has_credential_totp_candidate,
parse_otp_login,
poll_otp_value,
resolve_otp_value,
@@ -181,7 +182,7 @@ class TestResolveOtpValuePlaceholderFallthrough:
result = await resolve_otp_value(task)
assert result is credential_value
- credential.assert_called_once_with("wr_test")
+ credential.assert_called_once_with("wr_test", None)
poll.assert_not_called()
@pytest.mark.asyncio
@@ -227,6 +228,44 @@ class TestResolveOtpValuePlaceholderFallthrough:
assert result is polled
poll.assert_awaited_once()
+ @pytest.mark.asyncio
+ async def test_unlinked_credential_falls_through_to_polling(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ """SKY-15181 end to end: a block with a webhook and no linked credential must poll, even
+ though the run context holds the login block's TOTP-bearing credential (still active)."""
+ monkeypatch.setattr(pyotp.TOTP, "now", lambda _self: "111111")
+ fake_context = _FakeWorkflowRunContext(
+ values={"credentials": {"username": "u", "password": "p", "totp": "tot_b"}},
+ secrets={"tot_b_value": _VALID_TOTP_SEED},
+ )
+ fake_app = SimpleNamespace(
+ WORKFLOW_CONTEXT_MANAGER=SimpleNamespace(
+ get_workflow_run_context=lambda _wr_id: fake_context,
+ has_workflow_run_context=lambda _wr_id: True,
+ ),
+ DATABASE=SimpleNamespace(
+ workflow_runs=SimpleNamespace(get_workflow_run=AsyncMock(return_value=None)),
+ ),
+ )
+ monkeypatch.setattr(otp_service, "app", fake_app)
+
+ task = SimpleNamespace(
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ totp_verification_url="https://example.com/email-otp-webhook",
+ totp_identifier=None,
+ navigation_payload={},
+ )
+ polled = OTPValue(value="654321", type=OTPType.TOTP)
+ poll = AsyncMock(return_value=polled)
+ monkeypatch.setattr(otp_service, "poll_otp_value", poll)
+
+ with skyvern_context.scoped(_scoped_context(active="credentials")):
+ result = await resolve_otp_value(task, allowed_credential_parameter_keys=[])
+
+ assert result is polled
+ poll.assert_awaited_once()
+
class TestResolveOtpValueExpectedOtpTypeGating:
"""resolve_otp_value must honor expected_otp_type for the credential-TOTP branch, the same way
@@ -276,7 +315,7 @@ class TestResolveOtpValueExpectedOtpTypeGating:
result = await resolve_otp_value(self._task(), expected_otp_type=OTPType.TOTP)
assert result is credential_value
- credential.assert_called_once_with(None)
+ credential.assert_called_once_with(None, None)
poll.assert_not_called()
@@ -1872,6 +1911,42 @@ class TestTryGenerateTotpFromCredential:
assert result is not None
assert result.value == "131313"
+ def test_block_with_no_linked_credential_ignores_stale_active_key_and_run_scan(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ """SKY-15181: an email-OTP block links no credential, so neither the login block's stale
+ active key nor the run-wide single-candidate scan may produce a code."""
+ fake = _FakeWorkflowRunContext(
+ values={"credentials": {"username": "u_b", "password": "p_b", "totp": "tot_b"}},
+ secrets={"tot_b_value": _VALID_TOTP_SEED},
+ )
+ self._patch_workflow_context(monkeypatch, fake)
+
+ with skyvern_context.scoped(_scoped_context(active="credentials")):
+ result = try_generate_totp_from_credential("wr_test", allowed_credential_parameter_keys=[])
+ candidate = has_credential_totp_candidate("wr_test", allowed_credential_parameter_keys=[])
+
+ assert result is None
+ assert candidate is False
+
+ def test_block_linked_credential_wins_over_stale_active_key(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ """A stale active key from an earlier block yields to the current block's own credential."""
+ fake = _FakeWorkflowRunContext(
+ values={
+ "login_credentials": {"username": "u_a", "password": "p_a", "totp": "tot_a"},
+ "mfa_credentials": {"username": "u_b", "password": "p_b", "totp": "tot_b"},
+ },
+ secrets={"tot_a_value": _VALID_TOTP_SEED, "tot_b_value": _OTHER_TOTP_SEED},
+ )
+ self._patch_workflow_context(monkeypatch, fake)
+ monkeypatch.setattr(pyotp.TOTP, "now", lambda self: "111111" if self.secret == _VALID_TOTP_SEED else "222222")
+
+ with skyvern_context.scoped(_scoped_context(active="login_credentials")):
+ result = try_generate_totp_from_credential("wr_test", allowed_credential_parameter_keys=["mfa_credentials"])
+
+ assert result is not None
+ assert result.value == "222222"
+
def test_ordinary_input_dict_with_placeholder_totp_is_not_a_candidate(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
@@ -2811,6 +2886,71 @@ def test_has_otp_source_credential_totp_candidate_gated_on_expected_type(
organization_id="o_1",
workflow_run_id="wr_1",
)
- monkeypatch.setattr(otp_service, "has_credential_totp_candidate", lambda run_id: True)
+ monkeypatch.setattr(otp_service, "has_credential_totp_candidate", lambda run_id, *a, **k: True)
assert otp_service.has_otp_source(task, expected_otp_type=expected_otp_type) is expected
+
+
+class TestScopeSuppressionRegressionSentinel:
+ """SKY-15181 rollout: a workflow that accidentally relied on a cross-block credential (and has
+ no polling fallback) must emit the facetable sentinel when scoping suppresses it — the only
+ signal separating an intended fallthrough-to-polling from a run that used to pass and now fails."""
+
+ def _armed_run_context(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ fake = _FakeWorkflowRunContext(
+ values={"credentials": {"username": "u", "password": "p", "totp": "tot_b"}},
+ secrets={"tot_b_value": _VALID_TOTP_SEED},
+ )
+ fake_app = SimpleNamespace(
+ WORKFLOW_CONTEXT_MANAGER=SimpleNamespace(
+ get_workflow_run_context=lambda _wr_id: fake,
+ has_workflow_run_context=lambda _wr_id: True,
+ ),
+ )
+ monkeypatch.setattr(otp_service, "app", fake_app)
+
+ def _task(self, totp_verification_url: str | None = None) -> SimpleNamespace:
+ return SimpleNamespace(
+ task_id="tsk_test",
+ workflow_run_id="wr_test",
+ organization_id="o_test",
+ totp_verification_url=totp_verification_url,
+ totp_identifier=None,
+ navigation_payload=None,
+ )
+
+ @pytest.mark.asyncio
+ async def test_no_fallback_suppression_warns_from_resolver_and_offer_gate(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ self._armed_run_context(monkeypatch)
+ with skyvern_context.scoped(_scoped_context(active="credentials")):
+ with structlog.testing.capture_logs() as logs:
+ result = await resolve_otp_value(self._task(), allowed_credential_parameter_keys=[])
+ offered = otp_service.has_otp_source(
+ self._task(), expected_otp_type=OTPType.TOTP, allowed_credential_parameter_keys=[]
+ )
+ assert result is None and offered is False
+ sentinel = [r for r in logs if r["event"] == otp_service.SCOPE_SUPPRESSED_LEGACY_CREDENTIAL_EVENT]
+ assert len(sentinel) == 2
+ assert sentinel[0]["log_level"] == "warning"
+
+ @pytest.mark.asyncio
+ async def test_webhook_fallback_or_unscoped_run_stays_silent(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ self._armed_run_context(monkeypatch)
+ polled = OTPValue(value="654321", type=OTPType.TOTP)
+ monkeypatch.setattr(otp_service, "poll_otp_value", AsyncMock(return_value=polled))
+ fake_db = SimpleNamespace(
+ workflow_runs=SimpleNamespace(get_workflow_run=AsyncMock(return_value=None)),
+ )
+ otp_service.app.DATABASE = fake_db
+ with skyvern_context.scoped(_scoped_context(active="credentials")):
+ with structlog.testing.capture_logs() as logs:
+ with_webhook = await resolve_otp_value(
+ self._task(totp_verification_url="https://example.com/webhook"),
+ allowed_credential_parameter_keys=[],
+ )
+ unscoped = await resolve_otp_value(self._task())
+ assert with_webhook is polled
+ assert unscoped is not None
+ assert not [r for r in logs if r["event"] == otp_service.SCOPE_SUPPRESSED_LEGACY_CREDENTIAL_EVENT]
diff --git a/tests/unit/test_resolve_otp_value.py b/tests/unit/test_resolve_otp_value.py
index 1ac76aba9..149a85ab4 100644
--- a/tests/unit/test_resolve_otp_value.py
+++ b/tests/unit/test_resolve_otp_value.py
@@ -82,7 +82,7 @@ async def test_credential_returns_value_skipping_poll_even_when_url_configured()
result = await resolve_otp_value(task)
assert result is credential_value
- credential.assert_called_once_with(task.workflow_run_id)
+ credential.assert_called_once_with(task.workflow_run_id, None)
poll.assert_not_called()
@@ -326,7 +326,7 @@ async def test_credential_check_runs_even_when_workflow_run_id_missing(monkeypat
result = await resolve_otp_value(task)
assert result is None
- credential.assert_called_once_with(None)
+ credential.assert_called_once_with(None, None)
db_get.assert_not_awaited()
poll.assert_awaited_once()
diff --git a/tests/unit/test_taskv3_auth_tools.py b/tests/unit/test_taskv3_auth_tools.py
index ca582f1b4..749629471 100644
--- a/tests/unit/test_taskv3_auth_tools.py
+++ b/tests/unit/test_taskv3_auth_tools.py
@@ -115,11 +115,13 @@ async def test_build_auth_tools_offered_iff_resolve_otp_value_has_a_source(
task = _task(**overrides)
has_credential = case == "credential"
credential_value = OTPValue(value="424242", type=OTPType.TOTP)
- monkeypatch.setattr(otp_service, "has_credential_totp_candidate", lambda run_id: has_credential and bool(run_id))
+ monkeypatch.setattr(
+ otp_service, "has_credential_totp_candidate", lambda run_id, *a, **k: has_credential and bool(run_id)
+ )
monkeypatch.setattr(
otp_service,
"try_generate_totp_from_credential",
- lambda run_id: credential_value if has_credential and run_id else None,
+ lambda run_id, *a, **k: credential_value if has_credential and run_id else None,
)
poll = AsyncMock(return_value=OTPValue(value="111111", type=OTPType.TOTP))
monkeypatch.setattr(otp_service, "poll_otp_value", poll)
@@ -1194,3 +1196,22 @@ async def test_verification_tools_share_one_polling_budget(monkeypatch: pytest.M
code_result = await handlers["get_verification_code"]({})
assert code_result.status == "error" and "budget exhausted" in code_result.content
assert resolver_calls == calls_after_link_tool
+
+
+def test_block_credential_parameter_keys_script_mode_returns_none(monkeypatch: pytest.MonkeyPatch) -> None:
+ """SKY-15181 review: script-built blocks carry no parameters=, so deriving scope from them would
+ silently disable credential-TOTP for cached-script logins; script-mode runs stay legacy (None)."""
+ from types import SimpleNamespace as NS
+
+ from skyvern.forge import agent as agent_module
+
+ block = NS(parameters=[])
+ monkeypatch.setattr(
+ agent_module.app,
+ "WORKFLOW_CONTEXT_MANAGER",
+ NS(has_workflow_run_context=lambda _id: True, get_workflow_run_context=lambda _id: NS()),
+ )
+ with skyvern_context.scoped(SkyvernContext(script_mode=True)):
+ assert agent_module.block_credential_parameter_keys(block, "wr_test") is None
+ with skyvern_context.scoped(SkyvernContext()):
+ assert agent_module.block_credential_parameter_keys(NS(parameters=[]), "wr_test") == []