Skip to content

Automate browser based workflows with AI

Stop another session's short secret from redacting unrelated log paths, status codes and durations (SKY-17742) (#8804)

Shuchang Zhengba32bfafcf26
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index 5dbc4dc4c..ef464fc81 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -50,6 +50,7 @@ from skyvern.forge.sdk.copilot.config import (
)
from skyvern.forge.sdk.copilot.screenshot_utils import PendingFrameLease, ScreenshotEntry, ViewportFrame
from skyvern.forge.sdk.copilot.secret_scrub import (
+ clear_session_scrub_values,
origin_runs_bound_to_scrubber,
register_matching_origin_run_redaction_values,
)
@@ -162,6 +163,9 @@ _ABANDONED_DRIVER_RELEASES: set[asyncio.Task[bool] | asyncio.Task[None]] = set()
# Per session: attached turns in this process, and the newest generation any of them attached.
# Only the last one out releases, and it retires that newest generation rather than its own.
_ATTACHED_TURNS_PER_SESSION: dict[str, tuple[int, BrowserState]] = {}
+# Closed sessions whose scrub values wait for the last attached turn's release: that turn may still be
+# scrubbing a readback it took before the close.
+_SCRUB_VALUES_CLEARED_ON_RELEASE: set[str] = set()
# Set while a last-out release has popped its ledger entry but the evict has not finished; an attach
# in that window would record the generation the evict is about to retire.
_DRIVER_RELEASES_IN_FLIGHT: dict[str, asyncio.Event] = {}
@@ -1473,9 +1477,18 @@ async def close_browser_session_quietly(
except Exception:
LOG.debug("Failed to close browser session", session_id=session_id, exc_info=True)
return False
+ _clear_closed_session_scrub_values(session_id)
return True
+def _clear_closed_session_scrub_values(session_id: str) -> None:
+ """Only a landed close, never a retired id: a retired session can still be alive and reattached."""
+ if session_id in _ATTACHED_TURNS_PER_SESSION:
+ _SCRUB_VALUES_CLEARED_ON_RELEASE.add(session_id)
+ else:
+ clear_session_scrub_values(session_id)
+
+
def _discard_finished_driver_release(task: asyncio.Task[bool] | asyncio.Task[None]) -> None:
_ABANDONED_DRIVER_RELEASES.discard(task)
if not task.cancelled():
@@ -1520,6 +1533,9 @@ async def release_browser_driver_quietly(
)
return
_ATTACHED_TURNS_PER_SESSION.pop(attached.session_id, None)
+ if attached.session_id in _SCRUB_VALUES_CLEARED_ON_RELEASE:
+ _SCRUB_VALUES_CLEARED_ON_RELEASE.discard(attached.session_id)
+ clear_session_scrub_values(attached.session_id)
manager = app.PERSISTENT_SESSIONS_MANAGER
if not manager.supports_evict_and_reconnect():
# This process drives the browser itself; detaching its driver would strand the browser
diff --git a/skyvern/forge/sdk/copilot/secret_scrub.py b/skyvern/forge/sdk/copilot/secret_scrub.py
index fdbf794b6..20e65c930 100644
--- a/skyvern/forge/sdk/copilot/secret_scrub.py
+++ b/skyvern/forge/sdk/copilot/secret_scrub.py
@@ -15,6 +15,8 @@ from urllib.parse import quote, quote_plus
import structlog
+from skyvern.forge.sdk.core import skyvern_context
+
if TYPE_CHECKING:
from skyvern.forge.sdk.copilot.runtime import AgentContext
@@ -23,16 +25,25 @@ LOG = structlog.get_logger()
REDACTED_SECRET_PLACEHOLDER = "[REDACTED_SECRET]"
_SESSION_SCRUB_VALUES: dict[str, list[str]] = {}
-# No deterministic per-session teardown exists, so FIFO-evict to bound worker memory.
+# The Copilot chats that registered or read each session's values, so the chat's later log lines own them
+# even after its context is replaced.
+_SESSION_SCRUB_CHAT_IDS: dict[str, set[str]] = {}
+# A session the Copilot closes is dropped then; one closed by anything else is only FIFO-evicted.
_MAX_SCRUB_SESSIONS = 1024
# Substring-replacing a short value into stored content corrupts it: a six-digit OTP occurs by
# chance inside ports, counts, and ids. Rewrites of the stored workflow apply this floor — both the
-# persisted row and the draft an edit anchors against, which have to stay the same string. Log
-# redaction does not, because over-redacting a log line is cheap and leaking a secret into one is not.
+# persisted row and the draft an edit anchors against, which have to stay the same string.
MIN_PERSISTED_REDACTION_LENGTH = 8
+# A log line replaces another session's values inside text only from this length, because shorter ones occur by
+# chance in nearly every path and id. Shorter ones still match as whole tokens, and the line's own request,
+# browser session and chat apply at any length.
+MIN_CROSS_SESSION_LOG_REDACTION_LENGTH = 4
+
_ALL_VALUES_CACHE: tuple[tuple[tuple[str, tuple[str, ...]], ...], list[str]] | None = None
+# That list split at the cross-session floor, keyed on the list's identity, which changes only with the registry.
+_LOG_SPLIT_CACHE: tuple[list[str], int, list[str], frozenset[str]] | None = None
def _registry_fingerprint() -> tuple[tuple[str, tuple[str, ...]], ...]:
@@ -52,6 +63,17 @@ def _session_id(ctx: AgentContext) -> str | None:
return session_id if isinstance(session_id, str) and session_id else None
+def _nonempty(value: object) -> str | None:
+ # A context id can carry log provenance as a str subclass; registry keys stay plain strings.
+ return str.__str__(value) if isinstance(value, str) and value else None
+
+
+def _owned_scrub_values(context: object) -> set[str]:
+ # Log redaction fails closed on any error, so a partial context (a test double) must not raise here.
+ values = getattr(context, "copilot_scrub_values", None)
+ return values if isinstance(values, set) else set()
+
+
# encodeURIComponent leaves these literal where Python's quote does not.
_ENCODE_URI_COMPONENT_SAFE = "-_.!~*'()"
@@ -84,16 +106,23 @@ def register_secret_scrub_value(ctx: AgentContext, value: str | None) -> None:
values = getattr(ctx, "secret_scrub_values", None)
session_id = _session_id(ctx)
new_session = session_id is not None and session_id not in _SESSION_SCRUB_VALUES
- for variant in encoded_secret_variants(value):
+ variants = encoded_secret_variants(value)
+ for variant in variants:
if isinstance(values, list) and variant not in values:
values.append(variant)
if session_id is not None:
session_values = _SESSION_SCRUB_VALUES.setdefault(session_id, [])
if variant not in session_values:
session_values.append(variant)
+ context = skyvern_context.current()
+ if context is not None:
+ _owned_scrub_values(context).update(variants)
+ chat_id = _nonempty(getattr(context, "copilot_session_id", None))
+ if session_id is not None and chat_id is not None:
+ _SESSION_SCRUB_CHAT_IDS.setdefault(session_id, set()).add(chat_id)
if new_session:
while len(_SESSION_SCRUB_VALUES) > _MAX_SCRUB_SESSIONS:
- _SESSION_SCRUB_VALUES.pop(next(iter(_SESSION_SCRUB_VALUES)))
+ clear_session_scrub_values(next(iter(_SESSION_SCRUB_VALUES)))
def register_secret_scrub_values_from_structure(ctx: AgentContext, obj: Any) -> None:
@@ -159,16 +188,47 @@ def origin_runs_bound_to_scrubber(ctx: AgentContext) -> set[str]:
def clear_session_scrub_values(session_id: str | None) -> None:
if isinstance(session_id, str):
_SESSION_SCRUB_VALUES.pop(session_id, None)
+ _SESSION_SCRUB_CHAT_IDS.pop(session_id, None)
+
+
+def log_scrub_values() -> tuple[list[str], frozenset[str]]:
+ """A log line's values to replace inside text, longest first (its own request's, browser session's and chat's at
+ any length, other sessions' from ``MIN_CROSS_SESSION_LOG_REDACTION_LENGTH``), and every shorter registered value,
+ which it matches only as a whole token."""
+ cross_session, short = _cross_session_log_split(all_registered_secret_values())
+ owned: set[str] = set()
+ context = skyvern_context.current()
+ if context is not None:
+ owned.update(_owned_scrub_values(context))
+ browser_session_id = _nonempty(getattr(context, "browser_session_id", None))
+ if browser_session_id is not None:
+ owned.update(_SESSION_SCRUB_VALUES.get(browser_session_id, ()))
+ chat_id = _nonempty(getattr(context, "copilot_session_id", None))
+ if chat_id is not None:
+ for session_id, chat_ids in list(_SESSION_SCRUB_CHAT_IDS.items()):
+ if chat_id in chat_ids:
+ owned.update(_SESSION_SCRUB_VALUES.get(session_id, ()))
+ if not owned:
+ return cross_session, short
+ values = owned.union(cross_session)
+ return sorted((value for value in values if isinstance(value, str) and value), key=len, reverse=True), short
+
+
+def _cross_session_log_split(values: list[str]) -> tuple[list[str], frozenset[str]]:
+ global _LOG_SPLIT_CACHE
+ floor = MIN_CROSS_SESSION_LOG_REDACTION_LENGTH
+ cached = _LOG_SPLIT_CACHE
+ if cached is not None and cached[0] is values and cached[1] == floor:
+ return cached[2], cached[3]
+ cross_session = [value for value in values if len(value) >= floor]
+ short = frozenset(value for value in values if len(value) < floor)
+ _LOG_SPLIT_CACHE = (values, floor, cross_session, short)
+ return cross_session, short
def all_registered_secret_values() -> list[str]:
- """Every credential value registered by any session in this process, longest first.
-
- The log seam has no ``AgentContext`` to scope against — a credential value must never reach
- log output regardless of which session filled it. Callers that rewrite persisted content must
- use ``registered_scrub_values`` instead: replacing across sessions there would let one session's
- short value corrupt another's stored data.
- """
+ """Every credential value registered by any session in this process, longest first. Rewrites of persisted
+ content use ``registered_scrub_values`` instead, so one session's short value cannot corrupt another's data."""
global _ALL_VALUES_CACHE
fingerprint = _registry_fingerprint()
cached = _ALL_VALUES_CACHE
@@ -194,7 +254,18 @@ def _registered_scrub_values(ctx: AgentContext) -> list[str]:
merged.extend(value for value in values if isinstance(value, str) and value)
session_id = _session_id(ctx)
if session_id is not None:
- merged.extend(value for value in _SESSION_SCRUB_VALUES.get(session_id, []) if isinstance(value, str) and value)
+ session_values = [
+ value for value in _SESSION_SCRUB_VALUES.get(session_id, []) if isinstance(value, str) and value
+ ]
+ # A turn reading values an earlier one filled can log them too, so they become this request's own, and
+ # its chat's, which survives the context prepare_workflow swaps in.
+ context = skyvern_context.current()
+ if context is not None:
+ _owned_scrub_values(context).update(session_values)
+ chat_id = _nonempty(getattr(context, "copilot_session_id", None))
+ if session_values and chat_id is not None:
+ _SESSION_SCRUB_CHAT_IDS.setdefault(session_id, set()).add(chat_id)
+ merged.extend(session_values)
# Longest first so an overlapping shorter value never splits a longer one.
return sorted(set(merged), key=len, reverse=True)
@@ -206,12 +277,13 @@ def scrub_secrets_from_text(ctx: AgentContext, text: str) -> str:
def scrub_all_registered_from_text(text: str) -> str:
- """Session-agnostic counterpart to ``scrub_secrets_from_text`` for reporting seams.
-
- Exception text is serialized where no ``AgentContext`` is in scope, so this scrubs against
- every session's values for the same reason ``all_registered_secret_values`` does.
- """
- for value in all_registered_secret_values():
+ """Session-agnostic counterpart to ``scrub_secrets_from_text`` for span exceptions and model-facing text:
+ every session's values at any length, unlike a log line, plus the current request's own."""
+ values = all_registered_secret_values()
+ owned = _owned_scrub_values(skyvern_context.current())
+ if owned:
+ values = sorted(owned.union(values), key=len, reverse=True)
+ for value in values:
text = text.replace(value, REDACTED_SECRET_PLACEHOLDER)
return text
diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.py
index d6f2efecb..6d4b57f82 100644
--- a/skyvern/forge/sdk/core/skyvern_context.py
+++ b/skyvern/forge/sdk/core/skyvern_context.py
@@ -720,6 +720,9 @@ class SkyvernContext:
# scoped so bare tasks with no workflow-run context are still redacted; unioned into
# WorkflowContextManager.get_secret_values_for_run, which both redaction consumers read.
runtime_secret_values: set[str] = field(default_factory=builtins.set)
+ # Copilot credential values this request filled or scrubbed with. Its log lines and span exceptions
+ # scrub them at any length, while other sessions' values reach its log lines only above a length floor.
+ copilot_scrub_values: set[str] = field(default_factory=builtins.set, repr=False)
# Subset of runtime_secret_values that must also never reach the model's own view of tool
# output (e.g. a magic sign-in link), as opposed to values the model needs to read (e.g. a TOTP
# code) that are only scrubbed from artifacts/logs.
diff --git a/skyvern/forge/sdk/forge_log.py b/skyvern/forge/sdk/forge_log.py
index 045533ffb..390f7e0ea 100644
--- a/skyvern/forge/sdk/forge_log.py
+++ b/skyvern/forge/sdk/forge_log.py
@@ -1,5 +1,6 @@
import inspect
import logging
+import math
import random
import re
import sys
@@ -10,6 +11,7 @@ from contextvars import ContextVar
from copy import deepcopy
from dataclasses import dataclass
from decimal import Decimal
+from functools import lru_cache
from pathlib import Path
from types import TracebackType
from typing import Any, Callable, Iterator, SupportsIndex, TypeGuard, overload
@@ -969,6 +971,59 @@ def render_bounded_json(logger: logging.Logger, method_name: str, event_dict: Ev
)
+def _number_log_forms(node: int | float | Decimal) -> set[str]:
+ # The JSON renderer emits a Decimal as a float, and an integral float reads as its int too.
+ forms = {str(node)}
+ try:
+ as_float = float(node)
+ forms.add(str(as_float))
+ if math.isfinite(as_float) and as_float.is_integer():
+ forms.add(str(int(node)))
+ except (OverflowError, ValueError):
+ pass
+ return forms
+
+
+# Another session's short value is chance inside an id, path, date or decimal, so it matches only a whole token:
+# a letter, digit or "_" joins, as does ".", ":" or "-" beside a digit, but a %XX, \n, \r, \t or \uXXXX escape separates.
+_TOKEN_LEFT = r"(?:(?<![A-Za-z0-9_])(?<![0-9][.:-])|(?<=%[0-9A-Fa-f]{2})|(?<=\\[nrt])|(?<=\\u[0-9A-Fa-f]{4}))"
+_TOKEN_RIGHT = r"(?![A-Za-z0-9_])(?![.:-][0-9])"
+_SHORT_TOKEN = re.compile(rf"{_TOKEN_LEFT}[A-Za-z0-9]{{1,3}}{_TOKEN_RIGHT}")
+_SHORT_TOKEN_VALUE = re.compile(r"[A-Za-z0-9]{1,3}")
+
+
+def _is_token_char(char: str) -> bool:
+ return char.isascii() and char.isalnum()
+
+
+@lru_cache(maxsize=1)
+def _short_value_scrubber(values: frozenset[str], placeholder: str) -> Callable[[str], str] | None:
+ if not values:
+ return None
+ tokens = frozenset(value for value in values if _SHORT_TOKEN_VALUE.fullmatch(value))
+ others = sorted(values - tokens, key=len, reverse=True)
+ # A value with punctuation, such as "#7", gets a boundary only on an alphanumeric edge, so "x#7" still matches.
+ edged = (
+ re.compile(
+ "|".join(
+ (_TOKEN_LEFT if _is_token_char(value[0]) else "")
+ + re.escape(value)
+ + (_TOKEN_RIGHT if _is_token_char(value[-1]) else "")
+ for value in others
+ )
+ )
+ if others
+ else None
+ )
+
+ def scrub(text: str) -> str:
+ if tokens:
+ text = _SHORT_TOKEN.sub(lambda match: placeholder if match.group() in tokens else match.group(), text)
+ return text if edged is None else edged.sub(placeholder, text)
+
+ return scrub
+
+
def _registered_secret_scrubber(
*, logging_envelope: bool = False, protocol_level: str | None = None
) -> Callable[[Any, int], Any] | None:
@@ -976,11 +1031,14 @@ def _registered_secret_scrubber(
# existing scrub helpers; never initialize the app or log from this processor.
from skyvern.forge.sdk.copilot.secret_scrub import (
REDACTED_SECRET_PLACEHOLDER,
- all_registered_secret_values,
encoded_secret_variants,
+ log_scrub_values,
)
- variants = set(all_registered_secret_values())
+ copilot_values, short_values = log_scrub_values()
+ copilot_variants = set(copilot_values)
+ # The line's own values are already replaced as text by then, so this pass needs no exclusion for them.
+ short_scrub = _short_value_scrubber(short_values, REDACTED_SECRET_PLACEHOLDER)
registered: set[str] = set()
context = skyvern_context.current()
if context is not None:
@@ -1005,12 +1063,13 @@ def _registered_secret_scrubber(
registered.update(
value for value in run_context.secrets.values() if isinstance(value, str) and value
)
+ run_variants: set[str] = set()
for value in registered:
if isinstance(value, str) and value:
- variants.update(encoded_secret_variants(value))
- if not variants:
+ run_variants.update(encoded_secret_variants(value))
+ if not copilot_variants and not run_variants and short_scrub is None:
return None
- secrets = sorted(variants, key=len, reverse=True)
+ secrets = sorted(copilot_variants | run_variants, key=len, reverse=True)
seen: set[int] = set()
remaining = 10_000
@@ -1024,16 +1083,18 @@ def _registered_secret_scrubber(
if isinstance(node, str):
for secret in secrets:
node = node.replace(secret, REDACTED_SECRET_PLACEHOLDER)
- return node
+ return node if short_scrub is None else short_scrub(node)
if node is None or isinstance(node, bool):
return node
if isinstance(node, (int, float, Decimal)):
- # Keep ordinary numbers numeric. Decimal is emitted as a float by
- # the JSON renderer, so check that representation as well.
- text = str(node)
- if isinstance(node, Decimal):
- text += " " + str(float(node))
- return REDACTED_SECRET_PLACEHOLDER if any(secret in text for secret in secrets) else node
+ # A Copilot value replaces only a number that is the value, since a short one among a duration's
+ # digits is chance. The run's own secrets replace any number that contains them.
+ forms = _number_log_forms(node)
+ if not forms.isdisjoint(copilot_variants) or any(
+ secret in form for form in forms for secret in run_variants
+ ):
+ return REDACTED_SECRET_PLACEHOLDER
+ return node
marker = id(node)
if marker in seen:
diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py
index 6f3d7eaf0..073b633ff 100644
--- a/tests/unit/conftest.py
+++ b/tests/unit/conftest.py
@@ -191,6 +191,7 @@ def reset_copilot_driver_ledgers() -> Iterator[None]:
runtime._ATTACHED_TURNS_PER_SESSION.clear()
runtime._DRIVER_RELEASES_IN_FLIGHT.clear()
runtime._DRIVER_RELEASE_EPOCHS.clear()
+ runtime._SCRUB_VALUES_CLEARED_ON_RELEASE.clear()
_clear()
yield
diff --git a/tests/unit/test_api_handler_factory.py b/tests/unit/test_api_handler_factory.py
index df23fc99f..c803cb8df 100644
--- a/tests/unit/test_api_handler_factory.py
+++ b/tests/unit/test_api_handler_factory.py
@@ -665,7 +665,8 @@ async def test_llm_caller_preserves_generated_cost_and_tokens_during_real_redact
assert REDACTED_SECRET_PLACEHOLDER in accounting["prompt_name"]
usage = next(record for record in records if record.get("log_code") == "copilot_model_usage")
assert REDACTED_SECRET_PLACEHOLDER in usage["copilot.prompt_name"]
- assert records[-1]["llm_cost"] == records[-1]["input_tokens"] == REDACTED_SECRET_PLACEHOLDER
+ # Another session's short values never match inside a caller's numbers.
+ assert records[-1]["llm_cost"] == 0.25 and records[-1]["input_tokens"] == 7
@pytest.mark.asyncio
diff --git a/tests/unit/test_copilot_secret_scrub.py b/tests/unit/test_copilot_secret_scrub.py
index 45f3337b9..5bb716b5e 100644
--- a/tests/unit/test_copilot_secret_scrub.py
+++ b/tests/unit/test_copilot_secret_scrub.py
@@ -10,12 +10,13 @@ import html
import json
from collections.abc import Callable, Iterator
from typing import Any
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock
from urllib.parse import quote
import pytest
-from skyvern.forge.sdk.copilot import mcp_adapter, secret_scrub
+from skyvern.forge import app
+from skyvern.forge.sdk.copilot import mcp_adapter, runtime, secret_scrub
from skyvern.forge.sdk.copilot.agent import _MCP_RESULT_SECURITY_BOUNDARY
from skyvern.forge.sdk.copilot.mcp_adapter import PageStateReader, SchemaOverlay, SkyvernOverlayMCPServer
from skyvern.forge.sdk.copilot.output_utils import (
@@ -44,8 +45,10 @@ _FAKE_OTP = "392817"
@pytest.fixture(autouse=True)
def _isolate_session_scrub_registry() -> Iterator[None]:
secret_scrub._SESSION_SCRUB_VALUES.clear()
+ secret_scrub._SESSION_SCRUB_CHAT_IDS.clear()
yield
secret_scrub._SESSION_SCRUB_VALUES.clear()
+ secret_scrub._SESSION_SCRUB_CHAT_IDS.clear()
def _agent_ctx(browser_session_id: str = "pbs_1") -> AgentContext:
@@ -289,6 +292,42 @@ class TestCrossTurnSessionScrub:
turn2 = _agent_ctx()
assert scrub_secrets_from_text(turn2, f"value {_FAKE_PASSWORD}") == f"value {_FAKE_PASSWORD}"
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("close_lands", [True, False])
+ async def test_closing_a_session_drops_only_its_values(
+ self, monkeypatch: pytest.MonkeyPatch, close_lands: bool
+ ) -> None:
+ manager = MagicMock()
+ manager.close_session = AsyncMock(side_effect=None if close_lands else RuntimeError("backend down"))
+ monkeypatch.setattr(app, "PERSISTENT_SESSIONS_MANAGER", manager)
+ register_secret_scrub_value(_agent_ctx("pbs_closed"), _FAKE_PASSWORD)
+ register_secret_scrub_value(_agent_ctx("pbs_live"), _FAKE_OTP)
+
+ assert await runtime.close_browser_session_quietly("o_1", "pbs_closed") is close_lands
+
+ closed_readback = scrub_secrets_from_text(_agent_ctx("pbs_closed"), _FAKE_PASSWORD)
+ assert (closed_readback == _FAKE_PASSWORD) is close_lands
+ assert scrub_secrets_from_text(_agent_ctx("pbs_live"), _FAKE_OTP) == REDACTED_SECRET_PLACEHOLDER
+
+ @pytest.mark.asyncio
+ async def test_a_session_closed_under_an_attached_turn_keeps_its_values_until_that_turn_releases(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ manager = MagicMock()
+ manager.close_session = AsyncMock()
+ manager.supports_evict_and_reconnect = MagicMock(return_value=False)
+ monkeypatch.setattr(app, "PERSISTENT_SESSIONS_MANAGER", manager)
+ turn = _agent_ctx("pbs_1")
+ runtime.record_attached_browser_driver(turn, "pbs_1", MagicMock())
+ register_secret_scrub_value(turn, _FAKE_PASSWORD)
+
+ assert await runtime.close_browser_session_quietly("o_1", "pbs_1") is True
+ # A readback the turn took before the close is still scrubbed.
+ assert scrub_secrets_from_text(_agent_ctx("pbs_1"), _FAKE_PASSWORD) == REDACTED_SECRET_PLACEHOLDER
+
+ await runtime.release_browser_driver_quietly("o_1", turn.attached_browser_drivers["pbs_1"])
+ assert scrub_secrets_from_text(_agent_ctx("pbs_1"), _FAKE_PASSWORD) == _FAKE_PASSWORD
+
def test_session_registry_is_bounded_and_evicts_oldest(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(secret_scrub, "_MAX_SCRUB_SESSIONS", 3)
for i in range(5):
diff --git a/tests/unit/test_forge_log_redaction.py b/tests/unit/test_forge_log_redaction.py
index b9deb7989..23e2cc4f7 100644
--- a/tests/unit/test_forge_log_redaction.py
+++ b/tests/unit/test_forge_log_redaction.py
@@ -1,4 +1,5 @@
import asyncio
+import contextlib
import copy
import enum
import io
@@ -14,7 +15,7 @@ from datetime import datetime
from decimal import Decimal
from logging.handlers import BufferingHandler
from types import MappingProxyType, ModuleType
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, MagicMock
from urllib.parse import quote
import pytest
@@ -34,7 +35,13 @@ from skyvern.forge.log_redaction import (
from skyvern.forge.sdk import forge_log, log_artifacts
from skyvern.forge.sdk.artifact.models import ArtifactType
from skyvern.forge.sdk.copilot import secret_scrub
-from skyvern.forge.sdk.copilot.secret_scrub import REDACTED_SECRET_PLACEHOLDER
+from skyvern.forge.sdk.copilot.runtime import AgentContext
+from skyvern.forge.sdk.copilot.secret_scrub import (
+ REDACTED_SECRET_PLACEHOLDER,
+ clear_session_scrub_values,
+ register_secret_scrub_value,
+ scrub_secrets_from_text,
+)
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.forge_log import (
@@ -166,14 +173,40 @@ def test_proxy_field_families_render_by_semantics() -> None:
@pytest.fixture(autouse=True)
def _isolate_session_scrub_registry() -> Iterator[None]:
secret_scrub._SESSION_SCRUB_VALUES.clear()
+ secret_scrub._SESSION_SCRUB_CHAT_IDS.clear()
yield
secret_scrub._SESSION_SCRUB_VALUES.clear()
+ secret_scrub._SESSION_SCRUB_CHAT_IDS.clear()
def _register_credential(value: str) -> None:
secret_scrub._SESSION_SCRUB_VALUES.setdefault("pbs_1", []).append(value)
+def _copilot_turn(browser_session_id: str = "pbs_1") -> AgentContext:
+ return AgentContext(
+ organization_id="o_1",
+ workflow_id="w_1",
+ workflow_permanent_id="wpid_1",
+ workflow_yaml="",
+ browser_session_id=browser_session_id,
+ stream=MagicMock(),
+ )
+
+
+def _register_in_copilot_turn(*values: str) -> SkyvernContext:
+ """Register values the way a Copilot tool does, inside its own request; returns that request."""
+ request = SkyvernContext(request_id="req_copilot", copilot_session_id="wcc_1")
+ with skyvern_context.scoped(request):
+ for value in values:
+ register_secret_scrub_value(_copilot_turn(), value)
+ return request
+
+
+def _request_log_line() -> dict[str, object]:
+ return {"event": "request", "path": "/v1/credentials", "status_code": 401, "duration_seconds": 0.0123, "attempt": 1}
+
+
def test_redacts_url_encoded_bearer_token() -> None:
event = {
"event": "WebSocket /v1/stream/vnc/browser_session/pbs_xxx?token=Bearer%20eyJhbGciOiJSUzI1NiI&client_id=abc"
@@ -275,6 +308,105 @@ def test_redacts_a_credential_inside_a_tuple_value() -> None:
assert out["pair"] == ("user", REDACTED_SECRET_PLACEHOLDER)
+def test_a_short_value_from_another_session_leaves_unrelated_request_lines_alone() -> None:
+ _register_in_copilot_turn("1")
+ line = _request_log_line()
+
+ with skyvern_context.scoped(SkyvernContext(request_id="req_unrelated", browser_session_id="pbs_2")):
+ out = redact_registered_secrets(None, "info", dict(line)) # type: ignore[arg-type]
+
+ assert out == line
+
+
+ "scope",
+ [
+ "registering_request",
+ "registering_request_after_close",
+ "same_browser_session",
+ "same_chat",
+ "reader",
+ "reader_after_context_replaced",
+ ],
+)
+def test_a_short_value_is_still_scrubbed_on_its_own_sessions_lines(scope: str) -> None:
+ request = _register_in_copilot_turn("1")
+ if scope == "registering_request_after_close":
+ clear_session_scrub_values("pbs_1")
+ line_context = {
+ "registering_request": request,
+ "registering_request_after_close": request,
+ "same_browser_session": SkyvernContext(request_id="req_run", browser_session_id="pbs_1"),
+ "same_chat": SkyvernContext(request_id="req_next_turn", copilot_session_id="wcc_1"),
+ "reader": SkyvernContext(request_id="req_other_chat", copilot_session_id="wcc_2"),
+ "reader_after_context_replaced": SkyvernContext(request_id="req_other_chat", copilot_session_id="wcc_2"),
+ }[scope]
+
+ with skyvern_context.scoped(line_context):
+ if scope.startswith("reader"):
+ # Another chat's turn on the same browser scrubs a readback with the session's values.
+ scrub_secrets_from_text(_copilot_turn(), "readback")
+ if scope == "reader":
+ clear_session_scrub_values("pbs_1")
+ if scope == "reader_after_context_replaced":
+ # prepare_workflow swaps in a context that keeps only the request's chat id.
+ skyvern_context.replace(SkyvernContext(request_id="req_other_chat", copilot_session_id="wcc_2"))
+ out = redact_registered_secrets(None, "info", _request_log_line()) # type: ignore[arg-type]
+
+ assert out["path"] == f"/v{REDACTED_SECRET_PLACEHOLDER}/credentials"
+ assert out["attempt"] == REDACTED_SECRET_PLACEHOLDER
+ assert out["status_code"] == 401
+
+
+def test_a_value_from_another_session_applies_from_four_characters() -> None:
+ _register_in_copilot_turn(_FAKE_CREDENTIAL, "q7x", "z9k4", "48392017")
+ event = {"event": f"login failed for {_FAKE_CREDENTIAL}", "codes": "aq7xb xz9k4x", "code": 48392017}
+
+ with skyvern_context.scoped(SkyvernContext(request_id="req_unrelated")):
+ out = redact_registered_secrets(None, "info", event) # type: ignore[arg-type]
+
+ assert out["event"] == f"login failed for {REDACTED_SECRET_PLACEHOLDER}"
+ assert out["codes"] == f"aq7xb x{REDACTED_SECRET_PLACEHOLDER}x"
+ assert out["code"] == REDACTED_SECRET_PLACEHOLDER
+
+
[email protected]("request_context", [None, SkyvernContext(request_id="req_unrelated")])
+def test_a_short_value_from_another_session_is_still_scrubbed_as_a_whole_token(
+ request_context: SkyvernContext | None,
+) -> None:
+ _register_in_copilot_turn("123", "401", "1", "10", "45", "#7")
+ masked = REDACTED_SECRET_PLACEHOLDER
+ scrubbed = {
+ '{"value":"123","cvv": 123,"status":"401"}': f'{{"value":"{masked}","cvv": {masked},"status":"{masked}"}}',
+ "%22123%22": f"%22{masked}%22",
+ "line\\n123": f"line\\n{masked}",
+ "\\u0022123\\u0022": f"\\u0022{masked}\\u0022",
+ "code=x#7": f"code=x{masked}",
+ }
+ kept = ["a123", "0.123", "123.45", "10.0.0.1", "/v1/credentials", "block_1", "2026-10-04T00:45:48.123Z"]
+ event = {"event": "api.raw_request", "scrubbed": list(scrubbed), "kept": kept, "status_code": 401}
+
+ with skyvern_context.scoped(request_context) if request_context else contextlib.nullcontext():
+ out = redact_registered_secrets(None, "info", event) # type: ignore[arg-type]
+
+ assert out["scrubbed"] == list(scrubbed.values())
+ assert out["kept"] == kept
+ assert out["status_code"] == 401
+
+
+def test_a_copilot_value_replaces_only_its_own_number_and_a_run_secret_any_number_containing_it() -> None:
+ _register_in_copilot_turn("483920")
+ copilot_numbers = [483920, 483920.0, Decimal("483920"), 1483920, 4839201, 0.48392, 48392, 401]
+ run_numbers = [15551234567, 0.5551234567, Decimal("5551234567.25")]
+ event = {"event": "numbers", "copilot": copilot_numbers, "run": run_numbers}
+
+ with skyvern_context.scoped(SkyvernContext(request_id="req_run", runtime_secret_values={"5551234567"})):
+ out = redact_registered_secrets(None, "info", event) # type: ignore[arg-type]
+
+ assert out["copilot"] == [REDACTED_SECRET_PLACEHOLDER] * 3 + copilot_numbers[3:]
+ assert out["run"] == [REDACTED_SECRET_PLACEHOLDER] * 3
+
+
@pytest.fixture
def registered_log_stream(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest) -> Iterator[io.StringIO]:
monkeypatch.setattr(settings, "JSON_LOGGING", getattr(request, "param", True))
@@ -902,6 +1034,8 @@ async def test_generated_metadata_survives_short_secrets_and_actual_artifact_sel
def test_plain_metadata_names_and_copied_generated_values_are_still_caller_data(
monkeypatch: pytest.MonkeyPatch, registered_log_stream: io.StringIO, with_context: bool
) -> None:
+ # Provenance, not the cross-session floor, is under test: let these short values apply anywhere.
+ monkeypatch.setattr(secret_scrub, "MIN_CROSS_SESSION_LOG_REDACTION_LENGTH", 1)
_register_credential("123")
_register_credential("id")
context = SkyvernContext(workflow_run_id="wr_123", task_id="tsk_123") if with_context else None
diff --git a/tests/unit/test_span_exception_redaction.py b/tests/unit/test_span_exception_redaction.py
index 13ad45a05..d0b4a1c6d 100644
--- a/tests/unit/test_span_exception_redaction.py
+++ b/tests/unit/test_span_exception_redaction.py
@@ -18,6 +18,7 @@ from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanE
from skyvern.forge.sdk.copilot import secret_scrub
from skyvern.forge.sdk.copilot.secret_scrub import REDACTED_SECRET_PLACEHOLDER
+from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.trace import record_span_exception, traced_span
_REGISTERED_CREDENTIAL = "fake-span-pa55w0rd-4b7e2d"
@@ -85,6 +86,22 @@ def test_credential_is_redacted_from_the_span_error_status(registered_credential
assert REDACTED_SECRET_PLACEHOLDER in blob
+def test_a_short_credential_is_redacted_from_a_span_exception_with_no_context() -> None:
+ # Log lines apply another session's values only from four characters; span exceptions apply them at any length.
+ short_credential = "q7x"
+ secret_scrub._SESSION_SCRUB_VALUES.clear()
+ secret_scrub._SESSION_SCRUB_VALUES["pbs_other"] = [short_credential]
+ try:
+ assert skyvern_context.current() is None
+ events, descriptions = _record_and_export(short_credential)
+ finally:
+ secret_scrub._SESSION_SCRUB_VALUES.clear()
+
+ exported = events + "".join(d or "" for d in descriptions)
+ assert short_credential not in exported
+ assert REDACTED_SECRET_PLACEHOLDER in exported
+
+
def test_span_exception_stays_useful_for_debugging(registered_credential: str) -> None:
events, _ = _record_and_export(registered_credential)
assert "RuntimeError" in events