Skip to content

armgit / AiSOC

Public Imported from GitHub
0

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.

feat(security): watch a running container, not just its declared defaults (#1000)

Beenu Arora6a96ed3c00d5
diff --git a/.github/workflows/container-egress.yml b/.github/workflows/container-egress.yml
new file mode 100644
index 000000000..6904cc66f
--- /dev/null
+++ b/.github/workflows/container-egress.yml
@@ -0,0 +1,111 @@
+name: Container egress
+
+# "Runs entirely on your infrastructure" had two gates and a hole in the
+# middle. `scripts/check_default_egress.py` reads every service's declared
+# settings defaults; `tests/test_no_default_egress.py` puts a socket guard in
+# front of each service's import and ASGI lifespan. Both are honest about the
+# gap, and `tests/test_no_default_egress.py` names it outright:
+#
+# Neither this nor the static gate says anything about what the *running
+# containers* do. That needs egress-blocked integration, which the Helm
+# chart's default-deny NetworkPolicy addresses at deploy time and nothing
+# addresses in CI.
+#
+# This is that integration. Images built from this commit are started on a
+# docker network created with `--internal` — no route off the host — with a
+# DNS sinkhole as their only resolver, and every name they ask for is
+# recorded and classified with the same predicate the services enforce
+# air-gap policy with.
+#
+# Two controls, because zero observations is what a correct run and a blind
+# probe both look like, and the API's unconfigured startup legitimately
+# records zero: it dials Postgres on loopback, which needs no DNS.
+#
+# * the canary, which proves a container wired exactly like the probed ones
+# is seen by the sinkhole;
+# * the red-run proof, which shows the gate failing on an image that does
+# dial out, so a green run means something.
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+ branches: [main]
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: container-egress-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ container-egress:
+ name: A running container makes no outbound call
+ runs-on: ubuntu-latest
+ timeout-minutes: 30
+
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.11'
+
+ # Same reasoning as golden-pipeline.yml: image builds fill the runner
+ # and the resulting failure reads as something else entirely.
+ - name: Reclaim disk before building
+ run: |
+ set -euo pipefail
+ echo "before: $(df -h / | awk 'NR==2{print $4}') free"
+ sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc || true
+ echo "after: $(df -h / | awk 'NR==2{print $4}') free"
+
+ # From this commit, not from `:latest`. A published tag would answer a
+ # question about whatever was last released rather than about this
+ # change.
+ - name: Build the images to probe
+ run: docker compose build api fusion
+
+ # First, because everything after it is worthless if the probe is
+ # blind, and a control that runs last is a control that reports on a
+ # verdict already printed.
+ - name: The probe can see egress
+ run: python3 scripts/check_container_egress.py --service api --negative-control
+
+ - name: No public name is asked for on startup
+ run: python3 scripts/check_container_egress.py --service api --service fusion --seconds 45
+
+ # A green run proves these images made no outbound call. It does not
+ # prove the gate would notice one, which is the property that makes it
+ # worth having. golden-pipeline.yml proves itself the same way.
+ - name: The gate reds on an image that does dial out
+ run: |
+ set -euo pipefail
+ # Its own directory, not /tmp: the build context is sent whole, and
+ # a runner's /tmp carries files the daemon cannot read.
+ mkdir -p "$RUNNER_TEMP/egress-leak"
+ cat > "$RUNNER_TEMP/egress-leak/Dockerfile" <<'DOCKERFILE'
+ FROM python:3.11-slim
+ CMD ["python", "-c", "import socket, time\ntry:\n socket.gethostbyname('telemetry.example.net')\nexcept OSError:\n pass\ntime.sleep(120)\n"]
+ DOCKERFILE
+ # `--load` explicitly: a container-driver builder keeps the result
+ # in its own cache, and `docker run` would then report the image as
+ # missing rather than the gate reporting on it.
+ docker buildx build --load -t aisoc-egress-leak:probe "$RUNNER_TEMP/egress-leak"
+ if python3 scripts/check_container_egress.py \
+ --service leaky --probe-image aisoc-egress-leak:probe --seconds 20; then
+ echo "::error title=container-egress::the gate passed an image that resolved a public name — it proves nothing"
+ exit 1
+ fi
+ echo "confirmed: the gate fails when a container dials out"
+
+ - name: Leave nothing behind
+ if: always()
+ run: |
+ # Only what this job created. Networks are named `aisoc-egress-*`
+ # by the gate and removed by it on the way out; this is the
+ # belt-and-braces for a cancelled run.
+ docker ps -aq --filter 'name=aisoc-egress-' | xargs -r docker rm -f || true
+ docker network ls -q --filter 'name=aisoc-egress-' | xargs -r docker network rm || true
diff --git a/scripts/check_container_egress.py b/scripts/check_container_egress.py
new file mode 100644
index 000000000..00668e18c
--- /dev/null
+++ b/scripts/check_container_egress.py
@@ -0,0 +1,448 @@
+#!/usr/bin/env python3
+"""A *running* container is watched, not just its declared defaults.
+
+Why this exists
+---------------
+
+Two gates already cover part of "runs entirely on your infrastructure".
+``scripts/check_default_egress.py`` reads every service's declared settings
+defaults, and ``tests/test_no_default_egress.py`` puts a socket guard in front
+of each service's import and ASGI lifespan. Both are honest about what they
+are not, and ``tests/test_no_default_egress.py`` says it in as many words:
+
+ Neither this nor the static gate says anything about what the *running
+ containers* do. That needs egress-blocked integration, which the Helm
+ chart's default-deny NetworkPolicy addresses at deploy time and nothing
+ addresses in CI.
+
+That sentence is what this closes. A declaration can be right and an import
+path can be clean while the built image reaches out anyway — a base image's
+entrypoint, a package that phones home on first use, a scheduler thread that
+only starts under the real command, a client constructed after startup rather
+than during it. None of those are visible to either existing half, because
+neither of them ever runs the container.
+
+How it works
+------------
+
+The probe is an observation, not just a block, because a block alone cannot
+tell "made no call" apart from "made a call that failed":
+
+1. A docker network is created with ``--internal``. Docker installs no route
+ off the host for it, so nothing on that network can reach the internet
+ whatever it tries. This is the containment half, and it is what makes the
+ probe safe to run on a shared machine.
+2. A DNS sinkhole container joins that network and is made the *only*
+ resolver the service container has. It logs every question asked of it and
+ answers every one of them ``NXDOMAIN``.
+3. The service container is started from the image built for this commit,
+ with the same deliberately-empty environment the static halves use — no
+ URL of any kind, so every address observed came from the service itself.
+4. After the startup window the sinkhole's log is read and every name is
+ classified with the same public/private predicate the services enforce
+ air-gap policy with. A public name is a finding. An internal one
+ (``redis``, ``postgres``, ``*.internal``) is reported and passes, because
+ dialling your own Postgres is the product working.
+
+Proving the probe can see
+-------------------------
+
+A probe that observes nothing and a service that does nothing look identical,
+which is this repository's most-repeated failure shape, and it is not
+hypothetical here: the API's unconfigured startup dials Postgres on loopback,
+which needs no name resolution, so a correct run legitimately records **zero**
+lookups. Zero is exactly what a broken sinkhole records too.
+
+Two things separate them, and both are required rather than optional:
+
+``--negative-control``
+ A canary container is started with the *identical* network and ``--dns``
+ flags the service gets, and asked to resolve a public name. The sinkhole
+ must see it. This tests the property actually in doubt — that a container
+ placed on this network resolves through this sinkhole — rather than
+ whether some particular service happens to dial its configured URL during
+ startup, which is a different claim and a much weaker control.
+
+evidence of execution
+ A service container that emitted no log line and asked for no name is
+ reported as a finding, not a pass. Whatever it proves, it is not that the
+ image declines to phone home.
+
+Honest limits
+-------------
+
+* A dial straight to a **public IP literal** never asks DNS, so this probe
+ does not observe it. The ``--internal`` network still prevents it from
+ leaving the host, and ``check_default_egress.py`` reads the declared
+ defaults where such a literal would have to be written down, but between
+ them that case is prevented and statically checked rather than observed.
+* The window is bounded. A service that would phone home on a timer longer
+ than ``--seconds`` is not covered, and the window is printed so the bound
+ is legible rather than implied.
+* Only services named on the command line are covered, and the gate refuses
+ an empty set rather than passing vacuously.
+
+Usage
+-----
+::
+
+ python3 scripts/check_container_egress.py --service api --service fusion
+ python3 scripts/check_container_egress.py --service api --negative-control
+ python3 scripts/check_container_egress.py --self-test
+
+Exit codes: 0 clean, 1 findings, 2 the check itself could not run.
+"""
+
+from __future__ import annotations
+
+import argparse
+import ipaddress
+import json
+import os
+import shutil
+import subprocess
+import sys
+import time
+import uuid
+from pathlib import Path
+
+# `scripts/` is on sys.path when this file is run as a program, but not when a
+# test loads it by path with importlib. gate_toolkit sits beside it either way.
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from gate_toolkit import repo_root, self_test_if_requested
+
+self_test_if_requested(__file__)
+
+#: Lifted from ``services/api/app/core/airgap.py``. Kept as a literal rather
+#: than imported because this gate runs in a job with no service package on
+#: the path; ``test_check_container_egress.py`` asserts it still matches the
+#: service's own tuple so the two cannot drift.
+PRIVATE_SUFFIXES = (
+ ".local",
+ ".internal",
+ ".lan",
+ ".intranet",
+ ".corp",
+ ".home",
+ ".localdomain",
+ "localhost",
+)
+
+#: How long a service gets to start and do whatever it does on startup.
+DEFAULT_WINDOW_SECONDS = 45
+
+#: The smallest environment that is still an *unconfigured* one, matching
+#: ``tests/test_no_default_egress.py``. No URL of any kind: every address the
+#: probe observes therefore came from the service's own defaults.
+MINIMAL_ENV = {
+ "ENVIRONMENT": "development",
+ "SECRET_KEY": "offline-egress-probe-secret-key-32bytes!",
+ "JWT_SECRET": "offline-egress-probe-secret-key-32bytes!",
+ "AISOC_SERVICE_TOKEN": "offline-egress-probe-token",
+ "PYTHONUNBUFFERED": "1",
+}
+
+#: The name the canary resolves. RFC 2606 reserved, so it is unregistrable and
+#: could not be reached even if the network had a route out — which it does
+#: not: the sinkhole answers NXDOMAIN and ``--internal`` drops the packet.
+NEGATIVE_CONTROL_HOST = "egress-control.example.com"
+
+#: A DNS server that answers everything NXDOMAIN and prints every question it
+#: was asked, one name per line. Stdlib only, so it runs in any python image.
+SINKHOLE_SOURCE = r"""
+import socket, sys
+
+def qname(packet):
+ i, labels = 12, []
+ while i < len(packet):
+ length = packet[i]
+ if length == 0:
+ break
+ labels.append(packet[i + 1 : i + 1 + length].decode("utf-8", "replace"))
+ i += 1 + length
+ return ".".join(labels)
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind(("0.0.0.0", 53))
+print("SINKHOLE-READY", flush=True)
+while True:
+ try:
+ packet, peer = sock.recvfrom(4096)
+ except Exception as exc:
+ print("SINKHOLE-ERROR %r" % (exc,), flush=True)
+ continue
+ if len(packet) < 13:
+ continue
+ name = qname(packet)
+ if name:
+ print("QUERY %s" % name, flush=True)
+ # NXDOMAIN: flags QR=1, RD copied, RCODE=3. Question echoed, no answers.
+ header = packet[:2] + b"\x81\x83" + packet[4:6] + b"\x00\x00\x00\x00\x00\x00"
+ try:
+ sock.sendto(header + packet[12:], peer)
+ except Exception:
+ pass
+"""
+
+
+def is_private_host(host: str) -> bool:
+ """The air-gap predicate, applied to a name the sinkhole was asked for."""
+ if not host:
+ return False
+ host = host.lower().strip().rstrip(".")
+ if "." not in host:
+ # A bare label is a compose / k8s service name — internal by
+ # definition, and the same rule the services themselves apply.
+ return True
+ for suffix in PRIVATE_SUFFIXES:
+ if host == suffix.lstrip(".") or host.endswith(suffix):
+ return True
+ try:
+ ip = ipaddress.ip_address(host)
+ except ValueError:
+ return False
+ return ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_unspecified
+
+
+def parse_queries(sinkhole_log: str) -> list[str]:
+ """Every name the sinkhole was asked for, in order, deduplicated."""
+ seen: list[str] = []
+ for line in sinkhole_log.splitlines():
+ if not line.startswith("QUERY "):
+ continue
+ name = line[len("QUERY ") :].strip().lower().rstrip(".")
+ if name and name not in seen:
+ seen.append(name)
+ return seen
+
+
+def classify(queries: list[str]) -> tuple[list[str], list[str]]:
+ """``(public, internal)`` — only the first is a finding."""
+ public = [q for q in queries if not is_private_host(q)]
+ internal = [q for q in queries if is_private_host(q)]
+ return public, internal
+
+
+def _run(cmd: list[str], *, check: bool = False, timeout: int = 120) -> subprocess.CompletedProcess[str]:
+ return subprocess.run(cmd, capture_output=True, text=True, check=check, timeout=timeout)
+
+
+class Probe:
+ """One ``--internal`` network, one sinkhole, torn down whatever happens."""
+
+ def __init__(self, tag: str) -> None:
+ self.network = f"aisoc-egress-{tag}"
+ self.sinkhole = f"aisoc-egress-dns-{tag}"
+ self.started: list[str] = []
+
+ def __enter__(self) -> Probe:
+ # `--internal` is the containment: Docker installs no route off the
+ # host, so whatever the service tries cannot leave this machine.
+ _run(["docker", "network", "create", "--internal", self.network], check=True)
+ _run(
+ [
+ "docker",
+ "run",
+ "-d",
+ "--name",
+ self.sinkhole,
+ "--network",
+ self.network,
+ "python:3.11-slim",
+ "python",
+ "-c",
+ SINKHOLE_SOURCE,
+ ],
+ check=True,
+ )
+ self.started.append(self.sinkhole)
+ for _ in range(60):
+ if "SINKHOLE-READY" in _run(["docker", "logs", self.sinkhole]).stdout:
+ return self
+ time.sleep(1)
+ raise RuntimeError("the DNS sinkhole never reported ready")
+
+ def __exit__(self, *_exc: object) -> None:
+ # Only ever removes what this probe started. Another agent's stack on
+ # the same daemon is none of its business.
+ for name in reversed(self.started):
+ _run(["docker", "rm", "-f", name])
+ _run(["docker", "network", "rm", self.network])
+
+ def sinkhole_ip(self) -> str:
+ out = _run(
+ ["docker", "inspect", "-f", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}", self.sinkhole],
+ check=True,
+ ).stdout.strip()
+ if not out:
+ raise RuntimeError("the sinkhole container reported no address")
+ return out
+
+ def _attach_flags(self) -> list[str]:
+ """The network flags every probed container gets, canary included.
+
+ One definition, used by both, so the canary proves the wiring the
+ service actually inherits rather than a lookalike of it.
+ """
+ return ["--network", self.network, "--dns", self.sinkhole_ip()]
+
+ def observe(self, service: str, image: str, env: dict[str, str], seconds: int) -> tuple[list[str], str]:
+ """Run ``image`` for ``seconds`` and return (queried names, its log)."""
+ before = set(parse_queries(_run(["docker", "logs", self.sinkhole]).stdout))
+ name = f"aisoc-egress-svc-{service}-{uuid.uuid4().hex[:8]}"
+ cmd = ["docker", "run", "-d", "--name", name, *self._attach_flags()]
+ for key, value in env.items():
+ cmd += ["-e", f"{key}={value}"]
+ cmd.append(image)
+ started = _run(cmd)
+ if started.returncode != 0:
+ raise RuntimeError(f"could not start {image}: {started.stderr.strip()}")
+ self.started.append(name)
+ time.sleep(seconds)
+ logs = _run(["docker", "logs", name])
+ service_log = logs.stdout + logs.stderr
+ # Only what *this* container asked for. Attributing an earlier
+ # container's lookup to this one would be a finding against the wrong
+ # service, and on a clean run would be a finding against nobody.
+ queries = [q for q in parse_queries(_run(["docker", "logs", self.sinkhole]).stdout) if q not in before]
+ _run(["docker", "rm", "-f", name])
+ self.started.remove(name)
+ return queries, service_log
+
+ def canary(self) -> list[str]:
+ """Resolve a public name from a container wired exactly like a probed one."""
+ before = set(parse_queries(_run(["docker", "logs", self.sinkhole]).stdout))
+ _run(
+ [
+ "docker",
+ "run",
+ "--rm",
+ *self._attach_flags(),
+ "python:3.11-slim",
+ "python",
+ "-c",
+ f"import socket\ntry:\n socket.gethostbyname({NEGATIVE_CONTROL_HOST!r})\nexcept OSError:\n pass\n",
+ ],
+ timeout=180,
+ )
+ return [q for q in parse_queries(_run(["docker", "logs", self.sinkhole]).stdout) if q not in before]
+
+
+def image_for(service: str, root: Path) -> str:
+ """The image tag ``docker compose build`` produces for ``service``.
+
+ Read from the running daemon rather than guessed, because a guessed tag
+ that does not exist would make this gate fail for a reason that has
+ nothing to do with egress.
+ """
+ out = _run(["docker", "compose", "config", "--format", "json"], timeout=120)
+ if out.returncode != 0:
+ raise RuntimeError(f"`docker compose config` failed: {out.stderr.strip()[:400]}")
+ try:
+ services = json.loads(out.stdout).get("services") or {}
+ except json.JSONDecodeError as exc:
+ raise RuntimeError(f"`docker compose config` did not emit JSON: {exc}") from exc
+ entry = services.get(service)
+ if entry is None:
+ raise RuntimeError(f"{service!r} is not a service in the compose file at {root}")
+ image = entry.get("image")
+ if not image:
+ raise RuntimeError(f"{service!r} declares no image tag to probe")
+ return str(image)
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description="Watch a running container for outbound calls.")
+ parser.add_argument("--service", action="append", default=[], help="a compose service to probe (repeatable)")
+ parser.add_argument(
+ "--probe-image",
+ default=None,
+ help=(
+ "probe this image instead of resolving the service's tag from the compose file. "
+ "This exists so the gate can be shown going red against an image that does dial "
+ "out; CI names real services everywhere else."
+ ),
+ )
+ parser.add_argument("--seconds", type=int, default=DEFAULT_WINDOW_SECONDS, help="startup window to observe")
+ parser.add_argument(
+ "--negative-control",
+ action="store_true",
+ help="run a canary wired like the probed containers and require the sinkhole to see its lookup",
+ )
+ parser.add_argument("--repo-root", type=Path, default=None)
+ args = parser.parse_args(argv)
+
+ root = args.repo_root.resolve() if args.repo_root else repo_root()
+ if not (root / "docker-compose.yml").is_file():
+ print(f"ERROR: no docker-compose.yml under {root} — nothing to probe", file=sys.stderr)
+ return 2
+ if not args.service:
+ print("ERROR: name at least one --service. A probe that covers nothing must not report clean.", file=sys.stderr)
+ return 2
+ if shutil.which("docker") is None:
+ print("ERROR: docker is not on PATH — this gate observes running containers and cannot be faked", file=sys.stderr)
+ return 2
+
+ os.chdir(root)
+ problems: list[str] = []
+ observed: list[str] = []
+
+ try:
+ with Probe(uuid.uuid4().hex[:8]) as probe:
+ if args.negative_control:
+ seen = probe.canary()
+ public, _ = classify(seen)
+ if NEGATIVE_CONTROL_HOST not in public:
+ problems.append(
+ f"a canary wired exactly like the probed containers asked for {NEGATIVE_CONTROL_HOST} "
+ f"and the sinkhole did not record it. The probe is blind, so a clean run proves nothing. "
+ f"Names seen: {', '.join(seen) or 'none'}"
+ )
+ else:
+ observed.append(f"canary observed asking for {NEGATIVE_CONTROL_HOST} — the probe can see egress")
+ else:
+ for service in args.service:
+ image = args.probe_image or image_for(service, root)
+ queries, service_log = probe.observe(service, image, dict(MINIMAL_ENV), args.seconds)
+ public, internal = classify(queries)
+
+ if not service_log.strip() and not queries:
+ problems.append(
+ f"{service} ({image}) produced no log output and asked for no name in {args.seconds}s — "
+ "the probe observed nothing at all, which is not evidence that the image declines to "
+ "phone home. Check the image starts under `docker run` with no dependencies."
+ )
+ elif public:
+ problems.append(
+ f"{service} ({image}) asked for {len(public)} public name(s) within {args.seconds}s: {', '.join(public)}"
+ )
+ print(f"--- last lines of {service} ---", file=sys.stderr)
+ print("\n".join(service_log.splitlines()[-25:]), file=sys.stderr)
+ else:
+ observed.append(
+ f"{service}: 0 public, {len(internal)} internal "
+ f"({', '.join(internal) or 'none'}), {len(service_log.splitlines())} log line(s)"
+ )
+ except subprocess.TimeoutExpired as exc:
+ print(f"ERROR: a docker command timed out: {exc}", file=sys.stderr)
+ return 2
+ except (RuntimeError, subprocess.CalledProcessError) as exc:
+ print(f"ERROR: the probe could not run: {exc}", file=sys.stderr)
+ return 2
+
+ mode = "negative control" if args.negative_control else "egress probe"
+ if problems:
+ print(f"container-egress: {len(problems)} finding(s) — {mode}, {args.seconds}s window", file=sys.stderr)
+ for problem in problems:
+ print(f" {problem}", file=sys.stderr)
+ return 1
+
+ print(f"container-egress: OK — {mode}, {args.seconds}s window, {len(args.service)} service(s)")
+ for line in observed:
+ print(f" {line}")
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/scripts/tests/test_check_container_egress.py b/scripts/tests/test_check_container_egress.py
new file mode 100644
index 000000000..155508a90
--- /dev/null
+++ b/scripts/tests/test_check_container_egress.py
@@ -0,0 +1,119 @@
+"""The container egress probe classifies the way the services themselves do.
+
+The Docker-driven half of this gate is exercised in CI by
+``.github/workflows/container-egress.yml``, which runs a canary through the
+real sinkhole and then shows the gate going red against an image that does
+dial out. What is pinned here is everything that can be decided without a
+daemon: the sinkhole log parser, the public/private split, and the fact that
+the predicate has not drifted from the one the services enforce air-gap
+policy with.
+
+That last one matters most. The gate keeps its own copy of the private-suffix
+tuple because it runs in a job with no service package importable, and two
+copies of a security predicate that nothing compares are two predicates.
+"""
+
+from __future__ import annotations
+
+import ast
+import importlib.util
+import sys
+from pathlib import Path
+
+REPO_ROOT = Path(__file__).resolve().parents[2]
+GATE = REPO_ROOT / "scripts" / "check_container_egress.py"
+AIRGAP = REPO_ROOT / "services" / "api" / "app" / "core" / "airgap.py"
+
+
+def _load():
+ spec = importlib.util.spec_from_file_location("check_container_egress", GATE)
+ assert spec is not None and spec.loader is not None
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[spec.name] = module
+ spec.loader.exec_module(module)
+ return module
+
+
+gate = _load()
+
+
+# ── the predicate has not drifted from the service's own ─────────────────────
+
+
+def test_private_suffixes_match_the_predicate_the_services_enforce():
+ """A second copy of a security predicate is a second predicate."""
+ tree = ast.parse(AIRGAP.read_text(encoding="utf-8"))
+ service_suffixes = None
+ for node in ast.walk(tree):
+ # Annotated in the service module, so both assignment forms are read:
+ # matching only `Assign` would make this cross-check silently vacuous
+ # the moment someone adds or removes the annotation.
+ if isinstance(node, ast.AnnAssign) and getattr(node.target, "id", None) == "_PRIVATE_SUFFIXES" and node.value is not None:
+ service_suffixes = tuple(ast.literal_eval(node.value))
+ elif isinstance(node, ast.Assign) and any(getattr(t, "id", None) == "_PRIVATE_SUFFIXES" for t in node.targets):
+ service_suffixes = tuple(ast.literal_eval(node.value))
+ assert service_suffixes is not None, f"_PRIVATE_SUFFIXES not found in {AIRGAP}"
+ assert gate.PRIVATE_SUFFIXES == service_suffixes
+
+
+# ── what counts as leaving the building ──────────────────────────────────────
+
+
+def test_a_public_name_is_public():
+ assert gate.is_private_host("api.openai.com") is False
+ assert gate.is_private_host("raw.githubusercontent.com") is False
+
+
+def test_a_bare_label_is_a_compose_service_name():
+ """Dialling `redis` is the product working, not a leak."""
+ assert gate.is_private_host("redis") is True
+ assert gate.is_private_host("postgres") is True
+
+
+def test_internal_suffixes_and_private_literals_are_internal():
+ for host in ("db.internal", "kafka.local", "localhost", "10.0.0.4", "127.0.0.1", "::1"):
+ assert gate.is_private_host(host) is True, host
+
+
+def test_a_public_ip_literal_is_public():
+ assert gate.is_private_host("1.1.1.1") is False
+
+
+# ── reading the sinkhole ─────────────────────────────────────────────────────
+
+
+def test_only_query_lines_are_read_and_order_is_kept():
+ log = "SINKHOLE-READY\nQUERY api.openai.com\nQUERY redis\nSINKHOLE-ERROR whatever\nQUERY api.openai.com\n"
+ assert gate.parse_queries(log) == ["api.openai.com", "redis"]
+
+
+def test_a_trailing_dot_and_case_do_not_make_a_second_name():
+ assert gate.parse_queries("QUERY API.OpenAI.Com.\nQUERY api.openai.com\n") == ["api.openai.com"]
+
+
+def test_classification_splits_a_mixed_log_the_way_the_gate_reports_it():
+ public, internal = gate.classify(["api.openai.com", "redis", "db.internal", "8.8.8.8"])
+ assert public == ["api.openai.com", "8.8.8.8"]
+ assert internal == ["redis", "db.internal"]
+
+
+def test_an_empty_log_yields_no_findings_but_also_no_evidence():
+ """Zero queries is what a correct run *and* a blind probe both look like.
+
+ Which is why the gate requires the canary and refuses a container that
+ produced no output — neither of those is decided here, but this pins that
+ the parser itself does not manufacture a signal from silence.
+ """
+ assert gate.parse_queries("SINKHOLE-READY\n") == []
+ assert gate.classify([]) == ([], [])
+
+
+# ── the gate refuses to pass vacuously ───────────────────────────────────────
+
+
+def test_naming_no_service_is_an_error_not_a_clean_run():
+ assert gate.main([]) == 2
+
+
+def test_a_tree_with_no_compose_file_is_refused(tmp_path):
+ assert gate.main(["--service", "api", "--repo-root", str(tmp_path)]) == 2