fix: CLI 路径检查改用 is_safe_cli_path(允许绝对路径),更新文档

This commit is contained in:
2026-07-10 15:45:22 +08:00
parent ec3898ecb7
commit f7801b45a7
4 changed files with 40 additions and 23 deletions
+4 -4
View File
@@ -17,7 +17,7 @@ if sys.stdout.encoding != "utf-8":
sys.path.insert(0, str(Path(__file__).parent.parent))
from src.core.config import DEFAULT_CONFIG_PATH
from src.core.security import is_path_within_workspace
from src.core.security import is_safe_cli_path
from src.server.deps import get_state, get_default_collection
app = typer.Typer(
@@ -82,7 +82,7 @@ def ingest(
if file_paths:
total = 0
for fp in file_paths:
if not is_path_within_workspace(fp):
if not is_safe_cli_path(fp):
typer.echo(f"[SKIP] 不安全的路径: {fp}", err=True)
continue
# 支持通配符 (shell 展开或 Python glob)
@@ -90,7 +90,7 @@ def ingest(
if "*" in fp or "?" in fp:
matches = _glob.glob(fp, recursive=True)
for m in matches:
if not is_path_within_workspace(m):
if not is_safe_cli_path(m):
typer.echo(f"[SKIP] 不安全的路径: {m}", err=True)
continue
c = ingestor.ingest_file(m)
@@ -117,7 +117,7 @@ def ingest_dir(
collection: CollectionOpt = None,
):
_init_config(config)
if not is_path_within_workspace(dir_path):
if not is_safe_cli_path(dir_path):
typer.echo(f"错误: 不安全的路径 — {dir_path}", err=True)
raise typer.Exit(code=1)
state = get_state()
+12
View File
@@ -46,3 +46,15 @@ def is_path_within_workspace(path_str: str) -> bool:
except ValueError:
return False
return common == cwd
def is_safe_cli_path(path_str: str) -> bool:
"""CLI 路径安全检查 — 仅拒绝 .. 穿越组件,允许绝对路径和任意目录。
CLI 是本地工具,用户有权限访问系统中任意路径。
与 is_path_within_workspaceAPI 用,绑定当前目录)相比更宽松。
"""
parts = path_str.replace("\\", "/").split("/")
if ".." in parts:
return False
return True