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.

9.3 KB
Raw Blame
"""Server IDs rendered into page markup must never reach an inline handler.

Jinja autoescape turns ``'`` into ``'``. That is enough for an ordinary
attribute value, but not inside an ``on*`` event handler: the browser
entity-decodes attribute text BEFORE it compiles the handler, so the escaping
is undone and a quote in the ID closes the handler's string literal. CSP does
not stop it either, because ``script-src`` includes ``'unsafe-inline'``.

Each test renders a real template through its route handler with an ID built
to break out of a quoted JavaScript string (and to test the attribute
escaping with ``"``, ``&`` and ``<``). It then reads the attributes the way
the browser does (BeautifulSoup decodes entities like the HTML parser) and
checks two things:

* no ``on*`` attribute anywhere on the page contains the ID, and
* the ID survives intact as data on the element the page script reads,
  beside the ``data-*-action`` names that script dispatches on.

The click path through those listeners is exercised by vitest:
tests/js/pages/library-inline-runtime.test.js,
tests/js/pages/download-manager-inline-runtime.test.js and
tests/js/collection-upload-direct-runtime.test.js.
"""

import uuid
from contextlib import contextmanager
from unittest.mock import Mock, patch

from bs4 import BeautifulSoup
from starlette.requests import Request

from local_deep_research.web.routers import library as library_module
from local_deep_research.web.routers.library import (
    download_manager_page,
    library_page,
)
from local_deep_research.web.routers.rag import collection_upload_page

#: Distinctive text that only appears in the ID, so a match in any handler
#: means the ID reached it.
MARKER = "__inlineHandlerIdXss"
#: Closes a single-quoted JS string, runs the marker, comments out the rest.
#: The leading ``"&<`` exercise the attribute escaping of the data attribute.
HOSTILE_ID = f"id\"&<');window.{MARKER}=true;//"


def _request(app, path):
    request = Request(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "headers": [],
            "query_string": b"",
            "session": {},
        }
    )
    request.scope.update(
        {
            "app": app,
            "router": app.router,
            "scheme": "http",
            "server": ("testserver", 80),
            "root_path": "",
        }
    )
    return request


@contextmanager
def _fake_db_session(*args, **kwargs):
    yield Mock()


def _settings_manager(values):
    manager = Mock()
    manager.get_setting.side_effect = lambda key, default=None: values.get(
        key, default
    )
    return manager


def _soup(response):
    assert response.status_code == 200
    return BeautifulSoup(response.body.decode("utf-8"), "html.parser")


def _handlers_containing(soup, needle):
    """Every ``on*`` attribute on the page whose decoded value has needle."""
    return [
        (tag.name, name, value)
        for tag in soup.find_all(True)
        for name, value in tag.attrs.items()
        if name.lower().startswith("on") and needle in str(value)
    ]


def _inline_handlers(tag):
    return [name for name in tag.attrs if name.lower().startswith("on")]


def _library_service(document):
    service = Mock()
    service.get_library_stats.return_value = {
        "total_pdfs": 1,
        "total_size_mb": 0.0,
        "storage_path": "/tmp/library",
    }
    service.count_documents.return_value = 1
    service.get_documents.return_value = [document]
    service.get_unique_domains.return_value = []
    service.get_research_list_for_dropdown.return_value = []
    service.get_all_collections.return_value = []
    return service


def _library_document(document_id):
    return {
        "id": document_id,
        "research_id": "research-1",
        "document_title": "Inline handler fixture",
        "authors": ["A. Author"],
        "doi": None,
        "file_name": "fixture.pdf",
        "file_size": 2048,
        "file_type": "pdf",
        "domain": "example.org",
        "domain_category": None,
        "downloaded_at": "2026-10-01T12:00:00",
        "research_title": "Fixture research",
        "is_arxiv": False,
        "is_pubmed": False,
        "has_pdf": True,
        "has_text_db": True,
        "has_rag_indexed": False,
        "rag_chunk_count": 0,
    }


