~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Check ZAP's schema and coverage contracts without starting the application."""
# allow: no-sut-import — imports the standalone CI hook directly.
import importlib.util
import json
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import Mock, patch
ROOT = Path(__file__).resolve().parents[3]
SPEC = importlib.util.spec_from_file_location(
"zap_api_hooks", ROOT / "scripts/ci/zap_api_hooks.py"
)
HOOK = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(HOOK)
def messages(status=200, content_type="application/json"):
return [
{
"requestHeader": f"{method} {HOOK.BASE}{path} HTTP/1.1\r\nHost: 127.0.0.1:5000\r\nCookie: private-fixture\r\n",
"responseHeader": f"HTTP/1.1 {status} Response\r\nContent-Type: {content_type}\r\n",
"responseBody": "private-fixture-body",
}
for path, method in HOOK.OPERATIONS.items()
]
class ContractTests(unittest.TestCase):
def test_schema_uses_real_routes_and_has_research_request_bodies(self):
source = {
"openapi": "3.1.0",
"paths": {
path: {
method.lower(): {
"operationId": str(i),
"responses": {"200": {"description": "OK"}},
}
}
for i, (path, method) in enumerate(HOOK.OPERATIONS.items())
},
}
source["paths"]["/unrelated"] = {"get": {}}
result = HOOK.build_spec(source)
self.assertEqual(result["servers"], [{"url": HOOK.BASE}])
self.assertEqual(set(result["paths"]), set(HOOK.OPERATIONS))
for path, method in HOOK.OPERATIONS.items():
if method == "POST":
body = result["paths"][path]["post"]["requestBody"]["content"][
"application/json"
]["schema"]
self.assertIn("query", body["required"])
if path.endswith("analyze_documents"):
self.assertIn("collection_name", body["required"])
self.assertNotIn(
"requestBody", source["paths"]["/api/v1/quick_summary"]["post"]
)
del source["paths"]["/api/v1/quick_summary"]
with self.assertRaises(KeyError):
HOOK.build_spec(source)
def test_each_operation_needs_successful_active_scan_requests(self):
report = HOOK.coverage(messages(), 1)
HOOK.validate_coverage(report)
for index in range(len(HOOK.OPERATIONS)):
data = messages()
del data[index]
with self.subTest(index=index), self.assertRaises(ValueError):
HOOK.validate_coverage(HOOK.coverage(data, 1))
def test_login_errors_redirects_html_and_inactive_scans_cannot_pass(self):
for status in (302, 401, 403, 429, 500):
with self.subTest(status=status), self.assertRaises(ValueError):
HOOK.validate_coverage(HOOK.coverage(messages(status), 1))
with self.assertRaises(ValueError):
HOOK.validate_coverage(
HOOK.coverage(messages(content_type="text/html"), 1)
)
with self.assertRaises(ValueError):
HOOK.validate_coverage(HOOK.coverage(messages(), 0))
def test_an_unrelated_host_cannot_supply_coverage(self):
data = messages()
for message in data:
message["requestHeader"] = message["requestHeader"].replace(
"http://127.0.0.1:5000", "https://example.invalid"
)
with self.assertRaises(ValueError):
HOOK.validate_coverage(HOOK.coverage(data, 1))
def test_login_preflight_does_not_follow_redirects(self):
replies = [
(401, {}),
(200, {"csrf_token": "before-login"}),
(302, b""),
(200, {"csrf_token": "after-login"}),
(403, {}),
*((200, {"items": ["seed"]}) for _ in HOOK.OPERATIONS),
]
def add_cookie(jar, request):
request.add_header("Cookie", "fixture-session")
with (
patch.object(HOOK, "request", side_effect=replies) as request,
patch.object(
HOOK.http.cookiejar.CookieJar, "add_cookie_header", add_cookie
),
):
self.assertEqual(
HOOK.authenticate(), ("fixture-session", "after-login")
)
for call in request.call_args_list:
self.assertTrue(
any(
isinstance(handler, HOOK.NoRedirect)
for handler in call.args[0].handlers
)
)
self.assertEqual(request.call_args_list[2].args[3], "before-login")
self.assertEqual(request.call_args_list[-1].args[3], "after-login")
def test_authenticated_headers_are_bound_to_loopback_target(self):
zap = SimpleNamespace(
replacer=SimpleNamespace(add_rule=Mock(return_value="OK"))
)
with (
patch.object(
HOOK,
"authenticate",
return_value=("fixture-cookie", "fixture-csrf"),
),
patch.object(HOOK, "AUTHENTICATED", False),
):
HOOK.zap_started(zap, "/zap/wrk/zap-openapi.json")
self.assertTrue(HOOK.AUTHENTICATED)
self.assertEqual(zap.replacer.add_rule.call_count, 2)
for call in zap.replacer.add_rule.call_args_list:
self.assertEqual(
call.kwargs["url"], r"^http://127\.0\.0\.1:5000/.*$"
)
self.assertEqual(call.kwargs["matchtype"], "REQ_HEADER")
def test_evidence_comes_from_active_scan_message_ids_and_is_redacted(self):
zap = SimpleNamespace(
ascan=SimpleNamespace(
scans=[{"id": "0", "progress": "100"}],
messages_ids=Mock(return_value=["1", "2"]),
),
core=SimpleNamespace(messages_by_id=Mock(return_value=messages())),
)
with (
patch.object(HOOK, "AUTHENTICATED", True),
patch.object(HOOK.Path, "write_text") as write,
):
HOOK.zap_pre_shutdown(zap)
stored = write.call_args.args[0]
HOOK.validate_coverage(json.loads(stored))
self.assertNotIn("private-fixture", stored)
zap.ascan.messages_ids.assert_called_once_with("0")
zap.core.messages_by_id.assert_called_once_with("1,2")
if __name__ == "__main__":
unittest.main()