// PEP-189: the platform's port and domains are not for projects. A container that publishes a // reserved port (the web on 3100 — Traefik reaches it at host.docker.internal:3100, which OrbStack // maps to the host's 127.0.0.1) or routes a platform domain through Traefik labels takes over // peppa.telecom.quest. Pure helpers here; the watchdog side is runner/domain-guard.ts. /** Containers of the platform itself — exact names: agent sandboxes are `peppa-s` too. */ const OWN_CONTAINERS = ["peppa-bugsink", "peppa-act-runner", "peppa-postgres", "gitea", "gitea-pages", "traefik"]; export interface DockerContainer { id: string; name: string; /** `docker ps` Ports column: `127.0.0.1:3100->3100/tcp, 3000/tcp`. */ ports: string; /** `docker ps` Labels column: `k=v,k=v`. */ labels: string; } export interface GuardRules { ports: number[]; hosts: string[]; allow: string[]; } export interface Intruder { container: DockerContainer; /** Reserved host ports it publishes. */ ports: number[]; /** Platform domains its Traefik labels route. */ hosts: string[]; /** `com.docker.compose.project` label, when present. */ composeProject: string | null; } function hostOf(url: string | undefined): string | null { if (!url) return null; try { return new URL(url).hostname.toLowerCase(); } catch { return null; } } function portOf(url: string | undefined): number | null { if (!url) return null; try { const u = new URL(url); return u.port ? Number(u.port) : null; } catch { return null; } } const isLocal = (h: string | null) => !h || h === "localhost" || h === "127.0.0.1" || h === "::1" || h === "[::1]"; const list = (v: string | undefined) => (v ?? "").split(",").map((s) => s.trim()).filter(Boolean); /** Reserved ports and domains from env: the web's port, Bugsink, the bot's mock port and PEPPA_RESERVED_*. */ export function guardRules(env: NodeJS.ProcessEnv = process.env): GuardRules { const ports = new Set([3100, 3180, 3190]); const webPort = portOf(env.PEPPA_WEB_URL); if (webPort) ports.add(webPort); for (const p of list(env.PEPPA_RESERVED_PORTS)) if (Number.isInteger(Number(p))) ports.add(Number(p)); const hosts = new Set(); for (const u of [env.PEPPA_WEB_URL, env.BUGSINK_URL, env.GITEA_URL]) { const h = hostOf(u); if (!isLocal(h)) hosts.add(h!); } for (const h of list(env.PEPPA_RESERVED_HOSTS)) hosts.add(h.toLowerCase()); return { ports: [...ports].sort((a, b) => a - b), hosts: [...hosts].sort(), allow: [...OWN_CONTAINERS, ...list(env.PEPPA_GUARD_ALLOW)] }; } /** `docker ps --format '{{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Labels}}'` → containers. */ export function parseDockerPs(out: string): DockerContainer[] { return out .split("\n") .filter((l) => l.trim()) .map((l) => { const [id = "", name = "", ports = "", labels = ""] = l.split("\t"); return { id: id.trim(), name: name.trim(), ports: ports.trim(), labels: labels.trim() }; }) .filter((c) => c.id && c.name); } /** Host ports a container publishes: `0.0.0.0:80->80/tcp`, `[::]:80->80/tcp`, `127.0.0.1:3100->3100/tcp`. */ export function publishedPorts(ports: string): number[] { const out = new Set(); for (const m of ports.matchAll(/:(\d+)(?:-(\d+))?->/g)) { const from = Number(m[1]); const to = m[2] ? Number(m[2]) : from; for (let p = from; p <= to && p - from < 1000; p++) out.add(p); } return [...out]; } /** Hosts named in `Host(`…`)` of the container's Traefik router rules. */ export function traefikHosts(labels: string): string[] { const out = new Set(); for (const rule of labels.matchAll(/traefik\.http\.routers\.[^=,]+\.rule=([^]*?)(?=,[a-z0-9_.-]+=|$)/gi)) { for (const h of rule[1]!.matchAll(/Host\(([^)]*)\)/gi)) { for (const v of h[1]!.matchAll(/[`"']([^`"']+)[`"']/g)) out.add(v[1]!.toLowerCase()); } } return [...out]; } function label(labels: string, key: string): string | null { const m = labels.match(new RegExp(`(?:^|,)${key.replace(/\./g, "\\.")}=([^,]*)`)); return m ? m[1]! : null; } /** Containers that take a reserved port or a platform domain; the platform's own are skipped. */ export function findIntruders(containers: DockerContainer[], rules: GuardRules): Intruder[] { const out: Intruder[] = []; for (const c of containers) { if (rules.allow.includes(c.name)) continue; const ports = publishedPorts(c.ports).filter((p) => rules.ports.includes(p)); const hosts = traefikHosts(c.labels).filter((h) => rules.hosts.includes(h)); if (!ports.length && !hosts.length) continue; out.push({ container: c, ports, hosts, composeProject: label(c.labels, "com.docker.compose.project") }); } return out; } /** * Whose container it is: the name or compose project starts with `-` (`ia-2-smoke`) * or with `-` (`brendmanager-pr64`). Returns the matching task key / slug. */ export function intruderOwner(i: Intruder, taskKeys: string[], slugs: string[]): { taskKey?: string; slug?: string } { const names = [i.container.name, i.composeProject].filter((n): n is string => !!n).map((n) => n.toLowerCase()); const starts = (n: string, p: string) => n === p || n.startsWith(`${p}-`) || n.startsWith(`${p}_`); const task = [...taskKeys].sort((a, b) => b.length - a.length).find((k) => names.some((n) => starts(n, k.toLowerCase()))); if (task) return { taskKey: task }; const slug = [...slugs].sort((a, b) => b.length - a.length).find((s) => names.some((n) => starts(n, s.toLowerCase()))); return slug ? { slug } : {}; } /** True when the body is Peppa's own /api/health answer. */ export function isPeppaHealth(body: string): boolean { try { const j = JSON.parse(body) as { boot?: unknown; services?: unknown }; return typeof j.boot === "string" && Array.isArray(j.services); } catch { return false; } }