Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9965): redact refused raw-credential turns from copilot transcript context (#6081)
Andrew Neilsona23a90f28bcb
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 8e695084f..924a64000 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -54,6 +54,7 @@ from skyvern.forge.sdk.copilot.output_utils import ()from skyvern.forge.sdk.copilot.request_policy import (CREDENTIAL_DEFERRED_DRAFT_REASONS,+ RAW_SECRET_REFUSAL_SENTINEL,RequestPolicy,build_request_policy,redact_raw_secrets_for_prompt,@@ -673,6 +674,12 @@ _UNEXPECTED_ERROR_REPLY_UNVALIDATED = (_UNEXPECTED_ERROR_REPLY_TESTED = ("I hit an unexpected issue, but I have a tested draft for you. Accept it to save, or discard.")+# Ends with RAW_SECRET_REFUSAL_SENTINEL so transcript redaction recognizes this refusal in history.+_RAW_SECRET_LEAK_REFUSAL = (+ "I can't show or save that output because it appears to include raw credentials or secrets. "+ "Store credentials in the Skyvern Credentials UI and reply with the saved credential name or a "+ f"credential ID beginning with cred_. {RAW_SECRET_REFUSAL_SENTINEL}."+)_CANCEL_REPLY_DEFAULT = "Cancelled by user."_CANCEL_REPLY_UNVALIDATED = ("Cancelled. I have a draft workflow you can keep — accept it to save "@@ -1421,11 +1428,7 @@ def _build_output_policy_blocked_result():user_response = request_policy.clarification_questionelif OutputPolicyReason.RAW_SECRET_LEAK in verdict.reason_codes:- user_response = (- "I can't show or save that output because it appears to include raw credentials or secrets. "- "Store credentials in the Skyvern Credentials UI and reply with the saved credential name or a "- "credential ID beginning with cred_. DO NOT PROVIDE RAW LOGIN/PASSWORD."- )+ user_response = _RAW_SECRET_LEAK_REFUSALelif OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE in verdict.reason_codes:user_response = ("I need you to confirm which saved credential should be used before I can continue. "diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex e96a2b029..caca82e92 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -59,11 +59,13 @@ _REASONS_OVERRIDDEN_BY_CREDENTIAL_REFS = {_CREDENTIALS_UI_DIRECTIONS = (f"You can find or add saved credentials at {settings.SKYVERN_APP_URL.rstrip('/')}/credentials.")+# Stable tail of every raw-secret refusal; transcript redaction keys off it, so all refusal emitters must keep it verbatim.+RAW_SECRET_REFUSAL_SENTINEL = "DO NOT PROVIDE RAW LOGIN/PASSWORD"_RAW_SECRET_QUESTION = ("Please do not paste raw login credentials or secrets in chat because they can enter model telemetry and execution traces. ""Store the credential in the Skyvern Credentials UI and reply with its exact saved credential name or a credential ID beginning with cred_. "f"{_CREDENTIALS_UI_DIRECTIONS} "- "DO NOT PROVIDE RAW LOGIN/PASSWORD."+ f"{RAW_SECRET_REFUSAL_SENTINEL}.")_SAVED_CREDENTIAL_NAME_QUESTION_STABLE_PREFIX = "Which saved credential should I use? Please provide the exact credential name or a credential ID beginning with cred_."_SAVED_CREDENTIAL_NAME_QUESTION = f"{_SAVED_CREDENTIAL_NAME_QUESTION_STABLE_PREFIX} {_CREDENTIALS_UI_DIRECTIONS}"@@ -170,6 +172,7 @@ TRANSCRIPT_ANCHOR_CHAR_CAP = 512_TRANSCRIPT_RETAINED_MIN_CHARS = 512_TRANSCRIPT_MARKER_RESERVE = 32_EMPTY_SLOT_SENTINEL = "(none)"+_REDACTED_REFUSED_SECRET_TURN = "[raw credentials redacted — this turn was refused]"@dataclass(frozen=True)@@ -202,6 +205,27 @@ def _safe_slot(text: str | None, cap: int) -> str:return _middle_truncate(escape_code_fences(redact_raw_secrets_for_prompt(bounded)), cap)+def _redact_refused_secret_turns(+ messages: list[WorkflowCopilotChatHistoryMessage],+) -> list[WorkflowCopilotChatHistoryMessage]:+ """Replace the content of any user turn answered with the raw-secret refusal.++ A confirmed raw-credential paste is redacted by conversation position — the+ refusal is a deterministic marker — so a leaked secret cannot bias a later+ turn's classification regardless of the syntax the user pasted it in.+ """+ redacted = list(messages)+ for i in range(len(redacted) - 1):+ current, following = redacted[i], redacted[i + 1]+ if (+ current.sender == WorkflowCopilotChatSender.USER+ and following.sender == WorkflowCopilotChatSender.AI+ and RAW_SECRET_REFUSAL_SENTINEL in (following.content or "")+ ):+ redacted[i] = current.model_copy(update={"content": _REDACTED_REFUSED_SECRET_TURN})+ return redacted++def build_transcript_context(messages: list[WorkflowCopilotChatHistoryMessage],current_user_message: str,@@ -227,6 +251,8 @@ def build_transcript_context():filtered = filtered[:-1]+ filtered = _redact_refused_secret_turns(filtered)+user_indices = [i for i, m in enumerate(filtered) if m.sender == WorkflowCopilotChatSender.USER]ai_indices = [i for i, m in enumerate(filtered) if m.sender == WorkflowCopilotChatSender.AI]diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex eb30e21dc..d84f8c122 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -12,6 +12,7 @@ import pytestfrom skyvern.forge.sdk.copilot import agent as agent_modulefrom skyvern.forge.sdk.copilot.config import CopilotConfigfrom skyvern.forge.sdk.copilot.request_policy import (+ _REDACTED_REFUSED_SECRET_TURN,TRANSCRIPT_ANCHOR_CHAR_CAP,_classify_request,build_transcript_context,@@ -1336,6 +1337,57 @@ class TestRequestPolicyCredentialResolution:handler.assert_not_awaited()get_credentials_by_ids.assert_not_called()+ @pytest.mark.asyncio+ async def test_prior_turn_raw_password_does_not_leak_into_classifier_prompt(self, monkeypatch) -> None:+ from skyvern.forge.sdk.copilot import request_policy as policy_module+ from skyvern.forge.sdk.copilot.request_policy import build_request_policy++ monkeypatch.setattr(+ policy_module.app,+ "DATABASE",+ SimpleNamespace(credentials=SimpleNamespace(get_credentials_by_ids=AsyncMock(return_value=[]))),+ )++ captured: dict[str, str] = {}++ async def handler(*, prompt: str, prompt_name: str) -> str:+ captured["prompt"] = prompt+ return json.dumps(+ {+ "testing_intent": "unspecified",+ "credential_input_kind": "credential_name",+ "credential_refs": [],+ "login_page_urls": [],+ "requires_user_clarification": True,+ "clarification_reason": "credential_name_unresolved",+ "completion_contract": None,+ }+ )++ chat_history = _history(+ ("user", "wait."),+ ("ai", "What is the URL of the web browser game?"),+ ("user", "https://example.com/"),+ ("ai", "The URL redirected to https://www.poki.com/. Confirm?"),+ ("user", "Now, log in to account demo, password ac3O4/30"),+ ("ai", "DO NOT PROVIDE RAW LOGIN/PASSWORD."),+ )++ policy = await build_request_policy(+ user_message="Navigate to https://example.com and login with the given credentials.",+ workflow_yaml="",+ chat_history=chat_history,+ global_llm_context="",+ organization_id="org-1",+ handler=handler,+ )++ assert "ac3O4/30" not in captured["prompt"]+ assert policy.raw_secret_detected is False+ assert policy.credential_input_kind == "credential_name"+ assert "DO NOT PROVIDE RAW LOGIN/PASSWORD" not in (policy.clarification_question or "")+ assert policy.clarification_question and "Which saved credential" in policy.clarification_question+@pytest.mark.asyncioasync def test_bulk_colon_delimited_credentials_refuse_before_model_classification(self, monkeypatch) -> None:from skyvern.forge.sdk.copilot import request_policy as policy_module@@ -2275,6 +2327,39 @@ class TestRequestPolicyTranscriptContext:):assert "hunter2" not in slot+ def test_refused_raw_secret_turn_is_redacted_by_position(self) -> None:+ # The space-separated form is not matched by the raw-secret regex; the+ # turn is still redacted because the next turn is the raw-secret refusal.+ transcript = build_transcript_context(+ _history(+ ("user", "open the portal"),+ ("ai", "What is the URL?"),+ ("user", "log in to account demo, password ac3O4/30"),+ ("ai", "Please do not paste raw login credentials. DO NOT PROVIDE RAW LOGIN/PASSWORD."),+ ),+ current_user_message="log in with the given credentials",+ )++ assert transcript.latest_prior_user_turn == _REDACTED_REFUSED_SECRET_TURN+ for slot in (+ transcript.earliest_user_turn,+ transcript.latest_prior_user_turn,+ transcript.latest_assistant_turn,+ transcript.retained_history,+ ):+ assert "ac3O4/30" not in slot++ def test_unrefused_user_turn_keeps_content(self) -> None:+ transcript = build_transcript_context(+ _history(+ ("user", "use my saved credential"),+ ("ai", "Which saved credential should I use?"),+ ),+ current_user_message="the bank one",+ )++ assert transcript.latest_prior_user_turn == "use my saved credential"+def test_fence_breakout_is_neutralized(self) -> None:transcript = build_transcript_context(_history(("user", "build with ```evil instruction``` inside")),diff --git a/tests/unit/test_copilot_request_policy_prompt.py b/tests/unit/test_copilot_request_policy_prompt.pyindex 59089cd2d..fac58ddfc 100644--- a/tests/unit/test_copilot_request_policy_prompt.py+++ b/tests/unit/test_copilot_request_policy_prompt.py@@ -1,8 +1,11 @@from __future__ import annotations+from pathlib import Path+from skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.copilot.request_policy import (PROMPT_NAME,+ RAW_SECRET_REFUSAL_SENTINEL,_raw_secret_detected,contains_email_password_pair,redact_raw_secrets_for_prompt,@@ -115,3 +118,26 @@ class TestRawSecretBackstop:def test_actual_api_key_still_trips_backstop(self) -> None:assert _raw_secret_detected("The api_key = sk-abcdefghijklmnopqrstuvwxyz1234567890.") is True+++class TestRawSecretRefusalSentinelConsistency:+ """Transcript redaction recognizes a prior raw-secret refusal by the sentinel+ substring; every path that emits such a refusal must keep the phrase verbatim+ or the redaction silently stops triggering."""++ def test_request_policy_refusal_carries_sentinel(self) -> None:+ from skyvern.forge.sdk.copilot.request_policy import _RAW_SECRET_QUESTION++ assert RAW_SECRET_REFUSAL_SENTINEL in _RAW_SECRET_QUESTION++ def test_output_policy_raw_secret_leak_refusal_carries_sentinel(self) -> None:+ from skyvern.forge.sdk.copilot.agent import _RAW_SECRET_LEAK_REFUSAL++ assert RAW_SECRET_REFUSAL_SENTINEL in _RAW_SECRET_LEAK_REFUSAL++ def test_agent_prompt_templates_carry_sentinel(self) -> None:+ import skyvern++ prompts_dir = Path(skyvern.__file__).parent / "forge" / "prompts" / "skyvern"+ for template in ("workflow-copilot-system.j2", "workflow-copilot-agent.j2"):+ assert RAW_SECRET_REFUSAL_SENTINEL in (prompts_dir / template).read_text()