~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
"""Authenticated ZAP API scan and evidence checks for the disposable CI target."""
import copy
import http.cookiejar
import json
import sys
import urllib.error
import urllib.parse
import urllib.request
from pathlib import Path
BASE = "http://127.0.0.1:5000"
OPERATIONS = {
"/api/v1/": "GET",
"/api/v1/quick_summary": "POST",
"/api/v1/generate_report": "POST",
"/api/v1/analyze_documents": "POST",
"/history/api": "GET",
}
AUTHENTICATED = False
def build_spec(source):
"""Use real route definitions, adding bodies for manually parsed JSON."""
spec = copy.deepcopy(source)
if not spec.get("openapi", "").startswith("3."):
raise ValueError("Expected the application's OpenAPI 3 schema")
spec["servers"] = [{"url": BASE}]
spec["paths"] = {}
for path, method in OPERATIONS.items():
operation = copy.deepcopy(source["paths"][path][method.lower()])
if method == "POST":
properties = {
"query": {"type": "string", "default": "CI research fixture"}
}
required = ["query"]
if path.endswith("analyze_documents"):
properties["collection_name"] = {
"type": "string",
"default": "ci-fixture",
}
required.append("collection_name")
operation["requestBody"] = {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"required": required,
"properties": properties,
}
}
},
}
elif path == "/history/api":
operation["parameters"] = [
{
"name": name,
"in": "query",
"schema": {"type": "integer", "default": default},
}
for name, default in (("limit", 20), ("offset", 0))
]
spec["paths"][path] = {method.lower(): operation}
return spec
def request(opener, path, data=None, token=None, form=False):
headers = {"Accept": "application/json"}
if token:
headers["X-CSRFToken"] = token
if data is not None:
headers["Content-Type"] = (
"application/x-www-form-urlencoded" if form else "application/json"
)
data = (
urllib.parse.urlencode(data) if form else json.dumps(data)
).encode()
# Scheme and host are fixed to this job's disposable loopback server.
req = urllib.request.Request(BASE + path, data=data, headers=headers) # noqa: S310
try:
response = opener.open(req, timeout=30)
except urllib.error.HTTPError as exc:
response = exc
with response:
body = response.read()
if "application/json" in response.headers.get("Content-Type", ""):
body = json.loads(body)
return response.status, body
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
# The preflight only needs the local login response; never forward
# authentication headers to a redirect destination.
return None
def authenticate():
anonymous = urllib.request.build_opener(
urllib.request.ProxyHandler({}), NoRedirect()
)
if request(anonymous, "/api/v1/")[0] != 401:
raise ValueError("Anonymous API access did not fail with 401")
jar = http.cookiejar.CookieJar()
client = urllib.request.build_opener(
urllib.request.ProxyHandler({}),
urllib.request.HTTPCookieProcessor(jar),
NoRedirect(),
)
status, response = request(client, "/auth/csrf-token")
if status != 200:
raise ValueError("Could not obtain login CSRF token")
login_status, _ = request(
client,
"/auth/login",
{"username": "test_admin", "password": "testpass123"},
response["csrf_token"],
form=True,
) # pragma: allowlist secret
if login_status != 302:
raise ValueError("Fixture login did not succeed")
status, response = request(client, "/auth/csrf-token")
if status != 200:
raise ValueError("Could not obtain authenticated CSRF token")
token = response["csrf_token"]
# Authentication and CSRF protection stay enabled on this fixture server.
if (
request(client, "/api/v1/quick_summary", {"query": "CI fixture"})[0]
!= 403
):
raise ValueError(
"Authenticated API request without CSRF was not rejected"
)
for path, method in OPERATIONS.items():
body = {"query": "CI fixture"} if method == "POST" else None
if path.endswith("analyze_documents"):
body["collection_name"] = "ci-fixture"
status, response = request(client, path, body, token)
if status != 200 or not isinstance(response, dict):
raise ValueError(
f"Authenticated API preflight failed for {method} {path}"
)
if path == "/history/api" and not response.get("items"):
raise ValueError("Authenticated history fixture is empty")
# Capture the final cookie, after login/session bootstrap has completed.
probe = urllib.request.Request("http://127.0.0.1:5000/api/v1/")
jar.add_cookie_header(probe)
cookie = probe.get_header("Cookie")
if not cookie:
raise ValueError("Login produced no usable session cookie")
return cookie, token
def zap_started(zap, target):
global AUTHENTICATED
cookie, token = authenticate()
for name, value in (("Cookie", cookie), ("X-CSRFToken", token)):
result = zap.replacer.add_rule(
description=f"CI authenticated {name}",
enabled="true",
matchtype="REQ_HEADER",
matchregex="false",
matchstring=name,
replacement=value,
url=r"^http://127\.0\.0\.1:5000/.*$",
)
if result != "OK":
raise ValueError(
"ZAP could not configure authenticated request headers"
)
AUTHENTICATED = True
def coverage(messages, active_scans):
rows = {
f"{method} {path}": {"requests": 0, "successful_json_responses": 0}
for path, method in OPERATIONS.items()
}
for message in messages:
method, url, _ = message["requestHeader"].splitlines()[0].split(" ", 2)
parsed = urllib.parse.urlsplit(url)
if parsed.scheme and (
parsed.scheme != "http" or parsed.netloc != "127.0.0.1:5000"
):
continue
key = f"{method} {parsed.path}"
if key not in rows:
continue
status = int(message["responseHeader"].splitlines()[0].split()[1])
headers = message["responseHeader"].lower()
rows[key]["requests"] += 1
if 200 <= status < 300 and "content-type: application/json" in headers:
rows[key]["successful_json_responses"] += 1
return {"active_scans": active_scans, "operations": rows}
def validate_coverage(report):
if (
type(report.get("active_scans")) is not int
or report["active_scans"] < 1
):
raise ValueError("No completed active scan")
for path, method in OPERATIONS.items():
row = report["operations"][f"{method} {path}"]
if row["requests"] < 1 or row["successful_json_responses"] < 1:
raise ValueError(
f"No authenticated active-scan coverage for {method} {path}"
)
def zap_pre_shutdown(zap):
if not AUTHENTICATED:
raise ValueError("Authenticated scan setup never completed")
scans = zap.ascan.scans
if not scans or any(int(scan["progress"]) != 100 for scan in scans):
raise ValueError("Active scan did not complete")
ids = sorted(
{
str(message_id)
for scan in scans
for message_id in zap.ascan.messages_ids(scan["id"])
}
)
def scan_messages():
for start in range(0, len(ids), 100):
yield from zap.core.messages_by_id(
",".join(ids[start : start + 100])
)
report = coverage(scan_messages(), len(scans))
# Store counts only: never persist session cookies or request/response bodies.
Path("/zap/wrk/zap-api-coverage.json").write_text(
json.dumps(report, indent=2) + "\n", encoding="utf-8"
)
validate_coverage(report)
print(
f"Verified authenticated active-scan coverage for {len(OPERATIONS)} operations"
)
if __name__ == "__main__":
# CI-only entry point: the path is the coverage report the workflow
# itself passes in, not user input.
# bearer:disable python_lang_path_using_user_input
validate_coverage(json.loads(Path(sys.argv[1]).read_text(encoding="utf-8")))