Skip to content

Automate browser based workflows with AI

fix(SKY-8861): close CodeQL incomplete-url-substring-sanitization in recording tests (#5687)

Shuchang Zhengc38ff9bb297a
diff --git a/skyvern/forge/sdk/artifact/manager.py b/skyvern/forge/sdk/artifact/manager.py
index 367922dac..8d6d1c765 100644
--- a/skyvern/forge/sdk/artifact/manager.py
+++ b/skyvern/forge/sdk/artifact/manager.py
@@ -350,11 +350,57 @@ class ArtifactManager:
``claim_session_download_artifacts_for_run`` UPDATE to tag rows
whose ``created_at`` falls inside the run's window.
"""
- existing = await app.DATABASE.artifacts.find_artifact_for_browser_session(
+ return await self._create_browser_session_artifact(
organization_id=organization_id,
browser_session_id=browser_session_id,
uri=uri,
+ filename=filename,
artifact_type=ArtifactType.DOWNLOAD,
+ checksum=checksum,
+ )
+
+ async def create_browser_session_recording_artifact(
+ self,
+ *,
+ organization_id: str,
+ browser_session_id: str,
+ uri: str,
+ filename: str,
+ checksum: str | None = None,
+ ) -> str:
+ """Register a session-scoped recording (video) as a RECORDING Artifact row.
+
+ Mirrors :meth:`create_browser_session_download_artifact`. Called from
+ ``S3Storage.sync_browser_session_file(artifact_type="videos")`` once
+ Playwright finalizes the recording at session close. Idempotent on
+ ``(organization_id, browser_session_id, uri)`` — re-runs of the
+ end-of-session sync are safe.
+ """
+ return await self._create_browser_session_artifact(
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ uri=uri,
+ filename=filename,
+ artifact_type=ArtifactType.RECORDING,
+ checksum=checksum,
+ )
+
+ async def _create_browser_session_artifact(
+ self,
+ *,
+ organization_id: str,
+ browser_session_id: str,
+ uri: str,
+ filename: str,
+ artifact_type: ArtifactType,
+ checksum: str | None = None,
+ ) -> str:
+ """Shared idempotent insert keyed on ``(browser_session_id, uri, artifact_type)``."""
+ existing = await app.DATABASE.artifacts.find_artifact_for_browser_session(
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ uri=uri,
+ artifact_type=artifact_type,
)
if existing is not None:
return existing.artifact_id
@@ -362,17 +408,18 @@ class ArtifactManager:
artifact_id = generate_artifact_id()
await app.DATABASE.artifacts.create_artifact(
artifact_id=artifact_id,
- artifact_type=ArtifactType.DOWNLOAD,
+ artifact_type=artifact_type,
uri=uri,
organization_id=organization_id,
browser_session_id=browser_session_id,
checksum=checksum,
)
LOG.debug(
- "Registered session-scoped downloaded file as artifact",
+ "Registered session-scoped artifact",
artifact_id=artifact_id,
browser_session_id=browser_session_id,
filename=filename,
+ artifact_type=artifact_type.value,
)
return artifact_id
diff --git a/skyvern/forge/sdk/artifact/storage/azure.py b/skyvern/forge/sdk/artifact/storage/azure.py
index 461cae955..ced3e6c87 100644
--- a/skyvern/forge/sdk/artifact/storage/azure.py
+++ b/skyvern/forge/sdk/artifact/storage/azure.py
@@ -23,6 +23,7 @@ from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType, LogEntityT
from skyvern.forge.sdk.artifact.storage.base import (
FILE_EXTENTSION_MAP,
BaseStorage,
+ _file_infos_from_artifacts,
_file_infos_from_download_artifacts,
)
from skyvern.forge.sdk.models import Step
@@ -369,19 +370,58 @@ class AzureStorage(BaseStorage):
in_progress=True,
)
- async def list_recordings_in_browser_session(self, organization_id: str, browser_session_id: str) -> list[str]:
- """List all recording files for a browser session from Azure."""
- uri = f"azure://{settings.AZURE_STORAGE_CONTAINER_ARTIFACTS}/v1/{settings.ENV}/{organization_id}/browser_sessions/{browser_session_id}/videos"
- return [
- f"azure://{settings.AZURE_STORAGE_CONTAINER_ARTIFACTS}/{file}"
- for file in await self.async_client.list_files(uri=uri)
- ]
-
async def get_shared_recordings_in_browser_session(
self, organization_id: str, browser_session_id: str
) -> list[FileInfo]:
- """Get recording files with SAS URLs for a browser session."""
- object_keys = await self.list_recordings_in_browser_session(organization_id, browser_session_id)
+ """Get recording files for a browser session.
+
+ Artifact-first when the keyring is configured — see s3.py for the
+ rationale. Falls back to direct Azure LIST + SAS URLs for legacy
+ sessions and OSS-default deployments.
+ """
+ if settings.ARTIFACT_CONTENT_HMAC_KEYRING:
+ try:
+ artifacts = await app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type(
+ browser_session_id=browser_session_id,
+ organization_id=organization_id,
+ artifact_type=ArtifactType.RECORDING,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to look up browser-session recording artifacts; falling back to SAS URLs",
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ exc_info=True,
+ )
+ artifacts = []
+ artifacts = [
+ a for a in artifacts if a.uri and (a.uri.lower().endswith(".webm") or a.uri.lower().endswith(".mp4"))
+ ]
+ if artifacts:
+ file_infos = await _file_infos_from_artifacts(artifacts, artifact_type=ArtifactType.RECORDING)
+ file_infos.sort(key=lambda f: (f.modified_at is not None, f.modified_at), reverse=True)
+ return file_infos
+
+ # Legacy fallback: keyring unset, DB raised, or session pre-cutover
+ # with no rows at all. SKY-9286: drop entirely after the bake-in
+ # window (target 2026-05-03) — every call here is a billable
+ # ListBlobs request.
+ return await self._get_shared_recordings_in_browser_session_via_listing(
+ organization_id=organization_id, browser_session_id=browser_session_id
+ )
+
+ async def _get_shared_recordings_in_browser_session_via_listing(
+ self, *, organization_id: str, browser_session_id: str
+ ) -> list[FileInfo]:
+ # Direct Azure LIST: legacy fallback for pre-cutover sessions and
+ # OSS deployments without a keyring.
+ # SKY-9286: scheduled for removal once production sessions all have
+ # rows — every call here is a billable ListBlobs request.
+ listing_uri = f"azure://{settings.AZURE_STORAGE_CONTAINER_ARTIFACTS}/v1/{settings.ENV}/{organization_id}/browser_sessions/{browser_session_id}/videos"
+ object_keys = [
+ f"azure://{settings.AZURE_STORAGE_CONTAINER_ARTIFACTS}/{file}"
+ for file in await self.async_client.list_files(uri=listing_uri)
+ ]
if len(object_keys) == 0:
return []
@@ -667,16 +707,13 @@ class AzureStorage(BaseStorage):
tags = await self._get_tags_for_org(organization_id)
await self.async_client.upload_file_from_path(uri, local_file_path, tier=tier, tags=tags)
- # See s3.py — DB is the single source of truth for both the user-facing
- # listing and the agent's baseline / complete_on_download checks.
- # Partials get a row with checksum=None so the agent can detect "still
- # downloading"; the row is dropped on Chrome's atomic-rename
- # ``Change.deleted`` event.
- #
- # Exceptions propagate so the watcher's bounded retry can recover from
- # a transient DB outage — silently swallowing would leave the file in
- # Azure with no row, invisible to baseline diffs.
if artifact_type == "downloads":
+ # See s3.py — DB is the single source of truth for the user-facing
+ # listing and the agent's baseline / complete_on_download checks.
+ # Partials get a row with checksum=None; the row is dropped on
+ # Chrome's atomic-rename ``Change.deleted`` event. Exceptions
+ # propagate so the watcher's bounded retry can recover from a
+ # transient DB outage — both ops are idempotent.
is_partial = remote_path.endswith(BROWSER_DOWNLOADING_SUFFIX)
checksum = None if is_partial else calculate_sha256_for_file(local_file_path)
await app.ARTIFACT_MANAGER.create_browser_session_download_artifact(
@@ -686,6 +723,22 @@ class AzureStorage(BaseStorage):
filename=os.path.basename(remote_path),
checksum=checksum,
)
+ elif artifact_type == "videos":
+ # Recording uploaded once at session close — see s3.py. Artifact-
+ # row creation is best-effort: the only caller swallows
+ # exceptions without retry, so the gated legacy listing fallback
+ # in ``get_shared_recordings_in_browser_session`` is the safety
+ # net for missed writes (when the session has no RECORDING rows
+ # we fall through to the Azure LIST path, so a row-less recording
+ # still surfaces via the legacy SAS URL).
+ checksum = calculate_sha256_for_file(local_file_path)
+ await app.ARTIFACT_MANAGER.create_browser_session_recording_artifact(
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ uri=uri,
+ filename=os.path.basename(remote_path),
+ checksum=checksum,
+ )
return uri
diff --git a/skyvern/forge/sdk/artifact/storage/base.py b/skyvern/forge/sdk/artifact/storage/base.py
index 6c04c6425..7f9ca10db 100644
--- a/skyvern/forge/sdk/artifact/storage/base.py
+++ b/skyvern/forge/sdk/artifact/storage/base.py
@@ -10,16 +10,21 @@ from skyvern.forge.sdk.schemas.task_v2 import TaskV2, Thought
from skyvern.forge.sdk.schemas.workflow_runs import WorkflowRunBlock
-async def _file_infos_from_download_artifacts(artifacts: list[Artifact]) -> list[FileInfo]:
- """Build the API-shaped ``FileInfo`` list from DOWNLOAD artifact rows.
+async def _file_infos_from_artifacts(artifacts: list[Artifact], *, artifact_type: ArtifactType) -> list[FileInfo]:
+ """Build the API-shaped ``FileInfo`` list from a homogeneous batch of
+ artifact rows (e.g. all DOWNLOAD or all RECORDING).
Filename is the URI basename (the save site writes ``{base_uri}/{file}``);
checksum and modified_at come straight from the row, so retrieval needs
zero S3 round-trips.
- All artifacts in a single batch share the same organization (downloads are
- scoped to a run, which is scoped to an org), so the per-org URL TTL is
- resolved once and applied to every URL.
+ All artifacts in a single batch share the same organization (downloads /
+ recordings are scoped to a run or browser session, which is scoped to an
+ org), so the per-org URL TTL is resolved once and applied to every URL.
+
+ The ``artifact_type`` is only used for the URL's informational query
+ parameter — it does not affect the HMAC signature. Callers must pass rows
+ of a single type so the URL hint is correct.
"""
if not artifacts:
return []
@@ -31,7 +36,7 @@ async def _file_infos_from_download_artifacts(artifacts: list[Artifact]) -> list
url = app.ARTIFACT_MANAGER.build_signed_content_url(
artifact_id=artifact.artifact_id,
artifact_name=filename,
- artifact_type=ArtifactType.DOWNLOAD.value,
+ artifact_type=artifact_type.value,
expiry_seconds=expiry_seconds,
)
infos.append(
@@ -46,6 +51,16 @@ async def _file_infos_from_download_artifacts(artifacts: list[Artifact]) -> list
return infos
+async def _file_infos_from_download_artifacts(artifacts: list[Artifact]) -> list[FileInfo]:
+ """Backward-compat alias for DOWNLOAD-typed callers.
+
+ Forwards to :func:`_file_infos_from_artifacts` with the DOWNLOAD type so
+ pre-existing import sites keep working without each having to thread the
+ artifact_type through.
+ """
+ return await _file_infos_from_artifacts(artifacts, artifact_type=ArtifactType.DOWNLOAD)
+
+
# TODO: This should be a part of the ArtifactType model
FILE_EXTENTSION_MAP: dict[ArtifactType, str] = {
ArtifactType.RECORDING: "webm",
@@ -194,10 +209,6 @@ class BaseStorage(ABC):
) -> list[FileInfo]:
pass
- @abstractmethod
- async def list_recordings_in_browser_session(self, organization_id: str, browser_session_id: str) -> list[str]:
- pass
-
@abstractmethod
async def get_shared_recordings_in_browser_session(
self, organization_id: str, browser_session_id: str
diff --git a/skyvern/forge/sdk/artifact/storage/local.py b/skyvern/forge/sdk/artifact/storage/local.py
index 36f82b768..cc8ecba76 100644
--- a/skyvern/forge/sdk/artifact/storage/local.py
+++ b/skyvern/forge/sdk/artifact/storage/local.py
@@ -314,8 +314,10 @@ class LocalStorage(BaseStorage):
) -> list[str]:
return []
- async def list_recordings_in_browser_session(self, organization_id: str, browser_session_id: str) -> list[str]:
- """List all recording files for a browser session from local storage.
+ async def get_shared_recordings_in_browser_session(
+ self, organization_id: str, browser_session_id: str
+ ) -> list[FileInfo]:
+ """Get recording files with URLs for a browser session from local storage.
Videos are synced to the browser_sessions storage path when the session closes.
"""
@@ -328,20 +330,14 @@ class LocalStorage(BaseStorage):
/ "videos"
)
- recording_files: list[str] = []
- if videos_base.exists():
- for root, _, files in os.walk(videos_base):
- for file in files:
- file_path = Path(root) / file
- recording_files.append(f"file://{file_path}")
+ if not videos_base.exists():
+ return []
- return recording_files
+ file_uris: list[str] = []
+ for root, _, files in os.walk(videos_base):
+ for file in files:
+ file_uris.append(f"file://{Path(root) / file}")
- async def get_shared_recordings_in_browser_session(
- self, organization_id: str, browser_session_id: str
- ) -> list[FileInfo]:
- """Get recording files with URLs for a browser session from local storage."""
- file_uris = await self.list_recordings_in_browser_session(organization_id, browser_session_id)
if not file_uris:
return []
diff --git a/skyvern/forge/sdk/artifact/storage/s3.py b/skyvern/forge/sdk/artifact/storage/s3.py
index 485cd0395..a17010dc9 100644
--- a/skyvern/forge/sdk/artifact/storage/s3.py
+++ b/skyvern/forge/sdk/artifact/storage/s3.py
@@ -25,6 +25,7 @@ from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType, LogEntityT
from skyvern.forge.sdk.artifact.storage.base import (
FILE_EXTENTSION_MAP,
BaseStorage,
+ _file_infos_from_artifacts,
_file_infos_from_download_artifacts,
)
from skyvern.forge.sdk.models import Step
@@ -433,17 +434,61 @@ class S3Storage(BaseStorage):
in_progress=True,
)
- async def list_recordings_in_browser_session(self, organization_id: str, browser_session_id: str) -> list[str]:
- """List all recording files for a browser session from S3."""
- bucket = settings.AWS_S3_BUCKET_ARTIFACTS
- uri = f"s3://{bucket}/{self._PATH_VERSION}/{settings.ENV}/{organization_id}/browser_sessions/{browser_session_id}/videos"
- return [f"s3://{bucket}/{file}" for file in await self.async_client.list_files(uri=uri)]
-
async def get_shared_recordings_in_browser_session(
self, organization_id: str, browser_session_id: str
) -> list[FileInfo]:
- """Get recording files with presigned URLs for a browser session."""
- object_keys = await self.list_recordings_in_browser_session(organization_id, browser_session_id)
+ """Get recording files for a browser session.
+
+ Artifact-first when the keyring is configured: query RECORDING rows
+ scoped to the session and build short signed ``/v1/artifacts/{id}/content``
+ URLs from them — no S3 round-trip per file. Falls back to direct S3
+ LIST + presigned URLs for legacy sessions and OSS-default deployments
+ without HMAC signing.
+ """
+ if settings.ARTIFACT_CONTENT_HMAC_KEYRING:
+ try:
+ artifacts = await app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type(
+ browser_session_id=browser_session_id,
+ organization_id=organization_id,
+ artifact_type=ArtifactType.RECORDING,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to look up browser-session recording artifacts; falling back to presigned S3 URLs",
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ exc_info=True,
+ )
+ artifacts = []
+ # Defensive extension filter — same as the legacy listing path —
+ # in case a non-recording row sneaks under the same browser_session_id.
+ artifacts = [
+ a for a in artifacts if a.uri and (a.uri.lower().endswith(".webm") or a.uri.lower().endswith(".mp4"))
+ ]
+ if artifacts:
+ file_infos = await _file_infos_from_artifacts(artifacts, artifact_type=ArtifactType.RECORDING)
+ # Newest first — match the legacy listing path's ordering.
+ file_infos.sort(key=lambda f: (f.modified_at is not None, f.modified_at), reverse=True)
+ return file_infos
+
+ # Legacy fallback: keyring unset, DB raised, or session pre-cutover
+ # with no rows at all. SKY-9286: drop entirely after the bake-in
+ # window (target 2026-05-03) — every call here is a billable
+ # ListObjects request.
+ return await self._get_shared_recordings_in_browser_session_via_listing(
+ organization_id=organization_id, browser_session_id=browser_session_id
+ )
+
+ async def _get_shared_recordings_in_browser_session_via_listing(
+ self, *, organization_id: str, browser_session_id: str
+ ) -> list[FileInfo]:
+ # Direct S3 LIST: legacy fallback for sessions pre-cutover (no
+ # RECORDING artifact rows) and OSS deployments without a keyring.
+ # SKY-9286: scheduled for removal once production sessions all have
+ # rows — every call here is a billable ListObjects request.
+ bucket = settings.AWS_S3_BUCKET_ARTIFACTS
+ listing_uri = f"s3://{bucket}/{self._PATH_VERSION}/{settings.ENV}/{organization_id}/browser_sessions/{browser_session_id}/videos"
+ object_keys = [f"s3://{bucket}/{file}" for file in await self.async_client.list_files(uri=listing_uri)]
if len(object_keys) == 0:
return []
@@ -732,22 +777,21 @@ class S3Storage(BaseStorage):
sc = await self._get_storage_class_for_org(organization_id, self.bucket)
await self.async_client.upload_file_from_path(uri, local_file_path, storage_class=sc)
- # For downloaded files (only), register an Artifact row scoped to the
- # session so the DB is the single source of truth for both the
- # ``GET /v1/browser_sessions/{id}`` user-facing listing and the
- # agent's baseline-before/after / complete_on_download checks.
- # Partial files (``*.crdownload``) get a row too with checksum=None —
- # the agent's "still downloading" query reads URI-suffix from the
- # row. The row is dropped when Chrome's atomic rename fires
- # ``Change.deleted`` for the partial path.
- #
- # We deliberately let exceptions propagate so the watcher's bounded
- # retry can recover from a transient DB outage — silently swallowing
- # would leave the file in S3 with no row, invisible to baseline
- # diffs and complete_on_download. Both ``upload_file_from_path``
- # (S3 overwrite) and ``create_browser_session_download_artifact``
- # (idempotent on ``(session, uri)``) are safe to retry.
if artifact_type == "downloads":
+ # Register a DOWNLOAD Artifact row scoped to the session so the DB
+ # is the single source of truth for both the
+ # ``GET /v1/browser_sessions/{id}`` user-facing listing and the
+ # agent's baseline-before/after / complete_on_download checks.
+ # Partial files (``*.crdownload``) get a row too with
+ # checksum=None — the agent's "still downloading" query reads
+ # URI-suffix from the row. The row is dropped when Chrome's
+ # atomic rename fires ``Change.deleted`` for the partial path.
+ #
+ # Exceptions propagate so the watcher's bounded retry in
+ # ``browser_controller._watch_and_sync_directory`` can recover
+ # from a transient DB outage. Both ``upload_file_from_path`` (S3
+ # overwrite) and ``create_browser_session_download_artifact``
+ # (idempotent on ``(session, uri)``) are safe to retry.
is_partial = remote_path.endswith(BROWSER_DOWNLOADING_SUFFIX)
checksum = None if is_partial else calculate_sha256_for_file(local_file_path)
await app.ARTIFACT_MANAGER.create_browser_session_download_artifact(
@@ -757,6 +801,32 @@ class S3Storage(BaseStorage):
filename=os.path.basename(remote_path),
checksum=checksum,
)
+ elif artifact_type == "videos":
+ # Register a RECORDING artifact row so
+ # ``GET /v1/browser_sessions/{id}`` serves the recording via a
+ # short signed ``/v1/artifacts/{id}/content`` URL instead of a
+ # raw S3 presigned URL. Recordings are uploaded once at session
+ # close (Playwright finalizes the file when the browser context
+ # closes — there's no partial / mid-write state to track).
+ #
+ # Artifact-row creation is best-effort here. The only caller
+ # (``DefaultPersistentSessionsManager.close_session``) wraps the
+ # whole final-sync in ``except Exception: LOG.exception(...)``
+ # without retry, so propagating doesn't recover from a missed
+ # write the way the watcher's retry does for downloads. The
+ # safety net is the gated legacy listing fallback in
+ # ``get_shared_recordings_in_browser_session``: when the session
+ # has no RECORDING rows we fall through to the S3 LIST path, so
+ # a row-less recording still surfaces via the legacy presigned
+ # URL until a subsequent close writes the row.
+ checksum = calculate_sha256_for_file(local_file_path)
+ await app.ARTIFACT_MANAGER.create_browser_session_recording_artifact(
+ organization_id=organization_id,
+ browser_session_id=browser_session_id,
+ uri=uri,
+ filename=os.path.basename(remote_path),
+ checksum=checksum,
+ )
return uri
diff --git a/skyvern/forge/sdk/artifact/storage/test_azure_storage.py b/skyvern/forge/sdk/artifact/storage/test_azure_storage.py
index 411cf8ca2..6ecbfc93d 100644
--- a/skyvern/forge/sdk/artifact/storage/test_azure_storage.py
+++ b/skyvern/forge/sdk/artifact/storage/test_azure_storage.py
@@ -39,17 +39,19 @@ def azure_storage() -> AzureStorageForTests:
@pytest.fixture(autouse=True)
-def mock_browser_session_download_artifact_create(monkeypatch: pytest.MonkeyPatch) -> None:
- """Stub out the DB-side artifact-row insert for browser-session downloads.
+def mock_browser_session_artifact_create(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Stub out the DB-side artifact-row inserts for browser-session files.
Mirrors the s3 storage test fixture — see SKY-8861 follow-up. Patches
the module-level ``app`` reference in ``azure.py`` because the forge
- app isn't initialized in these storage-only tests.
+ app isn't initialized in these storage-only tests. Covers both
+ download and recording artifact creators.
"""
import skyvern.forge.sdk.artifact.storage.azure as azure_module
fake_app = MagicMock()
fake_app.ARTIFACT_MANAGER.create_browser_session_download_artifact = AsyncMock(return_value="a_test")
+ fake_app.ARTIFACT_MANAGER.create_browser_session_recording_artifact = AsyncMock(return_value="a_test")
monkeypatch.setattr(azure_module, "app", fake_app)
diff --git a/skyvern/forge/sdk/artifact/storage/test_s3_storage.py b/skyvern/forge/sdk/artifact/storage/test_s3_storage.py
index b8a03e46f..370986863 100644
--- a/skyvern/forge/sdk/artifact/storage/test_s3_storage.py
+++ b/skyvern/forge/sdk/artifact/storage/test_s3_storage.py
@@ -59,15 +59,17 @@ def aws_credentials(monkeypatch: pytest.MonkeyPatch) -> None:
@pytest.fixture(autouse=True)
-def mock_browser_session_download_artifact_create(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
- """Stub out the DB-side artifact-row insert for browser-session downloads.
-
- ``S3Storage.sync_browser_session_file(artifact_type="downloads")`` now
- awaits ``app.ARTIFACT_MANAGER.create_browser_session_download_artifact``
- (SKY-8861 follow-up). These storage tests run against a moto S3 with no
- forge app initialized, so we monkey-patch the module-level ``app``
- reference in ``s3.py`` — patching ``app.ARTIFACT_MANAGER`` directly
- would trip the lazy-init guard on AppHolder.
+def mock_browser_session_artifact_create(monkeypatch: pytest.MonkeyPatch) -> Generator[None, None, None]:
+ """Stub out the DB-side artifact-row inserts for browser-session files.
+
+ ``S3Storage.sync_browser_session_file`` now awaits
+ ``app.ARTIFACT_MANAGER.create_browser_session_download_artifact`` (for
+ ``artifact_type="downloads"``) and
+ ``app.ARTIFACT_MANAGER.create_browser_session_recording_artifact`` (for
+ ``artifact_type="videos"``). These storage tests run against a moto S3
+ with no forge app initialized, so we monkey-patch the module-level
+ ``app`` reference in ``s3.py`` — patching ``app.ARTIFACT_MANAGER``
+ directly would trip the lazy-init guard on AppHolder.
"""
from unittest.mock import MagicMock
@@ -75,6 +77,7 @@ def mock_browser_session_download_artifact_create(monkeypatch: pytest.MonkeyPatc
fake_app = MagicMock()
fake_app.ARTIFACT_MANAGER.create_browser_session_download_artifact = AsyncMock(return_value="a_test")
+ fake_app.ARTIFACT_MANAGER.create_browser_session_recording_artifact = AsyncMock(return_value="a_test")
monkeypatch.setattr(s3_module, "app", fake_app)
yield
diff --git a/tests/unit/test_browser_session_download_artifacts.py b/tests/unit/test_browser_session_download_artifacts.py
index a7fcb8967..c00354c1c 100644
--- a/tests/unit/test_browser_session_download_artifacts.py
+++ b/tests/unit/test_browser_session_download_artifacts.py
@@ -181,15 +181,18 @@ async def test_sync_browser_session_file_registers_download_artifact():
@pytest.mark.asyncio
-async def test_sync_browser_session_file_skips_artifact_for_non_download_types():
- """We only register artifact rows for downloads — videos, browser profiles, etc. stay untouched."""
+async def test_sync_browser_session_file_skips_artifact_for_unhandled_types():
+ """Non-download / non-recording artifact_types (e.g. ``har``) don't create
+ artifact rows — they're served by the legacy LIST path."""
storage = S3Storage()
storage.async_client = MagicMock()
storage.async_client.upload_file_from_path = AsyncMock()
- mock_create = AsyncMock()
+ mock_download = AsyncMock()
+ mock_recording = AsyncMock()
mock_artifact_manager = MagicMock()
- mock_artifact_manager.create_browser_session_download_artifact = mock_create
+ mock_artifact_manager.create_browser_session_download_artifact = mock_download
+ mock_artifact_manager.create_browser_session_recording_artifact = mock_recording
with (
patch.object(storage, "_get_storage_class_for_org", new=AsyncMock(return_value=MagicMock())),
@@ -200,12 +203,13 @@ async def test_sync_browser_session_file_skips_artifact_for_non_download_types()
await storage.sync_browser_session_file(
organization_id="o_1",
browser_session_id="pbs_1",
- artifact_type="videos",
- local_file_path="/tmp/recording.webm",
- remote_path="recording.webm",
+ artifact_type="har",
+ local_file_path="/tmp/network.har",
+ remote_path="network.har",
)
- mock_create.assert_not_awaited()
+ mock_download.assert_not_awaited()
+ mock_recording.assert_not_awaited()
@pytest.mark.asyncio
diff --git a/tests/unit/test_browser_session_recording_artifacts.py b/tests/unit/test_browser_session_recording_artifacts.py
new file mode 100644
index 000000000..a37c51d69
--- /dev/null
+++ b/tests/unit/test_browser_session_recording_artifacts.py
@@ -0,0 +1,376 @@
+"""Tests for the browser-session recording artifact pipeline.
+
+Recordings (Playwright .webm video) are uploaded once at session close via
+``S3Storage.sync_browser_session_file(artifact_type="videos")``. This change
+registers them as RECORDING artifact rows scoped to the session and serves
+them via short signed ``/v1/artifacts/{id}/content`` URLs from
+``get_shared_recordings_in_browser_session``.
+
+Mirrors the SKY-8861 download artifact pipeline in
+``test_browser_session_download_artifacts.py``.
+"""
+
+from __future__ import annotations
+
+from unittest.mock import AsyncMock, MagicMock, patch
+from urllib.parse import urlparse
+
+import pytest
+
+from skyvern.forge.sdk.artifact.manager import ArtifactManager
+from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType
+from skyvern.forge.sdk.artifact.storage.s3 import S3Storage
+
+_DUMMY_KEYRING_JSON = '{"current_kid": "k1", "keys": {"k1": {"secret": "0000000000000000000000000000000000000000000000000000000000000000"}}}'
+
+
+def _is_amazonaws_s3_url(url: str) -> bool:
+ """Strict hostname-suffix check (closes CodeQL py/incomplete-url-substring-sanitization)."""
+ host = urlparse(url).hostname
+ if host is None:
+ return False
+ return host == "s3.amazonaws.com" or host.endswith(".s3.amazonaws.com")
+
+
+def _make_recording_artifact(
+ artifact_id: str,
+ uri: str,
+ *,
+ browser_session_id: str = "pbs_1",
+ checksum: str | None = "sha-r",
+ created_at: str = "2026-04-26T00:00:00Z",
+) -> Artifact:
+ return Artifact(
+ artifact_id=artifact_id,
+ artifact_type=ArtifactType.RECORDING,
+ uri=uri,
+ organization_id="o_1",
+ browser_session_id=browser_session_id,
+ checksum=checksum,
+ created_at=created_at,
+ modified_at=created_at,
+ )
+
+
+def keyring_configured():
+ from skyvern.config import settings
+
+ with patch.object(settings, "ARTIFACT_CONTENT_HMAC_KEYRING", _DUMMY_KEYRING_JSON):
+ yield
+
+
+# ---------------------------------------------------------------------------
+# create_browser_session_recording_artifact (manager helper)
+# ---------------------------------------------------------------------------
+
+
+async def test_create_browser_session_recording_artifact_inserts_when_no_existing_row():
+ manager = ArtifactManager()
+ find_existing = AsyncMock(return_value=None)
+ mock_db_create = AsyncMock()
+
+ with (
+ patch(
+ "skyvern.forge.sdk.artifact.manager.app.DATABASE.artifacts.find_artifact_for_browser_session",
+ find_existing,
+ ),
+ patch(
+ "skyvern.forge.sdk.artifact.manager.app.DATABASE.artifacts.create_artifact",
+ mock_db_create,
+ ),
+ ):
+ artifact_id = await manager.create_browser_session_recording_artifact(
+ organization_id="o_1",
+ browser_session_id="pbs_1",
+ uri="s3://skyvern-artifacts/v1/local/o_1/browser_sessions/pbs_1/videos/2026-04-26/recording.webm",
+ filename="recording.webm",
+ checksum="sha-r",
+ )
+
+ assert artifact_id.startswith("a_")
+ mock_db_create.assert_awaited_once()
+ _, kwargs = mock_db_create.call_args
+ assert kwargs["artifact_type"] == ArtifactType.RECORDING
+ assert kwargs["browser_session_id"] == "pbs_1"
+ assert kwargs["organization_id"] == "o_1"
+ assert kwargs["checksum"] == "sha-r"
+ # Recordings are session-scoped, not run-scoped.
+ assert kwargs.get("run_id") is None
+
+
+async def test_create_browser_session_recording_artifact_is_idempotent():
+ """End-of-session sync may run twice on retry. The second call must reuse the existing row."""
+ manager = ArtifactManager()
+ existing = _make_recording_artifact(
+ "a_existing",
+ "s3://skyvern-artifacts/v1/local/o_1/browser_sessions/pbs_1/videos/2026-04-26/recording.webm",
+ )
+ find_existing = AsyncMock(return_value=existing)
+ mock_db_create = AsyncMock()
+
+ with (
+ patch(
+ "skyvern.forge.sdk.artifact.manager.app.DATABASE.artifacts.find_artifact_for_browser_session",
+ find_existing,
+ ),
+ patch(
+ "skyvern.forge.sdk.artifact.manager.app.DATABASE.artifacts.create_artifact",
+ mock_db_create,
+ ),
+ ):
+ artifact_id = await manager.create_browser_session_recording_artifact(
+ organization_id="o_1",
+ browser_session_id="pbs_1",
+ uri=existing.uri,
+ filename="recording.webm",
+ checksum="sha-r",
+ )
+
+ assert artifact_id == "a_existing"
+ mock_db_create.assert_not_awaited()
+
+
+# ---------------------------------------------------------------------------
+# Storage write site — sync_browser_session_file(artifact_type="videos")
+# ---------------------------------------------------------------------------
+
+
+async def test_sync_browser_session_file_registers_recording_artifact():
+ """A successful 'videos' sync must create a RECORDING artifact row."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+ storage.async_client.upload_file_from_path = AsyncMock()
+
+ mock_create = AsyncMock(return_value="a_new")
+ mock_artifact_manager = MagicMock()
+ mock_artifact_manager.create_browser_session_recording_artifact = mock_create
+
+ with (
+ patch.object(storage, "_get_storage_class_for_org", new=AsyncMock(return_value=MagicMock())),
+ patch("skyvern.forge.sdk.artifact.storage.s3.calculate_sha256_for_file", return_value="sha-r"),
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as app_module,
+ ):
+ app_module.ARTIFACT_MANAGER = mock_artifact_manager
+ await storage.sync_browser_session_file(
+ organization_id="o_1",
+ browser_session_id="pbs_1",
+ artifact_type="videos",
+ local_file_path="/tmp/recording.webm",
+ remote_path="recording.webm",
+ date="2026-04-26",
+ )
+
+ mock_create.assert_awaited_once()
+ _, kwargs = mock_create.call_args
+ assert kwargs["organization_id"] == "o_1"
+ assert kwargs["browser_session_id"] == "pbs_1"
+ assert kwargs["filename"] == "recording.webm"
+ assert kwargs["checksum"] == "sha-r"
+ assert kwargs["uri"].startswith("s3://") and "/browser_sessions/pbs_1/videos/" in kwargs["uri"]
+
+
+async def test_sync_browser_session_file_recording_failure_propagates():
+ """Contract: ``sync_browser_session_file(artifact_type="videos")``
+ raises when the artifact-row insert fails. The exception propagates to
+ the caller; ``DefaultPersistentSessionsManager.close_session`` is
+ responsible for catching it (which it does, with ``LOG.exception``).
+
+ This test covers the storage-layer half only. Resilience of the overall
+ pipeline (e.g., that a row-less recording still reaches the API
+ response) lives in the legacy listing fallback inside
+ ``get_shared_recordings_in_browser_session`` and is covered by the
+ fall-through tests in this module."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+ storage.async_client.upload_file_from_path = AsyncMock()
+
+ mock_create = AsyncMock(side_effect=RuntimeError("DB unreachable"))
+ mock_artifact_manager = MagicMock()
+ mock_artifact_manager.create_browser_session_recording_artifact = mock_create
+
+ with (
+ patch.object(storage, "_get_storage_class_for_org", new=AsyncMock(return_value=MagicMock())),
+ patch("skyvern.forge.sdk.artifact.storage.s3.calculate_sha256_for_file", return_value="sha-r"),
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as app_module,
+ ):
+ app_module.ARTIFACT_MANAGER = mock_artifact_manager
+ with pytest.raises(RuntimeError, match="DB unreachable"):
+ await storage.sync_browser_session_file(
+ organization_id="o_1",
+ browser_session_id="pbs_1",
+ artifact_type="videos",
+ local_file_path="/tmp/recording.webm",
+ remote_path="recording.webm",
+ )
+
+
+# ---------------------------------------------------------------------------
+# Read site — get_shared_recordings_in_browser_session (DB-first)
+# ---------------------------------------------------------------------------
+
+
+async def test_get_shared_recordings_returns_short_signed_urls(keyring_configured):
+ """When RECORDING rows exist, return short signed /v1/artifacts URLs — no
+ S3 round-trip per file, no presigned URLs."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+ storage.async_client.list_files = AsyncMock() # must NOT be called
+ storage.async_client.create_presigned_urls = AsyncMock() # must NOT be called
+
+ artifact = _make_recording_artifact(
+ "a_42",
+ "s3://skyvern-artifacts/v1/local/o_1/browser_sessions/pbs_1/videos/2026-04-26/recording.webm",
+ )
+ mock_list = AsyncMock(return_value=[artifact])
+ build_url = MagicMock(return_value="https://api.skyvern.com/v1/artifacts/a_42/content?expiry=x&kid=y&sig=z")
+
+ with (
+ patch("skyvern.forge.sdk.artifact.storage.base.app") as base_app,
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as s3_app,
+ ):
+ s3_app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type = mock_list
+ base_app.ARTIFACT_MANAGER.build_signed_content_url = build_url
+ base_app.ARTIFACT_MANAGER.resolve_artifact_url_expiry_seconds = AsyncMock(return_value=12 * 60 * 60)
+ result = await storage.get_shared_recordings_in_browser_session(
+ organization_id="o_1", browser_session_id="pbs_1"
+ )
+
+ assert len(result) == 1
+ assert result[0].url.startswith("https://api.skyvern.com/v1/artifacts/a_42/content")
+ assert result[0].artifact_id == "a_42"
+ storage.async_client.list_files.assert_not_awaited()
+ storage.async_client.create_presigned_urls.assert_not_awaited()
+
+
+async def test_get_shared_recordings_filters_unsupported_extensions(keyring_configured):
+ """Defensively drop rows whose URI doesn't look like a recording — even if
+ a stray non-video row landed under the session id."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+
+ good = _make_recording_artifact("a_good", "s3://b/v1/.../videos/2026-04-26/ok.webm")
+ bad = _make_recording_artifact("a_bad", "s3://b/v1/.../videos/2026-04-26/sneaky.exe")
+ mock_list = AsyncMock(return_value=[good, bad])
+ build_url = MagicMock(return_value="https://api.skyvern.com/v1/artifacts/a_good/content")
+
+ with (
+ patch("skyvern.forge.sdk.artifact.storage.base.app") as base_app,
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as s3_app,
+ ):
+ s3_app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type = mock_list
+ base_app.ARTIFACT_MANAGER.build_signed_content_url = build_url
+ base_app.ARTIFACT_MANAGER.resolve_artifact_url_expiry_seconds = AsyncMock(return_value=12 * 60 * 60)
+ result = await storage.get_shared_recordings_in_browser_session(
+ organization_id="o_1", browser_session_id="pbs_1"
+ )
+
+ assert [fi.artifact_id for fi in result] == ["a_good"]
+
+
+async def test_get_shared_recordings_sorts_newest_first(keyring_configured):
+ """Multiple recording rows: newest first, matches the legacy listing path's order."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+
+ older = _make_recording_artifact(
+ "a_old", "s3://b/v1/.../videos/2026-04-25/older.webm", created_at="2026-04-25T00:00:00Z"
+ )
+ newer = _make_recording_artifact(
+ "a_new", "s3://b/v1/.../videos/2026-04-26/newer.webm", created_at="2026-04-26T00:00:00Z"
+ )
+ mock_list = AsyncMock(return_value=[older, newer])
+ build_url = MagicMock(
+ side_effect=lambda artifact_id, **_: f"https://api.skyvern.com/v1/artifacts/{artifact_id}/content"
+ )
+
+ with (
+ patch("skyvern.forge.sdk.artifact.storage.base.app") as base_app,
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as s3_app,
+ ):
+ s3_app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type = mock_list
+ base_app.ARTIFACT_MANAGER.build_signed_content_url = build_url
+ base_app.ARTIFACT_MANAGER.resolve_artifact_url_expiry_seconds = AsyncMock(return_value=12 * 60 * 60)
+ result = await storage.get_shared_recordings_in_browser_session(
+ organization_id="o_1", browser_session_id="pbs_1"
+ )
+
+ assert [fi.artifact_id for fi in result] == ["a_new", "a_old"]
+
+
+async def test_get_shared_recordings_falls_back_to_presigned_for_legacy_session(keyring_configured):
+ """Legacy session: no RECORDING rows. Files in S3 still surface as presigned
+ URLs via the listing fallback so existing recordings remain reachable."""
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+ s3_key = "v1/local/o_1/browser_sessions/pbs_old/videos/2026-04-26/legacy.webm"
+ storage.async_client.list_files = AsyncMock(return_value=[s3_key])
+ storage.async_client.get_object_info = AsyncMock(
+ return_value={
+ "Metadata": {"sha256_checksum": "sha-old"},
+ "LastModified": None,
+ "ContentLength": 1024,
+ }
+ )
+ storage.async_client.create_presigned_urls = AsyncMock(
+ return_value=["https://skyvern-artifacts.s3.amazonaws.com/...?sig=old"]
+ )
+
+ mock_list = AsyncMock(return_value=[]) # no rows
+ build_url = MagicMock() # must NOT be called
+
+ with (
+ patch("skyvern.forge.sdk.artifact.storage.base.app") as base_app,
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as s3_app,
+ ):
+ s3_app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type = mock_list
+ base_app.ARTIFACT_MANAGER.build_signed_content_url = build_url
+ base_app.ARTIFACT_MANAGER.resolve_artifact_url_expiry_seconds = AsyncMock(return_value=12 * 60 * 60)
+ result = await storage.get_shared_recordings_in_browser_session(
+ organization_id="o_1", browser_session_id="pbs_old"
+ )
+
+ assert len(result) == 1
+ assert _is_amazonaws_s3_url(result[0].url)
+ build_url.assert_not_called()
+
+
+async def test_get_shared_recordings_keyring_unset_skips_artifact_lookup():
+ """OSS default (no keyring) skips the artifact path — webhook consumers
+ can't hit the signed endpoint without an API key."""
+ from skyvern.config import settings
+
+ storage = S3Storage()
+ storage.async_client = MagicMock()
+ s3_key = "v1/local/o_1/browser_sessions/pbs_1/videos/2026-04-26/recording.webm"
+ storage.async_client.list_files = AsyncMock(return_value=[s3_key])
+ storage.async_client.get_object_info = AsyncMock(
+ return_value={"Metadata": {"sha256_checksum": "sha-x"}, "LastModified": None, "ContentLength": 1024}
+ )
+ storage.async_client.create_presigned_urls = AsyncMock(
+ return_value=["https://skyvern-artifacts.s3.amazonaws.com/...?sig=fallback"]
+ )
+
+ mock_list = AsyncMock() # must NOT be called
+
+ with (
+ patch.object(settings, "ARTIFACT_CONTENT_HMAC_KEYRING", None),
+ patch("skyvern.forge.sdk.artifact.storage.s3.app") as s3_app,
+ ):
+ s3_app.DATABASE.artifacts.list_artifacts_for_browser_session_by_type = mock_list
+ result = await storage.get_shared_recordings_in_browser_session(
+ organization_id="o_1", browser_session_id="pbs_1"
+ )
+
+ assert len(result) == 1
+ assert _is_amazonaws_s3_url(result[0].url)
+ mock_list.assert_not_awaited()