mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
Eliminate the TOCTOU check-then-use window between wsResolveConfined and filesystem writes in wsWrite, wsMkdir and wsUpload (GHSA-3p3q). After wsResolveConfined canonicalizes the path within s.m.dir, open the workspace root via os.OpenRoot(cp.realRoot) and perform all directory creation, symlink verification and file writes using root-relative paths and root.OpenFile instead of host-path os.MkdirAll, os.WriteFile and saveUpload. Walking components with root.Lstat also blocks post-resolution symlink swaps targeting denied platform subtrees such as traffic/_ca or archives.
180 lines
6.4 KiB
Go
180 lines
6.4 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestWorkspaceWritePathTOCTOUSymlinkSwap(t *testing.T) {
|
|
swapParentToSymlink := func(t *testing.T, workDir, subName, target string) func() {
|
|
t.Helper()
|
|
swapped := false
|
|
return func() {
|
|
if swapped {
|
|
return
|
|
}
|
|
swapped = true
|
|
subPath := filepath.Join(workDir, subName)
|
|
if err := os.RemoveAll(subPath); err != nil {
|
|
t.Errorf("remove %s: %v", subPath, err)
|
|
return
|
|
}
|
|
if err := os.Symlink(target, subPath); err != nil {
|
|
t.Errorf("symlink %s -> %s: %v", subPath, target, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
t.Run("wsWrite_parent_swapped_outside_root", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
workDir := filepath.Join(base, "work")
|
|
outsideDir := filepath.Join(base, "outside")
|
|
if err := os.MkdirAll(filepath.Join(workDir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(outsideDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := newTestWorkspaceServer(workDir)
|
|
wsBeforeWriteHook = swapParentToSymlink(t, workDir, "sub", outsideDir)
|
|
t.Cleanup(func() { wsBeforeWriteHook = nil })
|
|
|
|
rr := doJSONReq(t, http.MethodPost, "/api/workspace/write",
|
|
map[string]string{"path": "sub/escaped.txt", "content": "PWNED"}, s.wsWrite)
|
|
if rr.Code == http.StatusOK {
|
|
t.Fatalf("wsWrite succeeded across TOCTOU parent symlink swap: status=%d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(outsideDir, "escaped.txt")); !os.IsNotExist(err) {
|
|
t.Fatalf("wsWrite wrote file outside workspace root: %s", filepath.Join(outsideDir, "escaped.txt"))
|
|
}
|
|
})
|
|
|
|
t.Run("wsMkdir_parent_swapped_outside_root", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
workDir := filepath.Join(base, "work")
|
|
outsideDir := filepath.Join(base, "outside")
|
|
if err := os.MkdirAll(filepath.Join(workDir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(outsideDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := newTestWorkspaceServer(workDir)
|
|
wsBeforeWriteHook = swapParentToSymlink(t, workDir, "sub", outsideDir)
|
|
t.Cleanup(func() { wsBeforeWriteHook = nil })
|
|
|
|
rr := doJSONReq(t, http.MethodPost, "/api/workspace/mkdir",
|
|
map[string]string{"path": "sub/escaped-dir/nested"}, s.wsMkdir)
|
|
if rr.Code == http.StatusOK {
|
|
t.Fatalf("wsMkdir succeeded across TOCTOU parent symlink swap: status=%d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(outsideDir, "escaped-dir")); !os.IsNotExist(err) {
|
|
t.Fatalf("wsMkdir created directory outside workspace root: %s", filepath.Join(outsideDir, "escaped-dir"))
|
|
}
|
|
})
|
|
|
|
t.Run("wsUpload_parent_swapped_outside_root", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
workDir := filepath.Join(base, "work")
|
|
outsideDir := filepath.Join(base, "outside")
|
|
if err := os.MkdirAll(filepath.Join(workDir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(outsideDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := newTestWorkspaceServer(workDir)
|
|
wsBeforeWriteHook = swapParentToSymlink(t, workDir, "sub", outsideDir)
|
|
t.Cleanup(func() { wsBeforeWriteHook = nil })
|
|
|
|
rr := doUploadReq(t, s, "sub", "escaped-upload.txt", "PWNED_UPLOAD")
|
|
if rr.Code == http.StatusOK {
|
|
t.Fatalf("wsUpload succeeded across TOCTOU parent symlink swap: status=%d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(outsideDir, "escaped-upload.txt")); !os.IsNotExist(err) {
|
|
t.Fatalf("wsUpload wrote file outside workspace root: %s", filepath.Join(outsideDir, "escaped-upload.txt"))
|
|
}
|
|
})
|
|
|
|
t.Run("wsWrite_parent_swapped_to_denied_subtree", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
workDir := filepath.Join(base, "work")
|
|
caDir := filepath.Join(workDir, "traffic", "_ca")
|
|
if err := os.MkdirAll(filepath.Join(workDir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(caDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := newTestWorkspaceServer(workDir)
|
|
wsBeforeWriteHook = swapParentToSymlink(t, workDir, "sub", caDir)
|
|
t.Cleanup(func() { wsBeforeWriteHook = nil })
|
|
|
|
rr := doJSONReq(t, http.MethodPost, "/api/workspace/write",
|
|
map[string]string{"path": "sub/ca-overwrite.pem", "content": "EVIL_KEY"}, s.wsWrite)
|
|
if rr.Code == http.StatusOK {
|
|
t.Fatalf("wsWrite succeeded across TOCTOU swap to denied traffic/_ca: status=%d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(caDir, "ca-overwrite.pem")); !os.IsNotExist(err) {
|
|
t.Fatalf("wsWrite wrote into denied traffic/_ca subtree")
|
|
}
|
|
})
|
|
|
|
t.Run("wsUpload_parent_swapped_to_denied_subtree", func(t *testing.T) {
|
|
base := t.TempDir()
|
|
workDir := filepath.Join(base, "work")
|
|
caDir := filepath.Join(workDir, "traffic", "_ca")
|
|
if err := os.MkdirAll(filepath.Join(workDir, "sub"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(caDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
s := newTestWorkspaceServer(workDir)
|
|
wsBeforeWriteHook = swapParentToSymlink(t, workDir, "sub", caDir)
|
|
t.Cleanup(func() { wsBeforeWriteHook = nil })
|
|
|
|
rr := doUploadReq(t, s, "sub", "ca-upload.pem", "EVIL_KEY")
|
|
if rr.Code == http.StatusOK {
|
|
t.Fatalf("wsUpload succeeded across TOCTOU swap to denied traffic/_ca: status=%d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
if _, err := os.Lstat(filepath.Join(caDir, "ca-upload.pem")); !os.IsNotExist(err) {
|
|
t.Fatalf("wsUpload wrote into denied traffic/_ca subtree")
|
|
}
|
|
})
|
|
|
|
t.Run("normal_write_mkdir_upload_succeed_inside_root", func(t *testing.T) {
|
|
workDir := t.TempDir()
|
|
s := newTestWorkspaceServer(workDir)
|
|
|
|
rr := doJSONReq(t, http.MethodPost, "/api/workspace/mkdir",
|
|
map[string]string{"path": "a/b"}, s.wsMkdir)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("normal wsMkdir failed: %d (%s)", rr.Code, rr.Body.String())
|
|
}
|
|
|
|
rr = doJSONReq(t, http.MethodPost, "/api/workspace/write",
|
|
map[string]string{"path": "a/b/hello.txt", "content": "HELLO"}, s.wsWrite)
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("normal wsWrite failed: %d (%s)", rr.Code, rr.Body.String())
|
|
}
|
|
if got, err := os.ReadFile(filepath.Join(workDir, "a", "b", "hello.txt")); err != nil || string(got) != "HELLO" {
|
|
t.Fatalf("wsWrite file = %q (err=%v), want HELLO", got, err)
|
|
}
|
|
|
|
rr = doUploadReq(t, s, "a/b", "up.txt", "UPLOADED")
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("normal wsUpload failed: %d (%s)", rr.Code, rr.Body.String())
|
|
}
|
|
if got, err := os.ReadFile(filepath.Join(workDir, "a", "b", "up.txt")); err != nil || string(got) != "UPLOADED" {
|
|
t.Fatalf("wsUpload file = %q (err=%v), want UPLOADED", got, err)
|
|
}
|
|
})
|
|
}
|