Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Refuse browser egress to a destination we cannot resolve (SKY-18836) (#9013)
Aaron Perez10b8d829c275
diff --git a/skyvern/forge/sdk/browser_egress_policy.py b/skyvern/forge/sdk/browser_egress_policy.pyindex aea09e022..312bc2f7f 100644--- a/skyvern/forge/sdk/browser_egress_policy.py+++ b/skyvern/forge/sdk/browser_egress_policy.py@@ -39,6 +39,14 @@ BLOCKED_HOST_SUFFIXES: tuple[str, ...] = (_BROWSER_EGRESS_ENROLLED_ATTR = "_skyvern_browser_egress_enrolled"+# A resolution failure is a refusal, not an allowance. The browser resolves the hostname itself+# (split-horizon DNS, or a proxy handed the name over HTTP CONNECT / SOCKS remote DNS), so a name+# the worker cannot resolve can still reach an internal address from the browser's network; under a+# two-valued contract an unchecked destination would be indistinguishable from a verified public+# one. Callers that report a destination to the browser match on this to answer with the resolver's+# verdict instead of a policy block.+UNRESOLVED_DESTINATION_REASON = "blocked egress: the worker could not resolve the destination"+class DestinationBlockedError(ValueError):"""A destination is malformed or violates browser egress policy."""@@ -77,9 +85,9 @@ def normalize_host(host: str) -> str:def to_ascii_host(host: str) -> str:"""IDNA/UTS-46 ToASCII so a Unicode hostname resolves the way the browser does.- Without this, ``getaddrinfo`` on a raw Unicode IDN host fails and hits the- resolve fail-open, while the browser IDNA-encodes and can still reach an- internal A-record. Falls back to the input when encoding is not applicable.+ Without this, ``getaddrinfo`` on a raw Unicode IDN host fails and the destination is refused+ unresolvable, while the browser IDNA-encodes it and reaches a real A-record. Falls back to the+ input when encoding is not applicable."""try:return host.encode("idna").decode("ascii")@@ -120,7 +128,8 @@ def _parse_whatwg_ipv4(host: str) -> ipaddress.IPv4Address | None:The WHATWG URL parser accepts hex (0xa9fea9fe), octal (0251.0376.0251.0376),plain-integer (2852039166), and shortened dotted (169.254.43518) hosts as IPv4;- without this they would fall through to the DNS check and its fail-open.+ without this they would fall through to the DNS check and be refused as unresolvable+ rather than named as the internal address they are."""parts = host.split(".")if not 1 <= len(parts) <= 4:@@ -275,13 +284,13 @@ async def resolve_public_destination_async(url: str, *, timeout_seconds: float)def classify_url(url: str) -> str | None:- """Return a block reason for an internal destination, or None if allowed."""+ """Return a block reason for an internal or unverifiable destination, or None if allowed."""try:resolve_public_destination(url)except DestinationBlockedError as exc:return str(exc)except DestinationResolutionError:- return None+ return UNRESOLVED_DESTINATION_REASONreturn Nonediff --git a/skyvern/forge/sdk/routes/streaming/cdp_input.py b/skyvern/forge/sdk/routes/streaming/cdp_input.pyindex 6bd3e88fd..6ceaf2409 100644--- a/skyvern/forge/sdk/routes/streaming/cdp_input.py+++ b/skyvern/forge/sdk/routes/streaming/cdp_input.py@@ -17,7 +17,7 @@ from opentelemetry import metricsfrom playwright.async_api import CDPSessionfrom websockets.exceptions import ConnectionClosedError, ConnectionClosedOK-from skyvern.exceptions import BlockedNavigationDestination, InvalidUrl+from skyvern.exceptions import BlockedNavigationDestination, InvalidUrl, UnresolvableNavigationHostfrom skyvern.forge import appfrom skyvern.forge.sdk.routes.routers import base_router, legacy_base_routerfrom skyvern.forge.sdk.routes.streaming.auth import auth, require_client_id@@ -383,6 +383,13 @@ async def _dispatch_navigate_event(# Fail closed before any request reaches the remote browser -- the same choke point# every real page.goto in the codebase funnels through (see navigate_with_retry).await asyncio.to_thread(validate_navigation_destination, url)+ except UnresolvableNavigationHost as error:+ # The guard refuses a host it cannot resolve, but the target having no address record is+ # the target's state, not a policy refusal: report it as a failed navigation, and keep the+ # input channel open so the next URL the user types still reaches the browser.+ LOG.info("CDP input: navigate target has no address record", **{log_id_key: log_id_value}, host=error.host)+ await websocket.send_json({"kind": "navigate-error", "reason": "failed"})+ returnexcept BlockedNavigationDestination as error:LOG.info("CDP input: navigate blocked", **{log_id_key: log_id_value}, reason=error.reason)await websocket.send_json({"kind": "navigate-error", "reason": "blocked"})@@ -408,8 +415,14 @@ async def _dispatch_navigate_event(# page.goto follows redirects at the network layer, so a validated public entry point# can still land on an internal host -- re-check the followed chain (SKY-13112 pattern).await revalidate_redirect_chain(response, validate_navigation_destination)- except BlockedNavigationDestination as error:- LOG.info("CDP input: navigate blocked via redirect", **{log_id_key: log_id_value}, reason=error.reason)+ # The browser already fetched every hop here, so a hop our resolver cannot answer for is a+ # split-horizon view of a live host rather than a dead one: refused on the same terms.+ except (BlockedNavigationDestination, UnresolvableNavigationHost) as error:+ LOG.info(+ "CDP input: navigate blocked via redirect",+ **{log_id_key: log_id_value},+ reason=error.error_message,+ )await _reset_page_after_navigation_failure(page, log_id_key, log_id_value)await websocket.send_json({"kind": "navigate-error", "reason": "blocked"})@@ -447,14 +460,17 @@ async def _dispatch_history_event(# guard resets the page, not the history -- so replaying an entry unvalidated would# reopen exactly the SSRF that _dispatch_navigate_event closes.await asyncio.to_thread(validate_navigation_destination, url)- except BlockedNavigationDestination as error:+ # An entry whose host no longer has an address record is a dead target, not a policy refusal,+ # and neither one may close the channel: the user still has the rest of their history.+ except (BlockedNavigationDestination, UnresolvableNavigationHost) as error:+ dead_target = isinstance(error, UnresolvableNavigationHost)LOG.info(- "CDP input: history navigation blocked",+ "CDP input: history navigation refused",**{log_id_key: log_id_value},kind=kind,- reason=error.reason,+ reason=error.error_message,)- await websocket.send_json({"kind": "navigate-error", "reason": "blocked"})+ await websocket.send_json({"kind": "navigate-error", "reason": "failed" if dead_target else "blocked"})returnif kind == "reloadEvent":diff --git a/skyvern/utils/url_validators.py b/skyvern/utils/url_validators.pyindex e9d0dd7ae..e1281f43a 100644--- a/skyvern/utils/url_validators.py+++ b/skyvern/utils/url_validators.py@@ -1,6 +1,7 @@import ipaddressimport socketfrom datetime import UTC, datetime, timedelta+from enum import StrEnumfrom http import HTTPStatusfrom typing import Annotated, Anyfrom urllib.parse import parse_qsl, quote, urljoin, urlparse, urlsplit, urlunsplit@@ -23,6 +24,13 @@ _NO_SUCH_HOST_DNS_ERRNOS = frozenset(getattr(socket, name) for name in ("EAI_NONAME", "EAI_NODATA") if hasattr(socket, name))++class HostVerdict(StrEnum):+ ALLOW = "allow"+ DENY = "deny"+ UNRESOLVABLE = "unresolvable"++_BLOCKED_INTERNAL_HOSTNAMES = frozenset({"localhost", "metadata.google.internal", "kubernetes.default.svc"})_BLOCKED_INTERNAL_SUFFIXES = (".local", ".localhost", ".internal", ".cluster.local")_LOCAL_BROWSER_HOSTNAMES = frozenset({"localhost", "host.docker.internal"})@@ -208,7 +216,15 @@ def is_tls_or_local_browser_address(address: str) -> bool:def validate_browser_host(host: str, *, resolve_dns: bool = False) -> None:- if not is_allowed_local_browser_host(host) and is_blocked_host(host, resolve_dns=resolve_dns):+ if is_allowed_local_browser_host(host):+ return+ verdict = classify_host(host, resolve_dns=resolve_dns)+ if verdict is HostVerdict.UNRESOLVABLE:+ # UnresolvableHost subclasses BlockedHost, so a caller handling the refusal is unaffected;+ # the distinct message is what keeps an unreachable browser host from reading as one our+ # SSRF policy named.+ raise UnresolvableHost(host=host)+ if verdict is HostVerdict.DENY:raise BlockedHost(host=host)@@ -242,17 +258,26 @@ def _is_internal_hostname(host: str) -> bool:return normalized.endswith(".svc")-def is_blocked_host(host: str, *, resolve_dns: bool = False) -> bool:+def classify_host(host: str, *, resolve_dns: bool = False) -> HostVerdict:+ """Whether ``host`` may be reached, must be refused, or could not be checked at all.++ ``UNRESOLVABLE`` exists so a resolution failure cannot be spelled the same way as a verified+ public host: the browser resolves the name itself (split-horizon DNS, or a proxy handed the+ name over HTTP CONNECT / SOCKS remote DNS), so a name the worker cannot resolve can still+ reach an internal address from the browser's network. It is a refusal, and callers that report+ the destination to a customer distinguish it so our own resolver's silence is not described as+ the target being internal.+ """normalized = _normalize_host(host)if not normalized:- return True+ return HostVerdict.DENYif _is_allowed_host(host):- return False+ return HostVerdict.ALLOWblocked = {b.lower().rstrip(".") for b in settings.BLOCKED_HOSTS}if normalized in blocked or _is_internal_hostname(normalized):- return True+ return HostVerdict.DENYip: ipaddress.IPv4Address | ipaddress.IPv6Address | Nonetry:@@ -260,24 +285,28 @@ def is_blocked_host(host: str, *, resolve_dns: bool = False) -> bool:except ValueError:ip = Noneexcept Exception:- return True+ return HostVerdict.DENYif ip is not None:- return is_blocked_ip(ip)+ return HostVerdict.DENY if is_blocked_ip(ip) else HostVerdict.ALLOWif not resolve_dns:- return False+ return HostVerdict.ALLOWtry:resolve_fetch_host_ips(normalized)except UnresolvableHost:- # UnresolvableHost subclasses BlockedHost, so it must be caught first. The browser resolves- # through the run proxy and may reach hosts the worker cannot; worker resolution failure is- # not a policy signal. Literal internal IPs and internal names are refused above, before DNS.- return False+ # UnresolvableHost subclasses BlockedHost, so it must be caught first.+ return HostVerdict.UNRESOLVABLEexcept BlockedHost:- return True- return False+ return HostVerdict.DENY+ return HostVerdict.ALLOW+++def is_blocked_host(host: str, *, resolve_dns: bool = False) -> bool:+ # Anything short of an explicit allow is refused, so a destination admitted here was admitted+ # because the check passed and not because it could not run.+ return classify_host(host, resolve_dns=resolve_dns) is not HostVerdict.ALLOWdef resolve_fetch_host_ips(host: str) -> tuple[str, ...]:@@ -335,9 +364,10 @@ def host_has_no_address_record(host: str) -> bool:Deliberately narrower than ``UnresolvableHost``: a timeout or SERVFAIL means the resolver couldnot answer, and reading that as a dead host would misattribute every navigation failure during- a resolver outage. Callers use this to attribute a navigation that already failed, never to- decide whether one is allowed -- ``is_blocked_host`` ignores worker-side resolution failures- for that reason, since the browser resolves through the run proxy.+ a resolver outage. Callers use this to attribute a navigation that is already being refused or+ has already failed, never to decide whether one is allowed -- an unresolvable destination is+ refused by ``classify_host`` either way, and this only separates "the target is gone" from+ "we could not check it"."""normalized = _normalize_host(host)if not normalized:diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex c2734e69c..6b99ac73e 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -101,7 +101,6 @@ from skyvern.exceptions import (SkyvernException,SkyvernHTTPException,SkyvernPageAnalysisTimeout,- UnresolvableHost,)from skyvern.experimentation.wait_utils import get_or_create_wait_config, get_wait_timefrom skyvern.forge import app@@ -12085,10 +12084,6 @@ def _get_click_count(action: ClickAction | UploadFileAction) -> int:return 1-def _is_policy_blocked_host(error: Exception) -> bool:- return isinstance(error, BlockedHost) and not isinstance(error, UnresolvableHost)--async def _locator_click(locator: Locator,click_count: int,@@ -12196,9 +12191,8 @@ async def chain_click(action_results = [ActionSuccess()]return action_results- # The browser resolves through the run proxy and may reach hosts the worker cannot;- # worker resolution failure is not a policy signal.- if _is_policy_blocked_host(e):+ # A refused or unresolvable href cannot be made safe by dispatching a click fallback.+ if isinstance(e, BlockedHost):action_results = [ActionFailure(FailToClick(action.element_id, msg=str(e)))]return action_results@@ -12354,9 +12348,8 @@ async def chain_click(try:navigated_href = await skyvern_element.try_navigate_via_href(page=page)except BlockedHost as e:- if _is_policy_blocked_host(e):- action_results = [ActionFailure(FailToClick(action.element_id, msg=str(e)))]- return action_results+ action_results = [ActionFailure(FailToClick(action.element_id, msg=str(e)))]+ return action_resultselse:if navigated_href:LOG.info(diff --git a/skyvern/webeye/navigation.py b/skyvern/webeye/navigation.pyindex 46a1db1d5..d5d35899c 100644--- a/skyvern/webeye/navigation.py+++ b/skyvern/webeye/navigation.py@@ -24,10 +24,19 @@ from skyvern.exceptions import (UnresolvableNavigationHost,)from skyvern.forge.sdk.core import skyvern_context-from skyvern.utils.url_validators import canonical_navigation_host, host_has_no_address_record, is_blocked_host+from skyvern.utils.url_validators import (+ HostVerdict,+ canonical_navigation_host,+ classify_host,+ host_has_no_address_record,+)LOG = structlog.get_logger()+# Both carry no part of the URL, so a redacting caller can re-raise the refusal verbatim.+_NO_ADDRESS_RECORD_REFUSAL = "refused before dispatch: the resolver reports no address record"+_UNVERIFIABLE_HOST_REASON = "unverifiable host: the destination could not be resolved for review"+NavigateFunc = Callable[[str], Awaitable[object]]SettleFunc = Callable[[], Awaitable[None]]SleepFunc = Callable[[float], Awaitable[None]]@@ -42,12 +51,14 @@ _MAX_REDIRECT_HOPS = 100def validate_navigation_destination(url: str) -> None:- """Fail closed unless ``url`` targets a public http(s) destination.+ """Fail closed unless ``url`` targets a verified public http(s) destination.Rejects local-resource schemes (``file://`` and anything other than http/https) andprivate, loopback, link-local, metadata, or otherwise-internal hosts, including- public-looking names that resolve to internal addresses. Every navigation entry point- that funnels through navigate_with_retry is guarded identically.+ public-looking names that resolve to internal addresses. A host the worker cannot resolve+ is refused too: the browser resolves the name itself, so a split-horizon record can reach an+ internal address the worker's view never shows. Every navigation entry point that funnels+ through navigate_with_retry is guarded identically.The host comes from the browser's WHATWG canonicalization, not stdlib urlparse, sonumeric-IP and backslash authority tricks that resolve to an internal host are caught."""@@ -59,8 +70,17 @@ def validate_navigation_destination(url: str) -> None:except InvalidUrl as error:raise BlockedNavigationDestination(url=url, reason="unsupported scheme or malformed url") from error- if not host or is_blocked_host(host, resolve_dns=True):+ verdict = classify_host(host, resolve_dns=True) if host else HostVerdict.DENY+ if verdict is HostVerdict.ALLOW:+ return+ if verdict is HostVerdict.DENY:raise BlockedNavigationDestination(url=url, reason="internal, loopback, link-local, or metadata host")+ # Refusing is the policy decision; naming the target internal would not be. A name the resolver+ # says has no address record is the target's own state, so it keeps the dead-host verdict and+ # error code instead of reading as our egress or our policy having rejected a live site.+ if host and host_has_no_address_record(host):+ raise UnresolvableNavigationHost(url=url, host=host, error_message=_NO_ADDRESS_RECORD_REFUSAL)+ raise BlockedNavigationDestination(url=url, reason=_UNVERIFIABLE_HOST_REASON)def _navigation_hop_urls(response: object) -> list[str]:@@ -285,6 +305,12 @@ async def navigate_with_retry(if not redacting:raiseraise BlockedNavigationDestination(url=display_url, reason=error.reason) from None+ except UnresolvableNavigationHost as error:+ if not redacting:+ raise+ raise UnresolvableNavigationHost(+ url=display_url, host=error.host, error_message=_NO_ADDRESS_RECORD_REFUSAL+ ) from Nonefor attempt in range(retry_times):strategy = degradation[min(attempt, len(degradation) - 1)]@@ -310,6 +336,14 @@ async def navigate_with_retry(# This one names a refused redirect hop, not the requested url, so redact that hop# rather than substituting the caller's display value for it.raise BlockedNavigationDestination(url=redact_url_secrets(error.url), reason=error.reason) from None+ except UnresolvableNavigationHost as error:+ # Only redirect revalidation reaches here, and the browser already fetched the hop it+ # refused: our resolver's silence is a split-horizon view of a live host, not the dead+ # target the verdict would otherwise claim.+ raise BlockedNavigationDestination(+ url=redact_url_secrets(error.url) if redacting else error.url,+ reason=_UNVERIFIABLE_HOST_REASON,+ ) from (None if redacting else error)except Exception as error:error_str = str(error)# Playwright names the destination in its own message, so the raw text carries thediff --git a/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py b/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.pyindex d49609355..c445a08ea 100644--- a/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py+++ b/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py@@ -4,6 +4,7 @@ user keeps interacting with a surface that no longer receives input."""import asyncioimport json+import socketfrom types import SimpleNamespacefrom typing import Anyfrom unittest.mock import Mock@@ -288,10 +289,14 @@ class _BlockingWebSocket(_FakeWebSocket):self.disconnected.set()[email protected]("public_dns")class TestNavigateEvent:"""SKY-13683: a live-view URL input navigates the remote page over the existingcdp_input WebSocket, gated by the same take-control check as mouse/keyboard input,- and validated through the same SSRF guard every real page navigation goes through."""+ and validated through the same SSRF guard every real page navigation goes through.++ The guard refuses a destination it cannot resolve, so these fixture origins need an address+ to get past it; a test that wants the refusal stubs the resolver itself."""@pytest.mark.asyncioasync def test_dropped_when_not_in_control(self) -> None:@@ -337,14 +342,14 @@ class TestNavigateEvent:channel = SimpleNamespace(interactor="user", client_id="c1")websocket = _FakeWebSocket([- json.dumps({"type": "navigateEvent", "url": "https://unresolvable.invalid"}),+ json.dumps({"type": "navigateEvent", "url": "https://refused.example.test"}),json.dumps({"type": "mouseEvent", "eventType": "mouseMoved", "x": 10, "y": 20}),])await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")- assert page.goto_calls == ["https://unresolvable.invalid", "about:blank"]+ assert page.goto_calls == ["https://refused.example.test", "about:blank"]assert page.goto_timeouts == [None, 5000]assert websocket.sent_json == [{"kind": "navigate-error", "reason": "failed"}]assert session.sent == [@@ -412,14 +417,14 @@ class TestNavigateEvent:channel = SimpleNamespace(interactor="user", client_id="c1")websocket = _PendingCommitWebSocket([- json.dumps({"type": "navigateEvent", "url": "https://public.invalid"}),+ json.dumps({"type": "navigateEvent", "url": "https://slow.example.test"}),json.dumps({"type": "mouseEvent", "eventType": "mouseMoved", "x": 10, "y": 20}),])await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")- assert page.goto_calls == ["https://public.invalid", "about:blank"]+ assert page.goto_calls == ["https://slow.example.test", "about:blank"]assert input_dispatch_urls == ["about:blank"]assert websocket.sent_json == [{"kind": "navigate-error", "reason": "failed"}]assert websocket.closed is None@@ -443,14 +448,14 @@ class TestNavigateEvent:channel = SimpleNamespace(interactor="user", client_id="c1")websocket = _FakeWebSocket([- json.dumps({"type": "navigateEvent", "url": "https://public.invalid"}),+ json.dumps({"type": "navigateEvent", "url": "https://redirected.example.test"}),json.dumps({"type": "mouseEvent", "eventType": "mouseMoved", "x": 10, "y": 20}),])await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")- assert page.goto_calls == ["https://public.invalid", "about:blank"]+ assert page.goto_calls == ["https://redirected.example.test", "about:blank"]assert input_dispatch_urls == ["about:blank"]assert websocket.sent_json == [{"kind": "navigate-error", "reason": "failed"}]assert websocket.closed is None@@ -473,6 +478,36 @@ class TestNavigateEvent:assert websocket.sent_json == [{"kind": "navigate-error", "reason": "blocked"}]assert websocket.closed is None+ @pytest.mark.asyncio+ async def test_unresolvable_destination_is_refused_without_closing_the_input_channel(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ """SKY-18836: the guard refuses a host it cannot resolve, so this path is newly reachable.+ The viewer must get a navigate-error and keep typing; closing the channel would strand the+ session on one dead URL."""++ def no_such_host(*args: object, **kwargs: object) -> list[object]:+ raise socket.gaierror(socket.EAI_NONAME, "nodename nor servname provided, or not known")++ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", no_such_host)+ page = _FakeNavigablePage()+ session = _FakeSession("s")+ input_session = _FakeInputSession(session, page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket(+ [+ json.dumps({"type": "navigateEvent", "url": "https://gone.example.com/"}),+ json.dumps({"type": "mouseEvent", "eventType": "mouseMoved", "x": 10, "y": 20}),+ ]+ )++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == []+ assert websocket.sent_json == [{"kind": "navigate-error", "reason": "failed"}]+ assert websocket.closed is None+ assert len(session.sent) == 1+@pytest.mark.asyncioasync def test_rejects_empty_url_without_dispatching(self) -> None:page = _FakeNavigablePage()@@ -575,6 +610,7 @@ def _dispatched(session: _FakeSession) -> list[tuple[str, dict]]:return [call for call in session.sent if call[0] != "Page.getNavigationHistory"][email protected]("public_dns")class TestHistoryNavigation:"""SKY-13724: the live-view browser chrome drives back/forward/reload over the samecdp_input socket, behind the same take-control gate, and re-validates the history entrydiff --git a/tests/unit/test_chain_click_blocked_anchor_navigation.py b/tests/unit/test_chain_click_blocked_anchor_navigation.pyindex fff3b0c56..26cc5e6ec 100644--- a/tests/unit/test_chain_click_blocked_anchor_navigation.py+++ b/tests/unit/test_chain_click_blocked_anchor_navigation.py@@ -307,12 +307,12 @@ def _public_resolver(host: str, port: int | None, *args: object, **kwargs: objecreturn [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", port or 0))]-_UNRESOLVABLE_HOST = "unresolvable.example.test"+_UNRESOLVABLE_HOST = "service.internal.test"def _resolver_with_unresolvable_host(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:if host == _UNRESOLVABLE_HOST:- raise OSError("dns unavailable")+ raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")return _public_resolver(host, port, *args, **kwargs)@@ -443,6 +443,9 @@ class TestChainClickBlockedAnchorNavigation:from skyvern.webeye.actions import handler as handler_modulefrom skyvern.webeye.actions.actions import ClickAction+ if not real_href_navigation:+ monkeypatch.setattr(handler_module, "validate_fetch_url", lambda url: url)+# Stub out the entire pre-blocking-check block so we jump straight to# find_blocking_element. In chain_click, the first Playwright click# is invoked via EventStrategyFactory.click_element; make it raise so@@ -670,25 +673,29 @@ class TestChainClickBlockedAnchorNavigation:coordinate_click.assert_not_awaited()@pytest.mark.asyncio- async def test_unresolvable_anchor_entry_url_continues_to_coordinate_click(+ async def test_unresolvable_internal_anchor_href_is_terminal_without_click_dispatch(self, monkeypatch: pytest.MonkeyPatch) -> None:- from skyvern.webeye.actions.responses import ActionSuccess+ from skyvern.webeye.actions.responses import ActionFailuremonkeypatch.setattr(dom_module, "validate_fetch_url", validate_fetch_url)monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _resolver_with_unresolvable_host)+ js_click = AsyncMock()results, coordinate_click, navigate = await self._run_chain_click(monkeypatch,blocked=True,tag=InteractiveElement.A,- href=f"https://{_UNRESOLVABLE_HOST}/target",+ href=f"http://{_UNRESOLVABLE_HOST}:8099/secret",real_href_navigation=True,+ js_mock=js_click,)- assert isinstance(results[-1], ActionSuccess)+ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)navigate.assert_awaited_once()- coordinate_click.assert_awaited_once()+ coordinate_click.assert_not_awaited()+ js_click.assert_not_awaited()@pytest.mark.asyncioasync def test_blocked_anchor_redirect_hop_is_terminal_without_coordinate_click(@@ -715,10 +722,10 @@ class TestChainClickBlockedAnchorNavigation:coordinate_click.assert_not_awaited()@pytest.mark.asyncio- async def test_unresolvable_anchor_redirect_hop_continues_to_coordinate_click(+ async def test_unresolvable_anchor_redirect_hop_is_terminal_without_coordinate_click(self, monkeypatch: pytest.MonkeyPatch) -> None:- from skyvern.webeye.actions.responses import ActionSuccess+ from skyvern.webeye.actions.responses import ActionFailurehref = "https://external.example.test/target"monkeypatch.setattr(dom_module, "validate_fetch_url", validate_fetch_url)@@ -733,9 +740,10 @@ class TestChainClickBlockedAnchorNavigation:navigation_response=_redirect_chain(href, f"https://{_UNRESOLVABLE_HOST}/final"),)- assert isinstance(results[-1], ActionSuccess)+ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)navigate.assert_awaited_once()- coordinate_click.assert_awaited_once()+ coordinate_click.assert_not_awaited()def test_dom_module_exports_try_navigate_via_href() -> None:@@ -1010,25 +1018,26 @@ class TestChainClickNavigationDestinationGuard:return results, elem, navigate, locator_click@pytest.mark.asyncio- async def test_unresolvable_host_reaches_coordinate_click(self, monkeypatch: pytest.MonkeyPatch) -> None:- from skyvern.webeye.actions.responses import ActionSuccess+ async def test_unresolvable_host_is_terminal_without_any_click(self, monkeypatch: pytest.MonkeyPatch) -> None:+ from skyvern.webeye.actions.responses import ActionFailuredef fails_dns(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:- raise OSError("dns unavailable")+ raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", fails_dns)with pytest.raises(BlockedHost) as exc_info:- validate_fetch_url("https://unresolvable.example.test/target")+ validate_fetch_url(f"http://{_UNRESOLVABLE_HOST}:8099/secret")results, elem, navigate, locator_click = await self._run(monkeypatch,navigation_error=exc_info.value,)- assert isinstance(results[-1], ActionSuccess)+ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)navigate.assert_awaited_once()locator_click.assert_not_awaited()- elem.coordinate_click.assert_awaited_once()+ elem.coordinate_click.assert_not_awaited()elem.click_in_javascript.assert_not_awaited()@pytest.mark.asyncio@@ -1211,22 +1220,26 @@ class TestChainClickPreDispatchAnchorGuard:elem.coordinate_click.assert_not_awaited()@pytest.mark.asyncio- async def test_unresolvable_href_reaches_coordinate_click(self, monkeypatch: pytest.MonkeyPatch) -> None:- from skyvern.webeye.actions.responses import ActionSuccess+ async def test_unresolvable_internal_href_is_refused_before_click_dispatch(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ from skyvern.webeye.actions.responses import ActionFailuredef fails_dns(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:- raise OSError("dns unavailable")+ raise socket.gaierror(socket.EAI_NONAME, "Name or service not known")monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", fails_dns)- href = "https://unresolvable.example.test/target"+ href = f"http://{_UNRESOLVABLE_HOST}:8099/secret"validator = MagicMock(wraps=validate_fetch_url)results, elem, click_element, _ = await self._run(monkeypatch, href=href, validator=validator)- assert isinstance(results[-1], ActionSuccess)+ assert len(results) == 1+ assert isinstance(results[0], ActionFailure)validator.assert_called_once_with(href)click_element.assert_not_awaited()- elem.coordinate_click.assert_awaited_once()+ elem.locator.click.assert_not_awaited()+ elem.coordinate_click.assert_not_awaited()elem.click_in_javascript.assert_not_awaited()@pytest.mark.asynciodiff --git a/tests/unit/test_code_block_sandbox.py b/tests/unit/test_code_block_sandbox.pyindex 75157d457..e26428ab5 100644--- a/tests/unit/test_code_block_sandbox.py+++ b/tests/unit/test_code_block_sandbox.py@@ -3095,6 +3095,7 @@ class TestCodeBlockMagicLink:return captured@pytest.mark.asyncio+ @pytest.mark.usefixtures("public_dns")async def test_polls_for_a_link_and_navigates_once_without_filling(self, monkeypatch: pytest.MonkeyPatch) -> None:from skyvern.forge.sdk.schemas.totp_codes import OTPTypefrom skyvern.forge.sdk.workflow.models.block import _bind_code_block_magic_linkdiff --git a/tests/unit/test_url_validators.py b/tests/unit/test_url_validators.pyindex 8a3269394..677ebf49f 100644--- a/tests/unit/test_url_validators.py+++ b/tests/unit/test_url_validators.py@@ -19,6 +19,8 @@ from skyvern.schemas.run_blocks import BaseRunBlockRequestfrom skyvern.schemas.runs import BlockRunRequest, TaskRunRequest, WorkflowRunRequestfrom skyvern.schemas.workflows import WorkflowCreateYAMLRequestfrom skyvern.utils.url_validators import (+ HostVerdict,+ classify_host,encode_url,is_blocked_host,prepend_scheme_and_validate_url,@@ -349,15 +351,19 @@ def test_is_blocked_host_allows_public_dns_answers(monkeypatch: pytest.MonkeyPatassert is_blocked_host("public.example.test", resolve_dns=True) is False-def test_is_blocked_host_does_not_treat_worker_dns_failure_as_a_policy_block(+def test_is_blocked_host_refuses_a_host_the_worker_cannot_resolve(monkeypatch: pytest.MonkeyPatch,) -> None:+ # SKY-18836: the browser resolves the name itself, so a public-suffix name with a+ # split-horizon internal A record is NXDOMAIN here and 10.x there. Admitting it because our+ # own check could not run is an egress-policy bypass, not a lenient read of DNS.def fails_dns(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:raise OSError("dns unavailable")monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", fails_dns)- assert is_blocked_host("public.example.test", resolve_dns=True) is False+ assert is_blocked_host("public.example.test", resolve_dns=True) is True+ assert classify_host("public.example.test", resolve_dns=True) is HostVerdict.UNRESOLVABLE@pytest.mark.parametrize("host", ["10.0.0.5", "127.0.0.1", "169.254.169.254", "localhost"])diff --git a/tests/unit/webeye/test_navigation_retry.py b/tests/unit/webeye/test_navigation_retry.pyindex d1a9d6a31..92bd2d78a 100644--- a/tests/unit/webeye/test_navigation_retry.py+++ b/tests/unit/webeye/test_navigation_retry.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport reimport socket+from collections.abc import Callablefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock@@ -246,6 +247,25 @@ def _resolver_cannot_answer(*args: object, **kwargs: object) -> list[object]:raise socket.gaierror(socket.EAI_AGAIN, "Temporary failure in name resolution")+def _resolves_once_then(failure: Callable[..., list[object]]) -> Callable[..., list[object]]:+ """A resolver that answers the pre-dispatch check and then stops answering.++ The pre-dispatch guard refuses a host it cannot resolve, so this is what still reaches the+ post-failure corroboration: a record that was there when the destination was reviewed and+ gone (or unanswerable) by the time the navigation failed.+ """+ answered = False++ def resolve(*args: object, **kwargs: object) -> list[object]:+ nonlocal answered+ if answered:+ return failure(*args, **kwargs)+ answered = True+ return [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", 0))]++ return resolve++async def _navigate_failing_with(error_message: str) -> FailedToNavigateToUrl:page = AsyncMock()page.goto = AsyncMock(side_effect=Exception(error_message))@@ -276,7 +296,7 @@ async def test_a_proxy_error_for_a_host_with_no_address_record_is_not_blamed_on_) -> None:# A proxied browser hands the hostname to the proxy, so a domain whose record is gone comes# back as the proxy failing to open a tunnel rather than as ERR_NAME_NOT_RESOLVED.- monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _no_such_host)+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _resolves_once_then(_no_such_host))error = await _navigate_failing_with(error_message)@@ -303,7 +323,7 @@ async def test_a_resolver_that_cannot_answer_does_not_reattribute_a_proxy_error() -> None:# EAI_AGAIN means the resolver never answered. Reading it as a dead host would reclassify every# egress failure fleet-wide the moment worker DNS degrades.- monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _resolver_cannot_answer)+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _resolves_once_then(_resolver_cannot_answer))error = await _navigate_failing_with("net::ERR_TUNNEL_CONNECTION_FAILED")@@ -350,15 +370,55 @@ def test_validate_navigation_destination_allows_public_targets(url: str) -> Nonevalidate_navigation_destination(url)-def test_validate_navigation_destination_allows_a_host_the_worker_cannot_resolve(+async def test_a_host_the_worker_cannot_resolve_is_refused_before_dispatch(monkeypatch: pytest.MonkeyPatch,) -> None:- def fails_dns(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:- raise OSError("dns unavailable")+ # SKY-18836: the browser resolves the hostname itself, so a public-suffix name with a+ # split-horizon internal A record is NXDOMAIN here and 10.x there. Admitting it because our+ # own check could not run would hand the browser an unreviewed internal destination.+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _no_such_host)+ page = AsyncMock()+ page.goto = AsyncMock()- monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", fails_dns)+ with pytest.raises(UnresolvableNavigationHost) as raised:+ await navigate_with_retry(+ navigate=lambda strategy: page.goto(_DEAD_URL, wait_until=strategy),+ url=_DEAD_URL,+ retry_times=5,+ settle=AsyncMock(),+ sleep=AsyncMock(),+ )++ page.goto.assert_not_awaited()+ # A resolver that answers "this name has no address" is reporting the target's own state, so+ # the refusal keeps the dead-host verdict rather than calling a public name internal.+ assert raised.value.host == "gone.example.com"+ assert is_egress_attributable_navigation_failure(raised.value) is False+++async def test_a_destination_the_resolver_cannot_answer_for_is_refused_before_dispatch(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", _resolver_cannot_answer)+ page = AsyncMock()+ page.goto = AsyncMock()++ with pytest.raises(BlockedNavigationDestination) as raised:+ await navigate_with_retry(+ navigate=lambda strategy: page.goto(_DEAD_URL, wait_until=strategy),+ url=_DEAD_URL,+ retry_times=5,+ settle=AsyncMock(),+ sleep=AsyncMock(),+ )- validate_navigation_destination("https://public.example.test/path")+ page.goto.assert_not_awaited()+ # A resolver that could not answer says nothing about the target, so the refusal must not+ # claim it is internal either.+ assert "unverifiable host" in raised.value.reason+ assert not isinstance(raised.value, UnresolvableNavigationHost)@pytest.mark.parametrize("url", BLOCKED_DESTINATIONS)diff --git a/tests/unit/workflow/test_web_search.py b/tests/unit/workflow/test_web_search.pyindex 7bc5f965c..7a9063bec 100644--- a/tests/unit/workflow/test_web_search.py+++ b/tests/unit/workflow/test_web_search.py@@ -2,6 +2,7 @@ import asyncioimport pytest+from skyvern.forge.sdk import browser_egress_policyfrom skyvern.forge.sdk.browser_egress_policy import DestinationBlockedErrorfrom skyvern.forge.sdk.settings_manager import SettingsManagerfrom skyvern.forge.sdk.workflow import web_search, web_search_client@@ -269,6 +270,25 @@ async def test_withheld_results_are_counted_rather_than_read_as_an_empty_search(assert observed["withheld_count"] == 3+async def test_a_result_the_worker_cannot_resolve_is_withheld(monkeypatch: pytest.MonkeyPatch) -> None:+ """SKY-18836, against the real classifier rather than a stand-in: admission has to require an+ explicit allow, or a search result whose name resolves to an internal address only from the+ browser's network is handed to the agent as a reviewed public destination."""++ def no_such_host(_host: str) -> list[str]:+ raise OSError("no such host")++ arm_search_api(monkeypatch, (200, PAGE))+ monkeypatch.setattr(browser_egress_policy, "resolve_host", no_such_host)+ monkeypatch.setattr(web_search, "classify_url_async", browser_egress_policy.classify_url_async)++ observed = await search_web("roofing")++ assert observed["results"] == []+ assert observed["withheld_count"] == 3++@pytest.mark.asyncio@pytest.mark.parametrize("failure", [UnicodeError("label too long"), DestinationBlockedError("blocked egress: URL has no host")]