Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-15287 Let Copilot use explicit access-link destinations (#8359)
Andrew Neilson52bb91661881
diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-raw-secret-safety.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-raw-secret-safety.j2index 30b8c3566..e3bf652c3 100644--- a/skyvern/forge/prompts/skyvern/workflow-copilot-raw-secret-safety.j2+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-raw-secret-safety.j2@@ -6,6 +6,9 @@ Return only JSON in this exact shape:- Cite each value in full. The server redacts exactly the spans you cite and keeps the rest of the turn, so a partial citation leaves the remainder of that value in place.- `clean` requires no citations. `detected` requires one or more citations.- A saved-credential name, identifier, placeholder, or description is not literal authentication material.+- First identify reusable account authentication. A value presented as a password is always reusable account authentication for this classification, including in a URL query or username/password URL userinfo. Reusable API keys and session credentials are also `detected` wherever they appear. An instruction to visit the URL never converts these credential types into scoped link capabilities. Cite the complete secret value, not the whole URL.+- Only after that check, treat a complete URL as operational destination input when the user explicitly directs the browser to open, navigate to, fetch from, or download from it and any remaining authentication-like material is a scoped capability conveyed by possession of that particular link (for example, a signed access link). Classify that delegated-destination turn as `clean`; cite only literal authentication material supplied separately from the destination.+- A URL merely mentioned as the source or location of authentication material is not a delegated browser destination. Authentication material supplied for use as authentication remains `detected`, including when the cited value appears inside such a URL.- Do not infer from earlier turns or outside context. Do not paraphrase citations.Latest user turn after deterministic high-confidence redaction:diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 304f6f585..a6f39c0f4 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -47,6 +47,7 @@ from skyvern.forge.sdk.copilot.request_slots import (from skyvern.forge.sdk.copilot.secret_redaction import (RAW_SECRET_PATTERNS,contains_email_password_pair,+ raw_secret_spans_for_prompt,redact_raw_secrets_for_prompt,)from skyvern.forge.sdk.copilot.tracing_setup import copilot_span@@ -239,12 +240,96 @@ async def _exonerate_saved_credential_citations(citations: list[str], organizati_MIN_RAW_SECRET_EVIDENCE_CHARS = 4+_RAW_SECRET_EVIDENCE_BEFORE_URI_VALUE_BOUNDARIES = "=?&#"+_RAW_SECRET_EVIDENCE_AFTER_URI_VALUE_BOUNDARIES = "&#"+_RAW_SECRET_EVIDENCE_SENTENCE_PUNCTUATION = ".,;:!?"+_RAW_SECRET_EVIDENCE_CLOSING_BOUNDARIES = ")]\"}'"+++def _redact_raw_secrets_outside_url_candidates(message: str) -> str:+ """Keep independently occurring URL tokens intact for the model-backed disclosure decision."""+ deterministic_secret_spans = raw_secret_spans_for_prompt(message)+ pieces: list[str] = []+ cursor = 0+ for candidate in URL_CANDIDATE_RE.finditer(message):+ if any(+ start < candidate.end()+ and candidate.start() < end+ and not (candidate.start() <= start and end <= candidate.end())+ for start, end in deterministic_secret_spans+ ):+ continue+ pieces.append(redact_raw_secrets_for_prompt(message[cursor : candidate.start()]))+ pieces.append(candidate.group(0))+ cursor = candidate.end()+ pieces.append(redact_raw_secrets_for_prompt(message[cursor:]))+ return "".join(pieces)+++def _raw_secret_evidence_uri_candidate_end(+ message: str,+ index: int,+ end: int,+ url_candidate_spans: tuple[tuple[int, int], ...],+) -> int | None:+ for candidate_start, candidate_end in url_candidate_spans:+ if candidate_start >= index or end > candidate_end:+ continue+ before = message[index - 1]+ if before in _RAW_SECRET_EVIDENCE_BEFORE_URI_VALUE_BOUNDARIES:+ return candidate_end+ return None+++def _raw_secret_evidence_is_url_userinfo_password(+ message: str,+ index: int,+ end: int,+ url_candidate_spans: tuple[tuple[int, int], ...],+) -> bool:+ for candidate_start, candidate_end in url_candidate_spans:+ if index < candidate_start or end > candidate_end:+ continue+ candidate = message[candidate_start:candidate_end]+ scheme_end = candidate.find("://")+ if scheme_end < 0:+ continue+ authority_start = scheme_end + 3+ authority_end = min(+ (position for delimiter in "/?#" if (position := candidate.find(delimiter, authority_start)) >= 0),+ default=len(candidate),+ )+ userinfo_end = candidate.rfind("@", authority_start, authority_end)+ if userinfo_end < 0:+ continue+ password_start = candidate.find(":", authority_start, userinfo_end)+ if password_start < 0:+ continue+ if index == candidate_start + password_start + 1 and end == candidate_start + userinfo_end:+ return True+ return False+++def _raw_secret_evidence_after_is_boundary(evidence: str, message: str, end: int) -> bool:+ if end >= len(message):+ return True+ after = message[end]+ if after.isspace() or after in _RAW_SECRET_EVIDENCE_CLOSING_BOUNDARIES:+ return True+ if after in _RAW_SECRET_EVIDENCE_SENTENCE_PUNCTUATION:+ following = message[end + 1] if end + 1 < len(message) else ""+ if not following or following.isspace() or following in _RAW_SECRET_EVIDENCE_CLOSING_BOUNDARIES:+ return True+ return not evidence[-1].isalnum() and after in ".,;:?"def _raw_secret_evidence_spans(evidence: str | None, user_message: str) -> list[tuple[int, int]]:if not evidence or len(evidence) < _MIN_RAW_SECRET_EVIDENCE_CHARS:return []message = user_message or ""+ url_candidate_spans = tuple(+ (candidate.start(), candidate.end()) for candidate in URL_CANDIDATE_RE.finditer(message)+ )spans: list[tuple[int, int]] = []start = 0while True:@@ -253,11 +338,25 @@ def _raw_secret_evidence_spans(evidence: str | None, user_message: str) -> list[return spansend = index + len(evidence)before = message[index - 1] if index else ""- after = message[end] if end < len(message) else ""- before_is_boundary = not before or before.isspace() or before in "([{\"'"- after_is_boundary = (- not after or after.isspace() or after in ")]\"}'" or (not evidence[-1].isalnum() and after in ".,;:?")+ uri_candidate_end = _raw_secret_evidence_uri_candidate_end(message, index, end, url_candidate_spans)+ is_userinfo_password = _raw_secret_evidence_is_url_userinfo_password(+ message,+ index,+ end,+ url_candidate_spans,+ )+ before_is_boundary = (+ not before+ or before.isspace()+ or before in "([{\"'"+ or uri_candidate_end is not None+ or is_userinfo_password+ )+ after_is_uri_boundary = is_userinfo_password or (+ uri_candidate_end is not None+ and (end == uri_candidate_end or message[end] in _RAW_SECRET_EVIDENCE_AFTER_URI_VALUE_BOUNDARIES))+ after_is_boundary = after_is_uri_boundary or _raw_secret_evidence_after_is_boundary(evidence, message, end)if before_is_boundary and after_is_boundary:spans.append((index, end))start = index + 1@@ -290,7 +389,7 @@ async def _screen_raw_secret_safety(*,organization_id: str,) -> _RawSecretSafetyScreen:- deterministic_safe_message = redact_raw_secrets_for_prompt(user_message)+ deterministic_safe_message = _redact_raw_secrets_outside_url_candidates(user_message)if handler is None:return _screen_unavailable("missing_handler")try:diff --git a/skyvern/forge/sdk/copilot/secret_redaction.py b/skyvern/forge/sdk/copilot/secret_redaction.pyindex b4151f74d..1000e0b23 100644--- a/skyvern/forge/sdk/copilot/secret_redaction.py+++ b/skyvern/forge/sdk/copilot/secret_redaction.py@@ -81,6 +81,18 @@ def contains_email_password_pair(text: str) -> bool:return bool(_email_password_pair_segments(text))+def raw_secret_spans_for_prompt(text: str) -> tuple[tuple[int, int], ...]:+ """Return deterministic secret spans in the original prompt text."""+ raw = text or ""+ spans = {(match.start(), match.end()) for pattern in RAW_SECRET_PATTERNS for match in pattern.finditer(raw)}+ for segment in _email_password_pair_segments(raw):+ start = 0+ while (index := raw.find(segment, start)) >= 0:+ spans.add((index, index + len(segment)))+ start = index + 1+ return tuple(sorted(spans))++def redact_raw_secrets_for_prompt(text: str) -> str:redacted = text or ""for pattern in RAW_SECRET_PATTERNS:diff --git a/tests/unit/test_copilot_raw_secret_safety.py b/tests/unit/test_copilot_raw_secret_safety.pyindex c01819c01..886cdf835 100644--- a/tests/unit/test_copilot_raw_secret_safety.py+++ b/tests/unit/test_copilot_raw_secret_safety.py@@ -22,6 +22,18 @@ from skyvern.forge.sdk.copilot.tools.guardrails import _authority_tool_error, _ufrom tests.unit.copilot_test_helpers import make_copilot_ctx_SCREEN_UNAVAILABLE_TURN = "[INPUT_UNAVAILABLE_SAFETY_SCREEN_INCOMPLETE]"+_ACCESS_LINK_VALUE = "A1B2C3D4E5F6"+_EXPLICIT_ACCESS_LINK_PROMPT = (+ "Make an agent that goes to "+ f"https://portal.example/?accesskey={_ACCESS_LINK_VALUE}#projects/123/dashboard\n\n"+ "and downloads all available files"+)+_EXPLICIT_TOKEN_LINK_PROMPT = (+ "Make an agent that downloads from https://portal.example/download?token=A1B2C3D4E5F6&signature=Z9Y8X7W6V5U4"+)+_STANDALONE_ACCESS_MATERIAL_PROMPT = (+ f"My access key is {_ACCESS_LINK_VALUE}. Use it as authentication material when drafting the workflow."+)async def _build(@@ -109,6 +121,183 @@ async def test_semantic_secret_is_redacted_without_discarding_the_turn() -> Noneassert policy.allow_missing_credentials_in_draft is True+async def test_explicit_access_link_destination_keeps_full_authoring_and_run_authority() -> None:+ policy, _ = await _build(+ _EXPLICIT_ACCESS_LINK_PROMPT,+ {"version": "1", "state": "clean", "citations": []},+ )++ agent_message, _ = _request_policy_agent_inputs(+ policy,+ user_message=_EXPLICIT_ACCESS_LINK_PROMPT,+ chat_history_text="",+ previous_user_message=None,+ )++ assert policy.raw_secret_safety_status == "clean"+ assert policy.raw_secret_safety_failure_kind == "none"+ assert policy.raw_secret_handling == "none"+ assert policy.canonical_user_message == _EXPLICIT_ACCESS_LINK_PROMPT+ assert policy.allow_update_workflow is True+ assert policy.allow_run_blocks is True+ assert policy.user_response_policy == "proceed"+ assert policy.clarification_question is None+ assert agent_message == _EXPLICIT_ACCESS_LINK_PROMPT+++async def test_explicit_destination_with_scoped_capability_parameters_reaches_model_unchanged() -> None:+ policy, handler = await _build(+ _EXPLICIT_TOKEN_LINK_PROMPT,+ {"version": "1", "state": "clean", "citations": []},+ )++ handler.assert_awaited_once()+ assert _EXPLICIT_TOKEN_LINK_PROMPT in handler.await_args.kwargs["prompt"]+ assert policy.raw_secret_safety_status == "clean"+ assert policy.canonical_user_message == _EXPLICIT_TOKEN_LINK_PROMPT+ assert policy.allow_update_workflow is True+ assert policy.allow_run_blocks is True+++async def test_secret_assignment_whose_value_is_a_url_keeps_deterministic_redaction() -> None:+ message = "api_key=https://portal.example/k/ABCDEF"+ policy, handler = await _build(+ message,+ {"version": "1", "state": "clean", "citations": []},+ )++ handler.assert_awaited_once()+ prompt = handler.await_args.kwargs["prompt"]+ assert message not in prompt+ assert "[REDACTED_SECRET]" in prompt+ assert policy.raw_secret_safety_status == "clean"+ assert policy.canonical_user_message == "[REDACTED_SECRET]"+++async def test_secret_assignment_starting_inside_url_and_ending_after_it_is_redacted() -> None:+ message = "Open https://portal.example/?api_key= ABCDEF"+ policy, handler = await _build(+ message,+ {"version": "1", "state": "clean", "citations": []},+ )++ handler.assert_awaited_once()+ prompt = handler.await_args.kwargs["prompt"]+ assert message not in prompt+ assert "[REDACTED_SECRET]" in prompt+ assert policy.canonical_user_message == "Open https://portal.example/?[REDACTED_SECRET]"+++async def test_account_row_pair_whose_value_is_a_url_keeps_deterministic_redaction() -> None:+ message = "Use [email protected]:www.portal.example"+ policy, handler = await _build(+ message,+ {"version": "1", "state": "clean", "citations": []},+ )++ handler.assert_awaited_once()+ prompt = handler.await_args.kwargs["prompt"]+ assert message not in prompt+ assert "[REDACTED_SECRET]" in prompt+ assert policy.canonical_user_message == "Use [REDACTED_SECRET]"+++async def test_url_auth_material_not_delegated_as_destination_remains_redacted() -> None:+ message = "Use the token from https://portal.example/account?token=A1B2C3D4E5F6 as authentication material."+ policy, handler = await _build(+ message,+ {"version": "1", "state": "detected", "citations": ["A1B2C3D4E5F6"]},+ )++ assert message in handler.await_args.kwargs["prompt"]+ assert policy.raw_secret_safety_status == "detected"+ assert policy.canonical_user_message == (+ "Use the token from https://portal.example/account?token=[REDACTED_SECRET] as authentication material."+ )+ assert policy.allow_update_workflow is True+ assert policy.allow_run_blocks is False+++async def test_reusable_password_inside_delegated_url_is_redacted_and_update_only() -> None:+ message = "Download from https://portal.example/export?password=Hunter2Portal!"+ policy, handler = await _build(+ message,+ {"version": "1", "state": "detected", "citations": ["Hunter2Portal!"]},+ )++ assert message in handler.await_args.kwargs["prompt"]+ assert policy.raw_secret_safety_status == "detected"+ assert policy.canonical_user_message == ("Download from https://portal.example/export?password=[REDACTED_SECRET]")+ assert policy.allow_update_workflow is True+ assert policy.allow_run_blocks is False+++async def test_reusable_password_in_url_userinfo_is_accepted_as_complete_citation() -> None:+ message = "Download from https://portal-user:[email protected]/export"+ policy, _ = await _build(+ message,+ {"version": "1", "state": "detected", "citations": ["Hunter2Portal!"]},+ )++ assert policy.raw_secret_safety_status == "detected"+ assert policy.raw_secret_safety_failure_kind == "none"+ assert policy.canonical_user_message == (+ "Download from https://portal-user:[REDACTED_SECRET]@portal.example/export"+ )+++async def test_partial_url_userinfo_password_citation_still_fails_closed() -> None:+ message = "Download from https://portal-user:[email protected]/export"+ policy, _ = await _build(+ message,+ {"version": "1", "state": "detected", "citations": ["Portal!"]},+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "invalid_citation"+ assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN++[email protected]("separator", ["@", ":"])+async def test_userinfo_delimiters_do_not_validate_partial_query_value_citations(separator: str) -> None:+ message = f"Download from https://portal.example/export?password=Hunter2{separator}Pass"+ policy, _ = await _build(+ message,+ {"version": "1", "state": "detected", "citations": ["Hunter2"]},+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "invalid_citation"+ assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN+++async def test_standalone_access_material_remains_redacted_and_update_only() -> None:+ policy, _ = await _build(+ _STANDALONE_ACCESS_MATERIAL_PROMPT,+ {"version": "1", "state": "detected", "citations": [_ACCESS_LINK_VALUE]},+ )++ assert policy.raw_secret_safety_status == "detected"+ assert policy.raw_secret_safety_failure_kind == "none"+ assert policy.raw_secret_handling == "redacted_draft"+ assert policy.canonical_user_message == (+ "My access key is [REDACTED_SECRET]. Use it as authentication material when drafting the workflow."+ )+ assert policy.allow_update_workflow is True+ assert policy.allow_run_blocks is False++@pytest.mark.asyncioasync def test_multiple_semantic_secret_citations_are_each_redacted() -> None:first = "Hunter2Portal!"@@ -150,6 +339,104 @@ async def test_partial_semantic_secret_citation_fails_closed() -> None:assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN+async def test_complete_uri_query_value_citation_is_accepted_and_redacted() -> None:+ policy, _ = await _build(+ _EXPLICIT_ACCESS_LINK_PROMPT,+ {"version": "1", "state": "detected", "citations": [_ACCESS_LINK_VALUE]},+ )++ assert policy.raw_secret_safety_status == "detected"+ assert policy.raw_secret_safety_failure_kind == "none"+ assert policy.canonical_user_message == (+ "Make an agent that goes to "+ "https://portal.example/?accesskey=[REDACTED_SECRET]#projects/123/dashboard\n\n"+ "and downloads all available files"+ )+++async def test_complete_uri_query_value_between_ampersands_is_accepted_and_redacted() -> None:+ message = f"Open https://portal.example/?view=all&accesskey={_ACCESS_LINK_VALUE}&project=123"+ policy, _ = await _build(+ message,+ {"version": "1", "state": "detected", "citations": [_ACCESS_LINK_VALUE]},+ )++ assert policy.raw_secret_safety_status == "detected"+ assert policy.raw_secret_safety_failure_kind == "none"+ assert policy.canonical_user_message == (+ "Open https://portal.example/?view=all&accesskey=[REDACTED_SECRET]&project=123"+ )+++ ("message", "expected"),+ [+ (+ f'Open "https://portal.example/?accesskey={_ACCESS_LINK_VALUE}"',+ 'Open "https://portal.example/?accesskey=[REDACTED_SECRET]"',+ ),+ (+ f"Open https://portal.example/?accesskey={_ACCESS_LINK_VALUE}.",+ "Open https://portal.example/?accesskey=[REDACTED_SECRET].",+ ),+ ],+)+async def test_complete_uri_query_value_before_sentence_boundary_is_accepted(+ message: str,+ expected: str,+) -> None:+ policy, _ = await _build(+ message,+ {"version": "1", "state": "detected", "citations": [_ACCESS_LINK_VALUE]},+ )++ assert policy.raw_secret_safety_status == "detected"+ assert policy.canonical_user_message == expected+++async def test_partial_uri_query_value_citation_still_fails_closed() -> None:+ policy, _ = await _build(+ _EXPLICIT_ACCESS_LINK_PROMPT,+ {"version": "1", "state": "detected", "citations": ["B2C3D4E5"]},+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "invalid_citation"+ assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN++[email protected]("separator", ["&", "#"])+async def test_uri_delimiter_does_not_validate_partial_citation_outside_url(separator: str) -> None:+ literal = f"A1B2C3D4{separator}E5F6"+ policy, _ = await _build(+ f"Use {literal} as authentication material.",+ {"version": "1", "state": "detected", "citations": ["A1B2C3D4"]},+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "invalid_citation"+ assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN++[email protected]("separator", ["&", "#"])+async def test_uri_delimiter_does_not_validate_partial_citation_after_delimiter_outside_url(separator: str) -> None:+ literal = f"Abc{separator}Def123"+ policy, _ = await _build(+ f"My password is {literal} ok",+ {"version": "1", "state": "detected", "citations": ["Def123"]},+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "invalid_citation"+ assert policy.canonical_user_message == _SCREEN_UNAVAILABLE_TURN++@pytest.mark.asyncioasync def test_secret_ending_in_punctuation_can_precede_sentence_punctuation() -> None:literal = "BillingKey-8391!"@@ -319,6 +606,7 @@ async def test_clean_verdict_keeps_deterministic_redaction_without_withdrawing_a{"version": "1", "state": "detected", "citations": ["not-in-turn-8391!"]},"invalid_citation",),+ ({"version": "1", "state": "detected", "citations": []}, "contradictory_verdict"),({"state": "detected", "citations": ["Hunter2Portal!"]}, "malformed_output"),("not-json", "malformed_output"),],@@ -403,6 +691,24 @@ async def test_safety_timeout_blocks(monkeypatch: pytest.MonkeyPatch) -> None:assert policy.raw_secret_safety_failure_kind == "timeout"+async def test_safety_provider_failure_blocks() -> None:+ handler = AsyncMock(side_effect=RuntimeError("provider unavailable"))+ policy = await build_request_policy_trust_floor(+ user_message="Hello",+ workflow_yaml="",+ chat_history=[],+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert policy.raw_secret_safety_status == "blocked"+ assert policy.raw_secret_safety_failure_kind == "provider_error"+ assert policy.allow_update_workflow is False+ assert policy.allow_run_blocks is False++@pytest.mark.asyncioasync def test_canonical_safe_turn_is_the_only_agent_input() -> None:literal = "Hunter2Portal!"