Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
fix(connectors,api): adopt the SSRF guard on every connector egress path (GHSA-cq4g reproduction + fix) (#1422)
Beenu Aroraa5664a5ef971
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a7b4a3fe9..c04d5ffdf 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- The kill switch can actually be engaged and TAXII collections actually becreated: both wrote strict-RLS tables through sessions that never set thetenant, and 500ed on every deployment (siblings of GHSA-898c, found by sweep).+- The connectors service adopts the platform SSRF guard on every construction+ path — the stateless test route, resource reads and scheduled polls — so a+ tenant-controlled `base_url` can no longer reach cloud metadata, loopback or+ internal services (GHSA-cq4g-g6wp-4m2w). The API refuses the same+ destinations at connector create/update, before they are encrypted and+ stored. `AISOC_SSRF_ALLOW_PRIVATE=1` re-admits on-prem SIEMs on private+ ranges; the cloud-metadata blocklist holds regardless.## [19.0.0] - 2026-10-12diff --git a/scripts/mypy_baseline.json b/scripts/mypy_baseline.jsonindex 1a59014f5..16a33007c 100644--- a/scripts/mypy_baseline.json+++ b/scripts/mypy_baseline.json@@ -477,6 +477,10 @@"app/connectors/posture/aws.py": {"no-redef": 1},+ "app/security/ssrf_guard.py": {+ "attr-defined": 6,+ "union-attr": 1+ },"tests/test_pipeline_filter_rules.py": {"arg-type": 1},diff --git a/scripts/sync_vendored_ssrf_guard.py b/scripts/sync_vendored_ssrf_guard.pyindex 87ad19f1d..e435efb86 100644--- a/scripts/sync_vendored_ssrf_guard.py+++ b/scripts/sync_vendored_ssrf_guard.py@@ -42,26 +42,39 @@ self_test_if_requested(__file__)REPO_ROOT = repo_root()SOURCE_FILE = REPO_ROOT / "services" / "agents" / "app" / "playbook" / "ssrf_guard.py"-VENDORED_FILE = REPO_ROOT / "services" / "api" / "app" / "_vendor" / "ssrf_guard.py"+#: Every vendored copy, kept byte-identical to the agents source. The+#: connectors copy exists because that service opens outbound HTTP to+#: tenant-controlled base URLs on every connector (GHSA-cq4g-g6wp-4m2w) and a+#: guard that drifts from the one the playbook engine enforces is two SSRF+#: policies wearing one name.+VENDORED_FILES = (+ REPO_ROOT / "services" / "api" / "app" / "_vendor" / "ssrf_guard.py",+ REPO_ROOT / "services" / "connectors" / "app" / "security" / "ssrf_guard.py",+)def _check() -> int:if not SOURCE_FILE.is_file():print(f"FAIL: source file missing: {SOURCE_FILE}", file=sys.stderr)return 1- if not VENDORED_FILE.is_file():- print(f"FAIL: vendored file missing: {VENDORED_FILE}", file=sys.stderr)+ failed = False+ for vendored in VENDORED_FILES:+ if not vendored.is_file():+ print(f"FAIL: vendored file missing: {vendored}", file=sys.stderr)+ failed = True+ continue+ if not filecmp.cmp(SOURCE_FILE, vendored, shallow=False):+ print(+ "FAIL: vendored ssrf_guard.py is out of sync with source.\n"+ f" Source: {SOURCE_FILE.relative_to(REPO_ROOT)}\n"+ f" Vendored: {vendored.relative_to(REPO_ROOT)}\n\n"+ "Re-run: python scripts/sync_vendored_ssrf_guard.py",+ file=sys.stderr,+ )+ failed = True+ if failed:return 1- if not filecmp.cmp(SOURCE_FILE, VENDORED_FILE, shallow=False):- print(- "FAIL: vendored ssrf_guard.py is out of sync with source.\n"- f" Source: {SOURCE_FILE.relative_to(REPO_ROOT)}\n"- f" Vendored: {VENDORED_FILE.relative_to(REPO_ROOT)}\n\n"- "Re-run: python scripts/sync_vendored_ssrf_guard.py",- file=sys.stderr,- )- return 1- print("OK: vendored ssrf_guard.py matches source.")+ print(f"OK: {len(VENDORED_FILES)} vendored ssrf_guard.py copies match source.")return 0@@ -69,10 +82,11 @@ def _sync() -> int:if not SOURCE_FILE.is_file():print(f"FAIL: source file missing: {SOURCE_FILE}", file=sys.stderr)return 1- VENDORED_FILE.parent.mkdir(parents=True, exist_ok=True)- shutil.copy2(SOURCE_FILE, VENDORED_FILE)- print(f"copied {SOURCE_FILE.relative_to(REPO_ROOT)} → {VENDORED_FILE.relative_to(REPO_ROOT)}")- print("\nDone. Don't forget to commit services/api/app/_vendor/ssrf_guard.py.")+ for vendored in VENDORED_FILES:+ vendored.parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(SOURCE_FILE, vendored)+ print(f"copied {SOURCE_FILE.relative_to(REPO_ROOT)} → {vendored.relative_to(REPO_ROOT)}")+ print("\nDone. Don't forget to commit the vendored copies.")return 0diff --git a/services/agents/app/playbook/ssrf_guard.py b/services/agents/app/playbook/ssrf_guard.pyindex e22b82d46..9e665aa15 100644--- a/services/agents/app/playbook/ssrf_guard.py+++ b/services/agents/app/playbook/ssrf_guard.py@@ -36,6 +36,14 @@ Operators can:cloud-metadata literals are likewise always rejected.* Set ``AISOC_SSRF_EXTRA_BLOCKED_HOSTS=foo.internal,bar.svc`` to extendthe metadata host blocklist.+* Set ``AISOC_SSRF_ALLOW_HOSTS=127.0.0.1,splunk.tunnel.local`` to admit+ exact hostnames that would otherwise be refused by IP classification —+ the escape for a vendor appliance reachable only through a local+ tunnel (an SSH port-forward lands the "remote" SIEM on loopback, which+ ``AISOC_SSRF_ALLOW_PRIVATE`` deliberately never admits). Exact,+ case-insensitive hostname matches only, no wildcards — and the+ **blocklist always wins**: listing a metadata literal here changes+ nothing, because the blocklist is consulted first.Failures raise :class:`SSRFError`, which propagates up to the engine andmarks the step as failed via the normal exception path.@@ -94,6 +102,17 @@ def _blocked_hosts() -> frozenset[str]:return _DEFAULT_BLOCKED_HOSTS | _env_set("AISOC_SSRF_EXTRA_BLOCKED_HOSTS")+def _allowed_hosts() -> frozenset[str]:+ """Operator-named hosts admitted without IP classification.++ Consulted *after* the blocklist, so a metadata literal placed here is+ still refused. Exact hostname matches only: an allowlist that took+ suffixes or wildcards would quietly admit ``evil-metadata.google.internal``+ shapes, and an operator who means one tunneled appliance can name it.+ """+ return _env_set("AISOC_SSRF_ALLOW_HOSTS")++def _allow_private_by_default() -> bool:return os.getenv("AISOC_SSRF_ALLOW_PRIVATE", "").strip().lower() in {"1", "true", "yes"}@@ -214,11 +233,19 @@ def validate_outbound_url(url: str, *, allow_private: bool | None = None) -> strif not host:raise SSRFError("URL has no hostname")- # Explicit blocklist takes precedence over IP / DNS checks.+ # Explicit blocklist takes precedence over IP / DNS checks — and over the+ # operator allowlist below, so a metadata literal cannot be re-admitted.blocked = _blocked_hosts()if host in blocked:raise SSRFError(f"hostname {host!r} is on the SSRF blocklist (cloud metadata or operator-blocked)")+ # An operator-named exact host (AISOC_SSRF_ALLOW_HOSTS) skips IP+ # classification: the escape for a vendor appliance that is only+ # reachable through a local tunnel, where the destination genuinely is+ # loopback and AISOC_SSRF_ALLOW_PRIVATE deliberately does not help.+ if host in _allowed_hosts():+ return url+if allow_private is None:allow_private = _allow_private_by_default()diff --git a/services/agents/tests/test_playbook_ssrf_guard.py b/services/agents/tests/test_playbook_ssrf_guard.pyindex 32133cf82..c5af562bd 100644--- a/services/agents/tests/test_playbook_ssrf_guard.py+++ b/services/agents/tests/test_playbook_ssrf_guard.py@@ -436,3 +436,36 @@ class TestIsDisallowedAddress:blocked, _ = _is_disallowed_address(ipaddress.ip_address(ip_str), allow_private=False)assert blocked is False+++class TestOperatorAllowHosts:+ """AISOC_SSRF_ALLOW_HOSTS admits exact hosts; the blocklist still wins.++ The escape exists for a vendor appliance reachable only through a local+ tunnel: an SSH port-forward lands the "remote" SIEM on loopback, which+ AISOC_SSRF_ALLOW_PRIVATE deliberately never admits.+ """++ def test_a_listed_loopback_host_is_admitted(self, monkeypatch):+ monkeypatch.setenv("AISOC_SSRF_ALLOW_HOSTS", "127.0.0.1")+ assert validate_outbound_url("http://127.0.0.1:8089/services") == "http://127.0.0.1:8089/services"++ def test_an_unlisted_loopback_host_is_still_refused(self, monkeypatch):+ monkeypatch.setenv("AISOC_SSRF_ALLOW_HOSTS", "127.0.0.1")+ with pytest.raises(SSRFError):+ validate_outbound_url("http://[::1]:8089/")++ def test_the_blocklist_wins_over_the_allowlist(self, monkeypatch):+ monkeypatch.setenv("AISOC_SSRF_ALLOW_HOSTS", "169.254.169.254")+ with pytest.raises(SSRFError):+ validate_outbound_url("http://169.254.169.254/latest/meta-data/")++ def test_matching_is_exact_not_suffix(self, monkeypatch):+ monkeypatch.setenv("AISOC_SSRF_ALLOW_HOSTS", "127.0.0.1")+ with pytest.raises(SSRFError):+ validate_outbound_url("http://evil127.0.0.1.nip.io.invalid/")++ def test_unset_means_nothing_is_admitted(self, monkeypatch):+ monkeypatch.delenv("AISOC_SSRF_ALLOW_HOSTS", raising=False)+ with pytest.raises(SSRFError):+ validate_outbound_url("http://127.0.0.1:8089/")diff --git a/services/api/app/_vendor/ssrf_guard.py b/services/api/app/_vendor/ssrf_guard.pyindex e22b82d46..9e665aa15 100644--- a/services/api/app/_vendor/ssrf_guard.py+++ b/services/api/app/_vendor/ssrf_guard.py@@ -36,6 +36,14 @@ Operators can:cloud-metadata literals are likewise always rejected.* Set ``AISOC_SSRF_EXTRA_BLOCKED_HOSTS=foo.internal,bar.svc`` to extendthe metadata host blocklist.+* Set ``AISOC_SSRF_ALLOW_HOSTS=127.0.0.1,splunk.tunnel.local`` to admit+ exact hostnames that would otherwise be refused by IP classification —+ the escape for a vendor appliance reachable only through a local+ tunnel (an SSH port-forward lands the "remote" SIEM on loopback, which+ ``AISOC_SSRF_ALLOW_PRIVATE`` deliberately never admits). Exact,+ case-insensitive hostname matches only, no wildcards — and the+ **blocklist always wins**: listing a metadata literal here changes+ nothing, because the blocklist is consulted first.Failures raise :class:`SSRFError`, which propagates up to the engine andmarks the step as failed via the normal exception path.@@ -94,6 +102,17 @@ def _blocked_hosts() -> frozenset[str]:return _DEFAULT_BLOCKED_HOSTS | _env_set("AISOC_SSRF_EXTRA_BLOCKED_HOSTS")+def _allowed_hosts() -> frozenset[str]:+ """Operator-named hosts admitted without IP classification.++ Consulted *after* the blocklist, so a metadata literal placed here is+ still refused. Exact hostname matches only: an allowlist that took+ suffixes or wildcards would quietly admit ``evil-metadata.google.internal``+ shapes, and an operator who means one tunneled appliance can name it.+ """+ return _env_set("AISOC_SSRF_ALLOW_HOSTS")++def _allow_private_by_default() -> bool:return os.getenv("AISOC_SSRF_ALLOW_PRIVATE", "").strip().lower() in {"1", "true", "yes"}@@ -214,11 +233,19 @@ def validate_outbound_url(url: str, *, allow_private: bool | None = None) -> strif not host:raise SSRFError("URL has no hostname")- # Explicit blocklist takes precedence over IP / DNS checks.+ # Explicit blocklist takes precedence over IP / DNS checks — and over the+ # operator allowlist below, so a metadata literal cannot be re-admitted.blocked = _blocked_hosts()if host in blocked:raise SSRFError(f"hostname {host!r} is on the SSRF blocklist (cloud metadata or operator-blocked)")+ # An operator-named exact host (AISOC_SSRF_ALLOW_HOSTS) skips IP+ # classification: the escape for a vendor appliance that is only+ # reachable through a local tunnel, where the destination genuinely is+ # loopback and AISOC_SSRF_ALLOW_PRIVATE deliberately does not help.+ if host in _allowed_hosts():+ return url+if allow_private is None:allow_private = _allow_private_by_default()diff --git a/services/api/app/api/v1/endpoints/connectors.py b/services/api/app/api/v1/endpoints/connectors.pyindex f3d4f4caf..c7a3ec1f1 100644--- a/services/api/app/api/v1/endpoints/connectors.py+++ b/services/api/app/api/v1/endpoints/connectors.py@@ -36,6 +36,7 @@ from __future__ import annotationsimport jsonimport loggingimport re+import socketimport uuidfrom dataclasses import dataclassfrom datetime import UTC, datetime@@ -71,6 +72,7 @@ from fastapi import APIRouter, Depends, HTTPException, status # noqa: E402from pydantic import BaseModel, Field # noqa: E402from sqlalchemy import select, update # noqa: E402+from app._vendor.ssrf_guard import SSRFError, validate_outbound_url # noqa: E402from app.api.v1.deps import AuthUser, DBSession, require_permission # noqa: E402from app.api.v1.limits import refuse_if_over_limit # noqa: E402from app.core import connectors_auth # noqa: E402@@ -780,6 +782,37 @@ async def list_connectors(return [_build_connector_response(c) for c in connectors]+def _refuse_internal_destinations(*configs: dict[str, Any] | None) -> None:+ """Refuse connector config whose URL-shaped values point inside the deployment.++ Registration-time feedback for GHSA-cq4g-g6wp-4m2w. The connectors+ service enforces the same rule at every construction site (test, resource+ reads, scheduled polls), so this is the early 422 in the wizard rather+ than the security boundary — but a destination that can never be polled+ should be refused before it is vault-encrypted and stored. Matches on the+ value's shape, not the key name, for the same reason the connectors-side+ helper does: ``base_url`` is one of a dozen spellings. A hostname that+ merely fails to resolve passes through — the connector's own "DNS lookup+ failed" is the right wizard message — while metadata, loopback and+ private ranges (without ``AISOC_SSRF_ALLOW_PRIVATE``) are refused.+ """+ for config in configs:+ for field_name, value in (config or {}).items():+ items = value if isinstance(value, list) else [value]+ for item in items:+ if not isinstance(item, str) or not item.startswith(("http://", "https://")):+ continue+ try:+ validate_outbound_url(item)+ except SSRFError as exc:+ if isinstance(exc.__cause__, socket.gaierror):+ continue+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=f"{field_name}: destination refused: {exc}",+ ) from exc++@router.post("", response_model=ConnectorResponse, status_code=status.HTTP_201_CREATED)async def create_connector(request: CreateConnectorRequest,@@ -799,6 +832,12 @@ async def create_connector(# part of this handler.await refuse_if_over_limit(db, current_user.tenant_id, "connectors")+ # Before encryption and storage: a destination inside the deployment can+ # never be polled (the connectors service refuses it at every+ # construction site), so saving it would only manufacture a permanently+ # unhealthy instance (GHSA-cq4g-g6wp-4m2w).+ _refuse_internal_destinations(request.auth_config, request.connector_config)+catalog_entry = await _validate_connector_type(request.connector_type, current_user.tenant_id)category = request.category or catalog_entry.get("category") or "uncategorized"@@ -869,6 +908,10 @@ async def update_connector(if connector is None:raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Connector not found")+ # Same rule as create: a saved destination edited to point inside the+ # deployment is the update-model half of GHSA-cq4g-g6wp-4m2w.+ _refuse_internal_destinations(request.auth_config, request.connector_config)+updates: dict[str, Any] = {}for field in ("name", "is_enabled", "auth_config", "connector_config", "tags"):val = getattr(request, field, None)diff --git a/services/api/tests/test_connectors_endpoint_ssrf.py b/services/api/tests/test_connectors_endpoint_ssrf.pynew file mode 100644index 000000000..f81d78cd3--- /dev/null+++ b/services/api/tests/test_connectors_endpoint_ssrf.py@@ -0,0 +1,52 @@+"""Connector registration refuses destinations inside the deployment.++GHSA-cq4g-g6wp-4m2w, the API-side half. The connectors service enforces the+same rule at every construction site (test route, resource reads, scheduled+polls) and is the security boundary; this check is the early 422 in the setup+wizard, so a destination that could never be polled is refused before it is+vault-encrypted and stored as a permanently unhealthy instance.+"""++from __future__ import annotations++import pytest+from app.api.v1.endpoints.connectors import _refuse_internal_destinations+from fastapi import HTTPException++INTERNAL = [+ "http://169.254.169.254/latest/meta-data/iam/security-credentials/",+ "http://127.0.0.1:8089/",+ "http://10.0.0.5/",+ "http://2130706433/",+]++[email protected]("url", INTERNAL)+def test_internal_destinations_are_refused(url: str) -> None:+ with pytest.raises(HTTPException) as exc_info:+ _refuse_internal_destinations({"base_url": url})+ assert exc_info.value.status_code == 422+ assert "destination refused" in str(exc_info.value.detail)+++def test_the_key_name_does_not_matter() -> None:+ """Value shape, not key list: base_url is one of a dozen spellings."""+ with pytest.raises(HTTPException):+ _refuse_internal_destinations({}, {"gitlab_url": "http://169.254.169.254/"})+++def test_public_and_non_url_values_pass() -> None:+ _refuse_internal_destinations(+ {"token": "x" * 48, "page_size": 500},+ {"base_url": "https://93.184.216.34:8089", "ssl_verify": True},+ )+++def test_none_configs_pass() -> None:+ _refuse_internal_destinations(None, None)+++def test_an_unresolvable_hostname_is_not_a_refusal() -> None:+ """The connector's own 'DNS lookup failed' is the right wizard message,+ and a host the guard cannot resolve is one nothing can connect to."""+ _refuse_internal_destinations({"base_url": "https://no-such-host.invalid:8089"})diff --git a/services/connectors/app/api/router.py b/services/connectors/app/api/router.pyindex 71287882c..e670a6d48 100644--- a/services/connectors/app/api/router.py+++ b/services/connectors/app/api/router.py@@ -39,6 +39,7 @@ from app.db.connector_repo import fetch_enabled_connectorsfrom app.db.engine import get_enginefrom app.federated.query import QueryError, parse_unified_queryfrom app.security.credential_vault import CredentialVaultError, get_vault+from app.security.egress import EgressRefused, refuse_internal_destinationsfrom app.security.tenant_scope import require_console_or_service_authlogger = structlog.get_logger()@@ -209,7 +210,19 @@ def _instantiate_or_422(cls: type, kwargs: dict[str, Any]) -> Any:Pulled out into a helper because four endpoints now share the exactsame construction path; keeping it inline meant duplicating the``TypeError`` handling four times.++ Destination validation happens here, before the constructor, so every+ route that builds a connector refuses an internal URL the same way+ (GHSA-cq4g-g6wp-4m2w) — a connector object that exists is one a later+ edit can call."""+ try:+ refuse_internal_destinations(kwargs)+ except EgressRefused as exc:+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=str(exc),+ ) from exctry:return cls(**kwargs)except TypeError as exc:@@ -304,6 +317,18 @@ async def test_connector_connection(connector_id: str, payload: TestConnectionRe# boundary.kwargs = {**payload.auth_config, **payload.connector_config}+ # Before the constructor, and before any socket could open: a tenant-set+ # base_url pointing at cloud metadata, loopback or an internal service is+ # refused here rather than fetched and reflected (GHSA-cq4g-g6wp-4m2w —+ # this exact route was the reproduced entry point).+ try:+ refuse_internal_destinations(kwargs)+ except EgressRefused as exc:+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=str(exc),+ ) from exc+try:connector = cls(**kwargs)except TypeError as exc:diff --git a/services/connectors/app/scheduler.py b/services/connectors/app/scheduler.pyindex eecd29f8c..4361ba8e4 100644--- a/services/connectors/app/scheduler.py+++ b/services/connectors/app/scheduler.py@@ -79,6 +79,7 @@ from app.pipeline import (diff_fingerprints,)from app.security.credential_vault import CredentialVault, CredentialVaultError, get_vault+from app.security.egress import EgressRefused, refuse_internal_destinationsfrom app.vuln_promotion_sql import (MARK_OVERDUE_SQL,MARK_PROMOTED_SQL,@@ -553,6 +554,27 @@ class ConnectorScheduler:runtime_config = {k: v for k, v in (target.connector_config or {}).items() if k not in {"poll_interval_seconds", "checkpoint"}}kwargs = {**auth, **runtime_config}+ # A stored destination inside the deployment must not be polled —+ # rows saved before registration-time validation existed were never+ # checked, and a hostname can rebind between saves and polls+ # (GHSA-cq4g-g6wp-4m2w). The failure is recorded so the instance+ # shows unhealthy in the UI rather than silently doing nothing; an+ # on-prem SIEM on RFC 1918 space is re-admitted by setting+ # AISOC_SSRF_ALLOW_PRIVATE=1.+ try:+ refuse_internal_destinations(kwargs)+ except EgressRefused as exc:+ logger.error(+ "connector.scheduler.destination_refused id=%s type=%s field=%s err=%s "+ "(set AISOC_SSRF_ALLOW_PRIVATE=1 if this deployment's SIEM lives on a private range)",+ connector_id,+ target.connector_type,+ exc.field_name,+ exc.reason,+ )+ await self._record_failure(target.id)+ return+started = datetime.now(UTC)try:connector = connector_class(**kwargs)diff --git a/services/connectors/app/security/egress.py b/services/connectors/app/security/egress.pynew file mode 100644index 000000000..7ff22605a--- /dev/null+++ b/services/connectors/app/security/egress.py@@ -0,0 +1,78 @@+"""Refuse connector egress to destinations inside the deployment.++GHSA-cq4g-g6wp-4m2w. The central SSRF guard (``validate_outbound_url``) was+applied across ``services/api`` and ``services/agents`` and nowhere in this+service — yet this is the service whose whole job is opening outbound HTTP to+tenant-controlled base URLs. Reproduced live: a **viewer-role** console JWT+posted ``POST /connectors/splunk/test`` with ``base_url`` pointing at a+loopback listener, and the service fetched ``/services/server/info`` from it+and reflected the parsed response — an authenticated tenant user could reach+cloud metadata (169.254.169.254), internal services, and loopback from this+service's network position, with the test response as an oracle.++What is validated+-----------------+Every **value** in the merged constructor kwargs that is an ``http(s)://``+URL, under whatever key the connector's schema chose (``base_url``,+``api_url``, ``instance_url``, ``gitlab_url``, ``spool_url``, …). Matching on+the value's shape rather than a list of key names is deliberate: the census+found 18 connectors declaring ``base_url`` and a dozen other spellings, and a+key-name list is the kind of one-directional check that silently misses the+next connector. Lists are walked for their string items.++Known residual, stated rather than hidden: a connector that assembles its URL+from a bare host/subdomain field (``host``, ``domain``, ``api_hostname``,+``subdomain``) is not covered by the value-shape rule — synthesizing URLs from+those would refuse legitimate values like a bare vendor subdomain. Those+fields are tenant-vendor hostnames rather than full destinations, and the+vendors' own TLS endpoints bound what they can reach.++Two refusal classes, deliberately (same split as the outbound-webhook+delivery path): a *policy* refusal — metadata endpoint, loopback, private+range without ``AISOC_SSRF_ALLOW_PRIVATE``, bad scheme — raises+:class:`EgressRefused`, while a hostname that merely fails to resolve is let+through: the connector cannot connect to it either, and its own "DNS lookup+failed" diagnostic is the message the setup wizard should show.++``AISOC_SSRF_ALLOW_PRIVATE=1`` relaxes the private-range check for on-prem+deployments whose SIEM genuinely lives on RFC 1918 space; the cloud-metadata+blocklist holds regardless, exactly as it does for playbook HTTP steps.+"""++from __future__ import annotations++import socket+from typing import Any++from app.security.ssrf_guard import SSRFError, validate_outbound_url++__all__ = ["EgressRefused", "refuse_internal_destinations"]+++class EgressRefused(ValueError):+ """A connector config named a destination inside the deployment."""++ def __init__(self, field_name: str, reason: str) -> None:+ self.field_name = field_name+ self.reason = reason+ super().__init__(f"{field_name}: destination refused: {reason}")+++def refuse_internal_destinations(kwargs: dict[str, Any]) -> None:+ """Validate every URL-shaped value in a connector's constructor kwargs.++ Raises :class:`EgressRefused` naming the offending field on a policy+ refusal. A resolution failure (the guard chains ``socket.gaierror``) is+ not a refusal — see the module docstring.+ """+ for field_name, value in kwargs.items():+ items = value if isinstance(value, list) else [value]+ for item in items:+ if not isinstance(item, str) or not item.startswith(("http://", "https://")):+ continue+ try:+ validate_outbound_url(item)+ except SSRFError as exc:+ if isinstance(exc.__cause__, socket.gaierror):+ continue+ raise EgressRefused(field_name, str(exc)) from excdiff --git a/services/connectors/app/security/ssrf_guard.py b/services/connectors/app/security/ssrf_guard.pynew file mode 100644index 000000000..9e665aa15--- /dev/null+++ b/services/connectors/app/security/ssrf_guard.py@@ -0,0 +1,270 @@+"""+SSRF guard for playbook outbound HTTP steps.++Playbooks let analysts configure ``http`` and ``notify`` steps that make+outbound HTTP requests to arbitrary URLs. Without validation, a playbook+author can target cloud instance metadata services (e.g. AWS+``169.254.169.254``, GCP ``metadata.google.internal``), loopback / private+RFC1918 addresses, or non-HTTP schemes like ``file://`` — a classic+Server-Side Request Forgery (SSRF) class of bug.++This module provides :func:`validate_outbound_url`, called by playbook step+handlers before they hand a URL to ``httpx``. It rejects:++* Schemes other than ``http`` / ``https`` (overridable via+ ``AISOC_SSRF_ALLOWED_SCHEMES``).+* URLs with userinfo (``http://user:pw@host/...``) — these are a common+ credential-smuggling and host-spoofing vector.+* Hostnames or IP literals that resolve to loopback, link-local, private,+ reserved, unspecified, or multicast ranges.+* A blocklist of well-known cloud metadata endpoints (AWS, GCP, Azure,+ Alibaba, Oracle) even if their hostnames happen to resolve to a public IP.+* Hostnames where *any* of the returned addresses is unsafe (defence in+ depth against DNS records that mix one public and one private answer).++Operators can:++* Set ``AISOC_SSRF_ALLOW_PRIVATE=1`` to relax the **private-range** check+ for on-prem deployments where playbooks must reach internal services.+ It does not relax loopback, and this header used to say it did —+ directly contradicting :func:`_is_disallowed_address`'s own docstring+ two hundred lines below, which has always said loopback is rejected+ either way. The code is the correct half: a playbook that can reach+ ``127.0.0.1`` can reach every unauthenticated service on the host+ running the engine, which is a larger hole than the one the flag+ exists to open. Link-local, multicast, reserved, unspecified and+ cloud-metadata literals are likewise always rejected.+* Set ``AISOC_SSRF_EXTRA_BLOCKED_HOSTS=foo.internal,bar.svc`` to extend+ the metadata host blocklist.+* Set ``AISOC_SSRF_ALLOW_HOSTS=127.0.0.1,splunk.tunnel.local`` to admit+ exact hostnames that would otherwise be refused by IP classification —+ the escape for a vendor appliance reachable only through a local+ tunnel (an SSH port-forward lands the "remote" SIEM on loopback, which+ ``AISOC_SSRF_ALLOW_PRIVATE`` deliberately never admits). Exact,+ case-insensitive hostname matches only, no wildcards — and the+ **blocklist always wins**: listing a metadata literal here changes+ nothing, because the blocklist is consulted first.++Failures raise :class:`SSRFError`, which propagates up to the engine and+marks the step as failed via the normal exception path.+"""++from __future__ import annotations++import ipaddress+import os+import socket+from urllib.parse import urlsplit+++class SSRFError(ValueError):+ """Raised when an outbound URL is blocked by the SSRF guard."""+++# ---------------------------------------------------------------------------+# Static blocklists+# ---------------------------------------------------------------------------++# Hostnames that must never be reached regardless of DNS resolution, e.g.+# operators sometimes wire ``metadata.google.internal`` to a public proxy.+# Comparison is case-insensitive against the URL's hostname (no port).+_DEFAULT_BLOCKED_HOSTS: frozenset[str] = frozenset(+ {+ # AWS / Alibaba IMDS literal+ "169.254.169.254",+ # GCP IMDS+ "metadata.google.internal",+ "metadata",+ # Azure IMDS — its IP literal is the same as AWS; hostname forms vary.+ "metadata.azure.com",+ # Alibaba Cloud+ "100.100.100.200",+ # Oracle Cloud Infrastructure+ "169.254.169.254.nip.io",+ }+)++_DEFAULT_ALLOWED_SCHEMES: frozenset[str] = frozenset({"http", "https"})+++def _env_set(name: str) -> frozenset[str]:+ """Read a comma-separated env var into a lowercase frozenset."""+ raw = os.getenv(name, "") or ""+ return frozenset(part.strip().lower() for part in raw.split(",") if part.strip())+++def _allowed_schemes() -> frozenset[str]:+ extra = _env_set("AISOC_SSRF_ALLOWED_SCHEMES")+ return extra or _DEFAULT_ALLOWED_SCHEMES+++def _blocked_hosts() -> frozenset[str]:+ return _DEFAULT_BLOCKED_HOSTS | _env_set("AISOC_SSRF_EXTRA_BLOCKED_HOSTS")+++def _allowed_hosts() -> frozenset[str]:+ """Operator-named hosts admitted without IP classification.++ Consulted *after* the blocklist, so a metadata literal placed here is+ still refused. Exact hostname matches only: an allowlist that took+ suffixes or wildcards would quietly admit ``evil-metadata.google.internal``+ shapes, and an operator who means one tunneled appliance can name it.+ """+ return _env_set("AISOC_SSRF_ALLOW_HOSTS")+++def _allow_private_by_default() -> bool:+ return os.getenv("AISOC_SSRF_ALLOW_PRIVATE", "").strip().lower() in {"1", "true", "yes"}+++# ---------------------------------------------------------------------------+# IP classification+# ---------------------------------------------------------------------------+++def _is_disallowed_address(ip: ipaddress._BaseAddress, *, allow_private: bool) -> tuple[bool, str]:+ """Return ``(blocked, reason)`` for an IP address.++ When ``allow_private`` is True we still reject loopback, link-local,+ unspecified, multicast, and reserved addresses — only the+ private/RFC1918 + ULA bands are loosened.+ """+ if ip.is_loopback:+ return True, "loopback address"+ if ip.is_link_local:+ return True, "link-local address (cloud metadata range)"+ if ip.is_unspecified:+ return True, "unspecified address"+ if ip.is_multicast:+ return True, "multicast address"+ if ip.is_reserved:+ return True, "reserved address"+ # Block IPv4-mapped/IPv4-compatible IPv6 against private v4 ranges.+ if isinstance(ip, ipaddress.IPv6Address):+ mapped = getattr(ip, "ipv4_mapped", None)+ if mapped is not None:+ return _is_disallowed_address(mapped, allow_private=allow_private)+ if not allow_private and ip.is_private:+ return True, "private/RFC1918 address"+ return False, ""+++def _coerce_ip(value: str) -> ipaddress._BaseAddress | None:+ """Try to parse ``value`` as an IP address (v4 or v6), return None if not."""+ try:+ return ipaddress.ip_address(value)+ except ValueError:+ return None+++def _resolve_host_addresses(host: str) -> list[ipaddress._BaseAddress]:+ """Resolve ``host`` via ``getaddrinfo`` and return all unique IP objects.++ Raises :class:`SSRFError` if the host cannot be resolved.+ """+ try:+ infos = socket.getaddrinfo(host, None, type=socket.SOCK_STREAM)+ except OSError as exc:+ raise SSRFError(f"could not resolve hostname {host!r}: {exc}") from exc++ addrs: list[ipaddress._BaseAddress] = []+ seen: set[str] = set()+ for info in infos:+ sockaddr = info[4]+ addr_str = sockaddr[0] if sockaddr else ""+ # IPv6 sockaddrs can include zone IDs ("fe80::1%eth0"); strip them.+ addr_str = addr_str.split("%", 1)[0]+ if not addr_str or addr_str in seen:+ continue+ seen.add(addr_str)+ ip = _coerce_ip(addr_str)+ if ip is not None:+ addrs.append(ip)+ if not addrs:+ raise SSRFError(f"hostname {host!r} resolved to no usable addresses")+ return addrs+++# ---------------------------------------------------------------------------+# Public API+# ---------------------------------------------------------------------------+++def validate_outbound_url(url: str, *, allow_private: bool | None = None) -> str:+ """Validate ``url`` for outbound HTTP calls from playbook steps.++ Parameters+ ----------+ url:+ The candidate URL string.+ allow_private:+ Override the env-driven default. ``True`` permits private/RFC1918+ addresses (still blocks loopback, link-local, metadata literals,+ and explicit blocklist entries). ``None`` falls back to+ ``AISOC_SSRF_ALLOW_PRIVATE``.++ Returns+ -------+ str+ The original URL on success.++ Raises+ ------+ SSRFError+ If the URL is blocked for any reason.+ """+ if not isinstance(url, str) or not url.strip():+ raise SSRFError("URL is empty")++ try:+ parts = urlsplit(url.strip())+ except ValueError as exc:+ raise SSRFError(f"could not parse URL: {exc}") from exc++ scheme = (parts.scheme or "").lower()+ if scheme not in _allowed_schemes():+ raise SSRFError(f"scheme {scheme!r} is not allowed (must be one of {sorted(_allowed_schemes())})")++ # Reject userinfo to avoid credential smuggling and host-confusion attacks.+ if parts.username or parts.password:+ raise SSRFError("URL must not include userinfo (user:password@host)")++ host = (parts.hostname or "").strip().lower()+ if not host:+ raise SSRFError("URL has no hostname")++ # Explicit blocklist takes precedence over IP / DNS checks — and over the+ # operator allowlist below, so a metadata literal cannot be re-admitted.+ blocked = _blocked_hosts()+ if host in blocked:+ raise SSRFError(f"hostname {host!r} is on the SSRF blocklist (cloud metadata or operator-blocked)")++ # An operator-named exact host (AISOC_SSRF_ALLOW_HOSTS) skips IP+ # classification: the escape for a vendor appliance that is only+ # reachable through a local tunnel, where the destination genuinely is+ # loopback and AISOC_SSRF_ALLOW_PRIVATE deliberately does not help.+ if host in _allowed_hosts():+ return url++ if allow_private is None:+ allow_private = _allow_private_by_default()++ # If host is an IP literal, validate it directly.+ literal_ip = _coerce_ip(host)+ if literal_ip is not None:+ blocked_flag, reason = _is_disallowed_address(literal_ip, allow_private=allow_private)+ if blocked_flag:+ raise SSRFError(f"host {host!r} is blocked: {reason}")+ return url++ # Otherwise, resolve the hostname and validate every returned address.+ addresses = _resolve_host_addresses(host)+ for ip in addresses:+ blocked_flag, reason = _is_disallowed_address(ip, allow_private=allow_private)+ if blocked_flag:+ raise SSRFError(f"hostname {host!r} resolves to a blocked address {ip} ({reason})")++ return url+++__all__ = ["SSRFError", "validate_outbound_url"]diff --git a/services/connectors/tests/test_egress_guard.py b/services/connectors/tests/test_egress_guard.pynew file mode 100644index 000000000..53ddf6f12--- /dev/null+++ b/services/connectors/tests/test_egress_guard.py@@ -0,0 +1,181 @@+"""Connector egress may not point inside the deployment (GHSA-cq4g-g6wp-4m2w).++Reproduced live before the fix: a **viewer-role** console JWT posted+``POST /connectors/splunk/test`` with ``base_url`` naming a loopback listener,+and this service fetched ``GET /services/server/info`` from it and reflected+the parsed response — an SSRF with a response oracle, reachable by any+authenticated tenant user, from the service that decrypts every tenant's+integration credentials at poll time.++Three layers are pinned here, because the entry points are three:+the stateless test route (the reproduced vector), ``_instantiate_or_422``+(every other router construction site), and the scheduler's poll path (stored+rows predating registration-time validation, and DNS rebinding between save+and poll).++The guard matches the **value's shape** (an ``http(s)://`` string under any+key), not a key-name list — the census found ``base_url`` plus a dozen other+spellings, and a key list is the one-directional check that misses the next+connector. IP-literal destinations are used throughout these tests so no DNS+resolution is needed and the suite is deterministic offline.+"""++from __future__ import annotations++import base64+import hashlib+import hmac+import json+import time+import uuid+from unittest.mock import AsyncMock++import httpx+import pytest+from app.security.egress import EgressRefused, refuse_internal_destinations++INTERNAL_URLS = [+ "http://169.254.169.254/latest/meta-data/iam/security-credentials/",+ "http://127.0.0.1:5432/",+ "http://[::1]:8000/",+ "http://0.0.0.0:8000/",+ "http://2130706433/", # 127.0.0.1 in decimal+ "http://10.0.0.5/",+ "http://192.168.1.10:8089/",+]+++# ---------------------------------------------------------------------------+# The helper itself+# ---------------------------------------------------------------------------++[email protected]("url", INTERNAL_URLS)+def test_internal_urls_are_refused_under_any_key(url: str) -> None:+ with pytest.raises(EgressRefused) as exc_info:+ refuse_internal_destinations({"some_vendor_specific_field": url})+ assert exc_info.value.field_name == "some_vendor_specific_field"+ assert "destination refused" in str(exc_info.value)+++def test_a_public_destination_is_accepted() -> None:+ # IP literal so the check needs no DNS. 93.184.216.34 is example.com.+ refuse_internal_destinations({"base_url": "https://93.184.216.34:8089"})+++def test_non_url_strings_and_non_strings_are_ignored() -> None:+ refuse_internal_destinations(+ {+ "token": "x" * 64,+ "page_size": 500,+ "ssl_verify": False,+ "saved_search": "AiSOC notables",+ "host_label": "prod-splunk", # not a URL shape; out of scope by design+ }+ )+++def test_list_values_are_walked() -> None:+ with pytest.raises(EgressRefused):+ refuse_internal_destinations({"urls": ["https://93.184.216.34/", "http://169.254.169.254/"]})+++def test_an_unresolvable_hostname_is_not_a_refusal() -> None:+ """A gaierror is the connector's own 'DNS lookup failed' to report, and a+ host the guard cannot resolve is one the client cannot connect to either.+ Deterministic both online (.invalid is NXDOMAIN by RFC 2606) and offline+ (resolution fails the same way)."""+ refuse_internal_destinations({"base_url": "https://no-such-host.invalid:8089"})+++def test_allow_private_relaxes_rfc1918_but_never_metadata(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv("AISOC_SSRF_ALLOW_PRIVATE", "1")+ refuse_internal_destinations({"base_url": "https://10.1.2.3:8089"}) # on-prem SIEM+ with pytest.raises(EgressRefused):+ refuse_internal_destinations({"base_url": "http://169.254.169.254/latest/meta-data/"})+++# ---------------------------------------------------------------------------+# The reproduced entry point: POST /connectors/{id}/test+# ---------------------------------------------------------------------------++SECRET = "egress-guard-test-secret-at-least-32-chars"+++def _b64(raw: bytes) -> str:+ return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()+++def _console_jwt(role: str = "viewer") -> str:+ header = _b64(json.dumps({"alg": "HS256", "typ": "JWT"}).encode())+ payload = _b64(+ json.dumps(+ {+ "sub": "egress-test",+ "tenant_id": str(uuid.uuid4()),+ "role": role,+ "type": "access",+ "exp": int(time.time()) + 600,+ }+ ).encode()+ )+ sig = _b64(hmac.new(SECRET.encode(), f"{header}.{payload}".encode(), hashlib.sha256).digest())+ return f"{header}.{payload}.{sig}"+++def client(monkeypatch: pytest.MonkeyPatch) -> httpx.AsyncClient:+ monkeypatch.setenv("SECRET_KEY", SECRET)+ monkeypatch.setenv("AISOC_CONNECTORS_DISABLE_SCHEDULER", "1")+ monkeypatch.delenv("AISOC_SSRF_ALLOW_PRIVATE", raising=False)+ from app.main import app++ transport = httpx.ASGITransport(app=app, raise_app_exceptions=False)+ return httpx.AsyncClient(transport=transport, base_url="http://testserver")+++ "base_url",+ [+ "http://169.254.169.254/latest/meta-data/iam/security-credentials/",+ "http://127.0.0.1:8000",+ "http://10.0.0.5:8089",+ ],+)+async def test_the_test_route_refuses_internal_destinations(client: httpx.AsyncClient, base_url: str) -> None:+ async with client as c:+ r = await c.post(+ "/api/v1/connectors/splunk/test",+ headers={"Authorization": f"Bearer {_console_jwt('viewer')}"},+ json={"auth_config": {"token": "x"}, "connector_config": {"base_url": base_url, "ssl_verify": False}},+ )+ assert r.status_code == 422, r.text+ assert "destination refused" in r.text+++async def test_the_test_route_still_reaches_the_connector_for_public_urls(+ client: httpx.AsyncClient, monkeypatch: pytest.MonkeyPatch+) -> None:+ """The guard must refuse before any socket opens, and only then. A public+ IP-literal destination (RFC 5737 TEST-NET ranges are themselves on the+ guard's blocklist, measured) passes the guard and reaches the connector's+ own test_connection — stubbed here so the test opens no real socket."""+ from app.connectors.splunk import SplunkConnector++ probe = AsyncMock(return_value={"success": True, "connector": "splunk", "version": "stub"})+ monkeypatch.setattr(SplunkConnector, "test_connection", probe)+ async with client as c:+ r = await c.post(+ "/api/v1/connectors/splunk/test",+ headers={"Authorization": f"Bearer {_console_jwt('viewer')}"},+ json={+ "auth_config": {"token": "x"},+ # example.com's address: public, so the guard admits it.+ "connector_config": {"base_url": "https://93.184.216.34:8089", "ssl_verify": False},+ },+ )+ assert "destination refused" not in r.text+ assert r.status_code == 200, r.text+ probe.assert_awaited_once()diff --git a/services/connectors/tests/test_scheduler.py b/services/connectors/tests/test_scheduler.pyindex f8722c45e..78f22c7c1 100644--- a/services/connectors/tests/test_scheduler.py+++ b/services/connectors/tests/test_scheduler.py@@ -1043,3 +1043,33 @@ def test_safe_normalize_still_normalizes_raw_event():out = _safe_normalize(_Normalizer(), {"id": "abc", "foo": "bar"})assert out["external_id"] == "abc"assert out["raw_event"] == {"id": "abc", "foo": "bar"}+++# ---------------------------------------------------------------------------+# Egress guard on the poll path (GHSA-cq4g-g6wp-4m2w)+# ---------------------------------------------------------------------------+++async def test_poll_refuses_a_stored_internal_destination(monkeypatch):+ """A row saved before registration-time validation existed must not be+ polled: the poll records a failure (visible as unhealthy in the UI) and+ the connector class is never even constructed, so no socket can open.+ This file rather than test_egress_guard.py because the repo-call fakes+ are patched by this module's autouse fixture."""+ inst = _make_instance(+ connector_type="fake_connector",+ auth_config={"token": "x"},+ connector_config={"base_url": "http://169.254.169.254/latest/meta-data/"},+ )+ fake_connector_class = MagicMock()+ monkeypatch.setattr("app.scheduler.CONNECTOR_REGISTRY", {"fake_connector": fake_connector_class})+ monkeypatch.delenv("AISOC_SSRF_ALLOW_PRIVATE", raising=False)++ fake_engine = _FakeEngine([inst])+ scheduler = ConnectorScheduler(engine=fake_engine, ingest_client=_FakeIngestClient(), vault=_FakeVault())+ await scheduler._poll_one(connector_id=inst.id)++ fake_connector_class.assert_not_called()+ assert fake_engine.failure_calls == [inst.id]+ assert fake_engine.success_calls == []diff --git a/tests/e2e/test_replay_cli_end_to_end.py b/tests/e2e/test_replay_cli_end_to_end.pyindex 54117aa0f..d3223eb38 100644--- a/tests/e2e/test_replay_cli_end_to_end.py+++ b/tests/e2e/test_replay_cli_end_to_end.py@@ -334,6 +334,13 @@ def stack(splunk: str) -> Iterator[dict[str, str]]:# verdict for the same evidence every time."AISOC_DETERMINISTIC": "1","AISOC_CORS_ORIGINS": "http://127.0.0.1",+ # The mock Splunk is a loopback HTTP server, and connector egress now+ # goes through the SSRF guard (GHSA-cq4g-g6wp-4m2w), which refuses+ # loopback regardless of AISOC_SSRF_ALLOW_PRIVATE. This is the guard's+ # operator escape for exactly this deployment shape — a vendor+ # reachable on a local address — named per exact host, with the+ # metadata blocklist still unoverridable.+ "AISOC_SSRF_ALLOW_HOSTS": "127.0.0.1",}services = [