Skip to content

~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.

43.7 KB
Raw Blame
"""Injection and template-output safety for the FastAPI port.

Scope: the *exceptions* to the safe defaults. Global Jinja autoescape
being on is asserted elsewhere; ORM usage being the norm is asserted by
the absence of raw SQL. What this module pins is every place the code
deliberately steps outside those defaults:

1. **SQL** — every site that builds a SQL *string* instead of handing an
   expression to the ORM, every ``LIKE`` pattern built from input, and
   the one ``getattr(Model, <request value>)`` column lookup.
2. **Path traversal outside static serving** — download/export filenames
   reaching ``Content-Disposition``, and the two ``/open_file_location``
   endpoints. (``/static/{path}`` is covered by its own module.)
3. **Template output safety** — every ``|safe`` and ``autoescape false``
   in ``web/templates``, JSON embedded in HTML (``|tojson`` in a script
   body vs. in an attribute), and the ``innerHTML`` lint fence.

Most of this is decided statically, because "is this string ever built
from a request value" is a property of the source, not of one response.
Where encoding behaviour itself is the question the tests *render* with
a real Jinja2 environment and assert the payload arrives in its escaped
form — asserting only that ``<script>`` is absent would pass just as
happily if the payload never reached the template at all.

Every scanner in this file ships with a pair of self-tests proving it
flags a deliberately-unsafe synthetic source and stays quiet on a safe
one; a scanner that silently matches nothing is the failure mode these
census tests are most exposed to.
"""

from __future__ import annotations

import ast
import json
import re
from pathlib import Path

import pytest
from jinja2 import Environment, TemplateSyntaxError, nodes
from markupsafe import Markup

REPO_ROOT = Path(__file__).resolve().parents[2]
SRC = REPO_ROOT / "src" / "local_deep_research"
TEMPLATES = SRC / "web" / "templates"
STATIC_JS = SRC / "web" / "static" / "js"

# Attack payloads live in named constants (never inline) so the repo's
# payload hooks and reviewers can see them at a glance.
SCRIPT_CLOSE = "</" + "script>"
BREAKOUT_HTML = SCRIPT_CLOSE + "<svg onload=alert(1)>"
ATTR_BREAKOUT = '" onfocus=alert(1) x="'
QUERY_PARAM_PAYLOAD = '"><img src=x onerror=alert(1)>'


def _py_sources(root: Path):
    """Yield ``(relative_path, source_text)`` for every Python file."""
    for path in sorted(root.rglob("*.py")):
        yield (
            path.relative_to(SRC).as_posix(),
            path.read_text(encoding="utf-8"),
        )


def _templates():
    """Yield ``(relative_path, template_text)`` for every template."""
    for path in sorted(TEMPLATES.rglob("*.html")):
        yield (
            path.relative_to(TEMPLATES).as_posix(),
            path.read_text(encoding="utf-8"),
        )


# ---------------------------------------------------------------------
# Scanner 1: SQL built as a string
# ---------------------------------------------------------------------

_EXEC_METHODS = {
    "execute",
    "executemany",
    "executescript",
    "exec_driver_sql",
}
# ``sa.text`` / ``sqlalchemy.text`` are SQL. ``ax.text`` / ``plt.text``
# are matplotlib and must not be confused for it.
_TEXT_OWNERS = {"sa", "sqlalchemy"}


def _is_static_str(node: ast.expr) -> bool:
    """True only for a string with no runtime-interpolated part."""
    if isinstance(node, ast.Constant):
        return isinstance(node.value, str)
    if isinstance(node, ast.JoinedStr):
        return all(isinstance(v, ast.Constant) for v in node.values)
    if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
        return _is_static_str(node.left) and _is_static_str(node.right)
    return False


def _is_sql_text_call(node: ast.expr) -> bool:
    """True for ``text(...)`` / ``sa.text(...)`` — not ``ax.text(...)``."""
    if not isinstance(node, ast.Call):
        return False
    func = node.func
    if isinstance(func, ast.Name):
        return func.id == "text"
    if isinstance(func, ast.Attribute) and func.attr == "text":
        return (
            isinstance(func.value, ast.Name) and func.value.id in _TEXT_OWNERS
        )
    return False


def _builds_string(node: ast.expr) -> bool:
    """True if the expression concatenates/formats a SQL string."""
    if isinstance(node, ast.JoinedStr):
        return not _is_static_str(node)
    if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod):
        return isinstance(node.left, (ast.Constant, ast.JoinedStr))
    if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Add):
        halves = _is_static_str(node.left) or _is_static_str(node.right)
        return halves and not _is_static_str(node)
    if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
        return node.func.attr in ("format", "join")
    return False


def find_dynamic_raw_sql(source: str) -> list[tuple[int, str]]:
    """Find SQL *strings* assembled at runtime.

    Returns ``(lineno, reason)`` for every ``text()`` whose argument is
    not a literal, and every ``.execute*()`` handed a string the code
    built rather than a literal or an ORM expression.
    """
    tree = ast.parse(source)
    found: dict[int, str] = {}
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call) or not node.args:
            continue
        first = node.args[0]
        if _is_sql_text_call(node) and not _is_static_str(first):
            found.setdefault(node.lineno, "text() over a built string")
            continue
        func = node.func
        if not isinstance(func, ast.Attribute):
            continue
        if func.attr not in _EXEC_METHODS:
            continue
        if _builds_string(first):
            found.setdefault(node.lineno, f"{func.attr}() over an f-string")
        elif _is_sql_text_call(first) and first.args:
            if not _is_static_str(first.args[0]):
                found.setdefault(
                    node.lineno, f"{func.attr}(text(built string))"
                )
    return sorted(found.items())


