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

Đang mở
#433 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@fx-kurumi đang làm issue này rồi.

Từ ngày 13/11/2026.

  • #435 của @fx-kurumi — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
20/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

现状问题

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/。
Ngôn ngữ chính
Go
Star
141
Fork
28
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
113

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Tencent/LoopForge

Tất cả issue của Tencent/LoopForge

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.