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.

fix(ris): prevent hostile source fields from injecting records (#6445)

LearningCircuit5d93e500dd67
diff --git a/changelog.d/6445.security.md b/changelog.d/6445.security.md
new file mode 100644
index 0000000000..348dd98c21
--- /dev/null
+++ b/changelog.d/6445.security.md
@@ -0,0 +1 @@
+Flatten control characters in RIS fields so embedded carriage returns and Unicode line separators cannot inject extra records or metadata.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 69a32c5107..486e7f50d5 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -1686,7 +1686,15 @@ class RISExporter:
# ER - End of reference
lines.append("ER - ")
- return "\n".join(lines)
+ # A field can contain bare CR or Unicode line separators even when
+ # extracted from one LF-delimited source line. Flatten them at the
+ # serialization boundary so only our separators create RIS fields.
+ return "\n".join(
+ "".join(
+ " " if is_line_breaking_char(char) else char for char in line
+ )
+ for line in lines
+ )
class LaTeXExporter:
diff --git a/tests/exporters/test_ris_injection_contracts.py b/tests/exporters/test_ris_injection_contracts.py
new file mode 100644
index 0000000000..0dd3df82a7
--- /dev/null
+++ b/tests/exporters/test_ris_injection_contracts.py
@@ -0,0 +1,157 @@
+"""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
+
+
+ "separator",
+ ["\r", "\v", "\f", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"],
+)
[email protected]("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
+ 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
+ )