Skip to content

fix: scanner false positives & symlink dodge; perf: policy cache - #10

Open
liuhaolin07 wants to merge 2 commits into
mainfrom
audit/code-audit-2026-10-24
Open

liuhaolin07 wants to merge 2 commits into
mainfrom
audit/code-audit-2026-10-24

Conversation

@liuhaolin07

Copy link
Copy Markdown
Owner

代码审计结果

对全部 \u0076core 模块(middleware / policy / scanner / capabilities / audit / agent / dots_client)做了一次完整审计,修复 4 项发现:

修复

1. Scanner sk- 正则误报(功能性 bug) — security/scanner.py
原模式 sk-[a-zA-Z0-9]+ 无词边界、无长度下限,task-123、risk-2026 等普通词命中("task-123")。LLM loop 会把含用户任务的整段对话序列化后过扫描器,任务文本带 task-xxx 即导致模型调用被误判为 sensitive_data 而终止。现改为与 model/dots_client.py 脱敏正则一致的 \u005cbsk-[a-zA-Z0-9_-]{8,}\u005cb,并预编译全部模式。

2. Symlink 绕过 blocked_files(安全加固) — security/middleware.py
封锁名单原来只匹配调用方传入的原始文件名;名为 notes.txt 的软链接指向 .env 即可绕过。现在对 resolve 后的绝对路径同样做封锁名检查。

3. 策略每次调用重新读盘解析(性能) — security/policy.py
load_policy 按路径缓存,以 (st_mtime_ns, st_size) 指纹校验:实测 5.5x 提速(20000 次加载 61.4s → 11.2s),文件修改即失效重载,异常从不缓存,失败语义不变。

4. CLI 退出码不一致 — main.py
确定性 demo 被拦截时恒返回 0,与 --llm 模式(返回 2)不一致;现统一为拦截返回 2。

测试

  • 30 → 35 个测试全部通过(新增 5 个回归测试逐项覆盖修复)
  • symlink 用例在 Windows 无开发者模式时跳过,CI 的 ubuntu-latest 会完整执行
  • CLI 实测:攻击流 exit=2、正常流 exit=0

审计中记录、未改动的点

  • blocks_file 的 endswith 分支与文件名相等判断语义重复(无害,为控制 diff 未动)
  • 审计日志为单进程 append、无文件锁(当前 demo 定位够用)

Audit findings addressed:

- scanner: 'sk-' pattern had no word boundary or minimum length, so
  'task-123' / 'risk-2026' matched as leaked keys and blocked clean
  model calls (the serialized loop payload contains the user task).
  Now mirrors the dots_client redaction shape; patterns precompiled.
- middleware: check the blocked-name list against the resolved path
  as well, so a benign-named symlink cannot dodge blocked_files.
- policy: cache parsed policies per path, revalidated against
  (st_mtime_ns, st_size) — ~5.5x faster on hot loops, identical
  change-on-disk semantics, errors never cached.
- main: deterministic demo now exits 2 on block, matching --llm.

Tests: 30 -> 35 (all pass; symlink case skips only where the platform
cannot create symlinks, e.g. Windows without developer mode).
The fixture named the target real.env, but blocks_file matches
filenames exactly, so '.env' never matched and the case asserted the
wrong expectation. The target is now .env; the benign-named link is
notes.txt as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant