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

Aperta
#433 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@fx-kurumi ci sta già lavorando.

Dal 13/11/2026.

  • #435 di @fx-kurumi — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
20/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
go
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

现状问题

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/。
Lingua principale
Go
Stelle
141
Fork
28
Merge medio
1g 15h
PR unite (30g)
113

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Tencent/LoopForge

Tutte le issue di Tencent/LoopForge

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.