| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 19:08:08 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 19:12:23 UTC |
| parent | 2d3dee6c787adc4574445a0183cdb5cd433fd28e |
| config/gofer.yaml | +5 | -4 |
| server/dir.go | +10 | -19 |
| server/write.go | +46 | -24 |
| server/write_test.go | +187 | -11 |
| test/test.sh | +17 | -13 |
diff --git a/config/gofer.yaml b/config/gofer.yaml index cf7a07a..e36daed 100644 --- a/config/gofer.yaml +++ b/config/gofer.yaml @@ -115,10 +115,11 @@ http: # If-Unmodified-Since is honored: a stale value returns 412. # WARNING: enabling PUT without "auth:" exposes the directory to # anonymous writes. - # NOTE: symlinks under the served directory are followed (same as - # for GET). A symlink-directory under the root therefore allows PUT - # to write outside the root. Don't place such symlinks if you don't - # want that. + # NOTE: PUT and DELETE are confined to the served directory. Symlinks + # under it are followed only if they are relative and stay within + # it; otherwise the request fails with a 500 error. Note this is + # stricter than GET, which follows all symlinks. The served directory + # itself (and the per-user directories) can be a symlink. #put: # "/pub/": true diff --git a/server/dir.go b/server/dir.go index 446cbf9..3c8ef34 100644 --- a/server/dir.go +++ b/server/dir.go @@ -12,9 +12,10 @@ import ( type FileSystem struct { fs http.FileSystem - // On-disk root of the served directory. Used by the PUT/DELETE handlers - // to resolve request paths to filesystem paths. Empty if the underlying - // http.FileSystem is not backed by a local directory. + // On-disk root of the served directory. The PUT/DELETE handlers open it + // on each request with os.OpenRoot, which confines them to it. Opening it + // each time means they see the same directory as GET if the path is + // created or replaced after startup. root string opts config.DirOpts @@ -70,23 +71,13 @@ func DeleteEnabled(opts *config.DirOpts, name string) bool { return prefixMatch(opts.Delete, name) } -// localPath resolves a URL path to an absolute on-disk path under fs.root. -// It returns an error if writes are not supported (fs.root is unset) or if -// the resulting path would escape the root. -func (fs *FileSystem) localPath(name string) (string, error) { - if fs.root == "" { - return "", os.ErrPermission +// relPath converts a clean URL path into a path relative to the root, as +// needed by os.Root. +func relPath(name string) string { + if name == "/" { + return "." } - absRoot, err := filepath.Abs(fs.root) - if err != nil { - return "", err - } - abs := filepath.Join(absRoot, filepath.FromSlash(name)) - rel, err := filepath.Rel(absRoot, abs) - if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { - return "", os.ErrPermission - } - return abs, nil + return strings.TrimPrefix(name, "/") } func (fs *FileSystem) excluded(name string) bool { diff --git a/server/write.go b/server/write.go index ae22d1c..35b6aec 100644 --- a/server/write.go +++ b/server/write.go @@ -1,11 +1,12 @@ package server import ( + "crypto/rand" + "errors" "io" "net/http" "os" "path" - "path/filepath" "time" "blitiri.com.ar/go/gofer/trace" @@ -19,6 +20,7 @@ import ( // PUT onto an existing directory returns 409 Conflict. // Parent directories are created automatically. // If-Unmodified-Since is honored when the target file already exists. +// Symlinks are only followed if they stay within the root (see os.Root). func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { tr, _ := trace.FromContext(r.Context()) @@ -34,17 +36,27 @@ func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { return } - abs, err := fs.localPath(name) + // The root itself is created if needed, like any other parent directory. + if err := os.MkdirAll(fs.root, 0755); err != nil { + tr.Printf("mkdirall %q: %v", fs.root, err) + toHTTPError(w, err) + return + } + + root, err := os.OpenRoot(fs.root) if err != nil { - tr.Printf("localPath %q: %v", name, err) + tr.Printf("openroot %q: %v", fs.root, err) toHTTPError(w, err) return } + defer root.Close() + rel := relPath(name) existed := false - if fi, err := os.Stat(abs); err == nil { + fi, err := root.Stat(rel) + if err == nil { if fi.IsDir() { - tr.Printf("PUT onto directory %q", abs) + tr.Printf("PUT onto directory %q", name) http.Error(w, "409 Conflict (is a directory)", http.StatusConflict) return @@ -55,26 +67,31 @@ func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { http.StatusPreconditionFailed) return } + } else if !errors.Is(err, os.ErrNotExist) { + // Includes symlinks that point outside of the root. + tr.Printf("stat %q: %v", name, err) + toHTTPError(w, err) + return } - if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil { - tr.Printf("mkdirall %q: %v", filepath.Dir(abs), err) + if err := root.MkdirAll(path.Dir(rel), 0755); err != nil { + tr.Printf("mkdirall %q: %v", path.Dir(rel), err) toHTTPError(w, err) return } // Atomic write: temp file in target directory, then rename. - tmp, err := os.CreateTemp(filepath.Dir(abs), ".gofer-put-*.tmp") + tmpName := path.Join(path.Dir(rel), ".gofer-put-"+rand.Text()+".tmp") + tmp, err := root.OpenFile(tmpName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600) if err != nil { - tr.Printf("createtemp: %v", err) + tr.Printf("create temp file %q: %v", tmpName, err) toHTTPError(w, err) return } - tmpName := tmp.Name() cleanup := true defer func() { if cleanup { - os.Remove(tmpName) + root.Remove(tmpName) } }() @@ -89,8 +106,8 @@ func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { toHTTPError(w, err) return } - if err := os.Rename(tmpName, abs); err != nil { - tr.Printf("rename %q -> %q: %v", tmpName, abs, err) + if err := root.Rename(tmpName, rel); err != nil { + tr.Printf("rename %q -> %q: %v", tmpName, rel, err) toHTTPError(w, err) return } @@ -99,16 +116,16 @@ func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { // Return Last-Modified reflecting the stored file, so clients have the // validator to use for subsequent If-Unmodified-Since requests without an // extra round trip (RFC 9110 ยง9.3.4). The body is stored untransformed. - if fi, err := os.Stat(abs); err == nil { + if fi, err := root.Stat(rel); err == nil { w.Header().Set("Last-Modified", fi.ModTime().UTC().Format(http.TimeFormat)) } if existed { - tr.Printf("PUT %q: replaced", abs) + tr.Printf("PUT %q: replaced", name) w.WriteHeader(http.StatusNoContent) } else { - tr.Printf("PUT %q: created", abs) + tr.Printf("PUT %q: created", name) w.WriteHeader(http.StatusCreated) } } @@ -118,6 +135,9 @@ func handlePut(fs *FileSystem, w http.ResponseWriter, r *http.Request) { // Required diropts: delete: { "<prefix>": true } for the request path. // Files only: DELETE on a directory returns 409 Conflict. // If-Unmodified-Since is honored. +// Symlinks in the parent directories are only followed if they stay within the +// root (see os.Root). A symlink as the target is removed, regardless of where +// it points to. func handleDelete(fs *FileSystem, w http.ResponseWriter, r *http.Request) { tr, _ := trace.FromContext(r.Context()) @@ -133,21 +153,23 @@ func handleDelete(fs *FileSystem, w http.ResponseWriter, r *http.Request) { return } - abs, err := fs.localPath(name) + root, err := os.OpenRoot(fs.root) if err != nil { - tr.Printf("localPath %q: %v", name, err) + tr.Printf("openroot %q: %v", fs.root, err) toHTTPError(w, err) return } + defer root.Close() + rel := relPath(name) - fi, err := os.Lstat(abs) + fi, err := root.Lstat(rel) if err != nil { - tr.Printf("lstat %q: %v", abs, err) + tr.Printf("lstat %q: %v", name, err) toHTTPError(w, err) return } if fi.IsDir() { - tr.Printf("DELETE on directory %q", abs) + tr.Printf("DELETE on directory %q", name) http.Error(w, "409 Conflict (is a directory)", http.StatusConflict) return } @@ -157,13 +179,13 @@ func handleDelete(fs *FileSystem, w http.ResponseWriter, r *http.Request) { return } - if err := os.Remove(abs); err != nil { - tr.Printf("remove %q: %v", abs, err) + if err := root.Remove(rel); err != nil { + tr.Printf("remove %q: %v", name, err) toHTTPError(w, err) return } - tr.Printf("DELETE %q", abs) + tr.Printf("DELETE %q", name) w.WriteHeader(http.StatusNoContent) } diff --git a/server/write_test.go b/server/write_test.go index 2dcd2fe..8ebde08 100644 --- a/server/write_test.go +++ b/server/write_test.go @@ -1,12 +1,14 @@ package server import ( + "errors" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "testing" + "testing/iotest" "blitiri.com.ar/go/gofer/config" "blitiri.com.ar/go/gofer/trace" @@ -27,6 +29,16 @@ func put(fs *FileSystem, path, body, ius string) *http.Response { return w.Result() } +// del issues a DELETE through handleDelete and returns the response. +func del(fs *FileSystem, path string) *http.Response { + req := httptest.NewRequest("DELETE", "http://unused"+path, nil) + tr := trace.New("test", path) + req = req.WithContext(trace.NewContext(req.Context(), tr)) + w := httptest.NewRecorder() + handleDelete(fs, w, req) + return w.Result() +} + // TestPutLastModified checks that PUT returns a Last-Modified header, and that // feeding it straight back as If-Unmodified-Since satisfies the precondition // (rather than failing it). This is what lets a client chain writes without an @@ -72,15 +84,6 @@ func TestWriteSiblingPrefix(t *testing.T) { Delete: map[string]bool{"/pub/": true}, }) - del := func(path string) *http.Response { - req := httptest.NewRequest("DELETE", "http://unused"+path, nil) - tr := trace.New("test", path) - req = req.WithContext(trace.NewContext(req.Context(), tr)) - w := httptest.NewRecorder() - handleDelete(fs, w, req) - return w.Result() - } - // Siblings are rejected, and the existing file is left untouched. resp := put(fs, "/public/secret.txt", "overwritten", "") if resp.StatusCode != http.StatusMethodNotAllowed { @@ -90,7 +93,7 @@ func TestWriteSiblingPrefix(t *testing.T) { if resp.StatusCode != http.StatusMethodNotAllowed { t.Errorf("PUT sibling file: status = %d, want 405", resp.StatusCode) } - resp = del("/public/secret.txt") + resp = del(fs, "/public/secret.txt") if resp.StatusCode != http.StatusMethodNotAllowed { t.Errorf("DELETE sibling: status = %d, want 405", resp.StatusCode) } @@ -106,8 +109,181 @@ func TestWriteSiblingPrefix(t *testing.T) { if resp.StatusCode != http.StatusCreated { t.Errorf("PUT in prefix: status = %d, want 201", resp.StatusCode) } - resp = del("/pub/f.txt") + resp = del(fs, "/pub/f.txt") if resp.StatusCode != http.StatusNoContent { t.Errorf("DELETE in prefix: status = %d, want 204", resp.StatusCode) } } + +// TestWriteSymlinks checks that PUT and DELETE follow symlinks that stay within +// the root, and refuse to go through the ones that don't. +func TestWriteSymlinks(t *testing.T) { + base := t.TempDir() + root := filepath.Join(base, "root") + outside := filepath.Join(base, "outside") + target := filepath.Join(outside, "target") + for _, d := range []string{filepath.Join(root, "sub"), outside} { + if err := os.MkdirAll(d, 0o755); err != nil { + t.Fatal(err) + } + } + if err := os.WriteFile(target, []byte("outside"), 0o644); err != nil { + t.Fatal(err) + } + links := map[string]string{ + "out-rel": "../outside", + "out-abs": outside, + "out-file": "../outside/target", + "in-rel": "sub", + // Absolute symlinks are always refused, even if they stay within. + "in-abs": filepath.Join(root, "sub"), + } + for name, dst := range links { + if err := os.Symlink(dst, filepath.Join(root, name)); err != nil { + t.Fatal(err) + } + } + + fs := NewFS(root, http.Dir(root), config.DirOpts{ + Put: map[string]bool{"/": true}, + Delete: map[string]bool{"/": true}, + }) + + // Going through the symlinks. + cases := []struct { + do func(*FileSystem, string) *http.Response + path string + want int + }{ + {putX, "/out-rel/new", http.StatusInternalServerError}, + {putX, "/out-abs/new", http.StatusInternalServerError}, + {putX, "/out-rel/target", http.StatusInternalServerError}, + {putX, "/out-rel/a/b", http.StatusInternalServerError}, + {putX, "/out-rel", http.StatusInternalServerError}, + {putX, "/out-file", http.StatusInternalServerError}, + {putX, "/in-abs/new", http.StatusInternalServerError}, + {del, "/out-rel/target", http.StatusInternalServerError}, + {del, "/out-abs/target", http.StatusInternalServerError}, + {del, "/in-abs/new", http.StatusInternalServerError}, + + {putX, "/in-rel/new", http.StatusCreated}, + {putX, "/in-rel/new", http.StatusNoContent}, + {del, "/in-rel/new", http.StatusNoContent}, + } + for _, c := range cases { + if resp := c.do(fs, c.path); resp.StatusCode != c.want { + t.Errorf("%q: status = %d, want %d", + c.path, resp.StatusCode, c.want) + } + } + for name, dst := range links { + got, err := os.Readlink(filepath.Join(root, name)) + if err != nil || got != dst { + t.Errorf("symlink %q changed: %q, %v", name, got, err) + } + } + + // DELETE on a symlink removes the link itself, wherever it points to. + for name := range links { + if resp := del(fs, "/"+name); resp.StatusCode != http.StatusNoContent { + t.Errorf("DELETE %q: status = %d, want 204", name, resp.StatusCode) + } + if _, err := os.Lstat(filepath.Join(root, name)); err == nil { + t.Errorf("symlink %q still exists", name) + } + } + + // Nothing outside the root has changed. + if got, err := os.ReadFile(target); err != nil || string(got) != "outside" { + t.Errorf("target file changed: %q, %v", got, err) + } + if des, err := os.ReadDir(outside); err != nil || len(des) != 1 { + t.Errorf("unexpected entries outside the root: %v, %v", des, err) + } +} + +// putX is put for tables, with a fixed body and no preconditions. +func putX(fs *FileSystem, path string) *http.Response { + return put(fs, path, "x", "") +} + +// TestWriteRootSymlink checks that the root itself can be a symlink, even if +// it points to somewhere else. This also applies to per-user directories. +func TestWriteRootSymlink(t *testing.T) { + base := t.TempDir() + dst := filepath.Join(base, "dst") + link := filepath.Join(base, "link") + if err := os.Mkdir(dst, 0o755); err != nil { + t.Fatal(err) + } + if err := os.Symlink(dst, link); err != nil { + t.Fatal(err) + } + + fs := NewFS(link, http.Dir(link), config.DirOpts{ + Put: map[string]bool{"/": true}, + Delete: map[string]bool{"/": true}, + }) + + if resp := putX(fs, "/f"); resp.StatusCode != http.StatusCreated { + t.Errorf("PUT: status = %d, want 201", resp.StatusCode) + } + if _, err := os.Stat(filepath.Join(dst, "f")); err != nil { + t.Errorf("file not in the symlink target: %v", err) + } + if resp := del(fs, "/f"); resp.StatusCode != http.StatusNoContent { + t.Errorf("DELETE: status = %d, want 204", resp.StatusCode) + } +} + +// TestPutMissingRoot checks that the root is created by PUT if it doesn't +// exist, and that DELETE doesn't need it. +func TestPutMissingRoot(t *testing.T) { + root := filepath.Join(t.TempDir(), "a", "b") + fs := NewFS(root, http.Dir(root), config.DirOpts{ + Put: map[string]bool{"/": true}, + Delete: map[string]bool{"/": true}, + }) + + if resp := del(fs, "/f"); resp.StatusCode != http.StatusNotFound { + t.Errorf("DELETE: status = %d, want 404", resp.StatusCode) + } + if _, err := os.Stat(root); err == nil { + t.Errorf("DELETE created the root") + } + + if resp := putX(fs, "/d/f"); resp.StatusCode != http.StatusCreated { + t.Errorf("PUT: status = %d, want 201", resp.StatusCode) + } + if _, err := os.Stat(filepath.Join(root, "d", "f")); err != nil { + t.Errorf("file not created: %v", err) + } +} + +// TestPutBodyError checks that if reading the body fails, the target file is +// left untouched and the temporary file is removed. +func TestPutBodyError(t *testing.T) { + root := t.TempDir() + if err := os.WriteFile(filepath.Join(root, "f"), []byte("old"), 0o644); err != nil { + t.Fatal(err) + } + fs := NewFS(root, http.Dir(root), config.DirOpts{ + Put: map[string]bool{"/": true}, + }) + + req := httptest.NewRequest("PUT", "http://unused/f", + iotest.ErrReader(errors.New("test error"))) + req = req.WithContext(trace.NewContext(req.Context(), trace.New("test", "/f"))) + w := httptest.NewRecorder() + handlePut(fs, w, req) + if w.Code != http.StatusInternalServerError { + t.Errorf("status = %d, want 500", w.Code) + } + + if got, err := os.ReadFile(filepath.Join(root, "f")); err != nil || string(got) != "old" { + t.Errorf("target file changed: %q, %v", got, err) + } + if des, err := os.ReadDir(root); err != nil || len(des) != 1 { + t.Errorf("unexpected directory entries: %v, %v", des, err) + } +} diff --git a/test/test.sh b/test/test.sh index 636eb4a..1ae178d 100755 --- a/test/test.sh +++ b/test/test.sh @@ -21,9 +21,10 @@ echo -n "original" > .writedir/existing touch -d "2020-01-01 00:00:00 UTC" .writedir/existing # Symlink under the writable root pointing at a sibling directory (outside -# root). Used by the security tests to verify documented symlink-follow -# behaviour and that DELETE only removes the link, not its target. -ln -sfn "$(pwd)/.symlink-target" .writedir/linked +# root). Used by the security tests to verify that PUT and DELETE don't go +# through it, and that DELETE only removes the link, not its target. +echo -n "original" > .symlink-target/target +ln -sfn "../.symlink-target" .writedir/linked # Per-user directory tests start from a clean slate; the per-user # subdirectories are created on the first PUT. @@ -388,24 +389,27 @@ exp "$BE/writable/%73ecret.txt" -method DELETE -status 404 # exact status, only that nothing was written. exp "$BE/writable/foo%00bar" -method PUT -reqbody "x" -statuslist 400,500 -# Symlink follow (documented behaviour): PUT through a symlink-directory -# inside the root writes to the symlink target. Don't place such symlinks -# unless you want this. +# Symlinks that lead outside the root (documented behaviour): PUT and DELETE +# fail, and don't touch the target. GET still follows them. exp $BE/writable/linked/via-symlink -method PUT \ - -reqbody "via-symlink" -status 201 -[ -f .symlink-target/via-symlink ] || \ - { echo "symlink follow broken"; exit 1; } + -reqbody "via-symlink" -status 500 +[ ! -e .symlink-target/via-symlink ] || \ + { echo "PUT went through a symlink outside the root"; exit 1; } +exp $BE/writable/linked/target -method DELETE -status 500 +[ -f .symlink-target/target ] || \ + { echo "DELETE went through a symlink outside the root"; exit 1; } +exp $BE/writable/linked/target -body "original" # DELETE on a symlink removes the link itself, not what it points to. exp $BE/writable/linked -method DELETE -status 204 [ ! -e .writedir/linked ] || \ { echo "symlink should be gone"; exit 1; } -[ -f .symlink-target/via-symlink ] || \ +[ -f .symlink-target/target ] || \ { echo "symlink target should still exist"; exit 1; } -# Final invariant: every PUT we issued landed under .writedir/ or -# .symlink-target/. Walk the test directory for stray files that match the -# names we tried to write through traversal. +# Final invariant: every PUT we issued landed under .writedir/. Walk the test +# directory for stray files that match the names we tried to write through +# traversal. for f in escaped escaped-encoded bypass double-enc collapsed; do for d in . .. /tmp; do if [ -e "$d/$f" ]; then