# Every module that assembles a SQL string, with the reason it is not a
# request-reachable injection. Reviewed by reading each site; a new
# entry means a new raw-SQL site that nobody has read yet.
REVIEWED_RAW_SQL_MODULES = {
    # DROP of ``_alembic_tmp_*`` names read back from the DB catalog
    "database/alembic_runner.py",
    # ATTACH path (quote-doubled, charset-checked) + integer PRAGMAs
    "database/backup/backup_service.py",
    # PRAGMA key/rekey from a hex digest; PRAGMAs from allow-listed
    # settings. PRAGMA takes no bind parameters.
    "database/sqlcipher_utils.py",
    # check_database_integrity picks one of two literal PRAGMAs
    # (quick_check / integrity_check) on has_encryption, and
    # _make_sqlcipher_connection sets busy_timeout via
    # sqlcipher_busy_timeout_pragma() (an int() of the range-checked
    # db_config.busy_timeout_ms env setting), and the gated WAL
    # checkpoints via _gated_checkpoint_busy_timeout_pragma() (an int()
    # of the lower of that setting and a 1000 ms constant); no request
    # value reaches any of these strings. PRAGMA takes no bind parameters.
    "database/encrypted_db.py",
    # PRAGMA user_version from a module-level int constant
    "journal_quality/db.py",
    # Migrations: table names are literals inside the migration itself.
    "database/migrations/versions/0008_fix_research_strategy_fk.py",
    "database/migrations/versions/0010_add_chat_tables.py",
    "database/migrations/versions/0013_remove_meta_search_engines.py",
    "database/migrations/versions/0021_add_note_tables.py",
    "database/migrations/versions/0025_split_context_warning_dismissals.py",
    # The refresh_interval_minutes backfill interpolates only column names
    # from its literal tuple ("refresh_interval_hours", "refresh_hours"),
    # kept when the inspector lists them: the catalog only filters, and no
    # name read from it reaches the SQL. Sentinels and setting types bind.
    "database/migrations/versions/0032_repair_pre_alembic_databases.py",
}

# Layers that serve HTTP requests. Raw SQL here would sit one variable
# away from a request value, so the bar is zero sites, not a review.
REQUEST_FACING_PACKAGES = (
    "web",
    "news",
    "chat",
    "research_library",
    "metrics",
    "settings",
    "api",
)


def test_raw_sql_scanner_flags_an_interpolated_query():
    unsafe = (
        "def q(conn, name):\n"
        "    return conn.execute(f'SELECT * FROM t WHERE n = {name}')\n"
    )
    hits = find_dynamic_raw_sql(unsafe)
    assert [reason for _, reason in hits] == ["execute() over an f-string"], (
        hits
    )


def test_raw_sql_scanner_flags_text_over_a_built_string():
    unsafe = (
        "from sqlalchemy import text\n"
        "def q(conn, col):\n"
        "    conn.execute(text('SELECT ' + col + ' FROM t'))\n"
    )
    assert find_dynamic_raw_sql(unsafe), "built text() must be flagged"


def test_raw_sql_scanner_is_quiet_on_safe_sql():
    safe = (
        "from sqlalchemy import text\n"
        "def q(conn, name):\n"
        "    conn.execute(\n"
        "        text('SELECT * FROM t WHERE n = :n'), {'n': name}\n"
        "    )\n"
        "    conn.execute(select(Thing).where(Thing.name == name))\n"
        "    ax.text(0, 0, f'label {name}')\n"
    )
    assert find_dynamic_raw_sql(safe) == []


def test_every_raw_sql_module_has_been_reviewed():
    """No module may assemble SQL strings without a written verdict."""
    seen = {rel for rel, src in _py_sources(SRC) if find_dynamic_raw_sql(src)}
    assert seen, "scanner found no raw SQL at all — it is broken"
    assert seen - REVIEWED_RAW_SQL_MODULES == set(), (
        "new raw-SQL site(s) with no injection verdict: "
        f"{sorted(seen - REVIEWED_RAW_SQL_MODULES)}"
    )
    assert REVIEWED_RAW_SQL_MODULES - seen == set(), (
        "stale allow-list entries (raw SQL gone): "
        f"{sorted(REVIEWED_RAW_SQL_MODULES - seen)}"
    )


@pytest.mark.parametrize("package", REQUEST_FACING_PACKAGES)
def test_request_facing_layers_contain_no_raw_sql(package):
    """Routers/services must reach the DB only through the ORM."""
    root = SRC / package
    if not root.is_dir():
        pytest.skip(f"package {package} not present")
    offenders = [
        f"{rel}:{line} {why}"
        for rel, src in _py_sources(root)
        for line, why in find_dynamic_raw_sql(src)
    ]
    assert offenders == []


# ---------------------------------------------------------------------
# Scanner 2: LIKE / ILIKE wildcard escaping
# ---------------------------------------------------------------------

_LIKE_METHODS = {"like", "ilike", "not_like", "notlike"}


