~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
"""RIS field-injection contracts for the reference export.
Report source titles and URLs derive from fetched web content. An RIS
file is line-structured — any newline smuggled into a field value could
inject further ``TY``/``ER`` records that a reference manager (Zotero,
EndNote) could import as real entries. Source extraction alone does not
exclude bare carriage returns or Unicode line separators. The exporter
flattens control characters at the RIS serialization boundary; these tests
pin both extraction and that final single-line field guarantee.
"""
from __future__ import annotations
import pytest
from local_deep_research.text_optimization.citation_formatter import (
RISExporter,
)
HOSTILE_SECTION_MARKER = "TY - JOUR"
def _export(sources_block: str) -> str:
content = f"# Report\n\nBody text.\n\n## Sources\n\n{sources_block}"
return RISExporter().export_to_ris(content)
class TestHostileTitlesCannotInjectFields:
def test_fake_field_text_in_a_title_stays_inside_the_ti_line(self):
ris = _export(
"[1] A study TI - injected is not a new field\n"
"URL: https://example.com/paper\n"
)
lines = ris.split("\n")
# Exactly one TY opener and one ER terminator for the one source.
assert sum(1 for line in lines if line == "TY - ELEC") == 1
assert sum(1 for line in lines if line == "ER - ") == 1
# The hostile text survives only as literal title content, on the
# single TI line — never as a line of its own.
hostile_lines = [
line for line in lines if line.strip() == "TI - injected"
]
assert hostile_lines == []
ti_lines = [line for line in lines if line.startswith("TI - ")]
assert len(ti_lines) == 1
assert "A study TI - injected" in ti_lines[0]
def test_multiline_hostile_source_never_emits_its_lines_verbatim(self):
ris = _export(
"[1] Real title of the paper\n"
f"{HOSTILE_SECTION_MARKER}\n"
"AU - Attacker, Injected\n"
"URL: https://example.com/paper\n"
)
lines = ris.split("\n")
# None of the injected raw lines may appear as structural output.
assert HOSTILE_SECTION_MARKER not in lines
assert "AU - Attacker, Injected" not in lines
assert sum(1 for line in lines if line == "TY - ELEC") == 1
def test_carriage_return_in_source_line_is_confined(self):
ris = _export(
"[1] Title with trailing carriage return\r\n"
"URL: https://example.com/paper\r\n"
)
lines = ris.split("\n")
assert "\r" not in ris
ti_lines = [line for line in lines if line.startswith("TI - ")]
assert len(ti_lines) == 1
assert ti_lines[0].strip() == (
"TI - Title with trailing carriage return"
)
class TestHostileUrlsCannotBreakOutOfTheUrField:
def test_url_with_fake_fields_stays_on_one_line(self):
ris = _export("[1] A paper\nURL: https://example.com/a?x=1 ER - \n")
lines = ris.split("\n")
ur_lines = [line for line in lines if line.startswith("UR - ")]
assert len(ur_lines) == 1
# The ER-looking suffix stayed inside the URL line (URL values
# are stripped, so compare without the trailing space).
assert "ER -" in ur_lines[0]
# And did not become its own structural terminator line.
assert sum(1 for line in lines if line == "ER - ") == 1
def test_publisher_domain_is_taken_not_the_raw_url(self):
ris = _export("[1] A paper\nURL: https://example.com/PB - evil\n")
lines = ris.split("\n")
pb_lines = [line for line in lines if line.startswith("PB - ")]
assert pb_lines == ["PB - example.com"]
class TestEntryStructureStaysCanonical:
def test_every_entry_is_ty_to_er_with_no_leakage_between(self):
ris = _export(
"[1] First paper\nURL: https://a.example/1\n\n"
"[2] Second paper\nURL: https://b.example/2\n"
)
lines = ris.split("\n")
# Two complete records, nothing outside them.
assert sum(1 for line in lines if line == "TY - ELEC") == 2
assert sum(1 for line in lines if line == "ER - ") == 2
first_ty = next(
i for i, line in enumerate(lines) if line == "TY - ELEC"
)
first_er = next(i for i, line in enumerate(lines) if line == "ER - ")
second_ty = next(
i
for i, line in enumerate(lines)
if line == "TY - ELEC" and i > first_er
)
assert first_ty < first_er < second_ty
@pytest.mark.parametrize(
"separator",
["\r", "\v", "\f", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"],
)
@pytest.mark.parametrize("field", ["title", "url", "doi", "author"])
def test_embedded_line_separators_cannot_inject_records(separator, field):
payload = (
f"benign{separator}ER - {separator}TY - JOUR{separator}TI - Injected"
)
title = payload if field == "title" else "A paper"
if field == "author":
title += f" by {payload}"
url = (
f"https://example.com/{payload}"
if field == "url"
else "https://example.com/paper"
)
doi = f"DOI: {payload}\n" if field == "doi" else ""
ris = _export(f"[1] {title}\nURL: {url}\n{doi}")
# splitlines recognizes CR and Unicode separators, unlike split("\n").
lines = ris.splitlines()
assert separator not in ris
assert [line for line in lines if line.startswith("TY - ")] == [
"TY - ELEC"
]
assert lines.count("ER - ") == 1
assert "TI - Injected" not in lines
if field == "url":
# A URL containing a line separator is discarded before RIS output;
# urlsplit can otherwise silently strip some of these characters.
assert not any(line.startswith("UR - ") for line in lines)
return
expected_tag = {"title": "TI", "url": "UR", "doi": "DO", "author": "AU"}[
field
]
assert any(
line.startswith(f"{expected_tag} - ") and "benign" in line
for line in lines
)