Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Validate destinations on two unguarded browser navigation paths (SKY-13113) (#7828)
Aaron Perezed81c439f2fb
diff --git a/skyvern/forge/sdk/routes/streaming/channels/execution.py b/skyvern/forge/sdk/routes/streaming/channels/execution.pyindex 8f3cc2714..96f96b2f0 100644--- a/skyvern/forge/sdk/routes/streaming/channels/execution.py+++ b/skyvern/forge/sdk/routes/streaming/channels/execution.py@@ -12,6 +12,7 @@ Channel data:from __future__ import annotations+import asyncioimport base64import typing as tfrom contextlib import asynccontextmanager@@ -24,6 +25,7 @@ from skyvern.config import settingsfrom skyvern.forge.sdk.routes.streaming.channels.cdp import CdpChannelfrom skyvern.forge.sdk.routes.streaming.payload_limits import MAX_SCREENSHOT_BYTESfrom skyvern.forge.sdk.routes.streaming.registries import get_vnc_channel+from skyvern.utils.url_validators import validate_fetch_urlfrom skyvern.webeye.main_world_eval import evaluate_in_main_worldif t.TYPE_CHECKING:@@ -95,7 +97,7 @@ class ExecutionChannel(CdpChannel):if not self.page:raise RuntimeError(f"{self.class_name} navigate: not connected to a page.")- normalized = self._normalize_url(url)+ normalized = await asyncio.to_thread(validate_fetch_url, self._normalize_url(url))await self.page.goto(normalized,@@ -218,6 +220,8 @@ class ExecutionChannel(CdpChannel):@staticmethoddef _normalize_url(url: str) -> str:+ """Settle the scheme only. This says nothing about the destination — callers must pass the+ result through ``validate_fetch_url`` before navigating."""candidate = url.strip()if not candidate:raise ValueError("URL must not be empty")diff --git a/skyvern/forge/sdk/routes/streaming/channels/message.py b/skyvern/forge/sdk/routes/streaming/channels/message.pyindex cba4f44a0..9a32f638b 100644--- a/skyvern/forge/sdk/routes/streaming/channels/message.py+++ b/skyvern/forge/sdk/routes/streaming/channels/message.py@@ -846,7 +846,10 @@ async def loop_stream_messages(message_channel: MessageChannel) -> None:await send_error(message.kind, str(exc))except Exception:# The target URL is user-controlled; log it server-side- # but don't reflect it back in the toast.+ # but don't reflect it back in the toast. BlockedHost from the+ # destination check lands here deliberately rather than in the ValueError+ # branch above: naming the rejected host would let a caller tell "blocked+ # internal" from "unreachable" and enumerate internal ranges from the toast.LOG.exception(f"{class_name} failed to navigate.",url=message.url,diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex d810658cc..b52f2958d 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -4946,8 +4946,9 @@ async def handle_download_file_action(# Priority 2: If download_url is provided, download from URLif action.download_url is not None:# the URL is usally requiring login credentials/cookides, so we should use browser navigation to access the URL instead of downloading the file directly+ validated_url = await asyncio.to_thread(validate_fetch_url, action.download_url)try:- await page.goto(action.download_url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS)+ await page.goto(validated_url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS)except Exception as e:error = str(e)# some cases use this method to download a file. but it will be redirected away soondiff --git a/tests/unit/forge/sdk/routes/streaming/test_message_reify.py b/tests/unit/forge/sdk/routes/streaming/test_message_reify.pyindex 285cd7c7e..db9ebfd0f 100644--- a/tests/unit/forge/sdk/routes/streaming/test_message_reify.py+++ b/tests/unit/forge/sdk/routes/streaming/test_message_reify.py@@ -6,13 +6,14 @@ import asyncioimport jsonimport typing as tfrom contextlib import asynccontextmanager-from unittest.mock import AsyncMock, MagicMock+from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom fastapi import WebSocketDisconnectfrom starlette.websockets import WebSocketStatefrom skyvern.config import settings+from skyvern.forge.sdk.routes.streaming.channels import execution as execution_modulefrom skyvern.forge.sdk.routes.streaming.channels import message as message_modulefrom skyvern.forge.sdk.routes.streaming.channels.execution import ExecutionChannel, LocalExecutionChannelfrom skyvern.forge.sdk.routes.streaming.channels.message import (@@ -229,6 +230,35 @@ class TestExecutionChannelHelpers:with pytest.raises(ValueError):ExecutionChannel._normalize_url("file:///etc/passwd")+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "url",+ ["http://127.0.0.1:8000/", "http://169.254.169.254/latest/meta-data/", "http://10.0.0.5/admin"],+ )+ async def test_navigate_refuses_internal_destination(self, url: str) -> None:+ """The live-session navigate control renders the response, so an internal host must fail closed."""+ channel = ExecutionChannel(vnc_channel=MagicMock(identity={"client_id": "abc"}))+ channel.page = MagicMock()+ channel.page.goto = AsyncMock()++ with pytest.raises(Exception):+ await channel.navigate(url)++ channel.page.goto.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_navigate_uses_the_validated_url(self, monkeypatch: pytest.MonkeyPatch) -> None:+ validate = MagicMock(return_value="https://example.test/validated")+ monkeypatch.setattr(execution_module, "validate_fetch_url", validate)+ channel = ExecutionChannel(vnc_channel=MagicMock(identity={"client_id": "abc"}))+ channel.page = MagicMock()+ channel.page.goto = AsyncMock()++ await channel.navigate("example.test")++ validate.assert_called_once_with("https://example.test")+ assert channel.page.goto.call_args[0][0] == "https://example.test/validated"+@pytest.mark.parametrize("url", ["", " "])def test_normalize_url_rejects_empty_url(self, url: str) -> None:with pytest.raises(ValueError, match="URL must not be empty"):@@ -267,7 +297,11 @@ class TestExecutionChannelHelpers:channel = ExecutionChannel(vnc_channel=vnc_channel)channel.page = page- await channel.navigate("example.com")+ with patch(+ "skyvern.forge.sdk.routes.streaming.channels.execution.validate_fetch_url",+ side_effect=lambda url: url,+ ):+ await channel.navigate("example.com")await channel.reload()await channel.go_back()await channel.go_forward()diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.pyindex 9a84b5998..f2efa24f7 100644--- a/tests/unit/test_download_file_action_handler.py+++ b/tests/unit/test_download_file_action_handler.py@@ -1258,7 +1258,10 @@ async def test_handle_download_file_action_with_download_url() -> None:step_id=step.step_id,)- with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ with (+ patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"),+ patch("skyvern.webeye.actions.handler.validate_fetch_url", side_effect=lambda url: url),+ ):result = await handle_download_file_action(action, page, scraped_page, task, step)# Verify page.goto was called with the correct URL (handler uses browser navigation for download_url)@@ -1299,7 +1302,10 @@ async def test_handle_download_file_action_with_download_url_same_filename() ->step_id=step.step_id,)- with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ with (+ patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"),+ patch("skyvern.webeye.actions.handler.validate_fetch_url", side_effect=lambda url: url),+ ):result = await handle_download_file_action(action, page, scraped_page, task, step)page.goto.assert_called_once()@@ -1472,7 +1478,10 @@ async def test_handle_download_file_action_download_url_error() -> None:page.goto = AsyncMock(side_effect=Exception("Download failed"))- with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ with (+ patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"),+ patch("skyvern.webeye.actions.handler.validate_fetch_url", side_effect=lambda url: url),+ ):result = await handle_download_file_action(action, page, scraped_page, task, step)assert len(result) == 1@@ -1552,7 +1561,10 @@ async def test_handle_download_file_action_download_url_err_aborted_swallowed()step_id=step.step_id,)- with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ with (+ patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"),+ patch("skyvern.webeye.actions.handler.validate_fetch_url", side_effect=lambda url: url),+ ):result = await handle_download_file_action(action, page, scraped_page, task, step)assert len(result) == 1@@ -4656,3 +4668,61 @@ async def test_handle_action_blocked_inline_recovery_is_time_bounded(tmp_path: Passert results[-1].download_triggered is Falseassert results[-1].needs_followup is Trueassert results[-1].followup_message == DOWNLOAD_NOT_TRIGGERED_FOLLOWUP_MESSAGE+++def _download_action(now: datetime, download_url: str) -> tuple[DownloadFileAction, ScrapedPage, object, object]:+ organization = make_organization(now)+ task = make_task(now, organization)+ step = make_step(now, task, step_id="step-1", status=StepStatus.created, order=0, output=None)+ scraped_page = ScrapedPage(+ elements=[],+ element_tree=[],+ element_tree_trimmed=[],+ _browser_state=MagicMock(),+ _clean_up_func=AsyncMock(return_value=[]),+ _scrape_exclude=None,+ )+ action = DownloadFileAction(+ file_name="downloaded_file.pdf",+ download_url=download_url,+ organization_id=task.organization_id,+ task_id=task.task_id,+ step_id=step.step_id,+ )+ return action, scraped_page, task, step+++ "download_url",+ ["file:///etc/passwd", "http://127.0.0.1:8000/", "http://169.254.169.254/latest/meta-data/"],+)+async def test_handle_download_file_action_refuses_unsafe_download_url(download_url: str) -> None:+ """download_url is model-supplied, so it must clear the same validator GOTO_URL clears."""+ action, scraped_page, task, step = _download_action(datetime.now(UTC), download_url)+ page = MagicMock()+ page.goto = AsyncMock(return_value=None)++ with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ result = await handle_download_file_action(action, page, scraped_page, task, step)++ page.goto.assert_not_awaited()+ assert len(result) == 1+ assert isinstance(result[0], ActionFailure)+++async def test_handle_download_file_action_navigates_to_validated_url(monkeypatch: pytest.MonkeyPatch) -> None:+ """The navigated URL is the validator's return value, not the raw action field."""+ validate = MagicMock(return_value="https://example.test/validated.pdf")+ monkeypatch.setattr("skyvern.webeye.actions.handler.validate_fetch_url", validate)+ action, scraped_page, task, step = _download_action(datetime.now(UTC), "https://example.test/file.pdf")+ page = MagicMock()+ page.goto = AsyncMock(return_value=None)++ with patch("skyvern.webeye.actions.handler.initialize_download_dir", return_value="/tmp"):+ result = await handle_download_file_action(action, page, scraped_page, task, step)++ validate.assert_called_once_with("https://example.test/file.pdf")+ assert page.goto.call_args[0][0] == "https://example.test/validated.pdf"+ assert isinstance(result[0], ActionSuccess)