"""C13 — the security addendum says what the code enforces, and its host table cannot go stale. A sentence in a security document that the code does not back is exactly the kind of claim a review reads as a control. So every number the addendum states is read back from the code here, and the host table is regenerated from config and compared. """ from __future__ import annotations import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent ADDENDUM = (ROOT / "docs/security/addendum-knowledge-specialist.md").read_text(encoding="utf-8") def _flat(text: str) -> str: return " ".join(text.split()) def test_the_host_table_is_fresh(): result = subprocess.run( [sys.executable, str(ROOT / "scripts/render_host_table.py"), "--check"], capture_output=True, text=True, check=False, ) assert result.returncode == 0, result.stderr def test_every_declared_host_is_in_the_addendum(): from src import config for src in config.sources().values(): for host in src["hosts"]: assert f"`{host}`" in ADDENDUM, host def test_the_numbers_it_states_are_the_codes(): from src import config from src.endpoints._common import ENDPOINT_BUDGET_SECONDS from src.identifiers import MAX_DISEASE_NAME, MAX_IDENTIFIERS text = _flat(ADDENDUM) assert ( f"{len(config.allowed_tool_names())} are approved and the {len(config.endpoint_tool_names())}" in text ) rates = {s["rate_per_second"] for s in config.sources().values()} timeouts = {s["timeout_seconds"] for s in config.sources().values()} assert rates == {3} and "At most 3 requests per second per source" in text assert timeouts == {30} and "30 seconds per call" in text assert f"{ENDPOINT_BUDGET_SECONDS} seconds per endpoint call" in text assert f"at most {MAX_IDENTIFIERS} per call" in text assert f"at most {MAX_DISEASE_NAME} characters" in text assert ( config.source("clinicaltrials")["ttl_seconds"] == 24 * 3600 and "24 hours for ClinicalTrials.gov" in text ) assert config.source("opentargets")["ttl_seconds"] == 7 * 86400 and "up to 7 days" in text assert config.pinned_tooluniverse_version() == "1.5.6" and "pinned to 1.5.6" in text def test_every_model_provider_it_names_is_refused(): from src.tu_client import LLM_KEY_VARS named = { "OpenAI": "OPENAI_API_KEY", "Azure OpenAI": "AZURE_OPENAI_API_KEY", "Gemini": "GEMINI_API_KEY", "Google": "GOOGLE_API_KEY", "OpenRouter": "OPENROUTER_API_KEY", "Anthropic": "ANTHROPIC_API_KEY", "vLLM": "VLLM_SERVER_URL", } for provider, var in named.items(): assert provider in ADDENDUM and var in LLM_KEY_VARS, provider def test_the_accepted_finding_it_cites_is_the_recorded_one(): accepted = (ROOT / "security/accepted-vulns.txt").read_text() expiries = set(re.findall(r"expires=(\d{4}-\d{2}-\d{2})", accepted)) assert expiries == {"2026-12-18"} and "expires on 2026-12-18" in _flat(ADDENDUM) # Every accepted id counts, not just CVE ids: some advisories arrive as GHSA ids only (batch 5). n_ids = len(re.findall(r"^(?:CVE|GHSA|PYSEC)-", accepted, re.MULTILINE)) assert f"{n_ids} pillow 12.3.0" in _flat(ADDENDUM) def test_it_states_the_egress_limit_honestly(): text = _flat(ADDENDUM) assert "Hugging Face Spaces offer no egress allow-list" in text assert "not a network control" in text def test_it_names_roles_not_people(): assert not re.search(r"\b(Annie|Voigt|Sears|Rosalie|Carl|Pelz|Brody)\b", ADDENDUM)