405303e82c
Batch 1 — CRITICAL (1): - 提取 is_safe_path() 到 src/core/security.py 公共模块 - CLI 和 ingest_obsidian.py 统一添加路径遍历防护 Batch 2 — HIGH (13) + 架构重构: - CLI 复用 deps.py AppState, 消除 30 行重复代码 - AppState/get_state 添加线程安全锁 - serve 命令传递 --config 到 uvicorn (H1) - OpenAIEmbedder 懒创建+复用 HTTP 客户端 (H2) - DashscopeEmbedder import 移到模块顶部 (H3) - 路径检查改用 os.path.commonpath (H4) - embedder.embed() 返回值长度检查 (H5) - 健康检查不泄露内部错误详情 (H7) - /api/v1/collections 添加 API Key 认证 (H8) - API Key 使用 hmac.compare_digest 恒定时间比较 (H9) - 添加 CORS 中间件 (H10) - ServerConfig 支持 SSL 配置 (H11) - HF_ENDPOINT 修改添加详细注释 (H12) Batch 3 — MEDIUM (20) + Splitter Protocol: - 定义 Splitter(Protocol) 接口, DocumentIngestor 接受可选 splitter - DashScope 响应添加结构验证 (M2) - ingest_obsidian.py 支持 CLI 参数和 OBSIDIAN_DIRS 环境变量 (M6) - scripts/serve.py 添加废弃警告 (M7) - content 限制 500KB, collection 正则限制字符集 (M12-M14) - 默认监听地址 127.0.0.1 (M16) - 添加安全响应头中间件 (M17) - verify_api_key 认证失败记录日志 (M19) Batch 4 — LOW (10): - CLI emoji 清理为纯文本标记 (L5) - logging.basicConfig 移到 FastAPI lifespan (L1) - VectorDB 添加 write_guard() 上下文管理器 (L3) - IngestRequest file_path/content 互斥校验 (L10) - ingest_obsidian.py 注释修正 (L6) 测试: 46 → 70 (+24) - tests/test_security.py: 11 个路径安全测试 - tests/test_deps.py: 11 个依赖注入测试 Co-Authored-By: Claude <noreply@anthropic.com>
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""is_safe_path 路径遍历防护测试."""
|
|
import pytest
|
|
from src.core.security import is_safe_path
|
|
|
|
|
|
class TestIsSafePath:
|
|
"""is_safe_path 函数测试."""
|
|
|
|
# -- 合法路径 --
|
|
def test_relative_path_ok(self):
|
|
"""相对路径应通过."""
|
|
assert is_safe_path("docs/readme.md") is True
|
|
assert is_safe_path("src/core/config.py") is True
|
|
|
|
def test_single_filename_ok(self):
|
|
"""仅文件名应通过."""
|
|
assert is_safe_path("readme.md") is True
|
|
assert is_safe_path("config.yaml") is True
|
|
|
|
def test_nested_relative_path_ok(self):
|
|
"""深层相对路径应通过."""
|
|
assert is_safe_path("a/b/c/d/e/file.md") is True
|
|
|
|
def test_dot_prefix_dir_ok(self):
|
|
"""以 . 开头的目录名(如 .config)是合法的."""
|
|
assert is_safe_path(".config/settings.yaml") is True
|
|
|
|
def test_current_dir_prefix_ok(self):
|
|
"""./ 前缀的路径应通过."""
|
|
assert is_safe_path("./docs/readme.md") is True
|
|
|
|
# -- 非法路径 --
|
|
def test_absolute_path_rejected(self):
|
|
"""绝对路径应拒绝."""
|
|
# Windows 绝对路径
|
|
assert is_safe_path("D:/Code/test.md") is False
|
|
assert is_safe_path("C:\\Windows\\system32") is False
|
|
|
|
def test_parent_dir_traversal_rejected(self):
|
|
""".. 目录穿越应拒绝."""
|
|
assert is_safe_path("../secret.txt") is False
|
|
assert is_safe_path("docs/../../../etc/passwd") is False
|
|
assert is_safe_path("foo/bar/..") is False
|
|
|
|
def test_encoded_traversal_rejected(self):
|
|
"""以 .. 开头的相对路径也被拒绝."""
|
|
assert is_safe_path("..") is False
|
|
assert is_safe_path("../..") is False
|
|
|
|
def test_windows_style_traversal_rejected(self):
|
|
"""Windows 风格路径穿越."""
|
|
assert is_safe_path("..\\..\\secret.txt") is False
|
|
|
|
# -- 边界情况 --
|
|
def test_empty_string(self):
|
|
"""空字符串."""
|
|
assert is_safe_path("") is True # normpath("") → ""
|
|
|
|
def test_dot_only(self):
|
|
"""仅 '.' 的路径."""
|
|
assert is_safe_path(".") is True # normpath(".") → ""
|