def find_unescaped_like(source: str) -> list[tuple[int, str]]:
    """Find ``.like()``/``.ilike()`` built from a value, missing escape.

    A pattern such as ``f"%{needle}%"`` lets a caller-supplied ``%`` or
    ``_`` widen the match unless the call passes ``escape=``.
    """
    tree = ast.parse(source)
    out = []
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call):
            continue
        func = node.func
        if not isinstance(func, ast.Attribute):
            continue
        if func.attr not in _LIKE_METHODS or not node.args:
            continue
        if _is_static_str(node.args[0]):
            continue
        if any(kw.arg == "escape" for kw in node.keywords):
            continue
        out.append((node.lineno, func.attr))
    return out


def test_like_scanner_flags_a_pattern_without_escape():
    unsafe = "q.filter(Doc.title.ilike(f'%{needle}%'))\n"
    assert find_unescaped_like(unsafe) == [(1, "ilike")]


def test_like_scanner_is_quiet_when_escape_is_passed():
    safe = (
        "q.filter(Doc.title.ilike(f'%{escape_like(needle)}%', "
        "escape='\\\\'))\n"
        "q.filter(Doc.url.like('%arxiv.org%'))\n"
    )
    assert find_unescaped_like(safe) == []


def test_every_dynamic_like_pattern_escapes_wildcards():
    offenders = [
        f"{rel}:{line} .{method}()"
        for rel, src in _py_sources(SRC)
        for line, method in find_unescaped_like(src)
    ]
    assert offenders == []


def test_like_scanner_actually_sees_the_real_like_calls():
    """Guard against the scanner matching nothing in the real tree."""
    total = 0
    for _rel, src in _py_sources(SRC):
        tree = ast.parse(src)
        for node in ast.walk(tree):
            if (
                isinstance(node, ast.Call)
                and isinstance(node.func, ast.Attribute)
                and node.func.attr in _LIKE_METHODS
                and node.args
                and not _is_static_str(node.args[0])
            ):
                total += 1
    assert total >= 10, (
        "expected many value-built LIKE patterns to audit, "
        f"found {total} — the walk is not reaching the source"
    )


# ---------------------------------------------------------------------
# Scanner 3: dynamic column lookup — getattr(Model, <value>)
# ---------------------------------------------------------------------


