Skip to content

fix(thesis): 投资论文仓库未校验 id 的文件名边界,路径型 id 会越出 <userData>/thesis 读写删 - #303

Merged
helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-thesis-repository-id-boundary
Oct 18, 2026
Merged

helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-thesis-repository-id-boundary

Conversation

@wxrbyte

@wxrbyte wxrbyte commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

问题与修复

Closes #302

ThesisRepository / ThesisImpactRepository 把调用方给的 id 直接插进文件名(`${id}.json`、`impacts/${thesisId}.json`),而 JsonFileStore 只做 join(rootDir, file),不校验名字(json-file-store.ts:43 / :48 / :66 / :102)⇒ 路径型 id 会越出 <userData>/thesis:

调用(id = ../escaped) 修复前 修复后
ThesisRepository.save 静默写入 <userData>/escaped.json,并把键 ../escaped 记进 index.json 抛 INVALID_ARGUMENT,index.json 不生成
list() / getBySymbol() 顺着被污染的索引键读到目录外文件(length=1) length=0
delete('../vanish') 静默删除 <userData>/vanish.json 抛 INVALID_ARGUMENT,文件保留
impacts.append({ thesisId: '../escaped' }) 写到 impacts/../escaped.json,即论文正文同名文件 抛 INVALID_ARGUMENT

修复方式复用同一文件族里 research 仓库已经写明并已被测试固定的契约(packages/shared/src/research/repository.ts:21-28):

const thesisFile = (id: unknown): string => {
  if (typeof id !== 'string' || !/^[a-zA-Z0-9_-]+$/.test(id)) {
    throw createCodeError('INVALID_ARGUMENT', 'Invalid thesis id.');
  }
  return `${id}.json`;
};

save / get / delete 与 impact 的 append / list 全部改走护栏;原来的私有方法 ThesisImpactRepository.path() 由模块级 impactFile() 取代。

可达路径:preload/index.ts:278 thesis:update → kernelHost.ts:771-773 thesisUpdate()(只做 requireObject)→ thesis/service.ts:122-124 updateThesis() 原样落盘 → repository.ts 拼文件名。即 <userData>/thesis 的写入边界完全取决于 renderer 传来的 id;thesisSchema(converter.ts:15-29)也只把 id 声明为 z.string()。

可复现测试报告

  • 环境:Windows 11;Bun 1.4.2;上游 main 3a17eca6;head b1384da0
  • 新增 packages/shared/src/thesis/repository.test.ts(7 条用例)
    • 修复前(用 swap_files.py 把 repository.ts 切回 main 原样字节):1 pass / 6 fail
    • 修复后:7 pass / 0 fail
  • bun test packages/shared/src/thesis --isolate:25 pass / 0 fail(converter / repository / service 三个文件)
  • bun x tsc --noEmit -p packages/shared/tsconfig.json:exit 0
  • bun run typecheck:core / i18n / shared / ui / electron 五工作区 exit 0
  • bun test packages/shared --isolate(全量):1127 pass / 2 fail,两条均为既存问题,可在 main 原始字节下复现:
    • ResearchService > lists runs newest-first —— beforeEach/afterEach 钩子超时(6.7s),时序偶发
    • langfuse backend > does not throw when Langfuse is down — agent path keeps a diagnostic —— 把 repository.ts 还原成 main 字节后单跑 packages/shared/src/evaluation/langfuse/langfuse.test.ts 同样 12 pass / 1 fail(A/B 后已还原,md5 e70c862c 校验一致)
  • 修复前/后各跑一次同一复现脚本并对比输出:修复前 save(id="../escaped"): NO_THROW、list() length=1、<base>/vanish.json ... survived: false;修复后全部 THREW INVALID_ARGUMENT、index.json exists: false、survived: true。临时复现脚本已删除(ls | grep -c __repro = 0)
  • secret scan(照抄 pr.yml 口径,排除 node_modules / docs / *.test.ts(x) / bun.lock):命中 0
  • 未运行:完整 e2e。PR checks 工作流本次开工实测仍为 disabled_manually,新 PR 不产生 check run,故按仓库等价命令在本地补齐上述检查,不声称 CI 通过

边界与范围

  • 不改 JsonFileStore:它是有意保持极简的存储底座,文件名边界的校验属于仓库层(research 侧也是这样分工的)。
  • 不改 id 生成方式(randomUUID)与现有全部 id,均满足 /^[a-zA-Z0-9_-]+$/,无行为回归。
  • 不改 thesisSchema、不改任何 IPC 契约;thesis:update 的调用方式与错误返回形状之外的行为不变(越界 id 现在会明确报 INVALID_ARGUMENT,而不是静默写错地方)。
  • 只触碰 packages/shared/src/thesis/repository.ts 与新增的测试文件。
  • 文件级碰撞:check_collisions.py 对 79 个 open PR 求交,repository.ts / service.ts / 新测试文件均 collisions=0。

UI 变化

无可见 UI 变化。纯持久化层护栏。

本地基线的补充说明

工作树是「Git Data API 推送」流程留下的累积基线,除本 PR 外还含本轮另外几个 PR 以及我在 #270 方向上尚未合并的改动(packages/shared/src/evaluation/aggregate.ts 的 higherIsBetter 方向判定及其测试)。因此上文的全量用例数比「main + 本 PR」多;失败项与这个数量无关,两条失败均可在 main 原始字节下复现。

ThesisRepository and ThesisImpactRepository interpolated the caller-supplied
id straight into a file name, and JsonFileStore only `join()`s it onto the
store root. A path-like id (`../escaped`) therefore wrote, read, and deleted
files outside `<userData>/thesis`, and the escaped key was persisted into
`index.json` so later `list()` / `getBySymbol()` calls kept reading out of
root.

The sibling research repositories already enforce the same "ids are opaque
names, not relative paths" contract (research/repository.ts:21-28) and pin it
with a regression test. Mirror it here: reject non-`/^[a-zA-Z0-9_-]+$/` ids
with INVALID_ARGUMENT before touching the file system, and fail closed when an
index already contains a path-like id.

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独立源码审查通过;精确 HEAD focused 25 pass / 0 fail。与最新 main 的本批累计组合独立验证 1831 pass / 7 skip / 0 fail,全 workspace typecheck、i18n 均通过。相关生产变更文件高信号 secret 扫描零命中;不把这些确定性回归声称为真实模型或完整桌面验收。

@helsome
helsome merged commit 629721c into helsome:main Oct 18, 2026
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.

fix(thesis): 投资论文仓库未校验 id 的文件名边界,路径型 id 会越出 <userData>/thesis 读写删

2 participants