~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
#!/usr/bin/env python3
"""Fail closed when literal template url_for names are absent from _URL_MAP."""
import ast
import os
import stat
import sys
from pathlib import Path
from typing import Final, NamedTuple
ROOT: Final = Path(__file__).resolve().parent.parent
APP_PATH: Final = ROOT / "src/local_deep_research/web/fastapi_app.py"
TEMPLATES_DIR: Final = ROOT / "src/local_deep_research/web/templates"
SETUP: Final = "_setup_template_globals"
RAW_END: Final = "Jinja raw region has no matching endraw"
ESCAPED_LITERAL: Final = "url_for route-name literals cannot contain escapes"
MUTATORS: Final = frozenset(
"update setdefault pop popitem clear __setitem__ __delitem__".split()
)
MAX_APP_BYTES = 2 * 1024 * 1024
MAX_TEMPLATE_BYTES = 2 * 1024 * 1024
MAX_TEMPLATE_TOTAL_BYTES = 32 * 1024 * 1024
MAX_TEMPLATE_FILES = 4096
MISSING: Final = (
"url_for name {!r} is absent from _URL_MAP. Fix: add it to _URL_MAP."
)
class Diagnostic(NamedTuple):
path: Path
line: int | None
message: str
def render(self) -> str:
line = f":{self.line}" if self.line is not None else ""
return f"{self.path}{line}: {self.message}"
class CheckResult(NamedTuple):
literal_names: frozenset[str]
diagnostics: tuple[Diagnostic, ...]
class _InputError(Exception):
def __init__(self, diagnostic: Diagnostic) -> None:
self.diagnostic = diagnostic
def _fatal(path: Path, message: str) -> Diagnostic:
return Diagnostic(path, None, f"{message}. Fix: restore the input.")
def _fail(path: Path, message: str) -> None:
raise _InputError(_fatal(path, message))
def _ensure(condition: bool, path: Path, message: str) -> None:
if not condition:
_fail(path, message)
def _map_value(statement: ast.stmt) -> ast.Dict | None:
match statement:
case ast.Assign(
targets=[ast.Name(id="_URL_MAP")], value=ast.Dict() as value
):
return value
case ast.AnnAssign(
target=ast.Name(id="_URL_MAP"), value=ast.Dict() as value
):
return value
return None
def _eager_children(node: ast.AST) -> tuple[ast.AST, ...]:
excluded = (
{"body"}
if isinstance(
node,
(ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef, ast.Lambda),
)
else set()
)
fields = (
value for field, value in ast.iter_fields(node) if field not in excluded
)
return tuple(
child
for value in fields
for child in (value if isinstance(value, list) else (value,))
if isinstance(child, ast.AST)
)
def _changes_map(node: ast.AST, after: tuple[int, int]) -> bool:
later = (getattr(node, "lineno", 0), getattr(node, "col_offset", 0)) > after
direct = (
isinstance(node, ast.Name)
and node.id == "_URL_MAP"
and isinstance(node.ctx, (ast.Store, ast.Del))
)
indexed = isinstance(node, ast.Subscript) and isinstance(
node.ctx, (ast.Store, ast.Del)
)
indexed = (
indexed
and isinstance(node.value, ast.Name)
and node.value.id == "_URL_MAP"
)
function = node.func if isinstance(node, ast.Call) else None
mutation = isinstance(function, ast.Attribute) and function.attr in MUTATORS
mutation = mutation and isinstance(function.value, ast.Name)
mutation = mutation and function.value.id == "_URL_MAP"
value = (
node.value
if isinstance(node, (ast.Assign, ast.AnnAssign, ast.NamedExpr))
else None
)
aliases = isinstance(value, ast.Name) and value.id == "_URL_MAP"
if isinstance(node, ast.Assign):
aliases = aliases and any(
isinstance(target, ast.Name) for target in node.targets
)
elif isinstance(node, (ast.AnnAssign, ast.NamedExpr)):
aliases = aliases and isinstance(node.target, ast.Name)
changed = later and (direct or indexed or mutation or aliases)
return changed or any(
_changes_map(child, after) for child in _eager_children(node)
)
def _read_text(path: Path, limit: int, message: str) -> tuple[str, int]:
try:
before = path.lstat()
except OSError:
_fail(path, message)
_ensure(stat.S_ISREG(before.st_mode), path, "Input is not a regular file")
_ensure(before.st_size <= limit, path, "Input exceeds size limit")
try:
flags = os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK
with os.fdopen(os.open(path, flags), "rb") as file:
opened = os.fstat(file.fileno())
_ensure(
stat.S_ISREG(opened.st_mode),
path,
"Input is not a regular file",
)
_ensure(
(opened.st_dev, opened.st_ino)
== (before.st_dev, before.st_ino),
path,
"Input changed while opening",
)
data = file.read(limit + 1)
except OSError:
_fail(path, message)
_ensure(len(data) <= limit, path, "Input exceeds size limit")
try:
return data.decode(), len(data)
except UnicodeDecodeError:
_fail(path, message)
def _load_map(path: Path) -> frozenset[str]:
source, _ = _read_text(
path, MAX_APP_BYTES, "Application source is unreadable"
)
try:
tree = ast.parse(source, filename=str(path))
except SyntaxError:
_fail(path, "Application source has syntax errors")
setups = [
node for node in tree.body if getattr(node, "name", None) == SETUP
]
_ensure(
len(setups) == 1 and isinstance(setups[0], ast.FunctionDef),
path,
"Expected one synchronous setup function",
)
setup = setups[0]
candidates = [
(statement, value)
for statement in setup.body
if (value := _map_value(statement)) is not None
]
_ensure(len(candidates) == 1, path, "Expected one direct literal map")
assignment, mapping = candidates[0]
names: set[str] = set()
for key, value in zip(mapping.keys, mapping.values, strict=True):
_ensure(key is not None, path, "Map cannot unpack mappings")
constants = isinstance(key, ast.Constant) and isinstance(
value, ast.Constant
)
_ensure(constants, path, "Map keys and values must be strings")
strings = isinstance(key.value, str) and isinstance(value.value, str)
_ensure(strings, path, "Map keys and values must be strings")
_ensure(key.value not in names, path, "Map has duplicate keys")
names.add(key.value)
after = (assignment.end_lineno, assignment.end_col_offset)
unchanged = not any(
_changes_map(statement, after) for statement in setup.body
)
_ensure(unchanged, path, "Map changes after its literal assignment")
return frozenset(names)
def _raw_tag(content: str) -> str:
stripped = content.lstrip()
if content.startswith(("-", "+")):
stripped = content[1:].lstrip()
elif stripped.startswith(("-", "+")):
return ""
for name in ("endraw", "raw"):
if not stripped.startswith(name):
continue
suffix = stripped[len(name) :]
if suffix[:1] and not suffix[:1].isspace() and suffix[:1] not in "-+":
return ""
suffix = suffix.lstrip()
marker = suffix[:1] if suffix[:1] in "-+" else ""
suffix = suffix[len(marker) :]
if suffix.strip() or (name == "raw" and marker == "+"):
return ""
return name
return ""
def _identifier(character: str) -> bool:
return character == "_" or character.isalnum()
class _Scanner:
def __init__(self, path: Path, text: str) -> None:
self.path, self.text = path, text
self.position, self.line = 0, 1
self.references: list[tuple[Path, int, str]] = []
def _advance(self, count: int = 1) -> None:
self.line += self.text[self.position : self.position + count].count(
"\n"
)
self.position += count
def _error(self, message: str) -> None:
_fail(self.path, message)
def _comment(self) -> None:
end = self.text.find("#}", self.position + 2)
if end < 0:
self._error("Jinja has an unmatched delimiter")
self._advance(end + 2 - self.position)
def _raw_candidate(self, *, raw: bool) -> str:
self._advance(2)
tag: list[str] = []
while self.position < len(self.text):
if self.text.startswith("{%", self.position):
tag.clear()
self._advance(2)
elif self.text.startswith("%}", self.position):
self._advance(2)
return _raw_tag("".join(tag))
else:
tag.append(self.text[self.position])
self._advance()
self._error(RAW_END if raw else "Jinja has an unmatched delimiter")
def _region(self, kind: str) -> str:
closing = "}}" if kind == "{{" else "%}"
quote = call = previous = significant = ""
escaped, depth, literal, tag = False, [], [], []
while self.position < len(self.text):
character = self.text[self.position]
if quote:
if kind == "{%":
tag.append(character)
if escaped:
escaped = False
elif character == "\\":
if call == "literal":
self._error(ESCAPED_LITERAL)
escaped = True
elif character == quote:
quote = ""
if call == "literal":
call = "finish"
elif call == "literal":
literal.append(character)
self._advance()
continue
if self.text.startswith(closing, self.position) and not depth:
self._advance(2)
return _raw_tag("".join(tag)) if kind == "{%" else ""
if self.text.startswith(("{{", "{%", "{#"), self.position) or (
not depth
and self.text.startswith(("}}", "%}", "#}"), self.position)
):
self._error("Jinja has an unmatched delimiter")
if (
call == "expect-open"
and not character.isspace()
and character != "("
):
call = ""
elif call == "expect-argument" and not character.isspace():
if self.text.startswith(
"name", self.position
) and not _identifier(
self.text[self.position + 4 : self.position + 5]
):
if kind == "{%":
tag.extend("name")
previous = significant = "e"
self._advance(4)
call = "expect-equals"
continue
if character not in "'\"":
call = ""
elif (
call == "expect-equals"
and not character.isspace()
and character != "="
):
call = ""
elif (
call == "expect-literal"
and not character.isspace()
and character not in "'\""
):
call = ""
elif call == "finish" and not character.isspace():
if character in ",)":
self.references.append(
(self.path, self.line, "".join(literal))
)
call = ""
if call == "expect-open" and character == "(":
call = "expect-argument"
elif call == "expect-equals" and character == "=":
call = "expect-literal"
elif (
call in {"expect-argument", "expect-literal"}
and character in "'\""
):
quote, call, literal = character, "literal", []
elif character in "'\"":
quote = character
elif call == "literal":
literal.append(character)
elif call == "" and self.text.startswith("url_for", self.position):
following = self.text[self.position + 7 : self.position + 8]
if (
not _identifier(previous)
and significant != "."
and not _identifier(following)
):
if kind == "{%":
tag.extend("url_for")
previous = significant = "r"
self._advance(7)
call = "expect-open"
continue
if kind == "{%":
tag.append(character)
if character in "([{":
depth.append({"(": ")", "[": "]", "{": "}"}[character])
elif character in ")]}":
if not depth or depth.pop() != character:
self._error("Jinja has an unmatched delimiter")
previous = character
if not character.isspace():
significant = character
self._advance()
self._error("Jinja has an unmatched delimiter")
def scan(self) -> tuple[tuple[Path, int, str], ...]:
raw = False
while self.position < len(self.text):
if raw:
start = self.text.find("{%", self.position)
if start < 0:
self._error(RAW_END)
self._advance(start - self.position)
tag = self._raw_candidate(raw=True)
if tag == "endraw":
raw = False
elif tag == "raw":
self._error("Jinja raw regions cannot be nested")
continue
if self.text.startswith("{#", self.position):
self._comment()
continue
kind = self.text[self.position : self.position + 2]
if kind in {"{{", "{%"}:
self._advance(2)
tag = self._region(kind)
if tag == "endraw":
self._error("Jinja endraw has no matching raw region")
raw = tag == "raw"
continue
self._advance()
return tuple(self.references)
def _template_paths(root: Path) -> tuple[Path, ...]:
try:
root_status = root.lstat()
except OSError:
_fail(root, "Missing template directory")
valid_root = not stat.S_ISLNK(root_status.st_mode) and stat.S_ISDIR(
root_status.st_mode
)
_ensure(valid_root, root, "Missing template directory")
paths: list[Path] = []
def walk_error(error: OSError) -> None:
_fail(Path(error.filename or root), "Cannot enumerate template files")
for directory, names, files in os.walk(
root, onerror=walk_error, followlinks=False
):
names.sort()
for name in (*names, *sorted(files)):
candidate = Path(directory, name)
try:
status = candidate.lstat()
except OSError:
_fail(candidate, "Cannot inspect template input")
valid = not stat.S_ISLNK(status.st_mode) and (
stat.S_ISREG(status.st_mode) or stat.S_ISDIR(status.st_mode)
)
_ensure(valid, candidate, "Template input is not a regular file")
if stat.S_ISREG(status.st_mode) and candidate.suffix == ".html":
paths.append(candidate)
_ensure(
len(paths) <= MAX_TEMPLATE_FILES,
root,
"Too many HTML templates",
)
_ensure(bool(paths), root, "No HTML templates")
return tuple(sorted(paths))
def _scan_templates(root: Path) -> tuple[tuple[Path, int, str], ...]:
paths = _template_paths(root)
references: list[tuple[Path, int, str]] = []
total = 0
for path in paths:
text, size = _read_text(
path, MAX_TEMPLATE_BYTES, "Cannot read UTF-8 template"
)
total += size
if total > MAX_TEMPLATE_TOTAL_BYTES:
_fail(root, "HTML templates exceed size limit")
references.extend(_Scanner(path, text).scan())
if not references:
_fail(root, "No literal url_for calls")
return tuple(references)
def check_url_targets(
fastapi_app_path: Path, templates_dir: Path
) -> CheckResult:
try:
names = _load_map(fastapi_app_path)
references = _scan_templates(templates_dir)
except _InputError as error:
return CheckResult(frozenset(), (error.diagnostic,))
literal_names = frozenset(reference[2] for reference in references)
errors = tuple(
Diagnostic(path, line, MISSING.format(name))
for path, line, name in sorted(
(
reference
for reference in references
if reference[2] not in names
),
key=lambda reference: (
str(reference[0]),
reference[1],
reference[2],
),
)
)
return CheckResult(literal_names, errors)
def main() -> int:
result = check_url_targets(APP_PATH, TEMPLATES_DIR)
if result.diagnostics:
for diagnostic in result.diagnostics:
print(diagnostic.render())
return 1
print(f"URL-map check passed: {len(result.literal_names)} names mapped.")
return 0
if __name__ == "__main__":
sys.exit(main())