Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
RBAC v2 slice 4b part 1: frontend API layer for role settings (SKY-18909) (#9049)
Aaron Perez863ed96dac87
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex a05059df0..e511c1dbd 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -10854,7 +10854,17 @@ async def handle_upload_file_action(locator = skyvern_element.locator- file_path = await handler_utils.download_file(file_url, action.model_dump(), task.organization_id)+ try:+ file_path = await handler_utils.download_file(file_url, action.model_dump(), task.organization_id)+ except handler_utils.UploadFileDownloadError as exc:+ context = skyvern_context.current()+ if (+ context is not None+ and context.pending_file_chooser is not None+ and context.pending_file_chooser.page is page+ ):+ context.cleanup_pending_file_chooser()+ return [ActionFailure(Exception(f"Failed to download file from {action.file_url}: {exc}"))]is_file_input = await skyvern_element.is_file_input()if not is_file_input:@@ -12493,7 +12503,17 @@ async def chain_click(file = pending_upload_files or []if not file and action.file_url:file_url = get_actual_value_of_parameter_if_secret_with_task(task, action.file_url)- file = await handler_utils.download_file(file_url, action.model_dump(), task.organization_id)+ try:+ file = await handler_utils.download_file(file_url, action.model_dump(), task.organization_id)+ except handler_utils.UploadFileDownloadError as exc:+ context = skyvern_context.current()+ if (+ context is not None+ and context.pending_file_chooser is not None+ and context.pending_file_chooser.page is page+ ):+ context.cleanup_pending_file_chooser()+ return [ActionFailure(Exception(f"Failed to download file from {action.file_url}: {exc}"))]is_filechooser_trigger = Falseis_upload_action = bool(action.file_url)diff --git a/skyvern/webeye/actions/handler_utils.py b/skyvern/webeye/actions/handler_utils.pyindex 8306f1b2e..51d75a510 100644--- a/skyvern/webeye/actions/handler_utils.py+++ b/skyvern/webeye/actions/handler_utils.py@@ -19,6 +19,10 @@ LOG = structlog.get_logger()_NATIVE_VALUE_SET_INPUT_TYPES = frozenset({"range", "date", "datetime-local", "month", "time", "week"})+class UploadFileDownloadError(Exception):+ pass++async def _uses_native_value_set_fill(locator: Locator) -> bool:try:input_type = await locator.evaluate("el => el.tagName === 'INPUT' ? el.type : null")@@ -32,34 +36,33 @@ async def download_file(file_url: str,action: dict[str, Any] | None = None,organization_id: str | None = None,-) -> str | list[str]:+) -> str:if file_url.startswith("/"):run_id = resolve_run_download_id(skyvern_context.current())try:return validate_local_file_path(file_url, run_id)- except PermissionError:+ except PermissionError as exc:# Expected when the LLM/user supplies an absolute path outside the run's download- # directory. The containment guard is doing its job; the action fails cleanly rather- # than surfacing as an unhandled ERROR with a traceback.+ # directory. The containment guard is doing its job; the caller turns this into an+ # action failure.LOG.warning(- "Local file path is outside the allowed download directory, continuing without it",+ "Local file path is outside the allowed download directory",action=action,file_url=file_url,run_id=run_id,)- return []+ raise UploadFileDownloadError("Local file path is outside the allowed download directory") from exctry:return await download_file_api(file_url, organization_id=organization_id)- except Exception:- # Fully self-recovering: the action proceeds without the file.+ except Exception as exc:LOG.warning(- "Failed to download file, continuing without it",+ "Failed to download file",action=action,file_url=file_url,exc_info=True,)- return []+ raise UploadFileDownloadError("File download failed") from excasync def strategy_aware_input(diff --git a/tests/unit/test_handler_utils_download_file.py b/tests/unit/test_handler_utils_download_file.pyindex 2381ec558..27677a267 100644--- a/tests/unit/test_handler_utils_download_file.py+++ b/tests/unit/test_handler_utils_download_file.py@@ -35,7 +35,7 @@ async def test_download_file_accepts_run_local_absolute_path(tmp_path: Path, mon@pytest.mark.asyncio-async def test_download_file_absolute_path_outside_run_dir_recovers(+async def test_download_file_absolute_path_outside_run_dir_fails_cleanly(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:run_id = "wr_upload"@@ -53,7 +53,8 @@ async def test_download_file_absolute_path_outside_run_dir_recovers(download_mock = AsyncMock(side_effect=AssertionError("remote download must not be called"))monkeypatch.setattr(handler_utils, "download_file_api", download_mock)- result = await handler_utils.download_file(str(outside_file), action={"action_type": "upload_file"})+ with pytest.raises(handler_utils.UploadFileDownloadError) as error:+ await handler_utils.download_file(str(outside_file), action={"action_type": "upload_file"})- assert result == []+ assert isinstance(error.value.__cause__, PermissionError)download_mock.assert_not_awaited()diff --git a/tests/unit/test_upload_file_wait.py b/tests/unit/test_upload_file_wait.pyindex 518b38b38..8c7a4be91 100644--- a/tests/unit/test_upload_file_wait.py+++ b/tests/unit/test_upload_file_wait.py@@ -14,8 +14,9 @@ from playwright.async_api import TimeoutError as PlaywrightTimeoutErrorfrom skyvern.exceptions import SkyvernPageAnalysisTimeoutfrom skyvern.webeye.actions import handler as handler_module-from skyvern.webeye.actions.actions import UploadFileAction+from skyvern.webeye.actions.actions import ClickAction, UploadFileActionfrom skyvern.webeye.actions.handler import _wait_for_upload_processing+from skyvern.webeye.actions.responses import ActionFailure, ActionSuccessfrom skyvern.webeye.browser_engine import BrowserEngineMetadata, BrowserEngineSelection@@ -162,6 +163,109 @@ async def test_upload_resolves_before_dispatch_and_reuses_exact_selection(selectassert wait.await_args.kwargs["engine_selection"] is selection[email protected]("local_path", [False, True], ids=["remote-fetch", "local-path-permission"])+async def test_upload_fetch_failure_does_not_fall_through_to_click_success(local_path: bool) -> None:+ file_url = "/outside/example.txt" if local_path else "https://files.example.test/example.txt"+ element = MagicMock(+ locator=MagicMock(),+ is_file_input=AsyncMock(return_value=False),+ find_file_input_in_children=AsyncMock(return_value=None),+ )+ dom = MagicMock(get_skyvern_element_by_id=AsyncMock(return_value=element))+ task = MagicMock(navigation_goal=file_url, navigation_payload={}, organization_id="org")+ click_fallback = AsyncMock(return_value=[ActionSuccess()])+ page = MagicMock()+ listener = Mock()+ context = handler_module.skyvern_context.SkyvernContext(+ pending_file_chooser=handler_module.skyvern_context.PendingFileChooserListener(+ page=page,+ file_paths="/tmp/previous.txt",+ handler=listener,+ )+ )++ with (+ patch.object(handler_module, "DomUtil", return_value=dom),+ patch.object(handler_module, "get_actual_value_of_parameter_if_secret_with_task", return_value=file_url),+ patch.object(+ handler_module.handler_utils,+ "download_file_api",+ new=AsyncMock(+ side_effect=AssertionError("local paths must not use the remote fetch")+ if local_path+ else RuntimeError("fetch failed")+ ),+ ),+ patch.object(handler_module.handler_utils, "resolve_run_download_id", return_value="wr_test"),+ patch.object(+ handler_module.handler_utils,+ "validate_local_file_path",+ side_effect=PermissionError("outside the allowed download directory")+ if local_path+ else AssertionError("remote URLs must not use local path validation"),+ ),+ patch.object(handler_module.SkyvernElement, "wait_until_enabled", new=AsyncMock(return_value=True)),+ patch.object(handler_module, "chain_click", new=click_fallback),+ patch.object(handler_module.skyvern_context, "current", return_value=context),+ ):+ results = await handler_module.handle_upload_file_action(+ UploadFileAction(element_id="file", file_url=file_url),+ page,+ MagicMock(),+ task,+ MagicMock(),+ )++ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)+ assert "Failed to download file" in results[0].exception_message+ click_fallback.assert_not_awaited()+ assert context.pending_file_chooser is None+ page.remove_listener.assert_called_once_with("filechooser", listener)+++async def test_click_upload_fetch_failure_returns_action_failure() -> None:+ file_url = "https://files.example.test/example.txt"+ element = MagicMock(get_id=Mock(return_value="file"), locator=MagicMock())+ dom = MagicMock(resolve_effective_click_target=AsyncMock(return_value=element))+ task = MagicMock(organization_id="org")+ page = MagicMock()+ listener = Mock()+ context = handler_module.skyvern_context.SkyvernContext(+ pending_file_chooser=handler_module.skyvern_context.PendingFileChooserListener(+ page=page,+ file_paths="/tmp/previous.txt",+ handler=listener,+ )+ )++ with (+ patch.object(handler_module, "DomUtil", return_value=dom),+ patch.object(handler_module, "get_actual_value_of_parameter_if_secret_with_task", return_value=file_url),+ patch.object(+ handler_module.handler_utils,+ "download_file_api",+ new=AsyncMock(side_effect=RuntimeError("fetch failed")),+ ),+ patch.object(handler_module.skyvern_context, "current", return_value=context),+ ):+ results = await handler_module.chain_click(+ task,+ MagicMock(),+ page,+ ClickAction(element_id="file", file_url=file_url),+ element,+ )++ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)+ assert results[0].exception_message == f"Failed to download file from {file_url}: File download failed"+ assert context.pending_file_chooser is None+ page.remove_listener.assert_called_once_with("filechooser", listener)++@pytest.mark.asyncioasync def test_swallows_playwright_timeout() -> None:mock_frame = AsyncMock()