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.

harden(fetch): bind library_resolver path-form regex to document-id pattern (#5496)

LearningCircuit80a54a006274
diff --git a/changelog.d/5485.security.md b/changelog.d/5485.security.md
new file mode 100644
index 0000000000..b45168b399
--- /dev/null
+++ b/changelog.d/5485.security.md
@@ -0,0 +1 @@
+`/library/document/...` URL resolution now requires the path segment to be a canonical document id (32/64-hex or UUID), rejecting guessable filenames at the regex instead of relying solely on a downstream database miss.
diff --git a/src/local_deep_research/advanced_search_system/tools/fetch/library_resolver.py b/src/local_deep_research/advanced_search_system/tools/fetch/library_resolver.py
index 953f4ef9c5..8fac35ba4e 100644
--- a/src/local_deep_research/advanced_search_system/tools/fetch/library_resolver.py
+++ b/src/local_deep_research/advanced_search_system/tools/fetch/library_resolver.py
@@ -27,6 +27,12 @@ Both shapes are intercepted here BEFORE the egress policy gate, so the
agent gets the real page content (or a helpful error) instead of a
generic "blocked by egress policy" denial that wastes a fetch slot
(A3 in ``research_f3045c5b_issue_analysis.md``).
+
+Both the path and bracket forms bind the document id to the canonical
+32/64-hex-or-UUID pattern, so a guessable filename (``report.pdf``) is
+rejected at the regex instead of relying on a downstream DB miss.
+Resolution always runs inside ``get_user_db_session(username)``, so a
+parsed reference can still only return a document the caller already owns.
"""
from __future__ import annotations
@@ -45,6 +51,18 @@ from ....utilities.chunk_anchor import (
)
+# Canonical document IDs are 32/64-hex blobs or a UUID. The path form is
+# bound to this same pattern as the bracket form (below) so a guessable
+# filename like ``/library/document/report.pdf`` is rejected at the regex
+# rather than reaching the DB only to miss.
+_DOCUMENT_ID_PATTERN = (
+ r"(?:"
+ r"[0-9a-fA-F]{32}|"
+ r"[0-9a-fA-F]{64}|"
+ r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
+ r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
+ r")"
+)
# Match canonical paths and the ``/lib/document/...`` abbreviation observed
# in agent tool calls. The same suffix is used by download_service.
#
@@ -54,17 +72,9 @@ from ....utilities.chunk_anchor import (
# the same ``Document.text_content`` read below — the suffix only tells a
# caller which UI route the citation pointed at.
_LIBRARY_PATH_RE = re.compile(
- r"^/(?:library|lib)/document/(?P<doc_id>[^/?#]+)"
+ rf"^/(?:library|lib)/document/(?P<doc_id>{_DOCUMENT_ID_PATTERN})"
rf"(?:/(?P<suffix>{LIBRARY_ROUTE_SUFFIX_ALTERNATION}))?/?$"
)
-_DOCUMENT_ID_PATTERN = (
- r"(?:"
- r"[0-9a-fA-F]{32}|"
- r"[0-9a-fA-F]{64}|"
- r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
- r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
- r")"
-)
_BRACKETED_DOCUMENT_RE = re.compile(
rf"^\[(?P<doc_id>{_DOCUMENT_ID_PATTERN})\]$"
)
@@ -89,6 +99,15 @@ class _LibraryReference:
def _decode_segment(value: str) -> str | None:
+ """Percent-decode one path segment, refusing separators and controls.
+
+ Now that ``_LIBRARY_PATH_RE``'s doc_id group is bound to hex/UUID, the only
+ live call site can never hand this a ``%``, so ``unquote`` is a no-op there
+ and none of the checks below fire. It is kept deliberately rather than
+ inlined away: it is the guard that makes loosening that group safe, and
+ deleting it would silently remove the safety net for whoever does. Anyone
+ widening the doc_id charset should confirm this is reachable again.
+ """
decoded = unquote(value)
if (
not decoded
@@ -153,8 +172,12 @@ def parse_library_url(url: str) -> tuple[str, Optional[str]] | None:
root document page. Any ``#fragment`` (e.g. ``#chunk-3``) is stripped
before matching, so a chunk-targeted citation resolves to its
document. In addition to canonical library paths, this accepts
- the ID-bound aliases emitted by the agent. Filename-only references remain
- on the normal egress-denial path because filenames are guessable.
+ the ID-bound aliases emitted by the agent. The ``doc_id`` segment must
+ match the canonical document-ID pattern (32/64-hex or UUID), so
+ filename-shaped values like ``report.pdf`` are rejected here and fall
+ through to the egress-denial path; any accepted reference is still
+ scoped to the caller's own documents by ``get_user_db_session`` at
+ lookup time.
"""
reference = _parse_library_reference(url)
if reference is None:
diff --git a/tests/advanced_search_system/tools/test_library_resolver.py b/tests/advanced_search_system/tools/test_library_resolver.py
index b3f1306ee5..2ef89d36b6 100644
--- a/tests/advanced_search_system/tools/test_library_resolver.py
+++ b/tests/advanced_search_system/tools/test_library_resolver.py
@@ -24,7 +24,8 @@ def test_parse_library_url_root_form():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123") == ("abc-123", None)
+ doc_id = "123e4567-e89b-12d3-a456-426614174000"
+ assert parse_library_url(f"/library/document/{doc_id}") == (doc_id, None)
def test_parse_library_url_pdf_suffix():
@@ -32,8 +33,9 @@ def test_parse_library_url_pdf_suffix():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123/pdf") == (
- "abc-123",
+ doc_id = "123e4567-e89b-12d3-a456-426614174000"
+ assert parse_library_url(f"/library/document/{doc_id}/pdf") == (
+ doc_id,
"pdf",
)
@@ -50,22 +52,30 @@ def test_parse_library_url_chunks_suffix_with_anchor():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123/chunks#chunk-5") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-5"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
"chunks",
)
# Chunk 0 is a real chunk — the anchor must not be treated as falsy.
- assert parse_library_url("/library/document/abc-123/chunks#chunk-0") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-0"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
"chunks",
)
# Bare chunks route, and the ``/lib/`` abbreviation.
- assert parse_library_url("/library/document/abc-123/chunks") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
"chunks",
)
- assert parse_library_url("/lib/document/abc-123/chunks#chunk-2") == (
- "abc-123",
+ assert parse_library_url(
+ "/lib/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-2"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
"chunks",
)
@@ -77,12 +87,16 @@ def test_parse_library_url_strips_fragment_on_root_form():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123#chunk-2") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000#chunk-2"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
None,
)
- assert parse_library_url("/library/document/abc-123/pdf#page=4") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000/pdf#page=4"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
"pdf",
)
@@ -97,8 +111,8 @@ def test_parse_library_url_absolute_alias_with_chunk_anchor():
)
assert parse_library_url(
- "https://library.document/abc-123/chunks#chunk-3"
- ) == ("abc-123", "chunks")
+ "https://library.document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3"
+ ) == ("123e4567-e89b-12d3-a456-426614174000", "chunks")
def test_parse_library_url_fragment_containing_scheme_not_misrouted():
@@ -108,8 +122,10 @@ def test_parse_library_url_fragment_containing_scheme_not_misrouted():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123#http://anything") == (
- "abc-123",
+ assert parse_library_url(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000#http://anything"
+ ) == (
+ "123e4567-e89b-12d3-a456-426614174000",
None,
)
@@ -154,7 +170,8 @@ def test_resolve_library_document_reads_document_for_chunk_url():
return_value=session_cm,
):
result = resolve_library_document(
- "/library/document/abc-123/chunks#chunk-3", "alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3",
+ "alice",
)
assert result is not None
@@ -162,7 +179,10 @@ def test_resolve_library_document_reads_document_for_chunk_url():
assert result["content"] == "full document body"
# The original URL (fragment included) is preserved so a downstream
# re-citation still points at the cited chunk.
- assert result["url"] == "/library/document/abc-123/chunks#chunk-3"
+ assert (
+ result["url"]
+ == "/library/document/123e4567-e89b-12d3-a456-426614174000/chunks#chunk-3"
+ )
def test_parse_library_url_trailing_slash():
@@ -170,8 +190,9 @@ def test_parse_library_url_trailing_slash():
parse_library_url,
)
- assert parse_library_url("/library/document/abc-123/") == (
- "abc-123",
+ doc_id = "123e4567-e89b-12d3-a456-426614174000"
+ assert parse_library_url(f"/library/document/{doc_id}/") == (
+ doc_id,
None,
)
@@ -197,6 +218,32 @@ def test_parse_library_url_rejects_guessable_filename_forms():
assert parse_library_url("/legacy/document/report%202024.txt") is None
assert parse_library_url("report 2024.txt") is None
+ # The path form is bound to the canonical document-ID pattern (32/64-hex
+ # or UUID), so a guessable filename under /library/document/ or
+ # /lib/document/ is rejected at the regex, not by a downstream DB miss.
+ assert parse_library_url("/library/document/report.pdf") is None
+ assert parse_library_url("/lib/document/secret.key") is None
+
+
+def test_parse_library_url_rejects_encoded_path_traversal():
+ """Encoded path traversal cannot escape the document segment.
+
+ Rejected by ``_LIBRARY_PATH_RE`` itself, before any decoding happens: the
+ doc_id group is hex/UUID only, so a ``%`` cannot appear in it and the match
+ fails. ``_decode_segment``'s own ``/`` check is never reached on this input
+ -- verified by instrumenting it -- so do not read this test as evidence
+ that decoding is what stops traversal here.
+
+ That matters if the doc_id group is ever loosened: ``_decode_segment`` is
+ not currently a live second layer behind it, because the charset the regex
+ admits can never contain a percent-escape for it to decode.
+ """
+ from local_deep_research.advanced_search_system.tools.fetch.library_resolver import (
+ parse_library_url,
+ )
+
+ assert parse_library_url("/library/document/%2e%2e%2fetc%2fpasswd") is None
+ assert parse_library_url("/lib/document/%2e%2e%2fetc%2fpasswd") is None
def test_parse_library_url_rejects_non_matching_shapes():
@@ -298,13 +345,17 @@ def test_resolve_library_document_returns_shape_for_full_text():
session_cm.return_value.__enter__.return_value = session
result = resolve_library_document(
- "/library/document/abc-123", username="alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username="alice",
)
assert result is not None
assert result["title"] == "T"
assert result["content"] == "full body"
- assert result["url"] == "/library/document/abc-123"
+ assert (
+ result["url"]
+ == "/library/document/123e4567-e89b-12d3-a456-426614174000"
+ )
# Snippet is the first ~200 chars of the content.
assert result["snippet"] == "full body"
@@ -407,7 +458,8 @@ def test_resolve_library_document_handles_empty_text():
session_cm.return_value.__enter__.return_value = session
result = resolve_library_document(
- "/library/document/abc-123", username="alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username="alice",
)
assert result is not None
@@ -434,7 +486,8 @@ def test_resolve_library_document_returns_none_for_unknown_doc():
assert (
resolve_library_document(
- "/library/document/missing", username="alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username="alice",
)
is None
)
@@ -448,7 +501,10 @@ def test_resolve_library_document_returns_none_when_no_username():
)
assert (
- resolve_library_document("/library/document/abc-123", username=None)
+ resolve_library_document(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username=None,
+ )
is None
)
@@ -468,7 +524,8 @@ def test_resolve_library_document_swallows_db_errors():
assert (
resolve_library_document(
- "/library/document/abc-123", username="alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username="alice",
)
is None
)
@@ -485,7 +542,8 @@ def test_resolve_library_document_handles_import_error():
):
assert (
resolve_library_document(
- "/library/document/abc-123", username="alice"
+ "/library/document/123e4567-e89b-12d3-a456-426614174000",
+ username="alice",
)
is None
)
@@ -587,7 +645,9 @@ def test_make_library_resolver_binds_username():
session_cm.return_value.__enter__.return_value = session
resolver = make_library_resolver("alice")
- result = resolver("/library/document/abc-123")
+ result = resolver(
+ "/library/document/123e4567-e89b-12d3-a456-426614174000"
+ )
assert result is not None
assert result["content"] == "hello"
@@ -602,7 +662,10 @@ def test_make_library_resolver_returns_none_for_no_username():
)
resolver = make_library_resolver(None)
- assert resolver("/library/document/abc-123") is None
+ assert (
+ resolver("/library/document/123e4567-e89b-12d3-a456-426614174000")
+ is None
+ )
def test_bracket_alias_is_recited_as_the_canonical_route():