Skip to content

Automate browser based workflows with AI

fix: harden SSRF validation for resolved hosts (#7491)

Aaron Perez4c4d68360ec2
diff --git a/skyvern/forge/sdk/api/llm/api_handler_factory.py b/skyvern/forge/sdk/api/llm/api_handler_factory.py
index bc36c9336..e76d76b77 100644
--- a/skyvern/forge/sdk/api/llm/api_handler_factory.py
+++ b/skyvern/forge/sdk/api/llm/api_handler_factory.py
@@ -1,3 +1,4 @@
+import asyncio
import copy
import dataclasses
import json
@@ -11,6 +12,7 @@ import structlog
from anthropic import NOT_GIVEN
from anthropic.types.beta.beta_message import BetaMessage as AnthropicMessage
from jinja2 import Template
+from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm.types.router import AllowedFailsPolicy
from litellm.utils import CustomStreamWrapper, ModelResponse
from openai import APIError, AsyncOpenAI, RateLimitError
@@ -51,6 +53,7 @@ from skyvern.forge.sdk.experimentation.prompt_families import effective_prompt_s
from skyvern.forge.sdk.models import SpeculativeLLMMetadata, Step
from skyvern.forge.sdk.schemas.ai_suggestions import AISuggestion
from skyvern.forge.sdk.schemas.task_v2 import TaskV2, Thought
+from skyvern.forge.sdk.settings_manager import SettingsManager
from skyvern.forge.sdk.trace import apply_context_attrs, traced
from skyvern.schemas.llm import (
LLMAllowedFailsPolicy,
@@ -59,6 +62,7 @@ from skyvern.schemas.llm import (
)
from skyvern.utils.image_resizer import Resolution, get_resize_target_dimension, resize_screenshots
from skyvern.utils.image_token_estimator import estimate_image_cost, estimate_image_tokens, provider_image_tokens
+from skyvern.utils.url_validators import validate_fetch_url
# Keep this server-only side effect out of the package __init__ so the legacy
# models shim can import without litellm. Legacy LLM calls enter this module.
@@ -66,6 +70,39 @@ configure_litellm_transport()
LOG = structlog.get_logger()
+
+class _NoRedirectAsyncHTTPHandler(AsyncHTTPHandler):
+ def create_client(self, *args: Any, **kwargs: Any) -> Any:
+ client = super().create_client(*args, **kwargs)
+ client.follow_redirects = False
+ return client
+
+
+def _build_custom_llm_http_client(llm_key: str, llm_config: LLMConfig) -> AsyncOpenAI | AsyncHTTPHandler | None:
+ if not is_custom_llm_key(llm_key):
+ return None
+
+ litellm_params = llm_config.litellm_params or {}
+ if llm_config.model_name.startswith("openai/"):
+ return AsyncOpenAI(
+ api_key=litellm_params.get("api_key"),
+ base_url=litellm_params.get("api_base"),
+ http_client=ForgeAsyncHttpxClientWrapper(follow_redirects=False),
+ )
+
+ return _NoRedirectAsyncHTTPHandler()
+
+
+async def _validate_custom_llm_api_base(llm_key: str, llm_config: LLMConfig | LLMRouterConfig) -> None:
+ if not is_custom_llm_key(llm_key) or SettingsManager.get_settings().ALLOW_CUSTOM_LLM_LOCAL_API_BASES:
+ return
+ if not isinstance(llm_config, LLMConfig):
+ raise InvalidLLMConfigError(llm_key)
+ api_base = (llm_config.litellm_params or {}).get("api_base")
+ if isinstance(api_base, str):
+ await asyncio.to_thread(validate_fetch_url, api_base)
+
+
# Vertex returns this trafficType for flex-tier calls. litellm (through 1.88.1 and
# main) neither maps it nor applies tier pricing in completion_cost, so flex bills at
# standard; we halve it at the cost sites (flex = 50% of standard, Google Vertex SKU).
@@ -1977,7 +2014,7 @@ class LLMAPIHandlerFactory:
_llm_span.set_attribute("llm_key", llm_key)
_llm_span.set_attribute("llm_model", llm_config.model_name)
_llm_span.set_attribute("prompt_name", prompt_name)
- active_parameters = base_parameters or {}
+ active_parameters = dict(base_parameters or {})
if parameters is None:
parameters = LLMAPIHandlerFactory.get_api_parameters(llm_config)
@@ -2024,6 +2061,7 @@ class LLMAPIHandlerFactory:
_bundle_response: bytes | None = None
_bundle_parsed: bytes | None = None
_bundle_rendered: bytes | None = None
+ custom_http_client: AsyncOpenAI | AsyncHTTPHandler | None = None
try:
if context and context.hashed_href_map and should_persist_llm_artifacts:
if _should_bundle:
@@ -2188,6 +2226,10 @@ class LLMAPIHandlerFactory:
t_llm_request = time.perf_counter()
llm_duration_seconds = 0.0
try:
+ await _validate_custom_llm_api_base(llm_key, llm_config)
+ custom_http_client = _build_custom_llm_http_client(llm_key, llm_config)
+ if custom_http_client is not None:
+ active_parameters["client"] = custom_http_client
# TODO (kerem): add a retry mechanism to this call (acompletion_with_retries)
# TODO (kerem): use litellm fallbacks? https://litellm.vercel.app/docs/tutorials/fallbacks#how-does-completion_with_fallbacks-work
response = await litellm.acompletion(
@@ -2443,6 +2485,11 @@ class LLMAPIHandlerFactory:
return parsed_response
finally:
+ if custom_http_client is not None:
+ try:
+ await custom_http_client.close()
+ except Exception:
+ LOG.warning("Failed to close custom LLM HTTP client", llm_key=llm_key, exc_info=True)
# Post-response artifact persistence cluster. bulk_create_artifacts
# does the S3 uploads + DB rows for every LLM artifact queued during
# this request; the bundled-path archive accumulate is fast in-memory.
@@ -2574,15 +2621,17 @@ class LLMCaller:
self.openai_client = None
openrouter_model_name = LLMAPIHandlerFactory._openrouter_model_name(self.llm_key, self.llm_config)
+ self._custom_openrouter = bool(openrouter_model_name and is_custom_llm_key(self.original_llm_key))
# openrouter/ keys always resolve to LLMConfig, never LLMRouterConfig
if openrouter_model_name and isinstance(self.llm_config, LLMConfig):
self.llm_key = openrouter_model_name.replace("openrouter/", "")
- litellm_params = self.llm_config.litellm_params or {}
- self.openai_client = AsyncOpenAI(
- api_key=litellm_params.get("api_key") or settings.OPENROUTER_API_KEY,
- base_url=litellm_params.get("api_base") or settings.OPENROUTER_API_BASE,
- http_client=ForgeAsyncHttpxClientWrapper(),
- )
+ if not self._custom_openrouter:
+ litellm_params = self.llm_config.litellm_params or {}
+ self.openai_client = AsyncOpenAI(
+ api_key=litellm_params.get("api_key") or settings.OPENROUTER_API_KEY,
+ base_url=litellm_params.get("api_base") or settings.OPENROUTER_API_BASE,
+ http_client=ForgeAsyncHttpxClientWrapper(),
+ )
elif self.llm_key == "OPENAI_COMPATIBLE" and LLMAPIHandlerFactory.is_github_copilot_endpoint():
# For GitHub Copilot, use the actual model name from OPENAI_COMPATIBLE_MODEL_NAME
self.llm_key = settings.OPENAI_COMPATIBLE_MODEL_NAME or self.llm_key
@@ -3023,7 +3072,16 @@ class LLMCaller:
timeout: float = settings.LLM_CONFIG_TIMEOUT,
**active_parameters: dict[str, Any],
) -> ModelResponse | CustomStreamWrapper | AnthropicMessage | UITarsResponse:
- if self.openai_client:
+ await _validate_custom_llm_api_base(self.original_llm_key, self.llm_config)
+ openai_client = self.openai_client
+ if self._custom_openrouter and isinstance(self.llm_config, LLMConfig):
+ litellm_params = self.llm_config.litellm_params or {}
+ openai_client = AsyncOpenAI(
+ api_key=litellm_params.get("api_key") or settings.OPENROUTER_API_KEY,
+ base_url=litellm_params.get("api_base") or settings.OPENROUTER_API_BASE,
+ http_client=ForgeAsyncHttpxClientWrapper(follow_redirects=False),
+ )
+ if openai_client:
# Extract OpenRouter-specific and GitHub Copilot-specific parameters
extra_headers = {}
if settings.SKYVERN_APP_URL:
@@ -3053,17 +3111,23 @@ class LLMCaller:
if active_parameters.get("reasoning_effort"):
openai_params["reasoning_effort"] = active_parameters["reasoning_effort"]
- completion = await self.openai_client.chat.completions.create(
- model=self.llm_key,
- messages=messages,
- extra_headers=extra_headers if extra_headers else None,
- timeout=timeout,
- **openai_params,
- )
- # Convert OpenAI ChatCompletion to litellm ModelResponse format
- # litellm.utils.convert_to_model_response_object expects a dict
- response_dict = completion.model_dump()
- return litellm.ModelResponse(**response_dict)
+ try:
+ completion = await openai_client.chat.completions.create(
+ model=self.llm_key,
+ messages=messages,
+ extra_headers=extra_headers if extra_headers else None,
+ timeout=timeout,
+ **openai_params,
+ )
+ # Convert OpenAI ChatCompletion to litellm ModelResponse format
+ # litellm.utils.convert_to_model_response_object expects a dict
+ return litellm.ModelResponse(**completion.model_dump())
+ finally:
+ if self._custom_openrouter:
+ try:
+ await openai_client.close()
+ except Exception:
+ LOG.warning("Failed to close custom OpenRouter client", exc_info=True)
if self.llm_key and "ANTHROPIC" in self.llm_key:
return await self._call_anthropic(messages, tools, timeout, **active_parameters)
diff --git a/skyvern/forge/sdk/routes/custom_llms.py b/skyvern/forge/sdk/routes/custom_llms.py
index 2a87f3f74..88ad3fe6a 100644
--- a/skyvern/forge/sdk/routes/custom_llms.py
+++ b/skyvern/forge/sdk/routes/custom_llms.py
@@ -1,3 +1,6 @@
+import asyncio
+from urllib.parse import urlparse
+
import structlog
from fastapi import Depends, HTTPException, Path
@@ -15,6 +18,7 @@ from skyvern.forge.sdk.schemas.custom_llms import (
CustomLLMConfig,
CustomLLMCreateRequest,
CustomLLMListResponse,
+ CustomLLMProvider,
CustomLLMResponse,
CustomLLMUpdateRequest,
custom_llm_from_org_auth_token,
@@ -22,10 +26,23 @@ from skyvern.forge.sdk.schemas.custom_llms import (
)
from skyvern.forge.sdk.schemas.organizations import ClearOrganizationAuthTokenResponse, Organization
from skyvern.forge.sdk.services import org_auth_service
+from skyvern.forge.sdk.settings_manager import SettingsManager
+from skyvern.utils.url_validators import validate_fetch_url
LOG = structlog.get_logger()
+async def _validate_custom_llm_api_base(config: CustomLLMConfig) -> None:
+ if SettingsManager.get_settings().ALLOW_CUSTOM_LLM_LOCAL_API_BASES or not config.api_base:
+ return
+ parsed = urlparse(config.api_base)
+ if parsed.scheme != "https" or parsed.port not in {None, 443}:
+ raise HTTPException(status_code=400, detail="Cloud api_base must use HTTPS on port 443")
+ if config.provider is CustomLLMProvider.OPENROUTER and parsed.hostname != "openrouter.ai":
+ raise HTTPException(status_code=400, detail="OpenRouter api_base must use openrouter.ai")
+ config.api_base = await asyncio.to_thread(validate_fetch_url, config.api_base)
+
+
async def _get_custom_llm(
organization_id: str,
custom_llm_id: str,
@@ -105,6 +122,7 @@ async def create_custom_llm(
request: CustomLLMCreateRequest,
current_org: Organization = Depends(org_auth_service.get_current_org),
) -> CustomLLMResponse:
+ await _validate_custom_llm_api_base(request.config)
token = await app.DATABASE.organizations.create_org_auth_token(
organization_id=current_org.organization_id,
token_type=OrganizationAuthTokenType.custom_llm,
@@ -136,6 +154,7 @@ async def update_custom_llm(
try:
existing_custom_llm = await _get_custom_llm(current_org.organization_id, custom_llm_id)
config = _config_with_preserved_api_key(request.config, existing_custom_llm)
+ await _validate_custom_llm_api_base(config)
token = await app.DATABASE.organizations.update_org_auth_token(
organization_id=current_org.organization_id,
token_type=OrganizationAuthTokenType.custom_llm,
diff --git a/skyvern/schemas/runs.py b/skyvern/schemas/runs.py
index 3ee34a251..04b16bed8 100644
--- a/skyvern/schemas/runs.py
+++ b/skyvern/schemas/runs.py
@@ -191,9 +191,16 @@ class TaskRunRequest(BaseModel):
return None
return normalize_run_with(v)
- @field_validator("url", "webhook_url", "totp_url")
+ @field_validator("url")
@classmethod
- def validate_urls(cls, url: str | None) -> str | None:
+ def validate_task_url(cls, url: str | None) -> str | None:
+ if not url:
+ return url
+ return validate_url(url)
+
+ @field_validator("webhook_url", "totp_url")
+ @classmethod
+ def validate_callback_urls(cls, url: str | None) -> str | None:
"""
Validates that URLs provided to Skyvern are properly formatted.
diff --git a/skyvern/services/task_v1_service.py b/skyvern/services/task_v1_service.py
index c327b5798..a040b44e2 100644
--- a/skyvern/services/task_v1_service.py
+++ b/skyvern/services/task_v1_service.py
@@ -1,3 +1,4 @@
+import asyncio
import hashlib
from typing import Any
@@ -20,6 +21,7 @@ from skyvern.forge.sdk.schemas.organizations import Organization
from skyvern.forge.sdk.schemas.task_generations import TaskGeneration, TaskGenerationBase
from skyvern.forge.sdk.schemas.tasks import Task, TaskRequest, TaskResponse, TaskStatus
from skyvern.schemas.runs import RunEngine, RunStatus, RunType
+from skyvern.utils.url_validators import validate_fetch_url
LOG = structlog.get_logger()
@@ -113,6 +115,8 @@ async def run_task(
background_tasks: BackgroundTasks | None = None,
) -> Task:
await _validate_task_v1_model_for_org(organization, task.model)
+ if task.url:
+ task.url = await asyncio.to_thread(validate_fetch_url, task.url)
created_task = await app.agent.create_task(task, organization.organization_id)
url_hash = generate_url_hash(task.url)
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index 5002782de..778cbcb5a 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -80,6 +80,7 @@ from skyvern.schemas.workflows import (
from skyvern.services.webhook_delivery import deliver_webhook_with_retries
from skyvern.utils.prompt_engine import load_prompt_with_elements
from skyvern.utils.strings import generate_random_string
+from skyvern.utils.url_validators import validate_fetch_url
from skyvern.webeye.actions.actions import ActionType
from skyvern.webeye.browser_state import BrowserState
from skyvern.webeye.scraper.scraped_page import ScrapedPage
@@ -312,6 +313,8 @@ async def initialize_task_v2(
trigger_type: WorkflowRunTriggerType | None = None,
) -> TaskV2:
await _validate_task_v2_model_for_org(organization, model)
+ if user_url:
+ user_url = await asyncio.to_thread(validate_fetch_url, user_url)
task_v2 = await app.DATABASE.observer.create_task_v2(
prompt=user_prompt,
@@ -452,9 +455,7 @@ async def initialize_task_v2_metadata(
url=url,
workflow_title=title,
)
- url = metadata.url
- if not url:
- raise UrlGenerationFailure()
+ metadata.url = await asyncio.to_thread(validate_fetch_url, metadata.url)
try:
await app.DATABASE.observer.update_thought(
diff --git a/skyvern/utils/url_validators.py b/skyvern/utils/url_validators.py
index 8cf15e26c..6ebc8f813 100644
--- a/skyvern/utils/url_validators.py
+++ b/skyvern/utils/url_validators.py
@@ -13,6 +13,22 @@ MAX_SAFE_REDIRECTS = 10
_BLOCKED_INTERNAL_HOSTNAMES = frozenset({"localhost", "metadata.google.internal", "kubernetes.default.svc"})
_BLOCKED_INTERNAL_SUFFIXES = (".local", ".localhost", ".internal", ".cluster.local")
+_BLOCKED_IP_NETWORKS = tuple(
+ ipaddress.ip_network(network)
+ for network in (
+ "127.0.0.0/8",
+ "10.0.0.0/8",
+ "172.16.0.0/12",
+ "192.168.0.0/16",
+ "169.254.0.0/16",
+ "100.64.0.0/10",
+ "::1/128",
+ "fc00::/7",
+ )
+)
+_BLOCKED_METADATA_IPS = frozenset(
+ ipaddress.ip_address(ip) for ip in ("169.254.169.254", "100.100.100.200", "fd00:ec2::254")
+)
def strip_query_params(url: str) -> str:
@@ -85,6 +101,10 @@ def _normalize_ip(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> ipaddres
def _is_blocked_ip(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
ip = _normalize_ip(ip)
+ if ip in _BLOCKED_METADATA_IPS:
+ return True
+ if any(ip.version == network.version and ip in network for network in _BLOCKED_IP_NETWORKS):
+ return True
return bool(
ip.is_private or ip.is_link_local or ip.is_loopback or ip.is_reserved or ip.is_multicast or ip.is_unspecified
)
@@ -144,24 +164,10 @@ def is_blocked_host(host: str, *, resolve_dns: bool = False) -> bool:
return False
try:
- infos = socket.getaddrinfo(normalized, None, type=socket.SOCK_STREAM)
- except OSError:
+ resolve_fetch_host_ips(normalized)
+ except BlockedHost:
return True
-
- resolved_any = False
- for info in infos:
- sockaddr = info[4]
- ip_str = sockaddr[0] if sockaddr else None
- if not ip_str:
- continue
- try:
- resolved_ip = ipaddress.ip_address(ip_str)
- except ValueError:
- continue
- resolved_any = True
- if _is_blocked_ip(resolved_ip):
- return True
- return not resolved_any
+ return False
def resolve_fetch_host_ips(host: str) -> tuple[str, ...]:
@@ -190,7 +196,7 @@ def resolve_fetch_host_ips(host: str) -> tuple[str, ...]:
try:
infos = socket.getaddrinfo(normalized, None, type=socket.SOCK_STREAM)
- except OSError:
+ except (OSError, UnicodeError):
raise BlockedHost(host=host)
resolved_ips: list[str] = []
@@ -224,7 +230,7 @@ def validate_url(url: str) -> str | None:
if not v.host:
return None
host = v.host
- blocked = is_blocked_host(host)
+ blocked = is_blocked_host(host, resolve_dns=False)
if blocked:
raise BlockedHost(host=host)
return str(v)
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index d6a9ef219..116f60a08 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -114,6 +114,7 @@ from skyvern.utils.prompt_engine import (
load_prompt_with_elements_tracked,
)
from skyvern.utils.prompt_truncation import truncate_extraction_schema, truncate_previous_extracted_information
+from skyvern.utils.url_validators import validate_fetch_url
from skyvern.webeye.actions import actions, handler_utils
from skyvern.webeye.actions.action_types import ActionType
from skyvern.webeye.actions.actions import (
@@ -4834,7 +4835,8 @@ async def handle_goto_url_action(
task: Task,
step: Step,
) -> list[ActionResult]:
- await page.goto(action.url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS)
+ validated_url = await asyncio.to_thread(validate_fetch_url, action.url)
+ await page.goto(validated_url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS)
# Navigation invalidates the current scraped page's element ids; stop the batch so the
# next step re-scrapes before any later actions run against the new DOM.
result = ActionSuccess()
@@ -4920,9 +4922,10 @@ async def handle_new_tab_action(
browser_state = app.BROWSER_MANAGER.get_for_task(task.task_id, workflow_run_id=task.workflow_run_id)
if browser_state is None:
return [ActionFailure(Exception("No browser state found for the task"), stop_execution_on_failure=False)]
+ validated_url = await asyncio.to_thread(validate_fetch_url, action.url)
new_page = await browser_state.new_page()
try:
- await browser_state.navigate_to_url(page=new_page, url=action.url)
+ await browser_state.navigate_to_url(page=new_page, url=validated_url)
except Exception as e:
# Don't leave a blank/failed tab as the newest page — the next scrape would fail it.
try:
diff --git a/skyvern/webeye/actions/parse_actions.py b/skyvern/webeye/actions/parse_actions.py
index bf061f93a..2b20f5935 100644
--- a/skyvern/webeye/actions/parse_actions.py
+++ b/skyvern/webeye/actions/parse_actions.py
@@ -302,7 +302,7 @@ def parse_action(
# LLM-generated navigation targets can be steered by page content, so block
# private/loopback/link-local hosts the same way the task URL boundary does.
host = urlparse(validated_url).hostname
- if not host or is_blocked_host(host):
+ if not host or is_blocked_host(host, resolve_dns=False):
LOG.warning("GOTO_URL action targets a blocked host, skipping action", url=validated_url)
return NullAction(**base_action_dict)
return GotoUrlAction(**base_action_dict, url=validated_url)
@@ -323,7 +323,7 @@ def parse_action(
LOG.warning("NEW_TAB action returned with an invalid url, skipping action", url=url)
return NullAction(**base_action_dict)
host = urlparse(validated_url).hostname
- if not host or is_blocked_host(host):
+ if not host or is_blocked_host(host, resolve_dns=False):
LOG.warning("NEW_TAB action targets a blocked host, skipping action", url=validated_url)
return NullAction(**base_action_dict)
return NewTabAction(**base_action_dict, url=validated_url)
diff --git a/skyvern/webeye/navigation.py b/skyvern/webeye/navigation.py
index c98a9c77e..3b98cb566 100644
--- a/skyvern/webeye/navigation.py
+++ b/skyvern/webeye/navigation.py
@@ -51,6 +51,8 @@ async def navigate_with_retry(
LOG.info("Trying to navigate to url", url=url, retry_time=attempt, wait_until=strategy)
try:
start_time = time.monotonic()
+ # SECURITY RESIDUAL (SKY-12525): redirects happen below this call; Route.fetch
+ # bypasses vendor networking. SKY-8484 tracks egress policy and future CDP interception.
await navigate(strategy)
elapsed = time.monotonic() - start_time
LOG.info("Page loading time", loading_time=elapsed, url=url, wait_until=strategy)
diff --git a/tests/unit/test_actions.py b/tests/unit/test_actions.py
index 7e01dde0a..e7476e014 100644
--- a/tests/unit/test_actions.py
+++ b/tests/unit/test_actions.py
@@ -619,6 +619,23 @@ def test_parse_goto_url_blocked_host_returns_null_action(url: str) -> None:
assert isinstance(action, NullAction)
[email protected]("action_type", ["GOTO_URL", "NEW_TAB"])
+def test_parse_navigation_does_not_resolve_dns(monkeypatch: pytest.MonkeyPatch, action_type: str) -> None:
+ resolver = MagicMock(side_effect=AssertionError("action parsing must not resolve DNS"))
+ monkeypatch.setattr(
+ "skyvern.utils.url_validators.socket.getaddrinfo",
+ resolver,
+ )
+
+ action = parse_action(
+ action={"action_type": action_type, "url": "https://navigation.example.test", "reasoning": "test"},
+ scraped_page=_mock_scraped_page(),
+ )
+
+ assert isinstance(action, GotoUrlAction if action_type == "GOTO_URL" else NewTabAction)
+ resolver.assert_not_called()
+
+
def test_parse_reload_page() -> None:
action = parse_action(
action={"action_type": "RELOAD_PAGE", "id": None, "reasoning": "test"},
diff --git a/tests/unit/test_api_handler_factory.py b/tests/unit/test_api_handler_factory.py
index d8493db81..6b4e86e38 100644
--- a/tests/unit/test_api_handler_factory.py
+++ b/tests/unit/test_api_handler_factory.py
@@ -14,6 +14,7 @@ from skyvern.forge.sdk.api.llm.api_handler_factory import (
EXTRACT_ACTION_PROMPT_NAME,
GEMINI_SAFETY_SETTINGS,
LLMAPIHandlerFactory,
+ LLMCaller,
)
from skyvern.forge.sdk.api.llm.models import LLMConfig
from skyvern.forge.sdk.artifact.models import ArtifactType
@@ -22,6 +23,79 @@ from skyvern.schemas.llm import LLMRouterConfig, LLMRouterModelConfig
from tests.unit.helpers import DummyLogger, FakeLLMResponse
+def _custom_llm_config(model_name: str, api_base: str = "https://llm.example.test/v1") -> LLMConfig:
+ return LLMConfig(model_name, [], False, False, {"api_key": "test-key", "api_base": api_base})
+
+
+ ("model_name", "http_client_attribute"),
+ [("openai/example-model", "_client"), ("ollama_chat/example-model", "client")],
+)
+async def test_custom_llm_http_clients_do_not_follow_redirects(
+ monkeypatch: pytest.MonkeyPatch,
+ model_name: str,
+ http_client_attribute: str,
+) -> None:
+ llm_config = _custom_llm_config(model_name)
+ monkeypatch.setattr(api_handler_factory.LLMConfigRegistry, "get_config", lambda _: llm_config)
+ monkeypatch.setattr(api_handler_factory.LLMConfigRegistry, "is_router_config", lambda _: False)
+ monkeypatch.setattr(api_handler_factory.skyvern_context, "current", lambda: None)
+ monkeypatch.setattr(
+ api_handler_factory.SettingsManager.get_settings(), "ALLOW_CUSTOM_LLM_LOCAL_API_BASES", False, raising=False
+ )
+ validate_api_base = MagicMock(return_value="https://llm.example.test/v1")
+ monkeypatch.setattr(api_handler_factory, "validate_fetch_url", validate_api_base)
+ monkeypatch.setattr(
+ api_handler_factory, "llm_messages_builder", AsyncMock(return_value=[{"role": "user", "content": "test"}])
+ )
+ monkeypatch.setattr(api_handler_factory.litellm, "completion_cost", lambda **_: 0.0)
+
+ completion = AsyncMock(return_value=FakeLLMResponse(model_name))
+ monkeypatch.setattr(api_handler_factory.litellm, "acompletion", completion)
+
+ handler = LLMAPIHandlerFactory.get_llm_api_handler("CUSTOM_LLM_redirect_test")
+ await handler(prompt="test prompt", prompt_name=EXTRACT_ACTION_PROMPT_NAME)
+
+ client = completion.await_args.kwargs["client"]
+ http_client = getattr(client, http_client_attribute)
+ assert http_client.follow_redirects is False
+ assert http_client.is_closed is True
+ validate_api_base.assert_called_once_with("https://llm.example.test/v1")
+ if http_client_attribute == "client":
+ retry_client = client.create_client(timeout=None, event_hooks=None)
+ assert retry_client.follow_redirects is False
+ await retry_client.aclose()
+
+
[email protected]("request_error", [None, RuntimeError("provider failed")], ids=["success", "failure"])
+async def test_custom_openrouter_client_is_scoped_to_request(
+ monkeypatch: pytest.MonkeyPatch, request_error: Exception | None
+) -> None:
+ llm_config = _custom_llm_config("openrouter/example-model", "https://openrouter.ai/api/v1")
+ monkeypatch.setattr(api_handler_factory.LLMConfigRegistry, "get_config", lambda _: llm_config)
+ completion = MagicMock()
+ completion.model_dump.return_value = {}
+ client = MagicMock()
+ client.chat.completions.create = AsyncMock(return_value=completion, side_effect=request_error)
+ client.close = AsyncMock()
+ openai_client = MagicMock(return_value=client)
+ monkeypatch.setattr(api_handler_factory, "AsyncOpenAI", openai_client)
+ monkeypatch.setattr(api_handler_factory.litellm, "ModelResponse", MagicMock())
+
+ caller = LLMCaller("CUSTOM_LLM_openrouter_redirect_test")
+ assert caller.openai_client is None
+ if request_error:
+ with pytest.raises(RuntimeError, match="provider failed"):
+ await caller._dispatch_llm_call(messages=[])
+ else:
+ await caller._dispatch_llm_call(messages=[])
+
+ assert openai_client.call_args.kwargs["http_client"].follow_redirects is False
+ client.close.assert_awaited_once()
+
+
@pytest.mark.asyncio
async def test_cached_content_not_added_for_non_gemini(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that cached_content is NOT added to non-Gemini models like GPT-4."""
diff --git a/tests/unit/test_custom_llms.py b/tests/unit/test_custom_llms.py
index ec0d1769b..f1a4feed0 100644
--- a/tests/unit/test_custom_llms.py
+++ b/tests/unit/test_custom_llms.py
@@ -1,11 +1,13 @@
+import socket
from collections.abc import Generator
from datetime import datetime, timezone
from types import SimpleNamespace
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, MagicMock
import pytest
from skyvern.config import settings
+from skyvern.exceptions import BlockedHost
from skyvern.forge.sdk.api.llm.api_handler_factory import LLMAPIHandlerFactory
from skyvern.forge.sdk.api.llm.config_registry import LLMConfigRegistry
from skyvern.forge.sdk.api.llm.custom_llm_registry import (
@@ -199,8 +201,11 @@ async def test_custom_llm_routes_register_update_and_delete_config(
@pytest.mark.asyncio
async def test_update_custom_llm_preserves_masked_api_key(
+ monkeypatch: pytest.MonkeyPatch,
fake_organizations: FakeOrganizationsRepository,
) -> None:
+ validate_api_base = AsyncMock()
+ monkeypatch.setattr(routes, "_validate_custom_llm_api_base", validate_api_base)
org = _org()
create_response = await routes.create_custom_llm(
CustomLLMCreateRequest(
@@ -234,6 +239,7 @@ async def test_update_custom_llm_preserves_masked_api_key(
registered_config = LLMConfigRegistry.get_config(custom_llm_key(custom_llm_id))
assert registered_config.litellm_params
assert registered_config.litellm_params["api_key"] == "sk-or"
+ assert validate_api_base.await_count == 2
deregister_custom_llm_config(custom_llm_id)
@@ -396,6 +402,72 @@ def test_cloud_custom_llm_api_base_blocks_local_targets(monkeypatch: pytest.Monk
)
+async def test_cloud_custom_llm_create_blocks_private_dns_answer_before_write(
+ monkeypatch: pytest.MonkeyPatch,
+ fake_organizations: FakeOrganizationsRepository,
+) -> None:
+ monkeypatch.setattr(SettingsManager.get_settings(), "ALLOW_CUSTOM_LLM_LOCAL_API_BASES", False, raising=False)
+ resolver = MagicMock(return_value=[(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("172.16.0.42", 443))])
+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", resolver)
+
+ request = CustomLLMCreateRequest(
+ config=CustomLLMConfig(
+ display_name="Cloud endpoint",
+ provider="openai_compatible",
+ model_name="example-model",
+ api_base="https://llm.example.test/v1",
+ api_key="test-key",
+ )
+ )
+ resolver.assert_not_called()
+
+ with pytest.raises(BlockedHost):
+ await routes.create_custom_llm(request, _org())
+
+ assert fake_organizations.tokens == []
+
+
+def test_stored_custom_llm_validation_allows_legacy_api_base_without_dns(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(SettingsManager.get_settings(), "ALLOW_CUSTOM_LLM_LOCAL_API_BASES", False, raising=False)
+ resolver = MagicMock(side_effect=AssertionError("stored custom LLM reads must not resolve DNS"))
+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", resolver)
+
+ CustomLLMConfig.model_validate_json(
+ '{"display_name":"Stored endpoint","provider":"openrouter","model_name":"example/model",'
+ '"api_base":"https://gateway.example.test/v1","api_key":"test-key"}'
+ )
+
+ resolver.assert_not_called()
+
+
+ ("provider", "api_base", "error"),
+ [
+ ("openai_compatible", "http://llm.example.test/v1", "Cloud api_base must use HTTPS on port 443"),
+ ("openai_compatible", "https://llm.example.test:8443/v1", "Cloud api_base must use HTTPS on port 443"),
+ ("openrouter", "https://gateway.example.test/v1", "OpenRouter api_base must use openrouter.ai"),
+ ],
+)
+async def test_cloud_custom_llm_api_base_restrictions_apply_only_at_write_boundary(
+ monkeypatch: pytest.MonkeyPatch,
+ provider: str,
+ api_base: str,
+ error: str,
+) -> None:
+ monkeypatch.setattr(SettingsManager.get_settings(), "ALLOW_CUSTOM_LLM_LOCAL_API_BASES", False, raising=False)
+ config = CustomLLMConfig(
+ display_name="Cloud endpoint",
+ provider=provider, # type: ignore[arg-type]
+ model_name="example/model",
+ api_base=api_base,
+ api_key="test-key",
+ )
+ with pytest.raises(routes.HTTPException, match=error):
+ await routes._validate_custom_llm_api_base(config)
+
+
def test_custom_llm_api_base_allows_local_targets_for_self_hosted(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(SettingsManager.get_settings(), "ALLOW_CUSTOM_LLM_LOCAL_API_BASES", True, raising=False)
@@ -413,6 +485,10 @@ async def test_task_v2_metadata_uses_selected_custom_llm(
monkeypatch: pytest.MonkeyPatch,
fake_organizations: FakeOrganizationsRepository,
) -> None:
+ monkeypatch.setattr(
+ "skyvern.utils.url_validators.socket.getaddrinfo",
+ lambda host, port, *args, **kwargs: [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", port or 0))],
+ )
org = _org()
custom_llm_id = "oat_custom_metadata"
register_custom_llm_config(
diff --git a/tests/unit/test_navigation_action_handlers.py b/tests/unit/test_navigation_action_handlers.py
index 532faa5c3..54d3e94d4 100644
--- a/tests/unit/test_navigation_action_handlers.py
+++ b/tests/unit/test_navigation_action_handlers.py
@@ -17,7 +17,9 @@ def _task() -> MagicMock:
@pytest.mark.asyncio
-async def test_goto_url_navigates_and_stops_batch() -> None:
+async def test_goto_url_navigates_and_stops_batch(monkeypatch: pytest.MonkeyPatch) -> None:
+ validate_url = MagicMock(return_value="https://example.test/page")
+ monkeypatch.setattr("skyvern.webeye.actions.handler.validate_fetch_url", validate_url)
page = MagicMock()
page.goto = AsyncMock()
@@ -29,6 +31,7 @@ async def test_goto_url_navigates_and_stops_batch() -> None:
# Navigation invalidates pre-nav element ids, so later actions in the batch must not run.
assert result[0].skip_remaining_actions is True
page.goto.assert_awaited_once()
+ validate_url.assert_called_once_with("https://example.test/page")
@pytest.mark.asyncio
diff --git a/tests/unit/test_tab_action_handlers.py b/tests/unit/test_tab_action_handlers.py
index 1b6185542..273549e71 100644
--- a/tests/unit/test_tab_action_handlers.py
+++ b/tests/unit/test_tab_action_handlers.py
@@ -27,6 +27,11 @@ def _mock_app(browser_state: MagicMock) -> MagicMock:
return mock_app
[email protected](autouse=True)
+def mock_navigation_validation(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr("skyvern.webeye.actions.handler.validate_fetch_url", lambda url: url)
+
+
@pytest.mark.asyncio
async def test_new_tab_opens_navigates_pins_and_stops_batch() -> None:
new_page = MagicMock()
diff --git a/tests/unit/test_task_request_validation.py b/tests/unit/test_task_request_validation.py
index 6e75ac471..e5fadf118 100644
--- a/tests/unit/test_task_request_validation.py
+++ b/tests/unit/test_task_request_validation.py
@@ -2,8 +2,72 @@
from __future__ import annotations
+import socket
+from types import SimpleNamespace
+from unittest.mock import AsyncMock, MagicMock
+
import pytest
+from skyvern.exceptions import BlockedHost
+
+
+def test_task_models_do_not_resolve_dns_during_validation(monkeypatch: pytest.MonkeyPatch) -> None:
+ from skyvern.forge.sdk.schemas.tasks import TaskRequest
+ from skyvern.schemas.runs import TaskRunRequest
+
+ monkeypatch.setattr(
+ "skyvern.utils.url_validators.socket.getaddrinfo",
+ MagicMock(side_effect=AssertionError("task model validation must not resolve DNS")),
+ )
+
+ TaskRunRequest(prompt="stored task", url="https://task.example.test")
+ TaskRequest(url="https://task.example.test")
+
+
[email protected]("task_version", ["v1", "v2"])
+async def test_task_write_rejects_hostname_resolving_to_blocked_ip(
+ monkeypatch: pytest.MonkeyPatch, task_version: str
+) -> None:
+ from skyvern.forge.sdk.schemas.tasks import TaskRequest
+ from skyvern.services import task_v1_service, task_v2_service
+
+ monkeypatch.setattr(
+ "skyvern.utils.url_validators.socket.getaddrinfo",
+ lambda host, port, *args, **kwargs: [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("10.0.0.42", 0))],
+ )
+ write = AsyncMock()
+
+ with pytest.raises(BlockedHost):
+ if task_version == "v1":
+ monkeypatch.setattr(task_v1_service.app.agent, "create_task", write)
+ await task_v1_service.run_task(
+ TaskRequest(url="https://task.example.test"), SimpleNamespace(organization_id="o_test")
+ )
+ else:
+ monkeypatch.setattr(task_v2_service.app.DATABASE.observer, "create_task_v2", write)
+ await task_v2_service.initialize_task_v2(
+ organization=SimpleNamespace(organization_id="o_test"),
+ user_prompt="test",
+ user_url="https://task.example.test",
+ )
+
+ write.assert_not_awaited()
+
+
+async def test_task_v1_empty_url_with_browser_session_skips_dns(monkeypatch: pytest.MonkeyPatch) -> None:
+ from skyvern.forge.sdk.schemas.tasks import TaskRequest
+ from skyvern.services import task_v1_service
+
+ create_task = AsyncMock(side_effect=RuntimeError("reached task write"))
+ monkeypatch.setattr(task_v1_service.app.agent, "create_task", create_task)
+ with pytest.raises(RuntimeError, match="reached task write"):
+ await task_v1_service.run_task(
+ TaskRequest(url="", browser_session_id="pbs_test"), SimpleNamespace(organization_id="o_test")
+ )
+ create_task.assert_awaited_once()
+
def test_data_extraction_goal_none_passes() -> None:
from skyvern.forge.sdk.schemas.tasks import TaskRequest
diff --git a/tests/unit/test_task_v2_task_run_creation.py b/tests/unit/test_task_v2_task_run_creation.py
index 717f698f8..95a010cb2 100644
--- a/tests/unit/test_task_v2_task_run_creation.py
+++ b/tests/unit/test_task_v2_task_run_creation.py
@@ -9,7 +9,10 @@ from skyvern.services.task_v2_service import DEFAULT_WORKFLOW_TITLE, initialize_
@pytest.mark.asyncio
-async def test_initialize_task_v2_populates_task_run_url_when_user_url_is_known() -> None:
+async def test_initialize_task_v2_populates_task_run_url_when_user_url_is_known(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr("skyvern.services.task_v2_service.validate_fetch_url", lambda url: url)
organization = SimpleNamespace(organization_id="org_123")
user_url = "https://example.com"
diff --git a/tests/unit/test_url_validators.py b/tests/unit/test_url_validators.py
index f9154389a..5d675a0fe 100644
--- a/tests/unit/test_url_validators.py
+++ b/tests/unit/test_url_validators.py
@@ -68,6 +68,7 @@ def test_is_blocked_host_bracketed_ipv6_internal(host: str) -> None:
"fe80::1",
"fc00::1",
"10.0.0.1",
+ "100.100.100.200",
"127.0.0.1",
"169.254.169.254",
"192.168.1.1",
@@ -109,6 +110,15 @@ def test_validate_url_allows_public_ipv6() -> None:
assert validate_url("https://[2001:4860:4860::8888]/") is not None
+ "url",
+ ["http://2130706433/", "http://0x7f000001/", "http://017700000001/", "http://127.1/", "http://0/"],
+)
+def test_validate_fetch_url_rejects_nonstandard_ip_encodings(url: str) -> None:
+ with pytest.raises(BlockedHost):
+ validate_fetch_url(url)
+
+
@pytest.mark.parametrize(
("allowed_entry", "host"),
[
@@ -131,6 +141,48 @@ def test_is_blocked_host_blocked_hosts_case_insensitive(host: str) -> None:
assert is_blocked_host(host) is True
+ "blocked_ip",
+ [
+ "127.0.0.2",
+ "10.0.0.2",
+ "172.16.0.2",
+ "192.168.0.2",
+ "169.254.0.2",
+ "100.64.0.2",
+ "100.100.100.200",
+ "169.254.169.254",
+ "::1",
+ "fc00::2",
+ "fd00:ec2::254",
+ ],
+)
+def test_is_blocked_host_rejects_any_blocked_dns_answer(monkeypatch: pytest.MonkeyPatch, blocked_ip: str) -> None:
+ family = socket.AF_INET6 if ":" in blocked_ip else socket.AF_INET
+
+ def resolves_with_blocked_answer(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:
+ return [
+ (socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", port or 0)),
+ (family, socket.SOCK_STREAM, 0, "", (blocked_ip, port or 0)),
+ ]
+
+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", resolves_with_blocked_answer)
+
+ assert is_blocked_host("public.example.test", resolve_dns=True) is True
+
+
+def test_is_blocked_host_allows_public_dns_answers(monkeypatch: pytest.MonkeyPatch) -> None:
+ def resolves_public(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:
+ return [
+ (socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", port or 0)),
+ (socket.AF_INET6, socket.SOCK_STREAM, 0, "", ("2606:2800:220:1:248:1893:25c8:1946", port or 0)),
+ ]
+
+ monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", resolves_public)
+
+ assert is_blocked_host("public.example.test", resolve_dns=True) is False
+
+
def test_validate_fetch_url_blocks_hostname_resolving_private_ip(monkeypatch: pytest.MonkeyPatch) -> None:
def resolves_to_private(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:
return [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("10.0.0.42", port or 0))]