From 37d71388eac63f72f270b20b4c87779b8ff6b351 Mon Sep 17 00:00:00 2001 From: ssenerg Date: Sun, 25 Oct 2026 12:55:42 +0330 Subject: [PATCH] Fix what the review of WebDAV found - A MOVE or COPY that replaces something no longer deletes it first: a file replaces a file in one step, as a PUT does, and anything else is set aside until the move or copy worked, and put back if it failed. With the trash off, a failed move used to lose the destination. A replaced file no longer goes to the trash either, so apps that save through a temporary file no longer fill the trash and the quota with every old version; a replaced folder still goes there. - On file systems that find names in any case, a MOVE that changes only a name's case renames it, where it used to put the file in the trash; so does the site's rename, which refused. Nested moves are judged by folded paths. - Replacing a file takes what it replaced off its home's usage, instead of counting the whole home and the trash again before the next write. - PUT, DELETE, MOVE, COPY and PROPPATCH honor If-Match and If-Unmodified-Since. - Locks belong to their users, who alone may refresh or end them; each user may hold 1,000, and an owner may be 1 KiB. Rights and the file system are checked before the lock table is held, and PROPFIND looks at it once. A lock of a free name makes an empty file (RFC 4918). - macOS's ._ files are read to the end rather than cut off at 64 KiB. - PROPPATCH of a file's time takes the right to overwrite it. - X-OC-Mtime: accepted only when the time was kept. - WebDAV's 405s list WebDAV's methods. - App-password sign-ins write last_used_at at most once a minute. - Shown times are formatted in one place. Co-Authored-By: Claude Opus 5.5 --- README.md | 7 +- internal/auth/postgres.go | 23 ++- internal/server/admin.go | 3 +- internal/server/case.go | 9 + internal/server/dav.go | 379 ++++++++++++++++++++++++++-------- internal/server/dav_test.go | 293 +++++++++++++++++++++++++- internal/server/file.go | 28 +++ internal/server/listing.go | 11 +- internal/server/manage.go | 16 +- internal/server/middleware.go | 6 +- internal/server/move.go | 91 ++++++-- internal/server/move_test.go | 4 +- internal/server/preview.go | 3 +- internal/server/quota.go | 32 ++- internal/server/resumable.go | 9 +- internal/server/share.go | 14 +- internal/server/sync.go | 9 +- internal/server/trash.go | 8 +- internal/server/upload.go | 39 ++-- 19 files changed, 816 insertions(+), 168 deletions(-) diff --git a/README.md b/README.md index 14b1c5b..aa4462c 100644 --- a/README.md +++ b/README.md @@ -435,7 +435,12 @@ much room a home has left (RFC 4331). Limits: dropped. - Other properties than modification times are refused. Locks are advisory: they keep other WebDAV clients that ask from taking the same - lock, and nothing else. + lock, and nothing else. Each user may hold 1,000; only they may refresh + or end theirs. +- A file that a PUT, MOVE or COPY replaces is gone, as on the site; a + folder that a MOVE or COPY replaces goes to the trash, once the move or + copy worked. `If-Match` and `If-Unmodified-Since` make changes depend on + the ETag or time a client saw. - WebDAV sends each file in one request, where the browser sends large ones in pieces: behind a proxy that limits the size of requests (Cloudflare: 100 MB on its Free and Pro plans), larger files cannot be diff --git a/internal/auth/postgres.go b/internal/auth/postgres.go index 0cc2b34..6b1c55d 100644 --- a/internal/auth/postgres.go +++ b/internal/auth/postgres.go @@ -136,15 +136,26 @@ func (s *PgStore) DeleteAppPassword(ctx context.Context, userID, id int64) error } func (s *PgStore) AppPasswordUser(ctx context.Context, username string, tokenHash []byte) (*User, error) { - u, err := scanUser(s.db.QueryRow(ctx, `SELECT u.id, u.username, u.password_hash, u.must_change_password, u.created_at, u.home_quota - FROM app_passwords a JOIN users u ON u.id = a.user_id - WHERE a.token_hash = $1 AND u.username = $2`, tokenHash, username)) + var ( + u User + lastUsed *time.Time + ) + err := s.db.QueryRow(ctx, `SELECT u.id, u.username, u.password_hash, u.must_change_password, u.created_at, u.home_quota, + a.last_used_at FROM app_passwords a JOIN users u ON u.id = a.user_id WHERE a.token_hash = $1 AND u.username = $2`, + tokenHash, username).Scan(&u.ID, &u.Username, &u.PasswordHash, &u.MustChangePassword, &u.CreatedAt, &u.HomeQuota, &lastUsed) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } if err != nil { return nil, err } - _, err = s.db.Exec(ctx, `UPDATE app_passwords SET last_used_at = now() - WHERE token_hash = $1 AND (last_used_at IS NULL OR last_used_at < now() - interval '1 minute')`, tokenHash) - return u, err + // Sync apps send many requests: only the first in a minute writes. + if lastUsed == nil || time.Since(*lastUsed) >= time.Minute { + if _, err := s.db.Exec(ctx, "UPDATE app_passwords SET last_used_at = now() WHERE token_hash = $1", tokenHash); err != nil { + return nil, err + } + } + return &u, nil } func (s *PgStore) SetHomeQuota(ctx context.Context, userID int64, quota *int64) error { diff --git a/internal/server/admin.go b/internal/server/admin.go index 0d73450..1fd247f 100644 --- a/internal/server/admin.go +++ b/internal/server/admin.go @@ -5,7 +5,6 @@ import ( "errors" "slices" "strings" - "time" "github.com/gofiber/fiber/v3" "go.uber.org/zap" @@ -267,7 +266,7 @@ func (s *Server) renderAdmin(c fiber.Ctx, status int, tab, notice, failure strin for _, u := range users { data.Users = append(data.Users, adminUser{ Name: u.Username, Role: u.Role, MustChange: u.MustChangePassword, Self: u.Username == me, - Added: u.CreatedAt.UTC().Format("Jan 2, 2006"), AddedISO: u.CreatedAt.UTC().Format(time.RFC3339), + Added: u.CreatedAt.UTC().Format("Jan 2, 2006"), AddedISO: isoTime(u.CreatedAt), }) if q := u.HomeQuota; q != nil { data.Users[len(data.Users)-1].Quota = cmp.Or(quotaText(*q), "none") diff --git a/internal/server/case.go b/internal/server/case.go index 6184329..ab4f4b8 100644 --- a/internal/server/case.go +++ b/internal/server/case.go @@ -113,3 +113,12 @@ func swapCase(name string) string { return r }, name) } + +// folded is p as the file system compares names: as spelled, or folded as +// checkNames found. +func (s *Server) folded(p string) string { + if s.fold == nil { + return p + } + return s.fold(p) +} diff --git a/internal/server/dav.go b/internal/server/dav.go index fb536af..fbb2df7 100644 --- a/internal/server/dav.go +++ b/internal/server/dav.go @@ -9,6 +9,7 @@ import ( "encoding/xml" "errors" "io" + "io/fs" "mime" "net/http" "net/url" @@ -87,6 +88,9 @@ func (s *Server) dav(c fiber.Ctx) error { return fiber.ErrMethodNotAllowed } +// inDav reports whether the request path p is WebDAV's. +func inDav(p string) bool { return p == davPrefix || strings.HasPrefix(p, davPrefix+"/") } + // davPath returns the clean URL path that the request path p, under // davPrefix, stands for. func davPath(p string) (string, bool) { @@ -173,6 +177,10 @@ func (s *Server) davBody(c fiber.Ctx) ([]byte, error) { return data, nil } +// appleDoubleLimit bounds what is read of a file appleDouble names before +// the connection is closed instead. +const appleDoubleLimit = 64 << 20 + // appleDouble reports whether name is one of the files that macOS keeps // beside others, which goftp, hiding names that start with a dot, takes // and drops. @@ -196,6 +204,8 @@ type davResource struct { size int64 mod time.Time real string // URL path where it really lives; "" for the way to a home + // locks describes the locks on it (see lockDiscoveries). + locks string } // propfind is the body of a PROPFIND request: allprop, propname, or the @@ -264,6 +274,7 @@ func (s *Server) davPropfind(c fiber.Ctx, urlPath string) error { res = append(res, s.wayHome(next)) } + s.lockDiscoveries(res) var b bytes.Buffer b.WriteString(xml.Header + ``) for _, r := range res { @@ -362,7 +373,7 @@ func (s *Server) davProp(c fiber.Ctx, r davResource, name xml.Name) (string, boo v = "" + "" case "lockdiscovery": - v = s.lockDiscovery(r.path) + v = r.locks case "quota-used-bytes", "quota-available-bytes": used, avail, ok := s.davQuota(r) if !ok || name.Local == "quota-used-bytes" && used < 0 { @@ -452,7 +463,10 @@ func (s *Server) davGet(c fiber.Ctx, urlPath string) error { func (s *Server) davPut(c fiber.Ctx, urlPath string) error { if appleDouble(path.Base(urlPath)) { - _, _ = s.davBody(c) + // Read to the end, so that the client is not cut off. + body := s.requestBody(c) + _, _ = io.CopyN(io.Discard, body, appleDoubleLimit) + s.finishBody(c, body) return c.SendStatus(fiber.StatusCreated) } if urlPath == "/" { @@ -474,9 +488,10 @@ func (s *Server) davPut(c fiber.Ctx, urlPath string) error { } if info, err := s.root.Stat(rootName(urlPath)); err == nil { c.Set(fiber.HeaderETag, etagOf(info.ModTime(), info.Size())) - } - if keep { - c.Set("X-OC-Mtime", "accepted") + // Said only if kept, as far as the file system keeps times. + if keep && info.ModTime().Sub(mtime).Abs() < time.Second { + c.Set("X-OC-Mtime", "accepted") + } } if created { return c.SendStatus(fiber.StatusCreated) @@ -503,6 +518,9 @@ func (s *Server) davDelete(c fiber.Ctx, urlPath string) error { if appleDouble(name) { return c.SendStatus(fiber.StatusNoContent) } + if err := s.davPreconditions(c, urlPath); err != nil { + return err + } if _, err := s.remove(c, dirPath, name, false); err != nil { return err } @@ -538,8 +556,10 @@ func (s *Server) davMkcol(c fiber.Ctx, urlPath string) error { } // davCopyMove copies or moves the entry at urlPath to the Destination -// header's path, replacing what is there (into the trash) unless -// "Overwrite: F" says not to. +// header's path, replacing what is there unless "Overwrite: F" says not +// to. A file replaces a file in one step; anything else is set aside until +// it is replaced (see replacing), so that a failed MOVE or COPY loses +// nothing. func (s *Server) davCopyMove(c fiber.Ctx, urlPath string, move bool) error { dst, ok := davDestination(c.Get("Destination")) switch { @@ -547,10 +567,20 @@ func (s *Server) davCopyMove(c fiber.Ctx, urlPath string, move bool) error { return explain(fiber.ErrBadRequest, "Say where it goes, in a Destination header under "+davPrefix+"/.") case urlPath == "/" || dst == "/" || dst == urlPath || hidden(dst): return fiber.ErrForbidden - case within(urlPath, dst) || within(dst, urlPath): - // Replacing a folder the entry is in would trash the entry too. + } + // Where the file system finds names in any case, a MOVE may spell a + // name anew; anything else to the same entry, or to or from what holds + // it, would replace the entry with itself. + respell := s.folded(dst) == s.folded(urlPath) + switch { + case respell && !move: + return fiber.ErrForbidden + case !respell && (within(s.folded(urlPath), s.folded(dst)) || within(s.folded(dst), s.folded(urlPath))): return explain(fiber.ErrConflict, "An entry cannot go where it is, or into itself.") } + if err := s.davPreconditions(c, urlPath); err != nil { + return err + } overwrite := !strings.EqualFold(strings.TrimSpace(c.Get("Overwrite")), "F") depth := c.Get("Depth") allow := s.visitorRules(c) @@ -562,43 +592,50 @@ func (s *Server) davCopyMove(c fiber.Ctx, urlPath string, move bool) error { if info.IsDir() && depth != "" && depth != "infinity" && (move || depth != "0") { return fiber.ErrBadRequest } - replaced := false - if g, _, _, err := s.lookup(dst, allow); err == nil { - _ = g.Close() - if !overwrite { - return fiber.ErrPreconditionFailed + srcDir, srcName := path.Split(urlPath) + dstDir, dstName := path.Split(dst) + do := func(replace bool) error { + var err error + switch { + case move: + _, err = s.moveEntry(c, srcDir, srcName, path.Clean(dstDir), dstName, replace) + case info.IsDir() && depth == "0": + err = s.makeFolder(c, dstDir, dstName) + default: + _, err = s.copyEntry(c, srcDir, srcName, path.Clean(dstDir), dstName, replace) } - // Checked before what is there goes to the trash, so that it does - // not go for nothing; the move or copy checks everything again. - may, err := s.mayEither(c, auth.ActWrite, dst) - if err == nil && may && move { - may, err = s.mayEither(c, auth.ActDelete, urlPath) + if statusOf(err) == fiber.StatusNotFound { + // The folder it would go into is missing. + return fiber.ErrConflict } - if err != nil { - return err - } else if !may { - return s.deny(c) - } - dir, name := path.Split(dst) - if _, err := s.remove(c, dir, name, false); err != nil { - return err + return err + } + var existing fs.FileInfo + if !respell { + if g, gi, _, err := s.lookup(dst, allow); err == nil { + _ = g.Close() + existing = gi } - s.dropLocks(dst) - replaced = true } - srcDir, srcName := path.Split(urlPath) - dstDir, dstName := path.Split(dst) switch { - case move: - _, err = s.moveEntry(c, srcDir, srcName, path.Clean(dstDir), dstName) - case info.IsDir() && depth == "0": - err = s.makeFolder(c, dstDir, dstName) + case existing == nil: + err = do(false) + case !overwrite: + return fiber.ErrPreconditionFailed + case existing.Mode().IsRegular() && info.Mode().IsRegular(): + err = do(true) default: - _, err = s.copyEntry(c, srcDir, srcName, path.Clean(dstDir), dstName) - } - if statusOf(err) == fiber.StatusNotFound { - // The folder it would go into is missing. - return fiber.ErrConflict + // Checked before anything is set aside. + may, merr := s.mayEither(c, auth.ActWrite, dst) + if merr == nil && may && move { + may, merr = s.mayEither(c, auth.ActDelete, urlPath) + } + if merr != nil { + return merr + } else if !may { + return s.deny(c) + } + err = s.replacing(c, dst, existing.IsDir(), func() error { return do(false) }) } if err != nil { return err @@ -606,12 +643,102 @@ func (s *Server) davCopyMove(c fiber.Ctx, urlPath string, move bool) error { if move { s.dropLocks(urlPath) } - if replaced { + if existing != nil { + s.dropLocks(dst) return c.SendStatus(fiber.StatusNoContent) } return c.SendStatus(fiber.StatusCreated) } +// replacing runs do, which puts something in the place of the entry at +// dst. The visitor needs the right to delete a folder there with all in +// it, or to overwrite a file. The entry is set aside under a hidden name +// first and put back if do fails. Once do succeeds, a file is gone, as one +// a PUT replaces, and a folder goes to the trash, as one a DELETE removes. +func (s *Server) replacing(c fiber.Ctx, dst string, isDir bool, do func() error) error { + dirPath, name := path.Split(dst) + act := auth.ActOverwrite + if isDir { + act = auth.ActDelete + } + dir, realDir, info, err := s.entry(c, dirPath, name, act) + if err != nil { + return err + } + defer dir.Close() + realPath := path.Join(realDir, name) + if info.IsDir() != isDir { + return explain(fiber.ErrConflict, name+" changed meanwhile: try again.") + } + if isDir { + _, err := s.checkTree(c, dir, name, dst, realPath, auth.ActDelete) + if err := s.treeError(err, "replace"); err != nil { + return err + } + } + aside := tempPrefix + rand.Text() + tempSuffix + release, err := s.acquireLock(dir, name, aside) + if err != nil { + return err + } + err = sameEntry(dir, name, info) + if err == nil { + if err = dir.Rename(name, aside); err != nil { + err = s.openError(err) + } + } + release() + if err != nil { + return err + } + if err := do(); err != nil { + if perr := renameNoReplace(dir, aside, name, info); perr != nil { + // Something took the name meanwhile. + if _, terr := s.toTrash(c, dir, aside, realPath, info); terr != nil { + s.log.Error("put back what a move or copy that failed was to replace", zap.String("path", dst), + zap.String("kept as", path.Join(realDir, aside)), zap.Error(perr)) + } + } + return err + } + switch { + case isDir && s.cfg.Trash.Keep > 0: + _, err = s.toTrash(c, dir, aside, realPath, info) + case isDir: + err = dir.RemoveAll(aside) + default: + err = dir.Remove(aside) + } + if err != nil { + s.log.Error("remove what a move or copy replaced", zap.String("path", dst), zap.String("kept as", path.Join(realDir, aside)), + zap.Error(err)) + } + syncDir(dir) + s.homeChanged(realPath) + return nil +} + +// davPreconditions evaluates If-Match and If-Unmodified-Since for a change +// of the entry at urlPath (412 if they fail): clients send them so as not +// to change what changed since they looked. +func (s *Server) davPreconditions(c fiber.Ctx, urlPath string) error { + if c.Get(fiber.HeaderIfMatch) == "" && c.Get(fiber.HeaderIfUnmodifiedSince) == "" { + return nil + } + var info fs.FileInfo + switch f, i, _, err := s.lookup(urlPath, s.visitorRules(c)); { + case err == nil: + _ = f.Close() + info = i + case statusOf(err) != fiber.StatusNotFound: + return err + } + if !preconditionsHold(c, info) { + return fiber.ErrPreconditionFailed + } + return nil +} + // davDestination returns the clean URL path that the Destination header h // names under davPrefix. Its host is not checked: behind proxies it may // differ, and only paths here are served. @@ -663,12 +790,17 @@ func (s *Server) davProppatch(c fiber.Ctx, urlPath string) error { return err } _ = f.Close() + if !preconditionsHold(c, info) { + return fiber.ErrPreconditionFailed + } isDir, realPath = info.IsDir(), real create, replace, err := s.uploadRightsAt(c, object(urlPath, isDir), object(realPath, isDir)) if err != nil { return err } - may = create || replace + // Changing a file takes the right to overwrite it, as replacing + // it does; a folder's time, the right to make it. + may = replace || isDir && create } var ok, forbidden, bad bytes.Buffer var mtime time.Time @@ -727,9 +859,10 @@ func davTime(v string) (time.Time, bool) { return unixTime(v) } -// davLock is an advisory WebDAV lock. +// davLock is an advisory WebDAV lock of the user user. type davLock struct { token, path, owner string + user int64 exclusive, deep bool // deep: it covers what is in the folder too timeout time.Duration expires time.Time @@ -737,9 +870,16 @@ type davLock struct { const ( maxLockTimeout = time.Hour - maxLocks = 10000 + maxLocks = 10000 // in all + maxLockOwner = 1 << 10 ) +// maxUserLocks bounds the locks of one user. +var maxUserLocks = 1000 + +// errLockConflict refuses a lock that another one keeps out. +var errLockConflict = errors.New("a conflicting lock is held") + // lockinfo is the body of a LOCK request. type lockinfo struct { XMLName xml.Name `xml:"DAV: lockinfo"` @@ -750,33 +890,26 @@ type lockinfo struct { } `xml:"DAV: owner"` } +// davLock takes a lock, or refreshes one the visitor holds. Rights and what +// is there are checked before the locks are looked at, which only takes a +// moment: that takes the file system. func (s *Server) davLock(c fiber.Ctx, urlPath string) error { data, err := s.davBody(c) if err != nil { return err } - timeout := lockTimeout(c.Get("Timeout")) - now := time.Now() - s.davMu.Lock() - defer s.davMu.Unlock() - for token, l := range s.davLocks { - if now.After(l.expires) { - delete(s.davLocks, token) - } - } + timeout, user := lockTimeout(c.Get("Timeout")), userOf(c).ID if len(bytes.TrimSpace(data)) == 0 { - // A refresh of a lock the client holds. - l := s.davLocks[ifToken(c.Get("If"))] - if l == nil || !covers(l, urlPath) { - return fiber.ErrPreconditionFailed - } - l.timeout, l.expires = timeout, now.Add(timeout) - return s.sendLock(c, fiber.StatusOK, l) + return s.refreshLock(c, urlPath, user, timeout) } var req lockinfo if err := xml.Unmarshal(data, &req); err != nil { return fiber.ErrBadRequest } + owner := strings.TrimSpace(cmp.Or(req.Owner.Href, req.Owner.Text)) + if len(owner) > maxLockOwner { + return explain(fiber.ErrBadRequest, "Name the owner of a lock in up to 1 KiB.") + } create, replace, err := s.uploadRights(c, urlPath) if err != nil { return err @@ -784,29 +917,91 @@ func (s *Server) davLock(c fiber.Ctx, urlPath string) error { if !create && !replace { return s.deny(c) } - l := &davLock{path: urlPath, exclusive: req.Exclusive != nil, deep: c.Get("Depth") != "0", timeout: timeout, - expires: now.Add(timeout), owner: strings.TrimSpace(cmp.Or(req.Owner.Href, req.Owner.Text))} - for _, other := range s.davLocks { - if (other.exclusive || l.exclusive) && (covers(other, urlPath) || covers(l, other.path)) { - return davError(c, fiber.StatusLocked, "no-conflicting-lock") + exists := false + if f, _, _, err := s.lookup(urlPath, s.visitorRules(c)); err == nil { + _ = f.Close() + exists = true + } + now := time.Now() + l := &davLock{token: newLockToken(), path: urlPath, owner: owner, user: user, exclusive: req.Exclusive != nil, + deep: c.Get("Depth") != "0", timeout: timeout, expires: now.Add(timeout)} + shown, err := s.addLock(l, now) + if errors.Is(err, errLockConflict) { + return davError(c, fiber.StatusLocked, "no-conflicting-lock") + } else if err != nil { + return err + } + status := fiber.StatusOK + if !exists { + // A lock of a free name makes an empty file there (RFC 4918, + // section 7.3); macOS's files beside files are not kept. + if !appleDouble(path.Base(urlPath)) { + _, err := s.saveFile(c, urlPath, 0, time.Time{}, &requestBody{r: strings.NewReader("")}, false) + if err != nil && !errors.Is(err, fiber.ErrPreconditionFailed) { + s.removeLock(l.token) + return err + } + } + status = fiber.StatusCreated + } + c.Set("Lock-Token", "<"+l.token+">") + return sendLock(c, status, shown) +} + +// addLock adds the lock l, unless another one keeps it out +// (errLockConflict) or there are too many, and describes it. +func (s *Server) addLock(l *davLock, now time.Time) (string, error) { + s.davMu.Lock() + defer s.davMu.Unlock() + mine := 0 + for token, other := range s.davLocks { + switch { + case now.After(other.expires): + delete(s.davLocks, token) + case (other.exclusive || l.exclusive) && (covers(other, l.path) || covers(l, other.path)): + return "", errLockConflict + case other.user == l.user: + mine++ } } - if len(s.davLocks) >= maxLocks { - return fiber.ErrServiceUnavailable + if len(s.davLocks) >= maxLocks || mine >= maxUserLocks { + return "", fiber.ErrServiceUnavailable + } + s.davLocks[l.token] = l + return activeLock(l), nil +} + +// refreshLock gives the lock named by the If header, which the user holds, +// a new timeout. +func (s *Server) refreshLock(c fiber.Ctx, urlPath string, user int64, timeout time.Duration) error { + s.davMu.Lock() + l := s.davLocks[ifToken(c.Get("If"))] + switch { + case l == nil || !covers(l, urlPath) || time.Now().After(l.expires): + s.davMu.Unlock() + return fiber.ErrPreconditionFailed + case l.user != user: + s.davMu.Unlock() + return fiber.ErrForbidden } + l.timeout, l.expires = timeout, time.Now().Add(timeout) + shown := activeLock(l) + s.davMu.Unlock() + return sendLock(c, fiber.StatusOK, shown) +} + +func (s *Server) removeLock(token string) { + s.davMu.Lock() + defer s.davMu.Unlock() + delete(s.davLocks, token) +} + +// newLockToken makes a lock token, a random UUID URN as RFC 4918 suggests. +func newLockToken() string { var b [16]byte _, _ = rand.Read(b[:]) h := hex.EncodeToString(b[:]) - l.token = "opaquelocktoken:" + h[:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:] - s.davLocks[l.token] = l - c.Set("Lock-Token", "<"+l.token+">") - status := fiber.StatusOK - if f, _, _, err := s.lookup(urlPath, s.visitorRules(c)); err == nil { - _ = f.Close() - } else { - status = fiber.StatusCreated - } - return s.sendLock(c, status, l) + return "opaquelocktoken:" + h[:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:] } // covers reports whether the lock l covers the entry at urlPath. @@ -837,12 +1032,14 @@ func ifToken(h string) string { return "opaquelocktoken:" + token } -func (s *Server) sendLock(c fiber.Ctx, status int, l *davLock) error { +// sendLock answers a LOCK with the lock shown, as activeLock describes it. +func sendLock(c fiber.Ctx, status int, shown string) error { c.Set(fiber.HeaderContentType, "application/xml; charset=utf-8") - return c.Status(status).SendString(xml.Header + `` + activeLock(l) + + return c.Status(status).SendString(xml.Header + `` + shown + "") } +// activeLock describes the lock l, whose fields davMu guards. func activeLock(l *davLock) string { scope, depth := "", "0" if l.exclusive { @@ -860,27 +1057,37 @@ func activeLock(l *davLock) string { l.token + "" + xmlEscape(davHref(l.path, false)) + "" } -// lockDiscovery describes the locks on the entry at urlPath. -func (s *Server) lockDiscovery(urlPath string) string { +// lockDiscoveries describes the locks on each of res, for their +// lockdiscovery properties, looking at the locks once. +func (s *Server) lockDiscoveries(res []davResource) { s.davMu.Lock() defer s.davMu.Unlock() - var b strings.Builder + if len(s.davLocks) == 0 { + return + } + at := make(map[string]int, len(res)) + for i, r := range res { + at[r.path] = i + } now := time.Now() for _, l := range s.davLocks { - if l.path == urlPath && now.Before(l.expires) { - b.WriteString(activeLock(l)) + if i, ok := at[l.path]; ok && now.Before(l.expires) { + res[i].locks += activeLock(l) } } - return b.String() } +// davUnlock ends a lock the visitor holds. func (s *Server) davUnlock(c fiber.Ctx, urlPath string) error { token := strings.Trim(strings.TrimSpace(c.Get("Lock-Token")), "<>") s.davMu.Lock() defer s.davMu.Unlock() l := s.davLocks[token] - if l == nil || l.path != urlPath { + switch { + case l == nil || l.path != urlPath: return davError(c, fiber.StatusConflict, "lock-token-matches-request-uri") + case l.user != userOf(c).ID: + return fiber.ErrForbidden } delete(s.davLocks, token) return c.SendStatus(fiber.StatusNoContent) diff --git a/internal/server/dav_test.go b/internal/server/dav_test.go index 7e73768..5151ab4 100644 --- a/internal/server/dav_test.go +++ b/internal/server/dav_test.go @@ -13,6 +13,7 @@ import ( "net/http/httptest" "net/url" "os" + "path" "path/filepath" "regexp" "slices" @@ -458,9 +459,13 @@ func TestDavMkcol(t *testing.T) { "/.dav/none/new": http.StatusConflict, // the folder is missing "/.dav/": http.StatusMethodNotAllowed, } { - if resp, _ := dav.do(t, "MKCOL", uri); resp.StatusCode != want { + resp, _ := dav.do(t, "MKCOL", uri) + if resp.StatusCode != want { t.Errorf("MKCOL %s: %d, want %d", uri, resp.StatusCode, want) } + if want == http.StatusMethodNotAllowed && resp.Header.Get("Allow") != davAllow { + t.Errorf("MKCOL %s: Allow %q", uri, resp.Header.Get("Allow")) + } } if resp, _ := dav.send(t, "MKCOL", "/.dav/docs/x", strings.NewReader("")); resp.StatusCode != http.StatusUnsupportedMediaType { t.Errorf("with a body: %d", resp.StatusCode) @@ -488,7 +493,7 @@ func TestDavCopyMove(t *testing.T) { t.Error("not moved") } // What is there is kept with "Overwrite: F", and otherwise replaced, - // into the trash. + // as a PUT replaces it. resp, _ = dav.do(t, "COPY", "/.dav/docs/b.txt", "Destination", "/.dav/docs/moved.txt", "Overwrite", "F") expectStatus(t, resp, http.StatusPreconditionFailed) if readFile(t, f.dir, "docs/moved.txt") != "a" || len(trashedOnDisk(t, f)) != 0 { @@ -496,7 +501,7 @@ func TestDavCopyMove(t *testing.T) { } resp, _ = dav.do(t, "COPY", "/.dav/docs/b.txt", "Destination", "/.dav/docs/moved.txt") expectStatus(t, resp, http.StatusNoContent) - if readFile(t, f.dir, "docs/moved.txt") != "b" || readFile(t, f.dir, "docs/b.txt") != "b" || len(trashedOnDisk(t, f)) == 0 { + if readFile(t, f.dir, "docs/moved.txt") != "b" || readFile(t, f.dir, "docs/b.txt") != "b" || len(trashedOnDisk(t, f)) != 0 { t.Error("not replaced") } trashed := len(trashedOnDisk(t, f)) @@ -620,6 +625,11 @@ func TestDavProppatch(t *testing.T) { if !strings.Contains(e[403].Raw, "getlastmodified") || !mtimeOf(t, file).Equal(time.Date(2021, 2, 3, 4, 5, 6, 0, time.UTC)) { t.Errorf("by a reader: %+v", e) } + // Operators may add files, but not change those there. + e = proppatch(davAs(t, f, userOfRole("operator")), `Wed, 01 Jan 2020 00:00:00 GMT`) + if !strings.Contains(e[403].Raw, "getlastmodified") || !mtimeOf(t, file).Equal(time.Date(2021, 2, 3, 4, 5, 6, 0, time.UTC)) { + t.Errorf("by an operator: %+v", e) + } if resp, _ := dav.send(t, "PROPPATCH", "/.dav/docs/a.txt", strings.NewReader(" 0 && (len(trashed) != 1 || readFile(t, f.dir, path.Join(trashDir, trashed[0], "dst", "old.txt")) != "old") { + t.Errorf("the trash: %v", trashed) + } + }) + } +} + +// What a MOVE replaces leaves the home, as what a PUT replaces does; and +// replacing counts what is written, without counting the home again. +func TestDavReplaceInHome(t *testing.T) { + ta := newTestAuth(t) + f := newFixtureWith(t, ta, withHomes, func(cfg *config.Config) { cfg.Homes.Quota = 10 }) + ta.addUser(t, "dora", "user") + dora := davAs(t, f, "dora") + put := func(name, content string, want int) { + t.Helper() + resp, _ := dora.send(t, "PUT", "/.dav/home/dora/"+name, strings.NewReader(content)) + expectStatus(t, resp, want) + } + put("a.txt", "123456", http.StatusCreated) + put("tmp.txt", "1234", http.StatusCreated) + // Saved as some apps save: to a temporary file, moved over the old one. + resp, _ := dora.do(t, "MOVE", "/.dav/home/dora/tmp.txt", "Destination", "/.dav/home/dora/a.txt") + expectStatus(t, resp, http.StatusNoContent) + if readFile(t, f.dir, "home/dora/a.txt") != "1234" || len(trashedOnDisk(t, f)) != 0 { + t.Error("not replaced in place") + } + put("b.txt", "12345", http.StatusCreated) + + v, ok := f.srv.uses.Load("dora") + if !ok { + t.Fatal("the home was not counted") + } + u := v.(*homeUse) + usage := func() (int64, time.Time) { + u.mu.Lock() + defer u.mu.Unlock() + return u.used, u.counted + } + _, counted := usage() + put("b.txt", "12", http.StatusNoContent) + if used, again := usage(); used != 6 || !again.Equal(counted) { + t.Errorf("after replacing: %d bytes, counted at %v, before at %v", used, again, counted) + } +} + +// Where the file system finds names in any case, a MOVE spells a name anew. +func TestDavRespell(t *testing.T) { + f, dav := newDavFixture(t, withTrash) + if f.srv.fold == nil { + t.Skip("this file system finds names only as spelled") + } + f.write(t, "docs/a.txt", "a") + f.write(t, "docs/sub/b.txt", "b") + names := func(dir string) []string { + t.Helper() + entries, err := os.ReadDir(filepath.Join(f.dir, dir)) + if err != nil { + t.Fatal(err) + } + var out []string + for _, e := range entries { + out = append(out, e.Name()) + } + return out + } + + resp, _ := dav.do(t, "MOVE", "/.dav/docs/a.txt", "Destination", "/.dav/docs/A.txt") + expectStatus(t, resp, http.StatusCreated) + resp, _ = dav.do(t, "MOVE", "/.dav/docs/sub/", "Destination", "/.dav/docs/Sub/") + expectStatus(t, resp, http.StatusCreated) + if got := names("docs"); !slices.Equal(got, []string{"A.txt", "Sub"}) || readFile(t, f.dir, "docs/A.txt") != "a" || len(trashedOnDisk(t, f)) != 0 { + t.Errorf("respelled: %v", got) + } + for _, tc := range []struct { + method, uri, dest string + want int + }{ + {"COPY", "/.dav/docs/A.txt", "/.dav/docs/a.txt", http.StatusForbidden}, // onto itself + {"MOVE", "/.dav/docs/Sub/b.txt", "/.dav/DOCS", http.StatusConflict}, // over what holds it + {"MOVE", "/.dav/DOCS/", "/.dav/docs/sub/x/", http.StatusConflict}, // into itself + } { + if resp, body := dav.do(t, tc.method, tc.uri, "Destination", tc.dest); resp.StatusCode != tc.want { + t.Errorf("%s %s to %s: %d, want %d: %s", tc.method, tc.uri, tc.dest, resp.StatusCode, tc.want, body) + } + } + if !exists(filepath.Join(f.dir, "docs", "Sub", "b.txt")) || len(trashedOnDisk(t, f)) != 0 { + t.Error("refused, but changed") + } + // The site's rename too. + resp, _ = post(t, f, "/docs/", "rename=A.txt&to=a.txt") + expectStatus(t, resp, http.StatusCreated) + if got := names("docs"); !slices.Equal(got, []string{"Sub", "a.txt"}) { + t.Errorf("renamed: %v", got) + } +} + +// Changes can be made to depend on what a client saw (RFC 9110, section +// 13.1). +func TestDavPreconditions(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "a") + resp, _ := dav.do(t, "HEAD", "/.dav/docs/a.txt") + etag, stale := resp.Header.Get("ETag"), `"1-1"` + if etag == "" { + t.Fatal("no ETag") + } + for _, tc := range []struct { + name, header, value string + }{ + {"a.txt", "If-Match", stale}, + {"a.txt", "If-Match", "W/" + etag}, + {"a.txt", "If-Unmodified-Since", "Mon, 02 Jan 2006 15:04:05 GMT"}, + {"new.txt", "If-Match", "*"}, + } { + if resp, _ := dav.send(t, "PUT", "/.dav/docs/"+tc.name, strings.NewReader("b"), tc.header, tc.value); resp.StatusCode != http.StatusPreconditionFailed { + t.Errorf("PUT %s with %s: %s: %d", tc.name, tc.header, tc.value, resp.StatusCode) + } + } + if readFile(t, f.dir, "docs/a.txt") != "a" || exists(filepath.Join(f.dir, "docs", "new.txt")) { + t.Error("written anyway") + } + resp, _ = dav.send(t, "PUT", "/.dav/docs/a.txt", strings.NewReader("b"), "If-Match", stale+", "+etag) + expectStatus(t, resp, http.StatusNoContent) + etag = resp.Header.Get("ETag") + + for _, tc := range [][]string{ + {"DELETE"}, + {"MOVE", "Destination", "/.dav/docs/b.txt"}, + {"COPY", "Destination", "/.dav/docs/b.txt"}, + {"PROPPATCH"}, + } { + var body io.Reader + if tc[0] == "PROPPATCH" { + body = strings.NewReader(`Wed, 01 Jan 2020 00:00:00 GMT`) + } + if resp, _ := dav.send(t, tc[0], "/.dav/docs/a.txt", body, append(tc[1:], "If-Match", stale)...); resp.StatusCode != http.StatusPreconditionFailed { + t.Errorf("%s: %d", tc[0], resp.StatusCode) + } + } + if readFile(t, f.dir, "docs/a.txt") != "b" || exists(filepath.Join(f.dir, "docs", "b.txt")) { + t.Error("changed anyway") + } + resp, _ = dav.do(t, "DELETE", "/.dav/docs/a.txt", "If-Match", etag) + expectStatus(t, resp, http.StatusNoContent) + // The site's PUT too. + if resp, _ := f.send(t, "PUT", "/docs/c.txt", strings.NewReader("c"), "If-Match", "*"); resp.StatusCode != http.StatusPreconditionFailed { + t.Errorf("the site's PUT: %d", resp.StatusCode) + } +} + +func lockRequest(t *testing.T, d *davClient, uri, owner string, headers ...string) (*http.Response, string) { + t.Helper() + return d.send(t, "LOCK", uri, strings.NewReader(``+ + ``+owner+``), append([]string{"Depth", "0"}, headers...)...) +} + +// Locks are their users': others may not end or refresh them, and each +// user takes only so many. +func TestDavLockOwners(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "a") + admin := davAs(t, f, userOfRole("admin")) + + resp, _ := lockRequest(t, dav, "/.dav/docs/a.txt", "me") + expectStatus(t, resp, http.StatusOK) + token := strings.Trim(resp.Header.Get("Lock-Token"), "<>") + if resp, _ := admin.do(t, "LOCK", "/.dav/docs/a.txt", "If", "(<"+token+">)"); resp.StatusCode != http.StatusForbidden { + t.Errorf("refreshed by another user: %d", resp.StatusCode) + } + if resp, _ := admin.do(t, "UNLOCK", "/.dav/docs/a.txt", "Lock-Token", "<"+token+">"); resp.StatusCode != http.StatusForbidden { + t.Errorf("ended by another user: %d", resp.StatusCode) + } + resp, _ = dav.do(t, "UNLOCK", "/.dav/docs/a.txt", "Lock-Token", "<"+token+">") + expectStatus(t, resp, http.StatusNoContent) + + if resp, _ := lockRequest(t, dav, "/.dav/docs/a.txt", strings.Repeat("x", maxLockOwner+1)); resp.StatusCode != http.StatusBadRequest { + t.Errorf("a long owner: %d", resp.StatusCode) + } + defer func(n int) { maxUserLocks = n }(maxUserLocks) + maxUserLocks = 2 + // A lock of a free name makes an empty file, but for macOS's files + // beside files. + for _, name := range []string{"new.txt", "._new.txt"} { + resp, _ := lockRequest(t, dav, "/.dav/docs/"+name, "me") + expectStatus(t, resp, http.StatusCreated) + } + if info, err := os.Stat(filepath.Join(f.dir, "docs", "new.txt")); err != nil || info.Size() != 0 || exists(filepath.Join(f.dir, "docs", "._new.txt")) { + t.Errorf("made %v, %v", info, err) + } + if resp, _ := lockRequest(t, dav, "/.dav/docs/a.txt", "me"); resp.StatusCode != http.StatusServiceUnavailable { + t.Errorf("one too many: %d", resp.StatusCode) + } + resp, _ = lockRequest(t, admin, "/.dav/docs/a.txt", "another") + expectStatus(t, resp, http.StatusOK) + other := strings.Trim(resp.Header.Get("Lock-Token"), "<>") + + got := dav.propfind(t, "/.dav/docs/", "1", ``) + if locks := got["/.dav/docs/a.txt"][200].Locks; !slices.Equal(locks, []string{other}) || len(got["/.dav/docs/new.txt"][200].Locks) != 1 { + t.Errorf("lockdiscovery: %v", got) + } +} + +// macOS's files beside files are read to the end, so that the connection +// goes on. +func TestDavAppleDoubleBody(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "a") + addr, _ := startServer(t, f) + conn, err := net.Dial("tcp", addr) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + r := bufio.NewReader(conn) + data := strings.Repeat("x", 200<<10) + fmt.Fprintf(conn, "PUT /.dav/docs/._a.txt HTTP/1.1\r\nHost: t\r\nAuthorization: %s\r\nContent-Length: %d\r\n\r\n%s", dav.auth, len(data), data) + resp, err := http.ReadResponse(r, nil) + if err != nil { + t.Fatal(err) + } + _, _ = io.Copy(io.Discard, resp.Body) + _ = resp.Body.Close() + if resp.StatusCode != http.StatusCreated || resp.Close { + t.Fatalf("PUT: %d, closed %v", resp.StatusCode, resp.Close) + } + fmt.Fprintf(conn, "PROPFIND /.dav/docs/a.txt HTTP/1.1\r\nHost: t\r\nAuthorization: %s\r\nDepth: 0\r\nContent-Length: 0\r\n\r\n", dav.auth) + if resp, err := http.ReadResponse(r, nil); err != nil || resp.StatusCode != http.StatusMultiStatus { + t.Errorf("the next request: %v, %v", resp, err) + } +} diff --git a/internal/server/file.go b/internal/server/file.go index e43f217..f62fef3 100644 --- a/internal/server/file.go +++ b/internal/server/file.go @@ -106,6 +106,34 @@ func etagOf(mod time.Time, size int64) string { return `"` + strconv.FormatInt(mod.UnixNano(), 16) + "-" + strconv.FormatInt(size, 16) + `"` } +// preconditionsHold evaluates If-Match and If-Unmodified-Since (RFC 9110, +// section 13.1) for a request that changes the entry info, nil if there is +// none: clients send them so as not to overwrite what changed since they +// looked. If-Match compares the tags of etagOf, strongly. +func preconditionsHold(c fiber.Ctx, info fs.FileInfo) bool { + if im := strings.TrimSpace(c.Get(fiber.HeaderIfMatch)); im != "" { + if info == nil { + return false + } + if im == "*" { + return true + } + etag := etagOf(info.ModTime(), info.Size()) + for tag := range strings.SplitSeq(im, ",") { + if strings.TrimSpace(tag) == etag { + return true + } + } + return false + } + if ius := c.Get(fiber.HeaderIfUnmodifiedSince); ius != "" && info != nil { + if t, err := http.ParseTime(ius); err == nil && info.ModTime().Truncate(time.Second).After(t) { + return false + } + } + return true +} + // fromStart reports whether sending ranges, or the whole file if there are // none, sends the file's first byte. func fromStart(ranges []byteRange) bool { diff --git a/internal/server/listing.go b/internal/server/listing.go index 91d18d7..ab21a2c 100644 --- a/internal/server/listing.go +++ b/internal/server/listing.go @@ -50,6 +50,13 @@ var pages = template.Must(template.New("").Funcs(template.FuncMap{ "expiryOptions": func() []shareTTL { return shareTTLs }, }).ParseFS(templateFS, "templates/*.html")) +// shownTime is how pages show the time t: in UTC, to the minute, until +// app.js shows it in the visitor's time, from isoTime's. +func shownTime(t time.Time) string { return t.UTC().Format("Jan 2, 2006 15:04") + " UTC" } + +// isoTime is t for a