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.

Reject unsupported Wayback replay aliases (#7593)

LearningCircuit85c9791075ac
diff --git a/changelog.d/7569.security.md b/changelog.d/7569.security.md
new file mode 100644
index 0000000000..d991823a60
--- /dev/null
+++ b/changelog.d/7569.security.md
@@ -0,0 +1 @@
+Reject unsupported Wayback replay URL spellings before fetching, preserving the selected archive capture boundary.
diff --git a/src/local_deep_research/web_search_engines/wayback_client.py b/src/local_deep_research/web_search_engines/wayback_client.py
index e673a3932e..e83f9996da 100644
--- a/src/local_deep_research/web_search_engines/wayback_client.py
+++ b/src/local_deep_research/web_search_engines/wayback_client.py
@@ -64,7 +64,8 @@ def capture_metadata_from_replay_url(url: str) -> dict | None:
# must not fall through to an unrestricted live-page fetch, but only
# the original, canonical capture spelling is accepted below.
prepared = urlsplit(Request("GET", url).prepare().url or "")
- if (prepared.hostname or "").rstrip(".") != "web.archive.org":
+ prepared_host = (prepared.hostname or "").rstrip(".")
+ if prepared_host not in {"web.archive.org", "archive.org"}:
return None
def has_replay_path(path):
@@ -72,12 +73,14 @@ def capture_metadata_from_replay_url(url: str) -> dict | None:
# layer for detection only; never fetch a normalized substitute.
wrapper = unquote(path.split("://", 1)[0]).replace("\\", "/")
return any(
- candidate.lstrip("/").partition("/")[0] == "web"
+ candidate.lstrip("/").partition("/")[0].lower() == "web"
for candidate in (wrapper, posixpath.normpath(wrapper))
)
if not any(has_replay_path(path) for path in (parsed.path, prepared.path)):
return None
+ if prepared_host != "web.archive.org":
+ raise ValueError("Unsupported archive replay capture URL")
replay = re.match(r"^/web/([0-9]{14})(?:id_|if_)?/https?://", parsed.path)
if replay is None:
raise ValueError("Unsupported archive replay capture URL")
diff --git a/tests/advanced_search_system/tools/test_archive_fetch.py b/tests/advanced_search_system/tools/test_archive_fetch.py
index 7ff9507869..dafd45dd20 100644
--- a/tests/advanced_search_system/tools/test_archive_fetch.py
+++ b/tests/advanced_search_system/tools/test_archive_fetch.py
@@ -2323,6 +2323,7 @@ def test_first_direct_replay_success_keeps_capture_metadata_without_lookup(
real_replay.browser.assert_not_called()
[email protected]("mode", FETCH_MODES[1:])
@pytest.mark.parametrize(
"replay",
[
@@ -2332,6 +2333,8 @@ def test_first_direct_replay_success_keeps_capture_metadata_without_lookup(
REPLAY.replace("https://", "https://user:password@", 1),
REPLAY.replace("web.archive.org", "web.archive.org:8443"),
REPLAY.replace("web.archive.org", "web.archive.org."),
+ REPLAY.replace("web.archive.org", "archive.org"),
+ REPLAY.replace("/web/", "/WEB/"),
REPLAY.replace("/web/", "//web/"),
REPLAY.replace("/web/", "/web//"),
REPLAY.replace("web.archive.org", "%77eb.archive.org"),
@@ -2348,6 +2351,8 @@ def test_first_direct_replay_success_keeps_capture_metadata_without_lookup(
"credentials",
"port",
"trailing_dot_host",
+ "redirector_host",
+ "uppercase_replay_path",
"double_leading_slash",
"double_capture_slash",
"prepared_host",
@@ -2358,15 +2363,18 @@ def test_first_direct_replay_success_keeps_capture_metadata_without_lookup(
"wildcard_timestamp",
],
)
-def test_invalid_direct_capture_never_uses_transport_or_budget(replay, runtime):
+def test_invalid_direct_capture_never_uses_transport_or_budget(
+ mode, replay, runtime
+):
collector = SearchResultsCollector([])
- ordinary = build_fetch_tool("full", collector)
+ ordinary = build_fetch_tool(mode, collector, model=runtime.model)
- assert not ordinary.invoke({"url": replay}).startswith("[1]")
+ assert not ordinary.invoke(_arguments(mode, url=replay)).startswith("[1]")
assert collector.results == []
assert collector.fetch_count == 0
runtime.getter.assert_not_called()
runtime.factory.assert_not_called()
+ runtime.model.invoke.assert_not_called()
@pytest.fixture
diff --git a/tests/utilities/test_wayback_replay_contract.py b/tests/utilities/test_wayback_replay_contract.py
new file mode 100644
index 0000000000..1a7d3a05f1
--- /dev/null
+++ b/tests/utilities/test_wayback_replay_contract.py
@@ -0,0 +1,312 @@
+"""Pin the shared replay grammar and deliberate citation/transport differences.
+
+Citation recognition preserves a target's spelling. Transport additionally
+requires a valid capture date and an HTTPS wrapper inside the capture guard.
+"""
+
+from urllib.parse import urlsplit
+
+import pytest
+
+from local_deep_research.utilities.search_utilities import (
+ count_distinct_sources,
+ format_links_to_markdown,
+)
+from local_deep_research.utilities.url_utils import (
+ _is_wayback_replay_url,
+ canonical_url_key,
+)
+from local_deep_research.web_search_engines import wayback_client
+
+
+CAPTURE = "20240203112233"
+TARGET = "https://example.com/history/?utm_source=capture#section"
+REPLAY = f"https://web.archive.org/web/{CAPTURE}/{TARGET}"
+
+
[email protected](autouse=True)
+def no_network(monkeypatch):
+ def unexpected_request(*args, **kwargs):
+ pytest.fail("Replay classification must not perform network access")
+
+ monkeypatch.setattr(wayback_client, "safe_get", unexpected_request)
+ monkeypatch.setattr("requests.sessions.Session.send", unexpected_request)
+ monkeypatch.setattr("socket.getaddrinfo", unexpected_request)
+
+
[email protected]("modifier", ["", "id_", "if_"])
+ "authority",
+ [
+ "web.archive.org",
+ "web.archive.org:443",
+ "WEB.ARCHIVE.ORG",
+ "WeB.ArChIvE.OrG:443",
+ ],
+)
+ "target",
+ [
+ pytest.param("https://example.com", id="bare_target_host"),
+ pytest.param("http://example.com/history/", id="http_target"),
+ pytest.param(
+ "https://Example.COM:443/History/%2f/%7Eauthor/",
+ id="target_case_port_and_encoding",
+ ),
+ pytest.param(
+ "https://example.com/history?%75tm_source=capture&&q=a+b&q=a%20b&",
+ id="raw_query_spelling",
+ ),
+ pytest.param(
+ "https://example.com/history/"
+ "?next=https://other.example/a%2Fb/&fbclid=capture#section",
+ id="nested_url_and_tracking",
+ ),
+ pytest.param(
+ "https://example.com/history/?q=%23encoded%3F&x=1&x=2#part%2f",
+ id="encoded_delimiters_and_fragment",
+ ),
+ pytest.param("https://example.com/history/?", id="empty_query"),
+ pytest.param("https://example.com/history/#", id="empty_fragment"),
+ pytest.param(
+ "https://example.com/history/#/route?utm_source=fragment",
+ id="fragment_route",
+ ),
+ ],
+)
+def test_supported_replays_share_grammar_and_preserve_identity(
+ modifier, authority, target
+):
+ replay = f"https://{authority}/web/{CAPTURE}{modifier}/{target}"
+
+ assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
+ assert wayback_client.capture_metadata_from_replay_url(replay) == {
+ "source_type": "archive",
+ "original_url": target,
+ "requested_url": target,
+ "timestamp": CAPTURE,
+ "formatted_date": "2024-02-03 11:22:33 UTC",
+ }
+ assert canonical_url_key(replay) == replay
+
+
+ "replay,error",
+ [
+ # These two public #7569 families must raise, never return None.
+ pytest.param(
+ REPLAY.replace("web.archive.org", "archive.org"),
+ "Unsupported archive replay capture URL",
+ id="archive_org_redirector_7569",
+ ),
+ pytest.param(
+ REPLAY.replace("/web/", "/WEB/"),
+ "Unsupported archive replay capture URL",
+ id="uppercase_web_7569",
+ ),
+ pytest.param(
+ REPLAY.replace("web.archive.org", "web.archive.org."),
+ "left the selected capture",
+ id="trailing_dot_host",
+ ),
+ pytest.param(
+ REPLAY.replace("/web/", "//web/"),
+ "Unsupported archive replay capture URL",
+ id="double_leading_slash",
+ ),
+ pytest.param(
+ REPLAY.replace("/web/", "/web//"),
+ "Unsupported archive replay capture URL",
+ id="double_capture_slash",
+ ),
+ pytest.param(
+ REPLAY.replace("web.archive.org", "%77eb.archive.org"),
+ "left the selected capture",
+ id="encoded_wrapper_host",
+ ),
+ pytest.param(
+ REPLAY.replace("/web/", "/%77eb/"),
+ "Unsupported archive replay capture URL",
+ id="encoded_wrapper_path",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, "%32" + CAPTURE[1:]),
+ "Unsupported archive replay capture URL",
+ id="encoded_timestamp",
+ ),
+ pytest.param(
+ REPLAY.replace("/web/", "/about/../web/"),
+ "Unsupported archive replay capture URL",
+ id="wrapper_dot_segments",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, "20240203"),
+ "Unsupported archive replay capture URL",
+ id="partial_timestamp",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, CAPTURE + "0"),
+ "Unsupported archive replay capture URL",
+ id="overlong_timestamp",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, "*"),
+ "Unsupported archive replay capture URL",
+ id="wildcard_timestamp",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, "٢٠٢٤٠٢٠٣١١٢٢٣٣"),
+ "Unsupported archive replay capture URL",
+ id="non_ascii_timestamp",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, CAPTURE + "im_"),
+ "Unsupported archive replay capture URL",
+ id="unsupported_modifier",
+ ),
+ pytest.param(
+ REPLAY.replace(CAPTURE, CAPTURE + "ID_"),
+ "Unsupported archive replay capture URL",
+ id="uppercase_modifier",
+ ),
+ pytest.param(
+ REPLAY.replace("https://", "https://user:password@", 1),
+ "left the selected capture",
+ id="wrapper_credentials",
+ ),
+ pytest.param(
+ REPLAY.replace("web.archive.org", "web.archive.org:8443"),
+ "left the selected capture",
+ id="wrapper_nondefault_port",
+ ),
+ pytest.param(
+ REPLAY.replace(TARGET, "https://user:[email protected]/history"),
+ "without credentials",
+ id="target_credentials",
+ ),
+ pytest.param(
+ REPLAY.replace(TARGET, "https:///history"),
+ "without credentials",
+ id="missing_target_host",
+ ),
+ pytest.param(
+ REPLAY.replace(TARGET, "https://example.com:65536/history"),
+ "Port out of range",
+ id="invalid_target_port",
+ ),
+ pytest.param(
+ REPLAY.replace(TARGET, "ftp://example.com/history"),
+ "Unsupported archive replay capture URL",
+ id="unsupported_target_scheme",
+ ),
+ ],
+)
+def test_unsupported_replay_spellings_are_not_citations_or_live_fetches(
+ replay, error
+):
+ assert _is_wayback_replay_url(replay, urlsplit(replay)) is False
+ with pytest.raises(ValueError, match=error):
+ wayback_client.capture_metadata_from_replay_url(replay)
+
+
+ "wrapper",
+ [
+ "http://web.archive.org",
+ "HTTP://WEB.ARCHIVE.ORG:80",
+ "http://web.archive.org:443",
+ "https://web.archive.org:80",
+ ],
+)
+def test_citation_spelling_survives_transport_https_and_port_restrictions(
+ wrapper,
+):
+ replay = f"{wrapper}/web/{CAPTURE}/{TARGET}"
+
+ assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
+ assert canonical_url_key(replay) == replay
+ with pytest.raises(ValueError, match="left the selected capture"):
+ wayback_client.capture_metadata_from_replay_url(replay)
+
+
+ "timestamp",
+ ["20240230112233", "20230229112233", "20241303112233", "00000203112233"],
+)
+def test_citation_grammar_preserves_dates_that_transport_calendar_rejects(
+ timestamp,
+):
+ replay = REPLAY.replace(CAPTURE, timestamp)
+
+ assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
+ assert canonical_url_key(replay) == replay
+ # Calendar error messages come from datetime and vary by Python version.
+ with pytest.raises(ValueError):
+ wayback_client.capture_metadata_from_replay_url(replay)
+
+
+ "url",
+ [
+ TARGET,
+ "https://web.archive.org/about/",
+ "https://archive.org/wayback/available?url=" + TARGET,
+ "https://web.archive.org/about/?next=" + REPLAY,
+ "https://archive.org/about/" + TARGET + "/web/" + CAPTURE,
+ REPLAY.replace("web.archive.org", "other.example"),
+ REPLAY.replace("web.archive.org", "web.archive.org.evil.example"),
+ REPLAY.removeprefix("https:"),
+ ],
+)
+def test_non_replays_are_recognized_by_neither_grammar(url):
+ assert _is_wayback_replay_url(url, urlsplit(url)) is False
+ assert wayback_client.capture_metadata_from_replay_url(url) is None
+
+
[email protected]("modifier", ["", "id_", "if_"])
+ "first,second",
+ [
+ ("https://example.com/history", "https://example.com/history/"),
+ (
+ "https://example.com/history?utm_source=first",
+ "https://example.com/history?utm_source=second",
+ ),
+ (
+ "https://example.com/history?q=a+b",
+ "https://example.com/history?q=a%20b",
+ ),
+ (
+ "https://example.com/history?x=1&y=2",
+ "https://example.com/history?y=2&x=1",
+ ),
+ (
+ "https://example.com/history#first",
+ "https://example.com/history#second",
+ ),
+ ("https://example.com/history", "https://example.com/history?"),
+ ("https://example.com/history", "https://example.com/history#"),
+ ],
+)
+def test_public_citation_entry_points_keep_replay_targets_distinct(
+ modifier, first, second
+):
+ replays = [
+ f"https://web.archive.org/web/{CAPTURE}{modifier}/{target}"
+ for target in (first, second)
+ ]
+ links = []
+ for index, (replay, target) in enumerate(zip(replays, (first, second)), 1):
+ assert _is_wayback_replay_url(replay, urlsplit(replay)) is True
+ metadata = wayback_client.capture_metadata_from_replay_url(replay)
+ assert metadata["original_url"] == target
+ assert canonical_url_key(replay) == replay
+ links.append({"index": str(index), "title": "Capture", "link": replay})
+
+ assert count_distinct_sources(links) == 2
+ rendered = format_links_to_markdown(links, uncited_mode="disabled")
+ assert [
+ line.removeprefix(" URL: ")
+ for line in rendered.splitlines()
+ if line.startswith(" URL: ")
+ ] == replays