【架构优化】从 runtime.WriteWorkspaceFile 中拆出路径安全校验 resolveWorkspaceEditPath 与新建分支 createWorkspaceFile

未關閉
#433 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@fx-kurumi 已經在處理了。

開始於 2026年11月13日。

  • #435 來自 @fx-kurumi —— 未關閉

評估

難度
3/5
預估耗時
1-2 天
新手友好度
20/100
Issue 類型
重構
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
go
領域
backend

研究方向

The work is in internal/runtime/workspace_edit.go, where WriteWorkspaceFile mixes content checks, path validation (absolute paths, .., hidden files, per-component Lstat symlink checks), the O_EXCL create-if-missing branch, and the versioned atomic replace. Read the function, then workspace_edit_test.go, to see current behaviour before extracting resolveWorkspaceEditPath and createWorkspaceFile without changing error text or check order. Done means go test ./internal/runtime/ passes with new tests for both helpers. Linked PR #435 already covers this change, so check it before starting.

由索引模型根據 Issue 內容生成。

描述

现状问题

internal/runtime/workspace_edit.go 的 WriteWorkspaceFile 约 93 行,一个函数里同时做了四件事:

  1. 内容校验(1 MB 上限、UTF-8、无 NUL);
  2. 相对路径规范化与安全校验(拒绝绝对路径/..、隐藏文件、逐级 Lstat 拒绝符号链接);
  3. expected == "missing" 时以 O_EXCL 新建文件(失败回滚删除);
  4. 已有文件的版本比对 + 临时文件写入后 rename 原子替换。

路径安全校验和新建分支混在主流程里,阅读和单独测试都不方便。

重构方案

只在同一文件内拆函数,不改逻辑:

  • 抽出 resolveWorkspaceEditPath(root, path, expected string) (string, error):返回规范化后的相对路径,包含原有的路径/隐藏文件/符号链接校验;
  • 抽出 createWorkspaceFile(base *os.Root, rel, content string) error:原 O_EXCL 新建 + Sync + 失败删除逻辑;
  • WriteWorkspaceFile 签名、错误文案、判断顺序保持不变。

影响范围

仅 internal/runtime/workspace_edit.go,对外函数 WriteWorkspaceFile 签名与行为不变,调用方(httpapi、runner)无需改动。

验证方式

  • 新增单元测试覆盖 resolveWorkspaceEditPath(合法路径、..、绝对路径、隐藏文件、符号链接、missing 时允许末级不存在)和 createWorkspaceFile(新建成功、已存在时拒绝);
  • 原有 workspace_edit_test.go 用例全部通过;
  • gofmt、go vet ./internal/runtime/、go test ./internal/runtime/。
主要語言
Go
星號
141
分支
28
平均合併
1 天 15 小時
30 天內合併 PR
113

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Tencent/LoopForge 的其他 Issue

查看 Tencent/LoopForge 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。