From 610e0b09e93e4b1940b9d50c9b0374ccab02a0d5 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 25 Jul 2026 08:10:49 +0200 Subject: [PATCH] test(http): cover download-permission gate on the checksum branch Refs GHSA-7whw-q6gh-xr59 --- http/resource_checksum_test.go | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 http/resource_checksum_test.go diff --git a/http/resource_checksum_test.go b/http/resource_checksum_test.go new file mode 100644 index 00000000..67f5fab5 --- /dev/null +++ b/http/resource_checksum_test.go @@ -0,0 +1,57 @@ +package fbhttp + +import ( + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/filebrowser/filebrowser/v2/settings" + "github.com/filebrowser/filebrowser/v2/users" +) + +// The ?checksum= branch reads the whole file to compute a digest, so it must be +// gated behind Perm.Download like the other read paths. A user without download +// permission must not obtain a digest of a file they cannot download. +func TestResourceChecksumRequiresDownloadPermission(t *testing.T) { + root := t.TempDir() + userScope := filepath.Join(root, "user") + if err := os.MkdirAll(userScope, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(userScope, "secret.txt"), []byte("classified"), 0o644); err != nil { + t.Fatal(err) + } + key := []byte("test-signing-key") + + get := func(t *testing.T, perm users.Permissions) *httptest.ResponseRecorder { + st := scopedUserStorage(t, userScope, perm, key) + req, _ := http.NewRequest(http.MethodGet, "/secret.txt?checksum=sha256", http.NoBody) + req.Header.Set("X-Auth", signToken(t, perm, key)) + rec := httptest.NewRecorder() + handle(resourceGetHandler, "", st, &settings.Server{}).ServeHTTP(rec, req) + return rec + } + + t.Run("denied without download permission", func(t *testing.T) { + rec := get(t, users.Permissions{}) + if rec.Code != http.StatusAccepted { + t.Fatalf("expected 202, got %d body=%q", rec.Code, rec.Body.String()) + } + if strings.Contains(strings.ToLower(rec.Body.String()), "checksum") { + t.Fatalf("digest leaked without download permission: %q", rec.Body.String()) + } + }) + + t.Run("allowed with download permission", func(t *testing.T) { + rec := get(t, users.Permissions{Download: true}) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "checksums") { + t.Fatalf("expected a checksum in the response, got %q", rec.Body.String()) + } + }) +}