【架构优化】从 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 摘要。