diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e357ad..8c6ced3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to this project are documented in this file. The format is b - **Hook `range` counts against the memory budget**: ranging over a body, slice, string, or map charges the iteration before the loop. A body larger than the hook budget now stops with the memory limit instead of allocating one element at a time with no check. - **Chat upload name search stops**: choosing `name`, `name-1`, `name-2`, … now returns an error when the filesystem rejects the path or after 10000 collisions, instead of looping while `Stat` keeps failing. - **Uploads do not follow a symlink**: chat and workspace uploads `Lstat` the destination and refuse to write when it is already a symlink. A dangling link is no longer treated as a free name. +- **Hardening follow-up for the upload and capture fixes**: existing spill blob directories and files are tightened on startup, hook `range` charges 32 bytes per pair, and uploads open with `O_NOFOLLOW` on Unix. The Windows upload path keeps the `Lstat` check. - **Docker Compose database passwords with URL-special characters**: `docker-compose.yml` no longer pastes `POSTGRES_PASSWORD` into `ARTEX_PG_DSN`. The server builds the URL from `ARTEX_PG_HOST`, `ARTEX_PG_PORT`, `ARTEX_PG_USER`, `ARTEX_PG_PASSWORD`, `ARTEX_PG_DBNAME` and `ARTEX_PG_SSLMODE`, percent-encoding the user and password, so `/`, `?`, `%`, `@`, `:` and spaces connect. An explicit `ARTEX_PG_DSN` still wins and is not rewritten. `ARTEX_PG_PASSWORD` is removed from the environment with the other startup secrets (#450). ## [0.7.0] - 2026-11-21 diff --git a/server/upload_open_unix.go b/server/upload_open_unix.go new file mode 100644 index 0000000..a6a6334 --- /dev/null +++ b/server/upload_open_unix.go @@ -0,0 +1,14 @@ +//go:build !windows + +package server + +import ( + "os" + "syscall" +) + +// openUploadFileNoFollow opens dest for writing without following a symlink at +// the final path component, closing the TOCTOU window after refuseUploadSymlink. +func openUploadFileNoFollow(dest string) (*os.File, error) { + return os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|syscall.O_NOFOLLOW, 0o644) +} diff --git a/server/upload_open_windows.go b/server/upload_open_windows.go new file mode 100644 index 0000000..3c3a4f1 --- /dev/null +++ b/server/upload_open_windows.go @@ -0,0 +1,9 @@ +//go:build windows + +package server + +import "os" + +func openUploadFileNoFollow(dest string) (*os.File, error) { + return os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) +} diff --git a/server/upload_symlink_test.go b/server/upload_symlink_test.go index 8371fbd..5f6a410 100644 --- a/server/upload_symlink_test.go +++ b/server/upload_symlink_test.go @@ -3,6 +3,7 @@ package server import ( "os" "path/filepath" + "runtime" "testing" ) @@ -31,3 +32,44 @@ func TestRefuseUploadSymlink(t *testing.T) { t.Fatal(err) } } + +func TestOpenUploadFileNoFollowRejectsSymlink(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("O_NOFOLLOW is Unix-only") + } + dir := t.TempDir() + target := filepath.Join(dir, "secret") + if err := os.WriteFile(target, []byte("keep"), 0o644); err != nil { + t.Fatal(err) + } + link := filepath.Join(dir, "link") + if err := os.Symlink(target, link); err != nil { + t.Skipf("symlink: %v", err) + } + if f, err := openUploadFileNoFollow(link); err == nil { + _ = f.Close() + t.Fatal("openUploadFileNoFollow accepted a symlink") + } + body, err := os.ReadFile(target) + if err != nil { + t.Fatal(err) + } + if string(body) != "keep" { + t.Fatalf("target changed to %q", body) + } +} + +func TestUniqueUploadPathSkipsDanglingSymlink(t *testing.T) { + dir := t.TempDir() + dangling := filepath.Join(dir, "note.txt") + if err := os.Symlink(filepath.Join(dir, "nonexistent"), dangling); err != nil { + t.Skipf("symlink: %v", err) + } + got, err := uniqueUploadPath(dir, "note.txt") + if err != nil { + t.Fatal(err) + } + if filepath.Base(got) != "note-1.txt" { + t.Fatalf("got %s, want note-1.txt", got) + } +} diff --git a/server/workspace.go b/server/workspace.go index 40237e7..430d92b 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -349,7 +349,7 @@ func saveUpload(hdr *multipart.FileHeader, dest string) error { return err } defer src.Close() - out, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) + out, err := openUploadFileNoFollow(dest) if err != nil { return err } diff --git a/traffic/blob_perm_test.go b/traffic/blob_perm_test.go index 4fc5a78..0371bd4 100644 --- a/traffic/blob_perm_test.go +++ b/traffic/blob_perm_test.go @@ -36,3 +36,51 @@ func TestSpillBlobIsOwnerOnly(t *testing.T) { t.Fatalf("blob dir mode %o, want 0700", dirInfo.Mode().Perm()) } } + +func TestOpenTightensExistingBlobPerms(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("POSIX file permissions not enforced on Windows") + } + dir := t.TempDir() + blobsRoot := filepath.Join(dir, "_blobs") + shaDir := filepath.Join(blobsRoot, "sha256") + bucket := filepath.Join(shaDir, "ab") + if err := os.MkdirAll(bucket, 0o755); err != nil { + t.Fatal(err) + } + for _, d := range []string{blobsRoot, shaDir, bucket} { + if err := os.Chmod(d, 0o755); err != nil { + t.Fatal(err) + } + } + blob := filepath.Join(bucket, "abcdef.bin") + if err := os.WriteFile(blob, []byte("secret-body"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Chmod(blob, 0o644); err != nil { + t.Fatal(err) + } + + tr, err := Open(dir, "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer tr.Close() + + for _, d := range []string{blobsRoot, shaDir, bucket} { + info, err := os.Stat(d) + if err != nil { + t.Fatal(err) + } + if info.Mode().Perm() != 0o700 { + t.Fatalf("%s mode %o, want 0700", d, info.Mode().Perm()) + } + } + info, err := os.Stat(blob) + if err != nil { + t.Fatal(err) + } + if info.Mode().Perm() != 0o600 { + t.Fatalf("%s mode %o, want 0600", blob, info.Mode().Perm()) + } +} diff --git a/traffic/hookvm/eval.go b/traffic/hookvm/eval.go index f4b22de..26b5d07 100644 --- a/traffic/hookvm/eval.go +++ b/traffic/hookvm/eval.go @@ -330,30 +330,30 @@ func (e *vm) execRange(s *ast.RangeStmt, sc *env) (flowSignal, any) { var items []kvPair switch x := e.eval(s.X, sc).(type) { case []byte: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) for i, v := range x { items = append(items, kvPair{int64(i), int64(v)}) } case []any: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) for i, v := range x { items = append(items, kvPair{int64(i), v}) } case []string: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) for i, v := range x { items = append(items, kvPair{int64(i), v}) } case string: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) for i, r := range x { items = append(items, kvPair{int64(i), int64(r)}) } case map[string]any: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) items = sortedMapPairs(x) case map[string][]string: - e.alloc(int64(len(x)) * 16) + e.alloc(int64(len(x)) * 32) items = sortedMapPairs(x) case nil: return flowNone, nil diff --git a/traffic/traffic.go b/traffic/traffic.go index 8b3ae51..1ff4053 100644 --- a/traffic/traffic.go +++ b/traffic/traffic.go @@ -295,11 +295,16 @@ type Traffic struct { // Open initializes the traffic tree, blob store and SQLite index under dir. func Open(dir, addr string) (*Traffic, error) { - for _, d := range []string{dir, filepath.Join(dir, "_index"), filepath.Join(dir, "_blobs"), filepath.Join(dir, "_ca")} { + for _, d := range []string{dir, filepath.Join(dir, "_index"), filepath.Join(dir, "_ca")} { if err := os.MkdirAll(d, 0o755); err != nil { return nil, err } } + blobsRoot := filepath.Join(dir, "_blobs") + if err := os.MkdirAll(blobsRoot, 0o700); err != nil { + return nil, err + } + tightenBlobPerms(blobsRoot) // busy_timeout is a per-connection setting, so it belongs in the DSN rather // than in a one-off Exec: the pool opens connections on demand, and an Exec // only configures whichever one happened to serve it — leaving every other @@ -847,6 +852,38 @@ type storedBody struct { // reader can identify them without fetching the blob. Either way, text bodies // are handed to the full-text index in full (up to maxIndexBody) — indexing is // independent of where the bytes end up. +// tightenBlobPerms restricts existing spilled blob directories to 0700 and +// regular blob files to 0600 on startup so pre-existing 0755/0644 artifacts +// from earlier versions do not stay world-readable. +func tightenBlobPerms(root string) { + _ = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return nil + } + if d.Type()&os.ModeSymlink != 0 { + return nil + } + info, err := d.Info() + if err != nil { + return nil + } + if d.IsDir() { + if info.Mode().Perm() != 0o700 { + if err := os.Chmod(path, 0o700); err != nil { + log.Printf("[traffic] failed to restrict blob directory %s: %v", path, err) + } + } + return nil + } + if info.Mode().IsRegular() && info.Mode().Perm() != 0o600 { + if err := os.Chmod(path, 0o600); err != nil { + log.Printf("[traffic] failed to restrict blob %s: %v", path, err) + } + } + return nil + }) +} + func (t *Traffic) spill(body []byte, contentType string) storedBody { return t.spillAt(body, contentType, maxInlineBody) }