Repository navigation
Conversation
为什么:1d M3 误删事故的根因是删除/移动/解包各处自行实现文件操作, 没有一处收口 lstat/realpath/根内约束;PackageArchive 名单大小写敏感、 无 zip bomb 上限,验签/inspect/unpack 同一路径多次打开有 TOCTOU。 - SafeFs(src/Infra/File):登记根 realpath 定型、根内限定大小写归一、 lstat 逐级拒软链(删除/移动永不跟随)、同目录临时文件 rename 原子写、 显式权限位、失败回收并上报、编码绕过(双重编码/NUL)与反斜杠拒绝 - LocalFileStorage 写/删/判存改走 SafeFs(上传链收口,键随机不变) - PackageArchive:名单比较大小写归一;条目数/解压总大小/压缩比三条 上限任一命中即拒;逐条流式写出不整读入内存;解包失败回收暂存目录 - PackageExecutor:zip 先流式拷入 iso-<hex> 独占隔离目录,验签/inspect/ unpack 只操作副本(TOCTOU 消解);跨根改名与树删除一律走 SafeFs 守卫入口;清理失败如实进 problems/notes 不吞掉 - PackageJournal:journal 原子写 + 0600;rollback 删除/挪回经守卫入口 - 测试:SafeFsTest 29 例(穿越/软链/编码/根外/原子性/权限位); PackageArchive/Executor 补大小写变体、zip bomb 三上限、反斜杠、 双重编码、失败回收、端到端 zip bomb/软链拒装等负向用例 依据 PR #126 file-upload.md 第八节 SafeFs 需求输入 1-15 与 packageops-supply-chain.md PKG-T1 缺口口径(10g M-03 / 1-8)。 Generated-by: Devin (swe-2-1-high)
验收返工:zip 中央目录 uncompressed size 可伪造,原实现只信声明值, 声明说谎时 zip bomb 可越限写盘(验收证据:声明 100 实际写出 600MiB)。 - SafeFs::writeStream 增加 maxBytes 实际字节硬切断:再写一块即越限时 中止、不落位、回收临时文件,按 SAFEFS_SIZE_EXCEEDED 拒绝 - PackageArchive 解包改为按实际解压字节执法:总大小剩余额度传入 SafeFs 硬切断;压缩比按累计实际字节对照全包压缩尺寸递增复核; 单条写完复核声明/实际字节,不一致按异常包拒绝 - 测试:PackageKit::forgeEntrySize 机械伪造 zip 头尺寸字段; 新增总大小硬切断/压缩比复核/声明实际不符/缺省上限四例伪造反例; FailMidwayStream 重写 testWriteFailureCleansTemp,真实覆盖 「临时文件已创建后中途失败」回收路径;补硬切断正负向用例
第二轮验收返工,修三项阻断: 1. comp_size 同为攻击者可控字段(deflate 流自带结束标记,改大不影响 解压,验收实测伪造后绕过压缩比闸端到端装成)。压缩比口径定为 archive amplification:实际解出总字节 / zip 文件真实字节数 (filesize),预检与解包递增复核同口径,zip 头部任何尺寸字段 不再作安全分母;padding 真实增大输入体积时按口径允许稀释 2. 根 manifest.json 计入同一 max_total_bytes 预算:预算先于写出检查、 writeBytes 增加 maxBytes 直通硬切断并返回字节数、声明/实际复核、 每次实际字节增加后按 amplification 递增复核 3. FailMidwayStream 注释按命名规范重写(不加豁免) 测试:forged comp_size 反例(生产默认上限即拒)+ 端到端拒装、 padding 稀释辨别性对照(comp 分母实现必拒)、伪造 manifest 尺寸 计入预算拒装+清理、manifest 声明/实际复核、合法预算内包不误伤
7 tasks done
supdger
added a commit
that referenced
this pull request
Nov 29, 2026
Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
威胁模型 M-03(#126
server/docs/security/file-upload.md第八节 15 条需求输入)要求统一安全文件操作。注意:#126 本身未合入本分支,仅作为需求输入读取;本分支基线是 origin/main(#81 合入提交)。实现摘要
SafeFs(src/Infra/File/SafeFs.php+SafeFsException):登记根 realpath 定型、根内限定大小写归一、lstat 逐级拒软链(删树/移动不跟随)、同目录临时文件 rename 原子写、显式权限位、编码绕过(双重编码/NUL)与反斜杠拒绝、writeStream/writeBytes可选 maxBytes 实际字节硬切断(SAFEFS_SIZE_EXCEEDED)LocalFileStorage写/删/判存改走 SafeFsPackageArchive/PackageExecutor/PackageJournal解包/暂存/备份/卸载/恢复全部收口;zip 先拷入 iso- 独占隔离目录再验签/inspect/unpack(TOCTOU 消解);zip bomb 三上限(条目数/总大小/压缩比),压缩比口径 archive amplification = 实际解出总字节 / zip 文件真实字节数,zip 头部尺寸字段不作安全分母;根 manifest 计入同一预算;声明/实际逐条复核;失败回收暂存目录并上报反例与正例
../绝对/盘符/反斜杠/NUL/百分号/双重编码)、软链逃出与跟随误删(含 1d M3 事故形态复现)、zip bomb 三上限、伪造 uncompressed size、伪造 compressed size、伪造 manifest 尺寸——全部有负向测试且被挡100%-off.txt、padding 稀释口径对照)验证(本地实跑)
vendor/bin/phpunit --testsuite unit:OK (799 tests, 7044 assertions)vendor/bin/phpstan analyse --memory-limit=1G:level max 无错误tools/pre-merge.sh --fast:exit 0 全部门禁通过(含前端三道、契约一致性、deptrac、基线只减不增)范围声明
未改前端、未动 composer.lock、未建库、未合并任何东西、未改 #126 文档、未新增豁免/baseline。后续项(不在本 PR):文件操作静态门禁最小规则、导出/清理任务收口 SafeFs。
开发模型 swe-2-1-high;经两轮独立验收返工后通过最终独立验收。