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.

4.8 KB
Raw Blame
"""Every publisher host the library promises to download from is still on the
allowlist.

Ported from ``tests/research_library/routes/test_library_routes.py``
(``TestIsDownloadableDomain``, ``TestMedRxivDomain``,
``TestSemanticScholarDomain``, ``TestAcademiaAndResearchGate``,
``TestEuropePMC``, ``TestSubdomainHandling``, ``TestAdditionalDomains``),
deleted in the Flask->FastAPI migration.

``tests/security/test_library_rag_security_fastapi.py::TestIsDownloadableDomainAllowlist``
is the successor for the MATCHING SEMANTICS -- case folding, dot-anchored
suffix matching, userinfo forgery, lookalike hosts, the PDF-shape
short-circuit, the pubmed substring special case -- and none of that is
re-tested here. But it exercises only five hosts (arxiv, nature, pubmed,
doi.org, openreview) out of the forty-odd in the list, so it is not a
successor for MEMBERSHIP: delete ``biorxiv.org``, ``semanticscholar.org`` or
``europepmc.org`` from ``downloadable_domains`` and it stays green while
every bioRxiv preprint in a user's research silently stops being offered for
download.

The list is data, and data rots differently from code: an entry removed in a
merge conflict, or a rename that drops a line, produces no error anywhere --
just a quietly smaller library. So the entries the deleted suite named are
pinned by name.

Every URL below has a path that is NOT PDF-shaped (no ``.pdf`` suffix, no
``/pdf/`` segment, no ``type=pdf``/``format=pdf`` query), so a True can only
come from the host allowlist. Otherwise the PDF short-circuit -- which runs
first and is host-independent -- would satisfy the whole file.
"""

import pytest

from local_deep_research.research_library.utils import is_downloadable_domain

#: Host -> a representative article URL on it, deliberately not PDF-shaped.
#: These are exactly the domains the deleted suite asserted by name.
NAMED_HOSTS = {
    "arxiv.org": "https://arxiv.org/abs/2301.00001",
    "biorxiv.org": "https://biorxiv.org/content/10.1101/2021.01.01",
    "medrxiv.org": "https://medrxiv.org/content/10.1101/2021.01.01",
    "ncbi.nlm.nih.gov": "https://ncbi.nlm.nih.gov/pmc/articles/PMC123",
    "europepmc.org": "https://europepmc.org/article/PMC/12345",
    "semanticscholar.org": "https://semanticscholar.org/paper/12345",
    "researchgate.net": "https://researchgate.net/publication/12345",
    "academia.edu": "https://academia.edu/12345/Paper_Title",
    "sciencedirect.com": (
        "https://sciencedirect.com/science/article/pii/S12345678"
    ),
    "springer.com": "https://springer.com/article/10.1007/s00123",
    "nature.com": "https://nature.com/articles/s41586-021-01234-5",
    "wiley.com": "https://wiley.com/doi/abs/10.1002/example",
    "ieee.org": "https://ieeexplore.ieee.org/document/12345",
    "acm.org": "https://dl.acm.org/doi/10.1145/12345",
    "plos.org": "https://plos.org/article/12345",
    "frontiersin.org": (
        "https://frontiersin.org/articles/10.3389/fimmu.2021.12345"
    ),
    "doi.org": "https://doi.org/10.1234/example",
    "ssrn.com": "https://papers.ssrn.com/sol3/12345",
    "openreview.net": "https://openreview.net/forum?id=abc123",
}


@pytest.mark.parametrize("url", sorted(NAMED_HOSTS.values()))
def test_a_named_publisher_host_is_downloadable(url):
    assert is_downloadable_domain(url) is True, (
        f"{url} is no longer recognised as downloadable -- an entry was "
        "dropped from research_library/utils/downloadable_domains, and "
        "every resource on that host silently stops being offered for "
        "download"
    )


@pytest.mark.parametrize("host", sorted(NAMED_HOSTS))
def test_the_www_form_of_each_named_host_is_downloadable(host):
    """Resource URLs arrive from search providers in whatever form the
    provider emitted, and ``www.`` is the commonest. It matches through the
    dot-anchored suffix rule, so this also proves the rule is still applied
    to every entry rather than only to the handful of hosts the security
    suite spot-checks."""
    assert is_downloadable_domain(f"https://www.{host}/article/1") is True, (
        f"www.{host} must match {host} through the dot-suffix rule"
    )


def test_a_publisher_lookalike_is_still_refused():
    """Non-vacuity control for the whole file: the assertions above must be
    the allowlist firing, not a function that returns True for everything.
    """
    for url in [
        "https://arxiv-preprints.test/abs/1",
        "https://nature.test/articles/1",
        "https://evil.test/article/1",
    ]:
        assert is_downloadable_domain(url) is False, url


def test_a_plain_consumer_site_is_refused():
    """The other half of the control, using the exact hosts the deleted
    suite named."""
    for url in [
        "https://google.com/search?q=test",
        "https://twitter.com/user",
        "https://youtube.com/watch?v=123",
    ]:
        assert is_downloadable_domain(url) is False, url