~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
fix(citations): label a look-alike host by the host it reaches (#6833)
Lukasb9ab6bd8c492
diff --git a/changelog.d/6833.security.md b/changelog.d/6833.security.mdnew file mode 100644index 0000000000..95dd6f71e8--- /dev/null+++ b/changelog.d/6833.security.md@@ -0,0 +1 @@+Domain-based citation labels name the host a link actually reaches. A look-alike host such as `arxiv.org.evil.example` was labelled `arxiv.org`, and a URL with userinfo showed it in the label.diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.pyindex b140a5675b..a71865cc1d 100644--- a/src/local_deep_research/text_optimization/citation_formatter.py+++ b/src/local_deep_research/text_optimization/citation_formatter.py@@ -1121,8 +1121,14 @@ class CitationFormatter:def _extract_domain(self, url: str) -> str:"""Extract domain name from URL."""try:- parsed = urlparse(url)- domain = parsed.netloc+ # The authority as browsers and requests read it: they end it at+ # a backslash, where urllib reads on, so+ # https://evil.example\@arxiv.org/ reaches evil.example.+ authority = urlparse("//" + urlparse(url).netloc.split("\\", 1)[0])+ # Without any userinfo: https://[email protected]/ reaches+ # evil.example, and https://user:[email protected]/ would put+ # the token in the label.+ domain = authority.netloc.rpartition("@")[2]# Remove www. prefix if presentif domain.startswith("www."):domain = domain[4:]@@ -1137,11 +1143,30 @@ class CitationFormatter:"medium.com": "medium.com",}+ # The host itself or a subdomain of it. A substring test on the+ # netloc also labelled arxiv.org.evil.example as arxiv.org. The+ # root dot of a fully qualified name (arxiv.org.) is dropped, as+ # utilities/arxiv.py does, since that host is still arxiv.org.+ # A port that is not a number hides the rest of the authority+ # from `hostname` (arxiv.org:443.evil.example), and requests+ # refuses it, so it earns no known label.+ try:+ _ = authority.port+ host = (authority.hostname or "").rstrip(".")+ except ValueError:+ host = ""for known, display in known_domains.items():- if known in domain:+ if host == known or host.endswith("." + known):return display- # For other domains, extract main domain+ # Preserve valid ports on other domains, as the existing+ # citation-label contract requires. For an invalid authority,+ # leave out a port that the check above refused (arxiv.org:99999);+ # a colon followed by more labels (arxiv.org:443.evil.example)+ # is still part of the untrusted name.+ name, _, port = domain.rpartition(":")+ if not host and name and "." not in port and "]" not in port:+ domain = nameparts = domain.split(".")if len(parts) >= 2:return ".".join(parts[-2:])diff --git a/tests/text_optimization/test_citation_formatter_contracts.py b/tests/text_optimization/test_citation_formatter_contracts.pyindex 94db9f52e0..cc9345fe86 100644--- a/tests/text_optimization/test_citation_formatter_contracts.py+++ b/tests/text_optimization/test_citation_formatter_contracts.py@@ -693,24 +693,108 @@ class TestSourceLabelFidelity:("https://not-arxiv.org.attacker.test/p", "arxiv.org"),("https://evil-github.com/x", "github.com"),("https://youtube.com.phish.test/v", "youtube.com"),+ # A registrable name that merely ends in the known one, with no+ # dot in between, one per known domain.+ ("https://notarxiv.org/abs/1", "arxiv.org"),+ ("https://evilreddit.com/r/x", "reddit.com"),+ ("https://not-youtube.com/v", "youtube.com"),+ ("https://mypypi.org/project/x", "pypi.org"),+ ("https://fakemilvus.io/docs", "milvus.io"),+ ("https://fakemedium.com/a", "medium.com"),],)- @pytest.mark.xfail(- strict=True,- reason=(- "SECURITY DEFECT: _extract_domain matches its known_domains "- "table with 'if known in domain', a substring test on the "- "whole netloc. A look-alike host is therefore labelled with "- "the trusted domain, so the reader sees [arxiv.org] on a "- "citation that resolves to arxiv.org.evil.example. The check "- "should be 'domain == known or domain.endswith(\".\" + known)'."- ),- )def test_lookalike_host_is_not_labelled_with_the_known_domain(self, url, impostor):assert CitationFormatter()._extract_domain(url) != impostor+ @pytest.mark.parametrize(+ "url,host",+ [+ ("https://[email protected]/abs/1", "evil.example"),+ ("https://user:[email protected]/report", "example.com"),+ ],+ )+ def test_userinfo_does_not_name_the_host(self, url, host):+ """The label names the host the link reaches, and no credentials."""+ assert CitationFormatter()._extract_domain(url) == host++ @pytest.mark.parametrize(+ "url",+ [+ "https://evil.example\\@arxiv.org/abs/1",+ "https://evil.example\\.arxiv.org/abs/1",+ "https://arxiv.org:443.evil.example/abs/1",+ ],+ )+ def test_an_authority_the_http_stack_reads_differently_names_where_it_goes(+ self, url+ ):+ """Browsers and requests end the authority at a backslash, and a port+ that is not a number hides the rest of it from urllib's hostname."""+ assert CitationFormatter()._extract_domain(url) == "evil.example"++ def test_a_lookalike_citation_renders_its_real_host(self):+ document = make_doc(+ "See [1].", "[1] Paper\nURL: https://arxiv.org.evil.example/p"+ )+ answer = answer_of(+ CitationFormatter(CitationMode.DOMAIN_HYPERLINKS).format_document(+ document+ )+ )+ assert "See [[evil.example]](https://arxiv.org.evil.example/p)." in (+ answer+ )++ @pytest.mark.parametrize(+ "url",+ [+ "https://export.arxiv.org/abs/2401.12345",+ "https://arxiv.org:443/abs/2401.12345",+ "https://ArXiv.org/abs/2401.12345",+ "https://arxiv.org./abs/2401.12345",+ ],+ )+ def test_a_subdomain_port_capital_or_root_dot_keeps_the_known_label(+ self, url+ ):+ assert CitationFormatter()._extract_domain(url) == "arxiv.org"++ @pytest.mark.parametrize(+ "url,label",+ [+ # A port requests refuses earns no known label, but the label+ # still leaves it out, as main did for known domains.+ ("https://arxiv.org:99999/abs/1", "arxiv.org"),+ ("https://arxiv.org:+443/abs/1", "arxiv.org"),+ ("https://example.com:8080/report", "example.com:8080"),+ ("http://[::1]:8080/x", "[::1]:8080"),+ ("http://[::1]/x", "[::1]"),+ ],+ )+ def test_port_labels_preserve_compatibility(self, url, label):+ assert CitationFormatter()._extract_domain(url) == label++ @pytest.mark.parametrize(+ "known",+ [+ "arxiv.org",+ "github.com",+ "reddit.com",+ "youtube.com",+ "pypi.org",+ "milvus.io",+ "medium.com",+ ],+ )+ def test_a_root_dot_keeps_every_known_label(self, known):+ # Every entry, not just arxiv.org: stripping more than the dot+ # (rstrip(".o")) leaves "arxiv.org" whole but turns milvus.io. into io.+ assert (+ CitationFormatter()._extract_domain(f"https://{known}./x") == known+ )+def test_arxiv_in_the_path_does_not_earn_its_tag(self):"""Regression control for the arXiv half of the defect below.