diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b51832c48..e971133b27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,14 @@ All notable changes to this project will be documented in this file. For commit ## v2.1.2 - **Bug Fixes**: - - Language switcher not working (#3114) (#3117) - - Viewer showing a stale image after the file changed on disk: single-file downloads are now revalidated by the browser (#3132) - **Notes**: - Removed "Editor quick save" setting (`userDefaults.fileViewer.quickSave`), the save button is now always present in the editor toolbar. + **Bug Fixes**: + - fix(webdav): OPTIONS now advertises only methods actually supported and permitted for the user; the DAV header reports class 1 only since LOCK/UNLOCK are not enabled (#3110) + - Language switcher not working (#3114) (#3117) + - Viewer showing a stale image after the file changed on disk: single-file downloads are now revalidated by the browser (#3132) + ## v2.1.1 Note: this release also contains all changes from v2.0.10-beta changelog @@ -43,7 +44,6 @@ Note: this release also contains all changes from v2.0.10-beta changelog - fix(backend): skip denied paths in multi-file downloads, 404 when none remain (#3100) @hippi345 - fix: persist the user scope path chosen in the edit dialog (#3065) @mvanhorn - ## v2.1.0 **Security**: diff --git a/backend/internal/web/webdav.go b/backend/internal/web/webdav.go index 4016773345..6c110c7d1a 100644 --- a/backend/internal/web/webdav.go +++ b/backend/internal/web/webdav.go @@ -51,11 +51,11 @@ func (f *fileInfoWrapper) mode() os.FileMode { // filteredFileSystem wraps a webdav.FileSystem and filters directory listings using FileInfoFaster type filteredFileSystem struct { - fs webdav.FileSystem - source string - user *users.User - filePerms users.SourceFilePermissions - httpReq *http.Request + fs webdav.FileSystem + source string + user *users.User + filePerms users.SourceFilePermissions + httpReq *http.Request // Cache FileInfoFaster results per path to avoid redundant calls within the same request fileInfoCache map[string]*iteminfo.ExtendedFileInfo } @@ -626,6 +626,10 @@ func webDAVHandler(w http.ResponseWriter, r *http.Request, d *Context) (int, err httpReq: r, } + if r.Method == http.MethodOptions { + return writeWebDAVOptions(w, r, filteredFS, prefix, filePerms) + } + wd := &webdav.Handler{ Prefix: prefix, FileSystem: filteredFS, @@ -657,6 +661,46 @@ func webDAVHandler(w http.ResponseWriter, r *http.Request, d *Context) (int, err return 200, nil // errors and responses (XML-formatted) are handled by webdav handler } +// writeWebDAVOptions answers OPTIONS directly so the Allow/DAV headers reflect +// the methods this server actually supports and the user's permissions, instead +// of the webdav library's hardcoded advertisement (which includes LOCK etc.). +func writeWebDAVOptions(w http.ResponseWriter, r *http.Request, ffs *filteredFileSystem, prefix string, perms users.SourceFilePermissions) (int, error) { + rest, ok := strings.CutPrefix(r.URL.Path, prefix) + if !ok || (rest != "" && rest[0] != '/') { + return http.StatusNotFound, fmt.Errorf("request path %q outside webdav prefix %q", r.URL.Path, prefix) + } + requestPath := path.Clean("/" + rest) + + var candidates []string + fi, err := ffs.Stat(r.Context(), requestPath) + if err != nil && !errors.Is(err, os.ErrNotExist) { + if errors.Is(err, os.ErrPermission) { + return http.StatusForbidden, err + } + return http.StatusInternalServerError, err + } + if err != nil { + candidates = []string{http.MethodPut, "MKCOL"} + } else if fi.IsDir() { + candidates = []string{"PROPFIND", http.MethodDelete, "COPY", "MOVE"} + } else { + candidates = []string{http.MethodGet, http.MethodHead, "PROPFIND", http.MethodPut, http.MethodDelete, "COPY", "MOVE"} + } + + allowed := []string{http.MethodOptions} + for _, m := range candidates { + if _, err := webDAVMethodPermission(m, perms); err == nil { + allowed = append(allowed, m) + } + } + + w.Header().Set("Allow", strings.Join(allowed, ", ")) + w.Header().Set("DAV", "1") + w.Header().Set("MS-Author-Via", "DAV") + w.WriteHeader(http.StatusOK) + return http.StatusOK, nil +} + func webDAVMethodPermission(method string, perms users.SourceFilePermissions) (int, error) { switch method { case "PROPFIND", http.MethodOptions: diff --git a/backend/internal/web/webdav_test.go b/backend/internal/web/webdav_test.go index 086783fd15..ef9e5f5b56 100644 --- a/backend/internal/web/webdav_test.go +++ b/backend/internal/web/webdav_test.go @@ -874,7 +874,6 @@ func TestWebDAV_CopyPreservesModTime(t *testing.T) { } } - // TestWebDAV_ListingsIncludeHidden verifies that dotfiles/dotdirs are listed // over WebDAV even when the user's showHidden UI preference is false, and that // hidden paths are writable (needed for two-way sync clients like rclone). @@ -994,3 +993,156 @@ func initTestIndex(t *testing.T, name, path string) { // For WebDAV tests, indices are already initialized in setupWebDAVTestEnv t.Helper() } + +// Test that OPTIONS advertises only the methods actually supported and +// permitted for the user, rather than the webdav library's hardcoded list. +func TestWebDAV_OPTIONS(t *testing.T) { + source1Path, _ := setupWebDAVTestEnv(t) + + fullAccessUser := &users.User{ + ID: 1, + FrontendUser: users.FrontendUser{ + Username: "optionsfull", + }, + BackendScopes: []users.BackendScope{ + {Path: source1Path, Scope: "/"}, + }, + BackendSourcePermissions: webDAVPermsForPaths(true, true, true, true, source1Path), + Version: users.SourcePermissionsMigrationVersion, + } + applyBackendSourcePerms(fullAccessUser, fullAccessUser.BackendSourcePermissions) + + readOnlyUser := &users.User{ + ID: 2, + FrontendUser: users.FrontendUser{ + Username: "optionsreadonly", + }, + BackendScopes: []users.BackendScope{ + {Path: source1Path, Scope: "/"}, + }, + BackendSourcePermissions: webDAVPermsForPaths(false, false, false, false, source1Path), + Version: users.SourcePermissionsMigrationVersion, + } + applyBackendSourcePerms(readOnlyUser, readOnlyUser.BackendSourcePermissions) + + initTestIndex(t, "source1", source1Path) + + // Methods the handler can never allow (they hit the default: -> 403 branch + // of webDAVMethodPermission) must never be advertised. + neverAllowed := []string{"LOCK", "UNLOCK", "PROPPATCH", "POST"} + + doOptions := func(t *testing.T, user *users.User, path string) map[string]bool { + t.Helper() + req := httptest.NewRequest(http.MethodOptions, "/dav/source1"+path, nil) + req.SetPathValue("source", "source1") + req.SetPathValue("path", path) + + w := httptest.NewRecorder() + status, err := webDAVHandler(w, req, &requestContext{User: user}) + if err != nil { + t.Fatalf("webDAVHandler OPTIONS %s: %v", path, err) + } + if w.Code != http.StatusOK { + t.Fatalf("OPTIONS %s: expected status 200, got %d (status=%d)", path, w.Code, status) + } + if dav := w.Header().Get("DAV"); dav != "1" { + t.Errorf("OPTIONS %s: expected DAV \"1\", got %q", path, dav) + } + if via := w.Header().Get("MS-Author-Via"); via != "DAV" { + t.Errorf("OPTIONS %s: expected MS-Author-Via \"DAV\", got %q", path, via) + } + + allowed := make(map[string]bool) + for _, m := range strings.Split(w.Header().Get("Allow"), ", ") { + if m != "" { + allowed[m] = true + } + } + if !allowed[http.MethodOptions] { + t.Errorf("OPTIONS %s: Allow %q missing OPTIONS", path, w.Header().Get("Allow")) + } + for _, m := range neverAllowed { + if allowed[m] { + t.Errorf("OPTIONS %s: Allow %q advertises unsupported method %s", path, w.Header().Get("Allow"), m) + } + } + // Consistency: everything advertised must pass webDAVMethodPermission + // for this user, so Allow never lies about what would succeed. + perms := user.BackendSourcePermissions[source1Path] + for m := range allowed { + if _, permErr := webDAVMethodPermission(m, perms); permErr != nil { + t.Errorf("OPTIONS %s: Allow advertises %s but webDAVMethodPermission denies it: %v", path, m, permErr) + } + } + return allowed + } + + t.Run("directory advertises only supported methods", func(t *testing.T) { + allowed := doOptions(t, fullAccessUser, "/public/") + for _, m := range []string{"PROPFIND", http.MethodDelete, "COPY", "MOVE"} { + if !allowed[m] { + t.Errorf("expected %s in Allow for directory, got %v", m, allowed) + } + } + // GET/PUT/MKCOL don't apply to a collection URL itself + for _, m := range []string{http.MethodGet, http.MethodHead, http.MethodPut, "MKCOL"} { + if allowed[m] { + t.Errorf("unexpected %s in Allow for directory, got %v", m, allowed) + } + } + }) + + t.Run("file advertises read and write methods", func(t *testing.T) { + allowed := doOptions(t, fullAccessUser, "/public/readme.txt") + for _, m := range []string{http.MethodGet, http.MethodHead, "PROPFIND", http.MethodPut, http.MethodDelete, "COPY", "MOVE"} { + if !allowed[m] { + t.Errorf("expected %s in Allow for file, got %v", m, allowed) + } + } + }) + + t.Run("missing path advertises creation methods", func(t *testing.T) { + allowed := doOptions(t, fullAccessUser, "/public/newfile.txt") + for _, m := range []string{http.MethodPut, "MKCOL"} { + if !allowed[m] { + t.Errorf("expected %s in Allow for missing path, got %v", m, allowed) + } + } + }) + + t.Run("restricted path does not advertise creation methods", func(t *testing.T) { + path := "/public/not-viewable.txt" + req := httptest.NewRequest(http.MethodOptions, "/dav/source1"+path, nil) + req.SetPathValue("source", "source1") + req.SetPathValue("path", path) + w := httptest.NewRecorder() + status, err := webDAVHandler(w, req, &requestContext{User: fullAccessUser}) + if err == nil { + t.Fatalf("expected error for restricted path, got status %d with Allow %q", status, w.Header().Get("Allow")) + } + if allow := w.Header().Get("Allow"); strings.Contains(allow, http.MethodPut) || strings.Contains(allow, "MKCOL") { + t.Errorf("restricted path advertises creation methods: %q", allow) + } + }) + + t.Run("read-only user is not told writes are allowed", func(t *testing.T) { + allowed := doOptions(t, readOnlyUser, "/public/readme.txt") + for _, m := range []string{http.MethodPut, http.MethodDelete, "MOVE", "COPY", "MKCOL"} { + if allowed[m] { + t.Errorf("read-only user unexpectedly sees %s in Allow: %v", m, allowed) + } + } + if !allowed["PROPFIND"] { + t.Errorf("expected PROPFIND in Allow for read-only user, got %v", allowed) + } + }) + + t.Run("read-only user sees no read methods without download", func(t *testing.T) { + allowed := doOptions(t, readOnlyUser, "/public/readme.txt") + for _, m := range []string{http.MethodGet, http.MethodHead} { + if allowed[m] { + t.Errorf("read-only (no download) user unexpectedly sees %s in Allow: %v", m, allowed) + } + } + }) +}