mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 03:00:52 +08:00
A knowledge-base name is three things at once: a kb_config.json key, a
directory under data/knowledge_bases, and a path segment in every per-KB API
route. `validate_knowledge_base_name` exists to keep those three uses
compatible — its docstring says so — but it was called from one of the eight
HTTP entry points that can write a name into the config. The seven connect-*
endpoints (Obsidian, MarginNote, folder, LightRAG server, WeKnora, IMA, and
the subagent connection) did `(name or "").strip()` and wrote the result.
A name holding a "/" registers fine that way and is then unreachable.
uvicorn percent-decodes the path before routing, so %2F is a real separator
by the time Starlette matches `{kb_name}` — compiled to `[^/]+`, which cannot
span it. All 29 per-KB routes 404, delete included, while the list endpoint
takes no path parameter and keeps rendering the KB. Connected KBs are also
exempt from the orphan prune, so nothing collects it either: visible, unusable,
and removable only by hand-editing kb_config.json.
Two changes, both needed. Validation moves onto the eight `register_*`
methods, which is the seam every route already goes through — no route file
changes, since they all map ValueError to 400 — and the now-unreachable
"name is required" guards go with it. And a path-free
`POST /knowledge-bases/delete` gives the entries that predate the guard a way
out; a body is never split into segments. Widening the path route to
`{kb_name:path}` would not work: it is declared ahead of the DELETE routes
for linked folders, GitHub sources and web sources, and a greedy converter
would silently swallow all three.
Two smaller fixes ride along. The delete handler's catch-all swallowed its
own HTTPException and answered 500 with "404: ... not found" in the detail;
it now re-raises. And CreateKbModal states the rule under the field instead
of letting an untranslated English 400 be the first the user hears of it.
test_marginnote4_kb's store-collision case used "My Lib" / "My/Lib"; the
slash is now refused before that guard is reached, so it uses "My.Lib" —
still a name the rule allows, still sanitized to the same My_Lib.db.
46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from deeptutor.knowledge.naming import validate_knowledge_base_name
|
|
|
|
|
|
def test_validate_knowledge_base_name_allows_unicode_and_spaces() -> None:
|
|
assert validate_knowledge_base_name(" 高等数学 KB ") == "高等数学 KB"
|
|
|
|
|
|
@pytest.mark.parametrize("name", ["bad/name", "bad\\name", "bad?name", "bad#name", "bad%name"])
|
|
def test_validate_knowledge_base_name_rejects_path_and_url_separators(name: str) -> None:
|
|
with pytest.raises(ValueError, match="reserved characters"):
|
|
validate_knowledge_base_name(name)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"register",
|
|
[
|
|
lambda m, name: m.register_obsidian_vault(name, "/tmp"),
|
|
lambda m, name: m.register_linked_kb(name, "/tmp", "llamaindex"),
|
|
lambda m, name: m.register_subagent_connection(name, agent_kind="claude_code"),
|
|
lambda m, name: m.register_lightrag_server_kb(name, "http://localhost:9621"),
|
|
lambda m, name: m.register_marginnote4_kb(name),
|
|
lambda m, name: m.register_ima_kb(name, "", "", "lib-1"),
|
|
lambda m, name: m.register_weknora_kb(name, "http://localhost:8080", "k", "kb-1"),
|
|
],
|
|
)
|
|
def test_every_register_path_rejects_a_url_separator(tmp_path, register) -> None:
|
|
"""The validator has to sit on the manager, not on one route.
|
|
|
|
``validate_knowledge_base_name`` used to be called by the create route
|
|
alone, so the seven connect-* endpoints wrote whatever the user typed
|
|
straight into ``kb_config.json``. A name holding a ``/`` is then
|
|
unaddressable: uvicorn decodes the path before routing and ``{kb_name}``
|
|
cannot span a separator, so every per-KB route 404s and the KB can be
|
|
listed but never deleted.
|
|
"""
|
|
from deeptutor.knowledge.manager import KnowledgeBaseManager
|
|
|
|
manager = KnowledgeBaseManager(base_dir=str(tmp_path))
|
|
with pytest.raises(ValueError, match="reserved characters"):
|
|
register(manager, "数学/物理")
|
|
assert manager.list_knowledge_bases() == []
|