def find_unguarded_model_getattr(source: str) -> list[tuple[int, str]]:
    """Find ``getattr(SomeModel, name)`` with no allow-list guard.

    ``getattr(Source, sort)`` turns a request string into a column. It
    is safe only while an ``in``/``not in`` membership test on the same
    name runs somewhere in the enclosing function.
    """
    tree = ast.parse(source)
    out = []
    for func in ast.walk(tree):
        if not isinstance(func, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        guarded = set()
        for node in ast.walk(func):
            if not isinstance(node, ast.Compare):
                continue
            if not isinstance(node.left, ast.Name):
                continue
            if any(isinstance(op, (ast.In, ast.NotIn)) for op in node.ops):
                guarded.add(node.left.id)
        for node in ast.walk(func):
            if not isinstance(node, ast.Call):
                continue
            if not isinstance(node.func, ast.Name):
                continue
            if node.func.id != "getattr" or len(node.args) < 2:
                continue
            owner, attr = node.args[0], node.args[1]
            if not isinstance(owner, ast.Name):
                continue
            if not owner.id[:1].isupper():
                continue
            if not isinstance(attr, ast.Name):
                continue
            if attr.id in guarded:
                continue
            out.append((node.lineno, f"getattr({owner.id}, {attr.id})"))
    return out


def test_getattr_scanner_flags_an_unguarded_column_lookup():
    unsafe = (
        "def page(sort):\n    col = getattr(Source, sort)\n    return col\n"
    )
    assert find_unguarded_model_getattr(unsafe) == [
        (2, "getattr(Source, sort)")
    ]


def test_getattr_scanner_is_quiet_when_the_name_is_allow_listed():
    safe = (
        "def page(sort):\n"
        "    if sort not in _SORT_COLUMNS:\n"
        "        sort = 'quality'\n"
        "    return getattr(Source, sort)\n"
    )
    assert find_unguarded_model_getattr(safe) == []


def test_no_unguarded_dynamic_column_lookup():
    offenders = [
        f"{rel}:{line} {what}"
        for rel, src in _py_sources(SRC)
        for line, what in find_unguarded_model_getattr(src)
    ]
    assert offenders == []


def test_journal_quality_sort_allow_list_names_real_columns():
    """The one guarded ``getattr`` must resolve to mapped columns only.

    A membership guard is worth nothing if the allow-list itself can
    name ``metadata`` or a relationship.
    """
    from local_deep_research.journal_quality.db import (
        _SORT_COLUMNS,
        Source,
    )

    assert _SORT_COLUMNS, "sort allow-list must not be empty"
    for name in _SORT_COLUMNS:
        column = getattr(Source, name, None)
        assert column is not None, f"{name} is not an attribute of Source"
        assert hasattr(column, "asc") and hasattr(column, "desc"), (
            f"{name} is not orderable — it is not a column"
        )


# ---------------------------------------------------------------------
# Scanner 4: Content-Disposition filenames (path traversal / header
# injection on the download side)
# ---------------------------------------------------------------------

_SANITIZERS = {
    "quote",
    "_url_quote",
    "urlencode",
    "sub",
    "sanitize_filename",
    "secure_filename",
}


def _sanitized(node: ast.expr, bindings: dict, depth: int = 0) -> bool:
    """True if ``node`` provably derives from an approved sanitizer."""
    if depth > 4:
        return False
    if isinstance(node, ast.Constant):
        return True
    if isinstance(node, ast.Call):
        func = node.func
        name = (
            func.id
            if isinstance(func, ast.Name)
            else func.attr
            if isinstance(func, ast.Attribute)
            else None
        )
        if name in _SANITIZERS:
            return True
        # Removing surrounding whitespace cannot introduce header syntax,
        # but stripping an unsanitized value does not make it safe.
        if (
            isinstance(func, ast.Attribute)
            and func.attr == "strip"
            and not node.args
            and not node.keywords
        ):
            return _sanitized(func.value, bindings, depth + 1)
        return False
    if isinstance(node, ast.Name):
        bound = bindings.get(node.id)
        if bound is None:
            return False
        return _sanitized(bound, bindings, depth + 1)
    if isinstance(node, ast.JoinedStr):
        return all(
            _sanitized(v.value, bindings, depth + 1)
            for v in node.values
            if isinstance(v, ast.FormattedValue)
        )
    if isinstance(node, ast.BoolOp):
        return all(_sanitized(v, bindings, depth + 1) for v in node.values)
    if isinstance(node, ast.IfExp):
        return _sanitized(node.body, bindings, depth + 1) and _sanitized(
            node.orelse, bindings, depth + 1
        )
    return False


def _local_bindings(scope: ast.AST) -> dict:
    """Map ``name -> first assigned expression`` within one scope."""
    bindings: dict[str, ast.expr] = {}
    for node in ast.walk(scope):
        if isinstance(node, ast.Assign) and len(node.targets) == 1:
            target = node.targets[0]
            if isinstance(target, ast.Name):
                bindings.setdefault(target.id, node.value)
    return bindings


def _disposition_hits(scope: ast.AST, bindings: dict, skip: set):
    """Yield unsanitized interpolations in filename-bearing f-strings."""
    for node in ast.walk(scope):
        if not isinstance(node, ast.JoinedStr) or id(node) in skip:
            continue
        literal = "".join(
            v.value
            for v in node.values
            if isinstance(v, ast.Constant) and isinstance(v.value, str)
        )
        if "filename" not in literal.lower():
            continue
        for part in node.values:
            if not isinstance(part, ast.FormattedValue):
                continue
            if not _sanitized(part.value, bindings):
                yield (node.lineno, ast.unparse(part.value)[:60])


def find_unsanitized_disposition(source: str) -> list[tuple[int, str]]:
    """Find ``Content-Disposition`` values holding a raw filename.

    A quote or CRLF in a raw filename ends the header value; a ``/`` or
    ``..`` in a suggested download name is a traversal hint to the
    client. Every interpolation must trace back to a sanitizer, chased
    through up to four assignments inside the enclosing function.
    """
    tree = ast.parse(source)
    functions = [
        node
        for node in ast.walk(tree)
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
    ]
    in_function = {
        id(node)
        for func in functions
        for node in ast.walk(func)
        if isinstance(node, ast.JoinedStr)
    }
    found: dict[tuple[int, str], None] = {}
    for func in functions:
        for hit in _disposition_hits(func, _local_bindings(func), set()):
            found.setdefault(hit, None)
    module_bindings = _local_bindings(tree)
    for hit in _disposition_hits(tree, module_bindings, in_function):
        found.setdefault(hit, None)
    return sorted(found)


def test_disposition_scanner_flags_a_raw_filename():
    unsafe = (
        "def dl(document):\n"
        "    name = document.filename\n"
        "    return Response(headers={\n"
        "        'Content-Disposition': f'attachment; filename=\"{name}\"'\n"
        "    })\n"
    )
    hits = find_unsanitized_disposition(unsafe)
    assert [expr for _, expr in hits] == ["name"], hits


def test_disposition_scanner_follows_a_sanitizer_through_two_hops():
    safe = (
        "def dl(research_id):\n"
        "    safe_id = re.sub(r'[^a-zA-Z0-9_-]', '', research_id)\n"
        "    filename = f'logs_{safe_id}.jsonl'\n"
        "    return Response(headers={\n"
        "        'Content-Disposition': "
        "f'attachment; filename=\"{filename}\"'\n"
        "    })\n"
    )
    assert find_unsanitized_disposition(safe) == []


@pytest.mark.parametrize(
    "disposition,filename,offenders",
    [
        (
            '"inline" if inline else "attachment"',
            're.sub(r"[^a-zA-Z0-9._() -]", "_", raw).strip()',
            [],
        ),
        ('raw if inline else "attachment"', "quote(raw)", ["disposition"]),
        ('"inline" if inline else raw', "quote(raw)", ["disposition"]),
        ('"attachment"', "raw.strip()", ["fallback"]),
        ('"attachment"', "quote(raw).strip(raw)", ["fallback"]),
        ('"attachment"', 'quote(raw).replace("safe", raw)', ["fallback"]),
    ],
)
def test_disposition_scanner_traces_choices_and_trimmed_names(
    disposition, filename, offenders
):
    source = (
        "def header(raw, inline):\n"
        f"    disposition = {disposition}\n"
        f"    fallback = {filename}\n"
        "    return f'{disposition}; filename=\"{fallback}\"'\n"
    )
    assert [
        expr for _, expr in find_unsanitized_disposition(source)
    ] == offenders


def test_every_download_filename_is_sanitized():
    offenders = [
        f"{rel}:{line} {expr}"
        for rel, src in _py_sources(SRC / "web")
        for line, expr in find_unsanitized_disposition(src)
    ]
    assert offenders == []


def test_disposition_scanner_sees_the_real_download_headers():
    """Pin both header builders and the shared helper's real route callers."""
    builders = set()
    shared_callers = set()
    for rel, src in _py_sources(SRC / "web"):
        tree = ast.parse(src)
        for node in ast.walk(tree):
            if isinstance(node, ast.JoinedStr):
                literal = "".join(
                    v.value
                    for v in node.values
                    if isinstance(v, ast.Constant) and isinstance(v.value, str)
                )
                if "filename" in literal.lower():
                    builders.add(rel)
            if isinstance(node, ast.Dict):
                for key, value in zip(node.keys, node.values):
                    if (
                        isinstance(key, ast.Constant)
                        and key.value == "Content-Disposition"
                        and isinstance(value, ast.Call)
                        and isinstance(value.func, ast.Name)
                        and value.func.id == "build_content_disposition"
                    ):
                        shared_callers.add(rel)
    assert {
        "web/routers/research.py",
        "web/utils/download_headers.py",
    } <= builders, builders
    assert {
        "web/routers/library.py",
        "web/routers/research.py",
    } <= shared_callers, shared_callers


def test_open_file_location_endpoints_accept_no_path():
    """Both ``/open_file_location`` handlers are hard-disabled.

    They are the only endpoints wired to
    ``PathValidator.validate_local_filesystem_path``, which by design
    reaches anywhere on the host outside a small deny-list. In the port
    they return 403 without reading a body, so no request-supplied path
    reaches the filesystem.
    """
    routers = SRC / "web" / "routers"
    handlers = []
    for path in sorted(routers.glob("*.py")):
        tree = ast.parse(path.read_text(encoding="utf-8"))
        for node in ast.walk(tree):
            if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                continue
            if node.name != "open_file_location":
                continue
            handlers.append((path.name, node))
    assert len(handlers) == 2, (
        f"expected the two known handlers, found {handlers}"
    )
    for filename, node in handlers:
        body = ast.unparse(node)
        assert "403" in body, f"{filename}: handler must refuse"
        for reader in ("request.json", "request.form", "await request"):
            assert reader not in body, (
                f"{filename}: handler now reads request input ({reader})"
            )
        assert "validate_local_filesystem_path" not in body


# ---------------------------------------------------------------------
# Template output safety: the ``|safe`` / autoescape-off census
# ---------------------------------------------------------------------

_SAFE_FILTER_RE = re.compile(r"\|\s*safe\b")
_AUTOESCAPE_OFF_RE = re.compile(
    r"\{%-?\s*autoescape\s+(?:false|False|0)\s*-?%\}"
)


def find_escape_exceptions(text: str) -> list[tuple[int, str]]:
    """Find every place a template opts out of autoescaping."""
    out = []
    for number, line in enumerate(text.splitlines(), start=1):
        if _SAFE_FILTER_RE.search(line):
            out.append((number, "|safe"))
        if _AUTOESCAPE_OFF_RE.search(line):
            out.append((number, "autoescape false"))
    return out


def test_escape_exception_scanner_flags_both_forms():
    unsafe = (
        "<p>{{ body|safe }}</p>\n"
        "{% autoescape false %}{{ body }}{% endautoescape %}\n"
    )
    assert find_escape_exceptions(unsafe) == [
        (1, "|safe"),
        (2, "autoescape false"),
    ]


def test_escape_exception_scanner_is_quiet_on_escaped_output():
    safe = "<p>{{ body }}</p>\n<p>{{ body|e }}</p>\n{{ x|safeish }}\n"
    assert find_escape_exceptions(safe) == []


# Every autoescape opt-out in web/templates, with the verdict on
# whether user content can reach it. See the dedicated tests below.
ESCAPE_EXCEPTION_CENSUS = {
    # Callers pass string literals only; enforced below.
    ("components/help_macros.html", "|safe"): 1,
}


def test_autoescape_opt_outs_are_exactly_the_reviewed_set():
    census: dict[tuple[str, str], int] = {}
    for rel, text in _templates():
        for _line, kind in find_escape_exceptions(text):
            census[(rel, kind)] = census.get((rel, kind), 0) + 1
    assert census == ESCAPE_EXCEPTION_CENSUS, (
        "the set of autoescape opt-outs changed; each new one needs a "
        "verdict on whether user content reaches it"
    )


# ---------------------------------------------------------------------
# The theme-JSON globals embedded in base.html
# ---------------------------------------------------------------------


def test_flask_theme_helper_was_replaced_not_dropped():
    """``web/utils/theme_helper.py`` is gone; the globals survive.

    On main a thin Flask wrapper registered three Jinja globals. The
    port deleted the wrapper, so the question is whether the ``|safe``
    sites in ``base.html`` still resolve — an unbound global would make
    the page render ``undefined`` rather than fail loudly.
    """
    assert not (SRC / "web" / "utils" / "theme_helper.py").exists()

    app_src = (SRC / "web" / "fastapi_app.py").read_text(encoding="utf-8")
    tree = ast.parse(app_src)
    registered = set()
    for node in ast.walk(tree):
        if not isinstance(node, ast.Assign) or len(node.targets) != 1:
            continue
        target = node.targets[0]
        if not isinstance(target, ast.Subscript):
            continue
        value = target.value
        if not (isinstance(value, ast.Attribute) and value.attr == "globals"):
            continue
        key = target.slice
        if isinstance(key, ast.Constant) and isinstance(key.value, str):
            registered.add(key.value)
    for name in ("get_themes_json", "get_theme_metadata"):
        assert name in registered, (
            f"base.html renders {name}() but the port does "
            "not register it as a template global"
        )


def test_theme_json_globals_return_markup_from_package_files_only():
    """The ``|safe`` payload is package data, not user data."""
    from local_deep_research.web import themes as themes_module

    assert isinstance(themes_module.get_themes_json(), Markup)
    assert isinstance(themes_module.get_theme_metadata(), Markup)

    # The only source of theme metadata is the package directory.
    package_dir = Path(themes_module.__file__).resolve().parent
    assert themes_module.THEMES_DIR.resolve() == package_dir
    loader_src = (SRC / "web" / "themes" / "loader.py").read_text(
        encoding="utf-8"
    )
    tree = ast.parse(loader_src)
    reads = {
        node.func.attr
        for node in ast.walk(tree)
        if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute)
    }
    assert "read_text" in reads
    # Every filesystem walk is rooted at self.themes_dir, which the
    # registry sets to THEMES_DIR. No request value can widen it.
    bindings = _local_bindings(tree)
    roots = [
        node.func.value
        for node in ast.walk(tree)
        if isinstance(node, ast.Call)
        and isinstance(node.func, ast.Attribute)
        and node.func.attr in ("glob", "rglob")
    ]
    assert roots, "theme loader no longer globs for theme files"
    for root in roots:
        source_expr = ast.unparse(root)
        if isinstance(root, ast.Name) and root.id in bindings:
            source_expr = ast.unparse(bindings[root.id])
        assert "themes_dir" in source_expr, (
            f"theme discovery walks {source_expr}, not themes_dir"
        )