def _render_library(app, document):
    with (
        patch.object(
            library_module,
            "get_user_db_session",
            side_effect=_fake_db_session,
        ),
        patch.object(
            library_module,
            "get_settings_manager",
            return_value=_settings_manager(
                {
                    "research_library.pdf_storage_mode": "database",
                    "research_library.shared_library": False,
                }
            ),
        ),
        patch.object(
            library_module,
            "LibraryService",
            return_value=_library_service(document),
        ),
        patch(
            "local_deep_research.database.library_init.get_default_library_id",
            return_value="col-default",
        ),
    ):
        return library_page(_request(app, "/library/"), username="alice")


def test_library_cards_keep_document_ids_out_of_inline_handlers(app):
    soup = _soup(_render_library(app, _library_document(HOSTILE_ID)))

    assert _handlers_containing(soup, MARKER) == []

    card = soup.select_one(".ldr-document-card")
    assert card is not None, "the fixture document did not render a card"
    assert card["data-doc-id"] == HOSTILE_ID

    body = card.select_one(".card-body")
    delete_blob = card.select_one(".ldr-btn-delete-blob")
    delete_doc = card.select_one(".ldr-btn-delete-doc")
    actions = [
        (body, "open"),
        (delete_blob, "delete-blob"),
        (delete_doc, "delete"),
    ]
    for element, action in actions:
        assert element is not None, f"no element for the {action!r} action"
        assert element.get("data-doc-action") == action
        assert _inline_handlers(element) == [], (action, element.attrs)


def _research_row(research_id):
    return {
        "id": research_id,
        "title": "Inline handler fixture research",
        "query": "inline handler fixture",
        "mode": "quick",
        "status": "completed",
        "created_at": "2026-10-01T12:00:00",
        "duration_seconds": 12,
        "total_resources": 3,
        "downloaded_count": 1,
        "downloadable_count": 2,
        "rating": None,
        "top_domains": [],
    }


def _render_download_manager(app, research):
    service = Mock()
    service.get_download_manager_summary_stats.return_value = {
        "total_researches": 1,
        "total_resources": 3,
        "already_downloaded": 1,
        "available_to_download": 1,
    }
    service.get_research_list_with_stats.return_value = [research]
    service.get_pdf_previews_batch.return_value = {}
    with (
        patch.object(
            library_module,
            "get_user_db_session",
            side_effect=_fake_db_session,
        ),
        patch.object(
            library_module,
            "get_settings_manager",
            return_value=_settings_manager(
                {
                    "research_library.pdf_storage_mode": "database",
                    "research_library.shared_library": False,
                }
            ),
        ),
        patch.object(library_module, "LibraryService", return_value=service),
    ):
        return download_manager_page(
            _request(app, "/library/download-manager"), username="alice"
        )


def test_download_manager_rows_keep_research_ids_out_of_inline_handlers(app):
    soup = _soup(_render_download_manager(app, _research_row(HOSTILE_ID)))

    assert _handlers_containing(soup, MARKER) == []

    row = soup.select_one(".ldr-research-item")
    assert row is not None, "the fixture research did not render a row"
    assert row["data-id"] == HOSTILE_ID

    buttons = row.select(".ldr-research-actions button")
    assert [button.get("data-research-action") for button in buttons] == [
        "view",
        "download-pdf",
        "download-text",
        "view-downloaded",
    ]
    for button in buttons:
        assert _inline_handlers(button) == [], button.attrs


def test_collection_upload_cancel_keeps_the_collection_id_out_of_inline_handlers(
    app,
):
    # The route only admits a well-formed UUID (tests/security/
    # test_collection_id_xss.py), so this page is checked with a real one.
    collection_id = str(uuid.uuid4())
    with (
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            side_effect=_fake_db_session,
        ),
        patch(
            "local_deep_research.utilities.db_utils.get_settings_manager",
            return_value=_settings_manager(
                {"research_library.upload_pdf_storage": "none"}
            ),
        ),
    ):
        response = collection_upload_page(
            _request(app, f"/library/collections/{collection_id}/upload"),
            collection_id=collection_id,
            username="alice",
        )
    soup = _soup(response)

    assert _handlers_containing(soup, collection_id) == []

    cancel = soup.select_one('[data-upload-action="cancel"]')
    assert cancel is not None, "the Cancel button lost its action name"
    assert cancel.get_text(" ", strip=True) == "Cancel"
    assert cancel["data-collection-id"] == collection_id
    assert _inline_handlers(cancel) == []