def test_shipped_theme_json_cannot_break_out_of_the_script_block():
    """Shipped theme metadata is JSON with no HTML-significant characters."""
    from local_deep_research.web import themes as themes_module

    for payload in (
        str(themes_module.get_themes_json()),
        str(themes_module.get_theme_metadata()),
    ):
        assert json.loads(payload) is not None
        for char in ("<", ">", "&"):
            assert char not in payload, (
                f"theme JSON contains unescaped {char!r}"
            )


@pytest.mark.parametrize("payload", [BREAKOUT_HTML, "<>&'\" café", "</ScRiPt>"])
def test_theme_json_escapes_metadata_before_inline_render(monkeypatch, payload):
    """Real helpers and the base template preserve data without HTML breakout."""
    from local_deep_research.defaults import get_default_theme
    from local_deep_research.web import themes as themes_module

    theme = themes_module.ThemeMetadata(
        id=payload, label=payload, icon="fa-star", group="other"
    )
    monkeypatch.setattr(
        themes_module.theme_registry._loader,
        "load_all_themes",
        lambda: {payload: theme},
    )
    ids = themes_module.get_themes_json()
    metadata = themes_module.get_theme_metadata()
    assert json.loads(ids) == [payload]
    assert json.loads(metadata) == {payload: theme.to_dict()}
    assert isinstance(ids, Markup) and isinstance(metadata, Markup)
    for value in (ids, metadata):
        assert all(char not in value for char in "<>&'")

    source = (TEMPLATES / "base.html").read_text(encoding="utf-8")
    script = next(
        match.group()
        for match in re.finditer(
            r"<script\b([^>]*)>.*?</script>", source, re.DOTALL
        )
        if not re.search(r"(?:^|\s)src\s*=", match.group(1))
    )
    rendered = (
        Environment(autoescape=True)
        .from_string(script)
        .render(
            get_themes_json=themes_module.get_themes_json,
            get_theme_metadata=themes_module.get_theme_metadata,
            default_theme=get_default_theme(),
            session={"username": "alice"},
        )
    )
    assert rendered.lower().count("</script>") == 1
    assert "&lt;" not in rendered  # JSON must not become HTML entities.
    assert str(ids) in rendered and str(metadata) in rendered


# ---------------------------------------------------------------------
# help_macros.html — ``{{ text|safe }}``
# ---------------------------------------------------------------------

_HELP_TIP_RE = re.compile(r"help_tip\s*\(")


def find_dynamic_help_tip_calls(text: str) -> list[tuple[int, str]]:
    """Find ``help_tip(...)`` whose first argument is not a literal.

    The macro renders its argument with ``|safe``, so a variable there
    is stored XSS the moment the variable holds user content.
    """
    out = []
    for number, line in enumerate(text.splitlines(), start=1):
        for match in _HELP_TIP_RE.finditer(line):
            rest = line[match.end() :].lstrip()
            if rest[:1] not in ('"', "'"):
                out.append((number, rest[:50]))
    return out


def test_help_tip_scanner_flags_a_variable_argument():
    unsafe = "{{ help_tip(collection.description) }}\n"
    assert find_dynamic_help_tip_calls(unsafe) == [
        (1, "collection.description) }}")
    ]


def test_help_tip_scanner_is_quiet_on_literal_arguments():
    safe = (
        '{{ help_tip("<strong>Tip:</strong> use defaults.") }}\n'
        "{{ help_tip('single quoted is fine too') }}\n"
    )
    assert find_dynamic_help_tip_calls(safe) == []


def test_help_tip_is_only_ever_called_with_a_literal():
    """The ``|safe`` in help_macros is safe only by call-site discipline."""
    offenders = []
    call_sites = 0
    for rel, text in _templates():
        if rel == "components/help_macros.html":
            continue
        call_sites += len(_HELP_TIP_RE.findall(text))
        offenders += [
            f"{rel}:{line} {snippet}"
            for line, snippet in find_dynamic_help_tip_calls(text)
        ]
    assert call_sites >= 10, (
        f"expected the known help_tip call sites, found {call_sites}"
    )
    assert offenders == []


# ---------------------------------------------------------------------
# JSON in HTML: |tojson in a script body vs. in an attribute
# ---------------------------------------------------------------------


def test_tojson_in_a_script_body_neutralises_a_script_close():
    """Payload must ARRIVE and arrive encoded — both halves asserted."""
    env = Environment(autoescape=True)  # autoescape on, as the app
    template = env.from_string(
        "<script>var c = {{ collections|tojson }};</script>"
    )
    rendered = template.render(collections=[{"name": BREAKOUT_HTML}])
    assert "onload=alert(1)" in rendered, "payload never reached render"
    assert BREAKOUT_HTML not in rendered
    assert "\\u003c/script\\u003e" in rendered


def test_plain_interpolation_in_a_script_body_is_html_escaped():
    """The bare ``{{ x }}`` inside a <script> string literal is safe.

    Script content is raw text: browsers do not entity-decode it, so
    the ``&#39;`` autoescape produces cannot close the JS string, and
    ``&lt;/script&gt;`` cannot close the element.
    """
    env = Environment(autoescape=True)  # autoescape on, as the app
    template = env.from_string("<script>console.log('{{ engine }}');</script>")
    rendered = template.render(engine=BREAKOUT_HTML)
    # Look only at the JS string the payload landed in, so the
    # template's own closing tag cannot satisfy the assertion.
    body = rendered[rendered.index("console.log(") : rendered.index("');")]
    assert "&lt;/script&gt;" in body, "payload never reached render"
    assert SCRIPT_CLOSE not in body
    assert "<svg" not in body


_TOJSON_ATTR_RE = re.compile(
    r'=\s*"\{\{(?P<expression>[^}]*\|\s*tojson[^}]*)\}\}"'
)


def _expression_is_uri_encoded(expression: str) -> bool:
    """Require encoding to apply to the whole attribute expression."""
    try:
        output = (
            Environment(autoescape=True).parse("{{" + expression + "}}").body[0]
        )
    except TemplateSyntaxError:
        return False
    if not isinstance(output, nodes.Output) or len(output.nodes) != 1:
        return False
    value = output.nodes[0]
    return (
        isinstance(value, nodes.Filter)
        and value.name == "urlencode"
        and not value.args
        and not value.kwargs
        and value.dyn_args is None
        and value.dyn_kwargs is None
    )


def find_tojson_in_attribute(text: str) -> list[int]:
    """Find ``|tojson`` used as a double-quoted attribute value.

    ``tojson`` escapes ``<``, ``>``, ``&`` and ``'`` but *not* ``"``,
    and its output is Markup so autoescape skips it. Require an outer
    URI encoder to protect the JSON delimiters in a double-quoted attribute.
    """
    return [
        number
        for number, line in enumerate(text.splitlines(), start=1)
        if any(
            not _expression_is_uri_encoded(match.group("expression"))
            for match in _TOJSON_ATTR_RE.finditer(line)
        )
    ]


def test_tojson_attribute_scanner_flags_an_attribute_use():
    unsafe = '<input value="{{ setting.value|tojson }}">\n'
    assert find_tojson_in_attribute(unsafe) == [1]


def test_tojson_attribute_scanner_is_quiet_on_script_use():
    safe = (
        "<script>var v = {{ setting.value|tojson }};</script>\n"
        '<input value="{{ setting.value }}">\n'
    )
    assert find_tojson_in_attribute(safe) == []


@pytest.mark.parametrize(
    "expression,expected",
    [
        ("value|tojson|urlencode", []),
        ("value|urlencode|tojson", [1]),
        ("(value|tojson|urlencode) ~ value", [1]),
        ("value|tojson if condition else value|tojson|urlencode", [1]),
    ],
)
def test_tojson_attribute_scanner_requires_terminal_encoding(
    expression, expected
):
    source = '<div data-args="{{ ' + expression + ' }}"></div>'
    assert find_tojson_in_attribute(source) == expected


def test_uri_encoded_json_round_trips_as_one_attribute():
    from html.parser import HTMLParser
    from urllib.parse import unquote

    class AttributeCollector(HTMLParser):
        def handle_starttag(self, tag, attrs):
            self.attributes = attrs

    value = ['"quoted" <tag> & \' π', {"label": "ordinary value"}]
    rendered = (
        Environment(autoescape=True)
        .from_string('<div data-args="{{ value|tojson|urlencode }}"></div>')
        .render(value=value)
    )
    parser = AttributeCollector()
    parser.feed(rendered)
    assert len(parser.attributes) == 1
    name, encoded = parser.attributes[0]
    assert name == "data-args"
    assert json.loads(unquote(encoded)) == value


def test_tojson_in_an_html_attribute_breaks_out_of_the_attribute():
    """Bare JSON does not preserve a double-quoted attribute boundary."""
    env = Environment(autoescape=True)  # autoescape on, as the app
    template = env.from_string('<input value="{{ v|tojson }}">')
    rendered = template.render(v={"k": ATTR_BREAKOUT})
    # The JSON's own quotes and the backslash-escaped inner quote land
    # raw, so the parser sees a fresh attribute after the value ends.
    assert "onfocus=alert(1)" in rendered
    assert '\\"' in rendered
    assert "&quot;" not in rendered


def test_no_template_puts_tojson_in_an_html_attribute():
    """Attribute JSON must be URI-encoded as a complete expression.

    The JSON's own quotes land raw inside the attribute, so the parser sees a
    fresh attribute after the value ends — see
    ``test_tojson_in_an_html_attribute_breaks_out_of_the_attribute``.
    URI-encoded event arguments preserve data without exposing JSON quotes
    to the HTML parser. Bare JSON and partially encoded expressions remain
    forbidden.
    """
    census = {
        rel: len(hits)
        for rel, text in _templates()
        if (hits := find_tojson_in_attribute(text))
    }
    assert census == {}, (
        "unencoded |tojson inside an HTML attribute; offending "
        f"templates (path: hit count): {census}. See "
        "test_tojson_in_an_html_attribute_breaks_out_of_the_attribute"
    )


def test_pagination_query_params_survive_the_port_url_encoded():
    """``request.args`` became ``request.query_params`` in library.html.

    Both return plain ``str``, so ``|urlencode`` still percent-encodes
    and autoescape still escapes the result inside the ``href``.
    """
    env = Environment(autoescape=True)  # autoescape on, as the app
    template = env.from_string(
        '<a href="?page=2&collection={{ value|urlencode }}">next</a>'
    )
    rendered = template.render(value=QUERY_PARAM_PAYLOAD)
    assert "%3Cimg" in rendered or "%3cimg" in rendered
    assert "<img" not in rendered
    assert '"><img' not in rendered


# ---------------------------------------------------------------------
# Notification templates: the other autoescape-off environment
# ---------------------------------------------------------------------


def test_notification_templates_are_plain_text_only():
    """``select_autoescape(["html", "xml"])`` leaves ``.jinja2`` raw.

    That is correct *while* the rendered message is plain text handed
    to Apprise: escaping would put ``&amp;`` in a chat message. It stops
    being correct the moment an HTML body format appears, so pin both
    ends — every template is ``.jinja2``, and nothing declares HTML.
    """
    template_dir = SRC / "notifications" / "templates"
    files = sorted(p.name for p in template_dir.iterdir() if p.is_file())
    assert files, "notification templates directory is empty"
    assert all(name.endswith(".jinja2") for name in files), files

    package_src = "\n".join(
        src for _rel, src in _py_sources(SRC / "notifications")
    )
    for html_marker in ("body_format", "NotifyFormat.HTML", "text/html"):
        assert html_marker not in package_src, (
            f"notifications now declare {html_marker}; the templates "
            "render unescaped and would become an HTML injection"
        )


# ---------------------------------------------------------------------
# JS sinks: keep the no-unsanitized fence up
# ---------------------------------------------------------------------

_DISABLE_RE = re.compile(
    r"eslint-disable(?:-next-line|-line)?\s+[^\n]*no-unsanitized"
)


def find_unjustified_disables(text: str) -> list[tuple[int, str]]:
    """Find ``no-unsanitized`` suppressions with no written reason."""
    out = []
    for number, line in enumerate(text.splitlines(), start=1):
        match = _DISABLE_RE.search(line)
        if not match:
            continue
        if "eslint-enable" in line:
            continue
        if "--" not in line[match.start() :]:
            out.append((number, line.strip()[:70]))
    return out


def test_disable_scanner_flags_a_bare_suppression():
    unsafe = (
        "// eslint-disable-next-line no-unsanitized/property\n"
        "el.innerHTML = `<b>${name}</b>`;\n"
    )
    assert find_unjustified_disables(unsafe) == [
        (1, "// eslint-disable-next-line no-unsanitized/property")
    ]


def test_disable_scanner_is_quiet_on_a_justified_suppression():
    safe = (
        "// eslint-disable-next-line no-unsanitized/property -- "
        "audited: all interpolations use escapeHtml\n"
        "el.innerHTML = `<b>${escapeHtml(name)}</b>`;\n"
    )
    assert find_unjustified_disables(safe) == []


def test_no_unsanitized_rules_are_enforced_as_errors():
    """The innerHTML fence must not have been downgraded by the port."""
    config = (REPO_ROOT / "eslint.config.js").read_text(encoding="utf-8")
    for rule in ("no-unsanitized/property", "no-unsanitized/method"):
        pattern = re.compile(re.escape(f'"{rule}": ') + r"\[?\s*\"error\"")
        assert pattern.search(config), (
            f"{rule} is no longer configured as an error"
        )


def test_every_innerhtml_suppression_carries_an_audit_note():
    offenders = []
    suppressions = 0
    for path in sorted(STATIC_JS.rglob("*.js")):
        text = path.read_text(encoding="utf-8", errors="replace")
        suppressions += len(_DISABLE_RE.findall(text))
        offenders += [
            f"{path.relative_to(STATIC_JS).as_posix()}:{line} {snippet}"
            for line, snippet in find_unjustified_disables(text)
        ]
    assert suppressions >= 20, (
        "expected the known audited innerHTML sites, found "
        f"{suppressions} — the walk is not reaching the JS tree"
    )
    assert offenders == []