From f3cd01ac4ccdd65c942203becbb2c03eaebd61d5 Mon Sep 17 00:00:00 2001 From: ssenerg Date: Sun, 25 Oct 2026 11:05:48 +0330 Subject: [PATCH] Sync folders with goftp over WebDAV, signed in with app passwords Sync apps (rclone bisync, FolderSync, GoodSync, Mountain Duck) and file managers reach the site's files at /.dav/, with the same rules, homes, quotas and trash. They sign in with HTTP Basic authentication and an app password: made on the new Sync page (in the account menu), shown once, stored as a SHA-256 hash, revocable one by one, and ended with every password change. Wrong ones count against the client's sign-in budget once they fail, so that a sync app's parallel requests are not refused. Modification times are kept: from rclone's and ownCloud's X-OC-Mtime on upload, and from PROPPATCH of getlastmodified or Windows' Win32LastModifiedTime. PROPFIND reports homes' quotas (RFC 4331); locks are advisory; macOS's ._ and .DS_Store files are taken and dropped. Finder's chunked uploads carry X-Expected-Entity-Length, and must be exactly that long. A PUT with Content-Range is refused rather than taken as the whole file. COPY and MOVE check the rights to do so before what they replace goes to the trash, and never replace a folder holding what they move. Co-Authored-By: Claude Opus 5.5 --- README.md | 57 ++ internal/auth/apppass.go | 77 ++ internal/auth/apppass_test.go | 149 +++ internal/auth/authtest/store.go | 82 +- internal/auth/postgres.go | 68 +- internal/auth/store.go | 21 +- internal/db/db_test.go | 2 +- internal/db/migrations/007_app_passwords.sql | 12 + internal/server/dav.go | 899 +++++++++++++++++++ internal/server/dav_test.go | 840 +++++++++++++++++ internal/server/file.go | 7 +- internal/server/mkdir.go | 11 +- internal/server/move.go | 54 +- internal/server/server.go | 10 + internal/server/server_test.go | 11 +- internal/server/sync.go | 147 +++ internal/server/templates/base.html | 17 + internal/server/templates/sync.html | 122 +++ internal/server/upload.go | 102 ++- 19 files changed, 2628 insertions(+), 60 deletions(-) create mode 100644 internal/auth/apppass.go create mode 100644 internal/auth/apppass_test.go create mode 100644 internal/db/migrations/007_app_passwords.sql create mode 100644 internal/server/dav.go create mode 100644 internal/server/dav_test.go create mode 100644 internal/server/sync.go create mode 100644 internal/server/templates/sync.html diff --git a/README.md b/README.md index 5b06144..14b1c5b 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ Postgres whose access is decided by [Casbin](https://casbin.org) policies. - Names can be searched across folders (see [Search](#search)), and links can share files and folders, or take uploads like a drop box (see [Share links](#share-links)). +- Sync apps and file managers reach the same files over WebDAV, signed in + with app passwords (see [Syncing with WebDAV](#syncing-with-webdav)). - Dotfiles are never listed or served. Paths are resolved with `os.Root`, so requests and symlinks cannot escape the served directory. @@ -390,6 +392,58 @@ Creating links needs the `share` action on what is shared, and for upload links `write` too. Admins have it; on upgrade, it is added only if the admin rule is still the default one. To let others share, e.g. `goftp policy add user:bob '/bob/*' share`. +## Syncing with WebDAV + +goftp serves its files over WebDAV at `/.dav/`, so that sync apps can keep +a folder on a computer or phone in sync with it, and file managers can +open it: `https://host/.dav/PATH` is `PATH`, with the same rules, homes, +quotas and trash as the site. **Sync**, in the account menu, shows the +address (your home, if you have one) and makes app passwords. + +Apps sign in with your username and an **app password**, never your own +password: make one for each app or device, so that you can revoke one +without the others. It is shown once (only a hash is stored), may be typed +without its dashes, and stops working when revoked, when you change your +password, or when you are given a new temporary one. Each user may have +20. Scripts: + +```sh +curl -H "Authorization: Bearer $TOKEN" -d action=create -d name=laptop https://host/.sync/ +# {"id":1,"name":"laptop","username":"bob","password":"abcd-efgh-...","url":"https://host/.dav/"} +curl -H "Authorization: Bearer $TOKEN" -d action=revoke -d id=1 https://host/.sync/ +``` + +With [rclone](https://rclone.org), which keeps modification times with +`vendor=owncloud`, a folder syncs both ways like this; the first run needs +`--resync`, and the second line can then run from cron: + +```sh +rclone config create goftp webdav url=https://host/.dav/home/bob/ vendor=owncloud user=bob pass=APP-PASSWORD --obscure +rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**" --resync +rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**" +``` + +FolderSync (Android), GoodSync, Mountain Duck and Cyberduck take the same +address, username and app password as a WebDAV account; Finder opens it +with Go → Connect to Server. goftp keeps the modification times that +apps send with uploads (`X-OC-Mtime`) or set afterwards (`PROPPATCH` of +`getlastmodified`, or Windows' `Win32LastModifiedTime`), and reports how +much room a home has left (RFC 4331). Limits: + +- Names that start with a dot are not synced, as goftp hides them: exclude + them in the app. macOS's `.DS_Store` and `._` files are taken and + 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. +- 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 + uploaded over WebDAV. + +Wrong app passwords count against the client's sign-in budget +(`limiter.*`). + ## Security notes - Serve over HTTPS: passwords and session tokens travel with every sign-in @@ -418,6 +472,9 @@ the admin rule is still the default one. To let others share, e.g. `goftp policy - Symlinks are followed only when they use relative targets that stay inside the served directory, do not lead into a dotfile or dot-directory, and lead where the visitor may go anyway. +- App passwords carry 120 bits, and only their SHA-256 hashes are stored. + They sign in to WebDAV alone, with HTTP Basic authentication, which + sends them with every request: serve WebDAV over HTTPS only. - Share links carry 256-bit tokens, which are kept out of the logs. Link passwords are hashed with Argon2id; wrong ones count against the client's sign-in budget, and each link takes at most 20 password checks diff --git a/internal/auth/apppass.go b/internal/auth/apppass.go new file mode 100644 index 0000000..4be70f1 --- /dev/null +++ b/internal/auth/apppass.go @@ -0,0 +1,77 @@ +package auth + +import ( + "context" + "crypto/rand" + "fmt" + "strings" + "time" + "unicode" + "unicode/utf8" +) + +// AppPassword lets an app, such as a sync app, sign in as its user over +// WebDAV, with a password of its own that can be revoked alone. +type AppPassword struct { + ID int64 + UserID int64 + Name string // what it is for, e.g. "rclone on my laptop" + CreatedAt time.Time + LastUsedAt time.Time // zero if never +} + +// MaxAppPasswords bounds the app passwords of one user. +const MaxAppPasswords = 20 + +// maxAppPasswordName bounds the length of an app password's name. +const maxAppPasswordName = 64 + +// appPasswordLength is the number of letters and digits of an app password, +// 120 bits of it; it is shown in groups of four, which may be left out. +const appPasswordLength = 24 + +// CreateAppPassword makes an app password named name for u, and returns it: +// it is not kept, so it can only be shown now. +func (s *Service) CreateAppPassword(ctx context.Context, u *User, name string) (string, *AppPassword, error) { + name = strings.TrimSpace(name) + if name == "" || utf8.RuneCountInString(name) > maxAppPasswordName || strings.ContainsFunc(name, unicode.IsControl) { + return "", nil, fmt.Errorf("%w: name it in up to %d characters", ErrInvalid, maxAppPasswordName) + } + raw := strings.ToLower(rand.Text()[:appPasswordLength]) + var groups []string + for i := 0; i < len(raw); i += 4 { + groups = append(groups, raw[i:i+4]) + } + ap := &AppPassword{UserID: u.ID, Name: name} + if err := s.store.CreateAppPassword(ctx, tokenHash(raw), ap); err != nil { + return "", nil, err + } + return strings.Join(groups, "-"), ap, nil +} + +// AppPasswords lists u's app passwords, newest first. +func (s *Service) AppPasswords(ctx context.Context, u *User) ([]AppPassword, error) { + return s.store.AppPasswords(ctx, u.ID) +} + +// RevokeAppPassword ends u's app password id (ErrNotFound if u has none +// of that ID). +func (s *Service) RevokeAppPassword(ctx context.Context, u *User, id int64) error { + return s.store.DeleteAppPassword(ctx, u.ID, id) +} + +// AppPasswordUser returns the user username, if password is one of their +// app passwords (ErrNotFound otherwise). Case and the dashes between groups +// do not matter. +func (s *Service) AppPasswordUser(ctx context.Context, username, password string) (*User, error) { + raw := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(password), "-", "")) + if len(raw) != appPasswordLength { + return nil, ErrNotFound + } + for _, c := range raw { + if !('a' <= c && c <= 'z' || '2' <= c && c <= '7') { + return nil, ErrNotFound + } + } + return s.store.AppPasswordUser(ctx, NormalizeUsername(username), tokenHash(raw)) +} diff --git a/internal/auth/apppass_test.go b/internal/auth/apppass_test.go new file mode 100644 index 0000000..8d28103 --- /dev/null +++ b/internal/auth/apppass_test.go @@ -0,0 +1,149 @@ +package auth_test + +import ( + "context" + "errors" + "regexp" + "strings" + "testing" + "time" + + "goftp/internal/auth" +) + +func TestAppPasswords(t *testing.T) { + services(t, func(t *testing.T, svc *auth.Service) { + ctx := context.Background() + temp, err := svc.CreateUser(ctx, "dora", "user") + if err != nil { + t.Fatal(err) + } + if _, err := svc.CreateUser(ctx, "erin", "user"); err != nil { + t.Fatal(err) + } + dora, erin := userNamed(t, svc, "dora"), userNamed(t, svc, "erin") + + for _, name := range []string{"", " ", strings.Repeat("x", 65), "a\nb"} { + if _, _, err := svc.CreateAppPassword(ctx, dora, name); !errors.Is(err, auth.ErrInvalid) { + t.Errorf("CreateAppPassword(%q): %v", name, err) + } + } + password, ap, err := svc.CreateAppPassword(ctx, dora, " rclone on my laptop ") + if err != nil { + t.Fatal(err) + } + if !regexp.MustCompile(`^[a-z2-7]{4}(-[a-z2-7]{4}){5}$`).MatchString(password) { + t.Errorf("password %q", password) + } + if ap.ID == 0 || ap.Name != "rclone on my laptop" || ap.CreatedAt.IsZero() || !ap.LastUsedAt.IsZero() { + t.Errorf("app password %+v", ap) + } + + // It signs in as dora, written as people may copy it. + for _, tc := range []struct{ username, password string }{ + {"dora", password}, + {"Dora", strings.ToUpper(password)}, + {"dora", strings.ReplaceAll(password, "-", "")}, + {"dora", " " + password + "\n"}, + } { + u, err := svc.AppPasswordUser(ctx, tc.username, tc.password) + if err != nil || u.ID != dora.ID { + t.Errorf("AppPasswordUser(%q, %q): %v, %v", tc.username, tc.password, u, err) + } + } + other, _, err := svc.CreateAppPassword(ctx, erin, "phone") + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct{ username, password string }{ + {"erin", password}, // someone else's + {"dora", other}, // and the other way round + {"dora", temp}, // the account's password + {"dora", password[:len(password)-1] + "1"}, // not base32 + {"dora", password + "a"}, + {"nobody", password}, + {"dora", ""}, + } { + if _, err := svc.AppPasswordUser(ctx, tc.username, tc.password); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("AppPasswordUser(%q, %q): %v", tc.username, tc.password, err) + } + } + + // Listed newest first, with when they were last used. + phone, _, err := svc.CreateAppPassword(ctx, dora, "phone") + if err != nil { + t.Fatal(err) + } + apps, err := svc.AppPasswords(ctx, dora) + if err != nil { + t.Fatal(err) + } + if len(apps) != 2 || apps[0].Name != "phone" || apps[1].ID != ap.ID || time.Since(apps[1].LastUsedAt) > time.Minute || + !apps[0].LastUsedAt.IsZero() { + t.Errorf("listed %+v", apps) + } + + // Revoked, one stops working and the others do not. + if err := svc.RevokeAppPassword(ctx, erin, ap.ID); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("revoked by someone else: %v", err) + } + if err := svc.RevokeAppPassword(ctx, dora, ap.ID); err != nil { + t.Fatal(err) + } + if err := svc.RevokeAppPassword(ctx, dora, ap.ID); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("revoked twice: %v", err) + } + if _, err := svc.AppPasswordUser(ctx, "dora", password); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("revoked app password: %v", err) + } + if _, err := svc.AppPasswordUser(ctx, "dora", phone); err != nil { + t.Errorf("the other app password: %v", err) + } + + // A user has only so many. + for i := 1; i < auth.MaxAppPasswords; i++ { + if _, _, err := svc.CreateAppPassword(ctx, dora, "app"); err != nil { + t.Fatal(i, err) + } + } + if _, _, err := svc.CreateAppPassword(ctx, dora, "one too many"); !errors.Is(err, auth.ErrTooMany) { + t.Errorf("over the limit: %v", err) + } + + // A new password ends them all: they may have been made by + // whoever learned the old one. + sess, err := svc.Login(ctx, "dora", temp) + if err != nil { + t.Fatal(err) + } + if _, err := svc.ChangePassword(ctx, sess.User, temp, "a long new password"); err != nil { + t.Fatal(err) + } + if _, err := svc.AppPasswordUser(ctx, "dora", phone); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("after a password change: %v", err) + } + if apps, err := svc.AppPasswords(ctx, dora); err != nil || len(apps) != 0 { + t.Errorf("after a password change: %v, %v", apps, err) + } + if _, err := svc.AppPasswordUser(ctx, "erin", other); err != nil { + t.Errorf("another user's: %v", err) + } + // So does a reset by an administrator, and deleting the user. + if _, err := svc.ResetPassword(ctx, "erin"); err != nil { + t.Fatal(err) + } + if _, err := svc.AppPasswordUser(ctx, "erin", other); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("after a reset: %v", err) + } + last, _, err := svc.CreateAppPassword(ctx, dora, "last") + if err != nil { + t.Fatal(err) + } + if err := svc.DeleteUser(ctx, "dora"); err != nil { + t.Fatal(err) + } + if _, err := svc.AppPasswordUser(ctx, "dora", last); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("of a deleted user: %v", err) + } + }) +} diff --git a/internal/auth/authtest/store.go b/internal/auth/authtest/store.go index a35900f..4b611c0 100644 --- a/internal/auth/authtest/store.go +++ b/internal/auth/authtest/store.go @@ -17,8 +17,9 @@ type Store struct { nextID int64 users map[int64]*auth.User sessions map[string]session - shares map[string]*auth.Share // by token hash - uploads map[string]*upload // by token hash + shares map[string]*auth.Share // by token hash + uploads map[string]*upload // by token hash + apps map[string]*auth.AppPassword // by token hash } type upload struct { @@ -36,7 +37,7 @@ var _ auth.Store = (*Store)(nil) func NewStore() *Store { return &Store{users: make(map[int64]*auth.User), sessions: make(map[string]session), shares: make(map[string]*auth.Share), - uploads: make(map[string]*upload)} + uploads: make(map[string]*upload), apps: make(map[string]*auth.AppPassword)} } // NewService returns a service with an empty store and the default policy. @@ -102,6 +103,7 @@ func (s *Store) SetPassword(_ context.Context, userID int64, passwordHash string } u.PasswordHash, u.MustChangePassword = passwordHash, mustChange s.deleteSessions(userID) + s.deleteApps(userID) if mustChange { s.deleteShares(userID) } @@ -132,9 +134,83 @@ func (s *Store) DeleteUser(_ context.Context, userID int64) error { delete(s.users, userID) s.deleteSessions(userID) s.deleteShares(userID) + s.deleteApps(userID) return nil } +func (s *Store) deleteApps(userID int64) { + for k, ap := range s.apps { + if ap.UserID == userID { + delete(s.apps, k) + } + } +} + +func (s *Store) CreateAppPassword(_ context.Context, tokenHash []byte, ap *auth.AppPassword) error { + s.mu.Lock() + defer s.mu.Unlock() + if _, ok := s.users[ap.UserID]; !ok { + return auth.ErrNotFound + } + n := 0 + for _, v := range s.apps { + if v.UserID == ap.UserID { + n++ + } + } + if n >= auth.MaxAppPasswords { + return auth.ErrTooMany + } + s.nextID++ + ap.ID, ap.CreatedAt = s.nextID, time.Now() + c := *ap + s.apps[string(tokenHash)] = &c + return nil +} + +func (s *Store) AppPasswords(_ context.Context, userID int64) ([]auth.AppPassword, error) { + s.mu.Lock() + defer s.mu.Unlock() + var out []auth.AppPassword + for _, ap := range s.apps { + if ap.UserID == userID { + out = append(out, *ap) + } + } + slices.SortFunc(out, func(a, b auth.AppPassword) int { return cmp.Compare(b.ID, a.ID) }) + return out, nil +} + +func (s *Store) DeleteAppPassword(_ context.Context, userID, id int64) error { + s.mu.Lock() + defer s.mu.Unlock() + for k, ap := range s.apps { + if ap.ID == id && ap.UserID == userID { + delete(s.apps, k) + return nil + } + } + return auth.ErrNotFound +} + +func (s *Store) AppPasswordUser(_ context.Context, username string, tokenHash []byte) (*auth.User, error) { + s.mu.Lock() + defer s.mu.Unlock() + ap, ok := s.apps[string(tokenHash)] + if !ok { + return nil, auth.ErrNotFound + } + u, ok := s.users[ap.UserID] + if !ok || u.Username != username { + return nil, auth.ErrNotFound + } + if time.Since(ap.LastUsedAt) > time.Minute { + ap.LastUsedAt = time.Now() + } + c := *u + return &c, nil +} + func (s *Store) CreateSession(_ context.Context, tokenHash []byte, u *auth.User, expires time.Time) error { s.mu.Lock() defer s.mu.Unlock() diff --git a/internal/auth/postgres.go b/internal/auth/postgres.go index f2a26ca..0cc2b34 100644 --- a/internal/auth/postgres.go +++ b/internal/auth/postgres.go @@ -73,7 +73,10 @@ func (s *PgStore) SetPassword(ctx context.Context, userID int64, passwordHash st if tag.RowsAffected() == 0 { return ErrNotFound } - if _, err = tx.Exec(ctx, "DELETE FROM sessions WHERE user_id = $1", userID); err != nil || !mustChange { + if _, err = tx.Exec(ctx, "DELETE FROM sessions WHERE user_id = $1", userID); err != nil { + return err + } + if _, err = tx.Exec(ctx, "DELETE FROM app_passwords WHERE user_id = $1", userID); err != nil || !mustChange { return err } _, err = tx.Exec(ctx, "DELETE FROM shares WHERE created_by = $1", userID) @@ -81,6 +84,69 @@ func (s *PgStore) SetPassword(ctx context.Context, userID int64, passwordHash st }) } +func (s *PgStore) CreateAppPassword(ctx context.Context, tokenHash []byte, ap *AppPassword) error { + return pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { + // The row lock serializes a user's app passwords, so they cannot + // exceed the limit together. + var id int64 + err := tx.QueryRow(ctx, "SELECT id FROM users WHERE id = $1 FOR UPDATE", ap.UserID).Scan(&id) + if errors.Is(err, pgx.ErrNoRows) { + return ErrNotFound + } + if err != nil { + return err + } + var n int + if err := tx.QueryRow(ctx, "SELECT count(*) FROM app_passwords WHERE user_id = $1", id).Scan(&n); err != nil { + return err + } + if n >= MaxAppPasswords { + return ErrTooMany + } + return tx.QueryRow(ctx, "INSERT INTO app_passwords (user_id, name, token_hash) VALUES ($1, $2, $3) RETURNING id, created_at", + id, ap.Name, tokenHash).Scan(&ap.ID, &ap.CreatedAt) + }) +} + +func (s *PgStore) AppPasswords(ctx context.Context, userID int64) ([]AppPassword, error) { + rows, err := s.db.Query(ctx, `SELECT id, user_id, name, created_at, last_used_at FROM app_passwords + WHERE user_id = $1 ORDER BY created_at DESC, id DESC`, userID) + if err != nil { + return nil, err + } + return pgx.CollectRows(rows, func(row pgx.CollectableRow) (AppPassword, error) { + var ( + ap AppPassword + lastUsed *time.Time + ) + err := row.Scan(&ap.ID, &ap.UserID, &ap.Name, &ap.CreatedAt, &lastUsed) + if lastUsed != nil { + ap.LastUsedAt = *lastUsed + } + return ap, err + }) +} + +func (s *PgStore) DeleteAppPassword(ctx context.Context, userID, id int64) error { + tag, err := s.db.Exec(ctx, "DELETE FROM app_passwords WHERE id = $1 AND user_id = $2", id, userID) + if err == nil && tag.RowsAffected() == 0 { + err = ErrNotFound + } + return err +} + +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)) + 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 +} + func (s *PgStore) SetHomeQuota(ctx context.Context, userID int64, quota *int64) error { tag, err := s.db.Exec(ctx, "UPDATE users SET home_quota = $2 WHERE id = $1", userID, quota) if err == nil && tag.RowsAffected() == 0 { diff --git a/internal/auth/store.go b/internal/auth/store.go index f394870..15c09d3 100644 --- a/internal/auth/store.go +++ b/internal/auth/store.go @@ -73,15 +73,30 @@ type Store interface { CreateUser(ctx context.Context, username, passwordHash string) (int64, error) UserByName(ctx context.Context, username string) (*User, error) ListUsers(ctx context.Context) ([]User, error) - // SetPassword also ends all of the user's sessions and, for a - // temporary password (mustChange), their share links. + // SetPassword also ends all of the user's sessions and app passwords + // and, for a temporary password (mustChange), their share links. SetPassword(ctx context.Context, userID int64, passwordHash string, mustChange bool) error - // DeleteUser also deletes the user's sessions and share links. + // DeleteUser also deletes the user's sessions, app passwords and share + // links. DeleteUser(ctx context.Context, userID int64) error // SetHomeQuota sets how much the user's home may hold (see // User.HomeQuota). SetHomeQuota(ctx context.Context, userID int64, quota *int64) error + // CreateAppPassword stores the app password ap of ap.UserID by the + // hash of the password, and sets its ID and CreatedAt; ErrTooMany if + // the user has MaxAppPasswords already, ErrNotFound if there is no + // such user. + CreateAppPassword(ctx context.Context, tokenHash []byte, ap *AppPassword) error + // AppPasswords lists the app passwords of userID, newest first. + AppPasswords(ctx context.Context, userID int64) ([]AppPassword, error) + // DeleteAppPassword deletes the app password id of userID. + DeleteAppPassword(ctx context.Context, userID, id int64) error + // AppPasswordUser returns the user username, if tokenHash is the hash + // of one of their app passwords, and notes it was used now; at most + // once a minute. + AppPasswordUser(ctx context.Context, username string, tokenHash []byte) (*User, error) + // CreateSession starts a session for u, unless u was deleted or changed // password since it was read (ErrNotFound). It may drop the user's // oldest sessions to bound their number. diff --git a/internal/db/db_test.go b/internal/db/db_test.go index 6e8d430..2769480 100644 --- a/internal/db/db_test.go +++ b/internal/db/db_test.go @@ -56,7 +56,7 @@ func TestConcurrentMigrations(t *testing.T) { if err := pools[0].QueryRow(ctx, "SELECT count(*) FROM casbin_rule").Scan(&rules); err != nil { t.Fatal(err) } - if versions != 6 || rules != 9 { + if versions != 7 || rules != 9 { t.Errorf("%d migrations recorded, %d rules seeded", versions, rules) } } diff --git a/internal/db/migrations/007_app_passwords.sql b/internal/db/migrations/007_app_passwords.sql new file mode 100644 index 0000000..adb52d4 --- /dev/null +++ b/internal/db/migrations/007_app_passwords.sql @@ -0,0 +1,12 @@ +-- App passwords let apps, such as sync apps over WebDAV, sign in as their +-- user with a password of their own, which can be revoked alone. Only the +-- SHA-256 of each is kept; changing the user's password ends them all. +CREATE TABLE app_passwords ( + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE, + name TEXT NOT NULL CHECK (length(name) BETWEEN 1 AND 64), + token_hash BYTEA NOT NULL UNIQUE, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + last_used_at TIMESTAMPTZ +); +CREATE INDEX app_passwords_user_id_idx ON app_passwords (user_id); diff --git a/internal/server/dav.go b/internal/server/dav.go new file mode 100644 index 0000000..fb536af --- /dev/null +++ b/internal/server/dav.go @@ -0,0 +1,899 @@ +package server + +import ( + "bytes" + "cmp" + "crypto/rand" + "encoding/base64" + "encoding/hex" + "encoding/xml" + "errors" + "io" + "mime" + "net/http" + "net/url" + "path" + "slices" + "strconv" + "strings" + "time" + + "github.com/gofiber/fiber/v3" + "go.uber.org/zap" + + "goftp/internal/auth" +) + +// WebDAV (RFC 4918) at /.dav/, for sync apps and file managers: the same +// files as the site, with the same rules, homes, quotas and trash, signed +// in to with HTTP Basic authentication and an app password (see +// auth.AppPassword). /.dav/PATH is PATH. Locks are advisory: they keep other +// WebDAV clients that ask from taking the same lock, and nothing else, as +// goftp's own locks guard every change. +const davPrefix = "/.dav" + +// davMethods are the methods WebDAV adds to HTTP's. +var davMethods = []string{"PROPFIND", "PROPPATCH", "MKCOL", "COPY", "MOVE", "LOCK", "UNLOCK"} + +const davAllow = "OPTIONS, GET, HEAD, PUT, DELETE, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, UNLOCK" + +// davBodyLimit bounds the XML bodies of WebDAV requests. +const davBodyLimit = 64 << 10 + +// davRoutes serves WebDAV under davPrefix. +func (s *Server) davRoutes() { + methods := append([]string{fiber.MethodGet, fiber.MethodHead, fiber.MethodPut, fiber.MethodDelete, fiber.MethodOptions}, davMethods...) + s.app.Add(methods, davPrefix, s.dav) + s.app.Add(methods, davPrefix+"/*", s.dav) +} + +func (s *Server) dav(c fiber.Ctx) error { + c.Set("DAV", "1, 2") + c.Set("MS-Author-Via", "DAV") + c.Set(fiber.HeaderCacheControl, "no-store") + if c.Method() == fiber.MethodOptions { + c.Set(fiber.HeaderAllow, davAllow) + return c.SendStatus(fiber.StatusOK) + } + if err := s.davSignIn(c); err != nil { + return err + } + urlPath, ok := davPath(c.Path()) + if !ok { + return fiber.ErrBadRequest + } + switch c.Method() { + case "PROPFIND": + return s.davPropfind(c, urlPath) + case "PROPPATCH": + return s.davProppatch(c, urlPath) + case "MKCOL": + return s.davMkcol(c, urlPath) + case "COPY": + return s.davCopyMove(c, urlPath, false) + case "MOVE": + return s.davCopyMove(c, urlPath, true) + case "LOCK": + return s.davLock(c, urlPath) + case "UNLOCK": + return s.davUnlock(c, urlPath) + case fiber.MethodGet, fiber.MethodHead: + return s.davGet(c, urlPath) + case fiber.MethodPut: + return s.davPut(c, urlPath) + case fiber.MethodDelete: + return s.davDelete(c, urlPath) + } + return fiber.ErrMethodNotAllowed +} + +// davPath returns the clean URL path that the request path p, under +// davPrefix, stands for. +func davPath(p string) (string, bool) { + rest, ok := strings.CutPrefix(p, davPrefix) + if !ok || rest != "" && rest[0] != '/' { + return "", false + } + if rest == "" { + rest = "/" + } + clean, _, err := cleanPath(rest) + return clean, err == nil +} + +// davHref is the WebDAV URL of the entry at urlPath. +func davHref(urlPath string, dir bool) string { + return escapePath(davPrefix + object(urlPath, dir)) +} + +// davSignIn signs the request in with HTTP Basic authentication: a username +// and one of their app passwords. Wrong ones count against the client's +// budget of failed sign-ins, as passwords do, but only once they failed: +// app passwords are too long to guess even in parallel, and sync apps send +// many requests at once, which counting each before it runs would refuse. +func (s *Server) davSignIn(c fiber.Ctx) error { + username, password, ok := basicAuth(c.Get(fiber.HeaderAuthorization)) + if !ok { + return davUnauthorized(c) + } + client := clientKey(c.IP()) + if s.limiter != nil { + if wait := s.limiter.attempt(client, false, time.Now()); wait > 0 { + retryAfter(c, wait) + return explain(fiber.ErrTooManyRequests, tooMany) + } + } + ctx, cancel := dbContext(c) + u, err := s.auth.AppPasswordUser(ctx, username, password) + cancel() + switch { + case errors.Is(err, auth.ErrNotFound): + s.log.Warn("app password refused", zap.String("ip", c.IP())) + if s.limiter != nil { + s.limiter.attempt(client, true, time.Now()) + } + return davUnauthorized(c) + case err != nil: + return err + } + c.Locals(sessionKey, &session{user: u}) + return nil +} + +func davUnauthorized(c fiber.Ctx) error { + c.Set(fiber.HeaderWWWAuthenticate, `Basic realm="goftp", charset="UTF-8"`) + return explain(fiber.ErrUnauthorized, "Sign in with your username and an app password: make one under Sync.") +} + +// basicAuth returns the username and password of an "Authorization: Basic" +// header. +func basicAuth(h string) (username, password string, ok bool) { + const prefix = "basic " + if len(h) <= len(prefix) || !strings.EqualFold(h[:len(prefix)], prefix) { + return "", "", false + } + b, err := base64.StdEncoding.DecodeString(strings.TrimSpace(h[len(prefix):])) + if err != nil { + return "", "", false + } + return strings.Cut(string(b), ":") +} + +// davBody reads the XML body of a WebDAV request. +func (s *Server) davBody(c fiber.Ctx) ([]byte, error) { + body := s.requestBody(c) + data, err := io.ReadAll(io.LimitReader(body, davBodyLimit+1)) + if err != nil { + return nil, s.uploadError(body, err) + } + if len(data) > davBodyLimit { + return nil, fiber.ErrRequestEntityTooLarge + } + s.finishBody(c, body) + return data, nil +} + +// 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. +func appleDouble(name string) bool { + return strings.HasPrefix(name, "._") || name == ".DS_Store" +} + +// statusOf returns the HTTP status of err. +func statusOf(err error) int { + var fe *fiber.Error + if errors.As(err, &fe) { + return fe.Code + } + return fiber.StatusInternalServerError +} + +// davResource is an entry as PROPFIND describes it. +type davResource struct { + path string // URL path + dir bool + size int64 + mod time.Time + real string // URL path where it really lives; "" for the way to a home +} + +// propfind is the body of a PROPFIND request: allprop, propname, or the +// properties asked for. +type propfind struct { + XMLName xml.Name `xml:"DAV: propfind"` + Allprop *struct{} `xml:"DAV: allprop"` + Propname *struct{} `xml:"DAV: propname"` + Prop *struct { + Names []xmlName `xml:",any"` + } `xml:"DAV: prop"` +} + +type xmlName struct { + XMLName xml.Name + Value string `xml:",chardata"` +} + +// davProps are the properties allprop returns. +var davProps = []string{"resourcetype", "getcontentlength", "getlastmodified", "creationdate", "getetag", "displayname", + "getcontenttype", "supportedlock", "lockdiscovery"} + +func (s *Server) davPropfind(c fiber.Ctx, urlPath string) error { + depth := c.Get("Depth") + if depth == "" { + depth = "1" + } + if depth != "0" && depth != "1" { + return davError(c, fiber.StatusForbidden, "propfind-finite-depth") + } + data, err := s.davBody(c) + if err != nil { + return err + } + var req propfind + if len(bytes.TrimSpace(data)) > 0 { + if err := xml.Unmarshal(data, &req); err != nil { + return fiber.ErrBadRequest + } + } + allow := s.visitorRules(c) + var res []davResource + f, info, realPath, err := s.lookup(urlPath, allow) + switch { + case err == nil: + res = append(res, davResource{path: urlPath, dir: info.IsDir(), size: info.Size(), mod: info.ModTime(), real: realPath}) + if info.IsDir() && depth == "1" { + items, err := s.folderItems(f, urlPath, realPath, allow) + if err != nil { + _ = f.Close() + return err + } + for _, it := range items { + res = append(res, davResource{path: it.Path, dir: it.IsDir, size: it.size, mod: it.mod, real: it.real}) + } + } + _ = f.Close() + case statusOf(err) == fiber.StatusNotFound && s.towardHome(c, urlPath) != "": + res = append(res, s.wayHome(urlPath)) + default: + return err + } + // Folders that rules close can still be the way to one's home. + if next := s.towardHome(c, urlPath); depth == "1" && res[0].dir && next != "" && + !slices.ContainsFunc(res[1:], func(r davResource) bool { return r.path == next }) { + res = append(res, s.wayHome(next)) + } + + var b bytes.Buffer + b.WriteString(xml.Header + ``) + for _, r := range res { + b.WriteString("") + xmlText(&b, davHref(r.path, r.dir)) + b.WriteString("") + switch { + case req.Propname != nil: + b.WriteString("") + for _, name := range davProps { + b.WriteString("") + } + b.WriteString("HTTP/1.1 200 OK") + case req.Prop != nil: + var found, missing bytes.Buffer + for _, n := range req.Prop.Names { + if v, ok := s.davProp(c, r, n.XMLName); ok { + found.WriteString(v) + } else { + emptyElement(&missing, n.XMLName) + } + } + propstat(&b, found.String(), "200 OK") + propstat(&b, missing.String(), "404 Not Found") + default: + var found bytes.Buffer + for _, name := range davProps { + if v, ok := s.davProp(c, r, xml.Name{Space: "DAV:", Local: name}); ok { + found.WriteString(v) + } + } + propstat(&b, found.String(), "200 OK") + } + b.WriteString("") + } + b.WriteString("") + c.Set(fiber.HeaderContentType, "application/xml; charset=utf-8") + return c.Status(fiber.StatusMultiStatus).Send(b.Bytes()) +} + +// towardHome returns, for the folder at urlPath on the way to the home of +// the visitor of c, the next folder on that way; "" for other paths. +func (s *Server) towardHome(c fiber.Ctx, urlPath string) string { + home := s.homeOf(userOf(c)) + rest, ok := strings.CutPrefix(home, strings.TrimSuffix(urlPath, "/")+"/") + if home == "" || !ok || rest == "" { + return "" + } + next, _, _ := strings.Cut(rest, "/") + return path.Join(urlPath, next) +} + +// wayHome describes the folder at urlPath on the way to a home, which says +// nothing of what else it holds. +func (s *Server) wayHome(urlPath string) davResource { + r := davResource{path: urlPath, dir: true, mod: time.Now()} + if info, err := s.root.Stat(rootName(urlPath)); err == nil { + r.mod = info.ModTime() + } + return r +} + +// davProp returns the property name of r, as XML, if it has it. +func (s *Server) davProp(c fiber.Ctx, r davResource, name xml.Name) (string, bool) { + if name.Space != "DAV:" { + return "", false + } + var v string + switch name.Local { + case "resourcetype": + if r.dir { + v = "" + } + case "getcontentlength": + if r.dir { + return "", false + } + v = strconv.FormatInt(r.size, 10) + case "getlastmodified": + v = r.mod.UTC().Format(http.TimeFormat) + case "creationdate": + v = r.mod.UTC().Format(time.RFC3339) + case "getetag": + v = xmlEscape(etagOf(r.mod, r.size)) + case "displayname": + v = xmlEscape(path.Base(r.path)) + if r.path == "/" { + v = "" + } + case "getcontenttype": + if r.dir { + return "", false + } + v = xmlEscape(contentTypeOf(r.path)) + case "supportedlock": + v = "" + + "" + case "lockdiscovery": + v = s.lockDiscovery(r.path) + case "quota-used-bytes", "quota-available-bytes": + used, avail, ok := s.davQuota(r) + if !ok || name.Local == "quota-used-bytes" && used < 0 { + return "", false + } + v = strconv.FormatInt(avail, 10) + if name.Local == "quota-used-bytes" { + v = strconv.FormatInt(used, 10) + } + default: + return "", false + } + return "" + v + "", true +} + +// davQuota returns how much the folder r holds and how much more it may +// (RFC 4331): in a home with a quota, against it; elsewhere, how much the +// disk takes before upload.min_free, with used -1 as it is not counted. +func (s *Server) davQuota(r davResource) (used, avail int64, ok bool) { + if !r.dir || r.real == "" { + return 0, 0, false + } + if name, isHome := s.homeName(r.real); isHome { + if u, _, q, err := s.homeUsage(name); err == nil && q > 0 { + return u, max(q-u, 0), true + } + } + free, known := freeSpace(s.root) + if !known { + return 0, 0, false + } + return -1, max(free-int64(s.cfg.Upload.MinFree), 0), true +} + +// contentTypeOf guesses the type of the file at urlPath from its name. +func contentTypeOf(urlPath string) string { + if t := mime.TypeByExtension(path.Ext(urlPath)); t != "" { + return t + } + return "application/octet-stream" +} + +func propstat(b *bytes.Buffer, props, status string) { + if props == "" { + return + } + b.WriteString("" + props + "HTTP/1.1 " + status + "") +} + +// emptyElement writes an element named name, without content. +func emptyElement(b *bytes.Buffer, name xml.Name) { + if name.Space == "DAV:" { + b.WriteString("") + return + } + b.WriteString("`) +} + +func xmlText(b *bytes.Buffer, s string) { _ = xml.EscapeText(b, []byte(s)) } + +func xmlEscape(s string) string { + var b bytes.Buffer + xmlText(&b, s) + return b.String() +} + +// davError answers with a WebDAV error element (RFC 4918 section 16). +func davError(c fiber.Ctx, status int, condition string) error { + c.Set(fiber.HeaderContentType, "application/xml; charset=utf-8") + return c.Status(status).SendString(xml.Header + `") +} + +func (s *Server) davGet(c fiber.Ctx, urlPath string) error { + f, info, _, err := s.lookup(urlPath, s.visitorRules(c)) + if err != nil { + return err + } + if info.IsDir() { + // Folders are for browsers to show. + _ = f.Close() + return s.redirect(c, escapePath(object(urlPath, true))) + } + return s.serveFile(c, f, info, path.Base(urlPath), nil) +} + +func (s *Server) davPut(c fiber.Ctx, urlPath string) error { + if appleDouble(path.Base(urlPath)) { + _, _ = s.davBody(c) + return c.SendStatus(fiber.StatusCreated) + } + if urlPath == "/" { + return fiber.ErrMethodNotAllowed + } + length := int64(c.Request().Header.ContentLength()) + if length < 0 { + // Finder sends files in chunks, saying how long they are. + n, err := strconv.ParseInt(c.Get("X-Expected-Entity-Length"), 10, 64) + if err != nil || n < 0 { + return fiber.ErrLengthRequired + } + length = n + } + mtime, keep := unixTime(c.Get("X-OC-Mtime")) + created, err := s.putFile(c, urlPath, length, mtime) + if err != nil { + return err + } + 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") + } + if created { + return c.SendStatus(fiber.StatusCreated) + } + return c.SendStatus(fiber.StatusNoContent) +} + +// unixTime parses a time in seconds since 1970, as the X-OC-Mtime header of +// ownCloud's clients and rclone gives a file's modification time. +func unixTime(v string) (time.Time, bool) { + secs, err := strconv.ParseFloat(strings.TrimSpace(v), 64) + if err != nil || secs <= 0 || secs > 1<<40 { + return time.Time{}, false + } + whole := int64(secs) + return time.Unix(whole, int64((secs-float64(whole))*1e9)), true +} + +func (s *Server) davDelete(c fiber.Ctx, urlPath string) error { + if urlPath == "/" { + return fiber.ErrForbidden + } + dirPath, name := path.Split(urlPath) + if appleDouble(name) { + return c.SendStatus(fiber.StatusNoContent) + } + if _, err := s.remove(c, dirPath, name, false); err != nil { + return err + } + s.dropLocks(urlPath) + return c.SendStatus(fiber.StatusNoContent) +} + +func (s *Server) davMkcol(c fiber.Ctx, urlPath string) error { + data, err := s.davBody(c) + if err != nil { + return err + } + if len(data) > 0 { + return fiber.ErrUnsupportedMediaType + } + if urlPath == "/" { + return fiber.ErrMethodNotAllowed + } + dirPath, name := path.Split(urlPath) + switch err := s.makeFolder(c, dirPath, name); statusOf(err) { + case fiber.StatusConflict: + // Something has the name already. + return fiber.ErrMethodNotAllowed + case fiber.StatusNotFound: + // The folder it would be in is missing. + return fiber.ErrConflict + default: + if err != nil { + return err + } + } + return c.SendStatus(fiber.StatusCreated) +} + +// 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. +func (s *Server) davCopyMove(c fiber.Ctx, urlPath string, move bool) error { + dst, ok := davDestination(c.Get("Destination")) + switch { + case !ok: + 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. + return explain(fiber.ErrConflict, "An entry cannot go where it is, or into itself.") + } + overwrite := !strings.EqualFold(strings.TrimSpace(c.Get("Overwrite")), "F") + depth := c.Get("Depth") + allow := s.visitorRules(c) + f, info, _, err := s.lookup(urlPath, allow) + if err != nil { + return err + } + _ = f.Close() + 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 + } + // 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 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 + } + 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) + 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 + } + if err != nil { + return err + } + if move { + s.dropLocks(urlPath) + } + if replaced { + return c.SendStatus(fiber.StatusNoContent) + } + return c.SendStatus(fiber.StatusCreated) +} + +// 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. +func davDestination(h string) (string, bool) { + u, err := url.Parse(strings.TrimSpace(h)) + if err != nil || h == "" { + return "", false + } + return davPath(u.EscapedPath()) +} + +// propertyupdate is the body of a PROPPATCH request. +type propertyupdate struct { + XMLName xml.Name `xml:"DAV: propertyupdate"` + Set []propGroup `xml:"DAV: set"` + Remove []propGroup `xml:"DAV: remove"` +} + +type propGroup struct { + Prop struct { + Props []xmlName `xml:",any"` + } `xml:"DAV: prop"` +} + +// mtimeProps name the properties that clients set a file's modification +// time with: WebDAV's own, Windows', and those of other clients. +var mtimeProps = []string{"getlastmodified", "lastmodified", "win32lastmodifiedtime", "modificationdate", "mtime"} + +// ignoredProps are properties Windows sets after it writes a file, which +// goftp has no use for but takes, as Windows reports failure otherwise. +var ignoredProps = []string{"win32creationtime", "win32lastaccesstime", "win32fileattributes"} + +// davProppatch sets the modification time of the entry at urlPath, as sync +// apps and file managers ask to after uploading; other properties are not +// kept. +func (s *Server) davProppatch(c fiber.Ctx, urlPath string) error { + data, err := s.davBody(c) + if err != nil { + return err + } + var req propertyupdate + if err := xml.Unmarshal(data, &req); err != nil { + return fiber.ErrBadRequest + } + may, isDir, realPath := true, false, "" + if !appleDouble(path.Base(urlPath)) { + f, info, real, err := s.lookup(urlPath, s.visitorRules(c)) + if err != nil { + return err + } + _ = f.Close() + 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 + } + var ok, forbidden, bad bytes.Buffer + var mtime time.Time + for _, set := range req.Set { + for _, p := range set.Prop.Props { + name := strings.ToLower(p.XMLName.Local) + switch { + case !may: + emptyElement(&forbidden, p.XMLName) + case slices.Contains(mtimeProps, name): + if t, valid := davTime(p.Value); valid { + mtime = t + emptyElement(&ok, p.XMLName) + } else { + emptyElement(&bad, p.XMLName) + } + case slices.Contains(ignoredProps, name): + emptyElement(&ok, p.XMLName) + default: + emptyElement(&forbidden, p.XMLName) + } + } + } + for _, remove := range req.Remove { + for _, p := range remove.Prop.Props { + emptyElement(&forbidden, p.XMLName) + } + } + if !mtime.IsZero() && realPath != "" { + if err := s.root.Chtimes(rootName(realPath), time.Now(), mtime); err != nil { + return s.openError(err) + } + } + var b bytes.Buffer + b.WriteString(xml.Header + ``) + xmlText(&b, davHref(urlPath, isDir)) + b.WriteString("") + propstat(&b, ok.String(), "200 OK") + propstat(&b, forbidden.String(), "403 Forbidden") + propstat(&b, bad.String(), "409 Conflict") + b.WriteString("") + c.Set(fiber.HeaderContentType, "application/xml; charset=utf-8") + return c.Status(fiber.StatusMultiStatus).Send(b.Bytes()) +} + +// davTime parses a modification time as clients send it: an HTTP date, +// RFC 3339, or seconds since 1970. +func davTime(v string) (time.Time, bool) { + v = strings.TrimSpace(v) + if t, err := http.ParseTime(v); err == nil { + return t, true + } + if t, err := time.Parse(time.RFC3339Nano, v); err == nil { + return t, true + } + return unixTime(v) +} + +// davLock is an advisory WebDAV lock. +type davLock struct { + token, path, owner string + exclusive, deep bool // deep: it covers what is in the folder too + timeout time.Duration + expires time.Time +} + +const ( + maxLockTimeout = time.Hour + maxLocks = 10000 +) + +// lockinfo is the body of a LOCK request. +type lockinfo struct { + XMLName xml.Name `xml:"DAV: lockinfo"` + Exclusive *struct{} `xml:"DAV: lockscope>exclusive"` + Owner struct { + Href string `xml:"DAV: href"` + Text string `xml:",chardata"` + } `xml:"DAV: owner"` +} + +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) + } + } + 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) + } + var req lockinfo + if err := xml.Unmarshal(data, &req); err != nil { + return fiber.ErrBadRequest + } + create, replace, err := s.uploadRights(c, urlPath) + if err != nil { + return err + } + 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") + } + } + if len(s.davLocks) >= maxLocks { + return fiber.ErrServiceUnavailable + } + 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) +} + +// covers reports whether the lock l covers the entry at urlPath. +func covers(l *davLock, urlPath string) bool { + return l.path == urlPath || l.deep && within(urlPath, l.path) +} + +// lockTimeout reads the Timeout header of a LOCK request (e.g. +// "Second-600"), up to maxLockTimeout. +func lockTimeout(h string) time.Duration { + for v := range strings.SplitSeq(h, ",") { + if n, ok := strings.CutPrefix(strings.TrimSpace(v), "Second-"); ok { + if secs, err := strconv.Atoi(n); err == nil && secs > 0 { + return min(time.Duration(secs)*time.Second, maxLockTimeout) + } + } + } + return maxLockTimeout +} + +// ifToken returns the first lock token of an If header. +func ifToken(h string) string { + _, rest, ok := strings.Cut(h, "") + return "opaquelocktoken:" + token +} + +func (s *Server) sendLock(c fiber.Ctx, status int, l *davLock) error { + c.Set(fiber.HeaderContentType, "application/xml; charset=utf-8") + return c.Status(status).SendString(xml.Header + `` + activeLock(l) + + "") +} + +func activeLock(l *davLock) string { + scope, depth := "", "0" + if l.exclusive { + scope = "" + } + if l.deep { + depth = "infinity" + } + owner := "" + if l.owner != "" { + owner = "" + xmlEscape(l.owner) + "" + } + return "" + scope + "" + depth + + "" + owner + "Second-" + strconv.Itoa(int(l.timeout.Seconds())) + "" + + l.token + "" + xmlEscape(davHref(l.path, false)) + "" +} + +// lockDiscovery describes the locks on the entry at urlPath. +func (s *Server) lockDiscovery(urlPath string) string { + s.davMu.Lock() + defer s.davMu.Unlock() + var b strings.Builder + now := time.Now() + for _, l := range s.davLocks { + if l.path == urlPath && now.Before(l.expires) { + b.WriteString(activeLock(l)) + } + } + return b.String() +} + +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 { + return davError(c, fiber.StatusConflict, "lock-token-matches-request-uri") + } + delete(s.davLocks, token) + return c.SendStatus(fiber.StatusNoContent) +} + +// dropLocks ends the locks on the entry at urlPath and what is in it, which +// was moved or deleted. +func (s *Server) dropLocks(urlPath string) { + s.davMu.Lock() + defer s.davMu.Unlock() + for token, l := range s.davLocks { + if within(l.path, urlPath) { + delete(s.davLocks, token) + } + } +} diff --git a/internal/server/dav_test.go b/internal/server/dav_test.go new file mode 100644 index 0000000..7e73768 --- /dev/null +++ b/internal/server/dav_test.go @@ -0,0 +1,840 @@ +package server + +import ( + "bufio" + "encoding/base64" + "encoding/json" + "encoding/xml" + "fmt" + "io" + "maps" + "net" + "net/http" + "net/http/httptest" + "net/url" + "os" + "path/filepath" + "regexp" + "slices" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/gofiber/fiber/v3" + + "goftp/internal/auth" + "goftp/internal/config" +) + +// davClient sends requests to a fixture's WebDAV, signed in with an app +// password. +type davClient struct { + f *fixture + auth string // the Authorization header +} + +func basicAuthHeader(username, password string) string { + return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)) +} + +// davAs returns a client of f's WebDAV that signs in as the user name, with +// a new app password. +func davAs(t *testing.T, f *fixture, name string) *davClient { + t.Helper() + u, err := f.auth.svc.UserNamed(t.Context(), name) + if err != nil { + t.Fatal(err) + } + password, _, err := f.auth.svc.CreateAppPassword(t.Context(), u, "test") + if err != nil { + t.Fatal(err) + } + return &davClient{f: f, auth: basicAuthHeader(name, password)} +} + +// newDavFixture serves a temporary directory with users of its own, who may +// get app passwords, and returns a WebDAV client of the superadmin. +func newDavFixture(t *testing.T, mutate ...func(*config.Config)) (*fixture, *davClient) { + t.Helper() + f := newFixtureWith(t, newTestAuth(t), mutate...) + return f, davAs(t, f, userOfRole("superadmin")) +} + +func (d *davClient) do(t *testing.T, method, uri string, headers ...string) (*http.Response, string) { + t.Helper() + return d.send(t, method, uri, nil, headers...) +} + +func (d *davClient) send(t *testing.T, method, uri string, body io.Reader, headers ...string) (*http.Response, string) { + t.Helper() + return d.f.send(t, method, uri, body, append([]string{"Authorization", d.auth}, headers...)...) +} + +// davProperties are the properties of an entry that tests look at. +type davProperties struct { + Collection *struct{} `xml:"resourcetype>collection"` + Length string `xml:"getcontentlength"` + Modified string `xml:"getlastmodified"` + ETag string `xml:"getetag"` + Name string `xml:"displayname"` + Type string `xml:"getcontenttype"` + Used string `xml:"quota-used-bytes"` + Available string `xml:"quota-available-bytes"` + Locks []string `xml:"lockdiscovery>activelock>locktoken>href"` + Raw string `xml:",innerxml"` +} + +type multistatus struct { + Responses []struct { + Href string `xml:"DAV: href"` + Propstats []struct { + Prop davProperties `xml:"DAV: prop"` + Status string `xml:"DAV: status"` + } `xml:"DAV: propstat"` + } `xml:"DAV: response"` +} + +// davEntry is what a multistatus says of an entry: its properties by the +// status given for them. +type davEntry map[int]davProperties + +// parseMultistatus returns the entries of a multistatus by their decoded +// paths. +func parseMultistatus(t *testing.T, body string) map[string]davEntry { + t.Helper() + var ms multistatus + if err := xml.Unmarshal([]byte(body), &ms); err != nil { + t.Fatalf("%v: %s", err, body) + } + entries := map[string]davEntry{} + for _, r := range ms.Responses { + p, err := url.PathUnescape(r.Href) + if err != nil { + t.Fatal(err) + } + e := davEntry{} + for _, ps := range r.Propstats { + fields := strings.Fields(ps.Status) + if len(fields) < 2 { + t.Fatalf("status %q", ps.Status) + } + code, _ := strconv.Atoi(fields[1]) + e[code] = ps.Prop + } + entries[p] = e + } + return entries +} + +func (d *davClient) propfind(t *testing.T, uri, depth, body string) map[string]davEntry { + t.Helper() + resp, out := d.send(t, "PROPFIND", uri, strings.NewReader(body), "Depth", depth, "Content-Type", "application/xml") + expectStatus(t, resp, http.StatusMultiStatus) + if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/xml") { + t.Errorf("Content-Type %q", ct) + } + return parseMultistatus(t, out) +} + +func mtimeOf(t *testing.T, p string) time.Time { + t.Helper() + info, err := os.Stat(p) + if err != nil { + t.Fatal(err) + } + return info.ModTime() +} + +func TestDavSignIn(t *testing.T) { + f, _ := newDavFixture(t, func(c *config.Config) { c.Limiter = config.LimiterConfig{MaxFailures: 4, Window: time.Minute} }) + ctx := t.Context() + svc := f.auth.svc + + // Clients ask what the server can do before they sign in. + resp, _ := f.as("").do(t, "OPTIONS", "/.dav/") + expectStatus(t, resp, http.StatusOK) + if resp.Header.Get("DAV") != "1, 2" || !strings.Contains(resp.Header.Get("Allow"), "PROPFIND") { + t.Errorf("OPTIONS: %v", resp.Header) + } + + name := userOfRole("user") + u, err := svc.UserNamed(ctx, name) + if err != nil { + t.Fatal(err) + } + password, _, err := svc.CreateAppPassword(ctx, u, "laptop") + if err != nil { + t.Fatal(err) + } + propfind := func(authorization string) (*http.Response, string) { + t.Helper() + return f.send(t, "PROPFIND", "/.dav/", nil, "Authorization", authorization, "Depth", "0") + } + refused := func(what string, resp *http.Response, body string) { + t.Helper() + if resp.StatusCode != http.StatusUnauthorized || resp.Header.Get("WWW-Authenticate") != `Basic realm="goftp", charset="UTF-8"` || + !strings.Contains(body, "app password") { + t.Errorf("%s: %d %v %q", what, resp.StatusCode, resp.Header, body) + } + } + // Only app passwords sign in, not sessions or the account's password. + for _, h := range []string{"", "Bearer " + f.auth.tokens["user"], "Basic !!!", basicAuthHeader(name, testPassword(name))} { + resp, body := propfind(h) + refused(h, resp, body) + } + // However they were copied. + for _, p := range []string{password, strings.ToUpper(password), strings.ReplaceAll(password, "-", "")} { + resp, _ := propfind(basicAuthHeader(name, p)) + expectStatus(t, resp, http.StatusMultiStatus) + } + // Sync apps send many requests at once, which do not count as + // failures while they run. + codes := make([]int, 12) + var wg sync.WaitGroup + for i := range codes { + wg.Go(func() { + req := httptest.NewRequest("PROPFIND", "/.dav/", nil) + req.Header.Set("Authorization", basicAuthHeader(name, password)) + req.Header.Set("Depth", "0") + if resp, err := f.srv.App().Test(req, fiber.TestConfig{Timeout: 5 * time.Second}); err == nil { + codes[i] = resp.StatusCode + _ = resp.Body.Close() + } + }) + } + wg.Wait() + if slices.ContainsFunc(codes, func(code int) bool { return code != http.StatusMultiStatus }) { + t.Errorf("at once: %v", codes) + } + + // Revoked ones stop working, and so do all once the password changes. + revoked, ap, err := svc.CreateAppPassword(ctx, u, "phone") + if err != nil { + t.Fatal(err) + } + if err := svc.RevokeAppPassword(ctx, u, ap.ID); err != nil { + t.Fatal(err) + } + resp, body := propfind(basicAuthHeader(name, revoked)) + refused("revoked", resp, body) + sess, err := svc.Login(ctx, name, testPassword(name)) + if err != nil { + t.Fatal(err) + } + if _, err := svc.ChangePassword(ctx, sess.User, testPassword(name), "another long password"); err != nil { + t.Fatal(err) + } + resp, body = propfind(basicAuthHeader(name, password)) + refused("after a password change", resp, body) + if n := len(f.logs.entries("app password refused")); n != 3 { + t.Errorf("%d refusals logged", n) + } + + // Failures count against the client as wrong passwords do. + fresh, _, err := svc.CreateAppPassword(ctx, u, "new laptop") + if err != nil { + t.Fatal(err) + } + resp, _ = propfind(basicAuthHeader(name, strings.Repeat("a", 24))) + expectStatus(t, resp, http.StatusUnauthorized) + resp, _ = propfind(basicAuthHeader(name, fresh)) + if resp.StatusCode != http.StatusTooManyRequests || resp.Header.Get("Retry-After") == "" { + t.Errorf("after 4 failures: %d %v", resp.StatusCode, resp.Header) + } +} + +func TestDavPropfind(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "hello") + f.write(t, "docs/.secret", "x") + f.write(t, "docs/sub/b.txt", "b") + f.write(t, "docs/naïve.txt", "n") + mod := time.Date(2023, 11, 14, 22, 13, 20, 0, time.UTC) + if err := os.Chtimes(filepath.Join(f.dir, "docs", "a.txt"), mod, mod); err != nil { + t.Fatal(err) + } + + got := dav.propfind(t, "/.dav/docs/", "1", "") + if paths := slices.Sorted(maps.Keys(got)); !slices.Equal(paths, []string{"/.dav/docs/", "/.dav/docs/a.txt", "/.dav/docs/naïve.txt", "/.dav/docs/sub/"}) { + t.Fatalf("listed %v", paths) + } + if a := got["/.dav/docs/a.txt"][200]; a.Collection != nil || a.Length != "5" || a.Modified != "Tue, 14 Nov 2023 22:13:20 GMT" || + a.Name != "a.txt" || !strings.HasPrefix(a.Type, "text/plain") || a.ETag != etagOf(mod, 5) { + t.Errorf("a.txt: %+v", a) + } + if sub := got["/.dav/docs/sub/"][200]; sub.Collection == nil || sub.Length != "" || sub.Name != "sub" { + t.Errorf("sub: %+v", sub) + } + if got := dav.propfind(t, "/.dav/docs/", "0", ""); len(got) != 1 || got["/.dav/docs/"][200].Collection == nil { + t.Errorf("depth 0: %v", got) + } + if got := dav.propfind(t, "/.dav", "1", ""); got["/.dav/"][200].Collection == nil || got["/.dav/docs/"][200].Collection == nil { + t.Errorf("the root: %v", got) + } + resp, body := dav.do(t, "PROPFIND", "/.dav/docs/", "Depth", "infinity") + if resp.StatusCode != http.StatusForbidden || !strings.Contains(body, "propfind-finite-depth") { + t.Errorf("depth infinity: %d %s", resp.StatusCode, body) + } + + // Properties asked for by name; those it lacks are listed as missing. + e := dav.propfind(t, "/.dav/docs/a.txt", "0", ` +`)["/.dav/docs/a.txt"] + if e[200].Length != "5" || e[200].Modified != "" || !strings.Contains(e[404].Raw, `color xmlns:x="urn:x"`) { + t.Errorf("by name: %+v", e) + } + e = dav.propfind(t, "/.dav/docs/a.txt", "0", ``)["/.dav/docs/a.txt"] + if !strings.Contains(e[200].Raw, "") || e[200].Length != "" { + t.Errorf("propname: %+v", e) + } + for uri, want := range map[string]int{"/.dav/docs/none.txt": 404, "/.dav/docs/.secret": 404} { + if resp, _ := dav.do(t, "PROPFIND", uri, "Depth", "0"); resp.StatusCode != want { + t.Errorf("%s: %d", uri, resp.StatusCode) + } + } + if resp, _ := dav.send(t, "PROPFIND", "/.dav/docs/", strings.NewReader(" time.Minute || resp.Header.Get("X-OC-Mtime") != "" { + t.Errorf("without a time: %v", got) + } + + // Finder sends files in chunks, saying how long they are. + addr, _ := startServer(t, f) + chunked := func(name, content, length string) (*http.Response, string) { + t.Helper() + conn, err := net.Dial("tcp", addr) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + if length != "" { + length = "X-Expected-Entity-Length: " + length + "\r\n" + } + fmt.Fprintf(conn, "PUT /.dav/docs/%s HTTP/1.1\r\nHost: t\r\nAuthorization: %s\r\n%sTransfer-Encoding: chunked\r\n\r\n%x\r\n%s\r\n0\r\n\r\n", + name, dav.auth, length, len(content), content) + resp, err := http.ReadResponse(bufio.NewReader(conn), nil) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + return resp, string(body) + } + resp, _ = chunked("finder.txt", "chunked", "7") + expectStatus(t, resp, http.StatusCreated) + if got := readFile(t, f.dir, "docs/finder.txt"); got != "chunked" { + t.Errorf("chunked: %q", got) + } + if resp, _ := chunked("unknown.txt", "x", ""); resp.StatusCode != http.StatusLengthRequired { + t.Errorf("without a length: %d", resp.StatusCode) + } + resp, body := chunked("short.txt", "short", "10") + if resp.StatusCode != http.StatusBadRequest || !strings.Contains(body, "not as long") { + t.Errorf("shorter than said: %d %s", resp.StatusCode, body) + } + if exists(filepath.Join(docs, "short.txt")) || len(leftovers(f.dir)) != 0 { + t.Errorf("left behind: %v", leftovers(f.dir)) + } + + resp, body = dav.send(t, "PUT", "/.dav/docs/new.txt", strings.NewReader("ab"), "Content-Range", "bytes 0-1/5") + if resp.StatusCode != http.StatusBadRequest || readFile(t, f.dir, "docs/new.txt") != "newer" { + t.Errorf("a part: %d %s", resp.StatusCode, body) + } + + for uri, want := range map[string]int{ + "/.dav/none/x.txt": http.StatusConflict, // the folder is missing + "/.dav/docs/.env": http.StatusForbidden, // goftp hides names that start with a dot + "/.dav/": http.StatusMethodNotAllowed, + } { + if resp, _ := dav.send(t, "PUT", uri, strings.NewReader("x")); resp.StatusCode != want { + t.Errorf("PUT %s: %d, want %d", uri, resp.StatusCode, want) + } + } + // macOS's files beside files are taken and dropped. + for _, name := range []string{"._a.txt", ".DS_Store"} { + resp, _ := dav.send(t, "PUT", "/.dav/docs/"+name, strings.NewReader("mac")) + expectStatus(t, resp, http.StatusCreated) + if exists(filepath.Join(docs, name)) { + t.Errorf("%s was kept", name) + } + } + resp, body = dav.send(t, "PROPPATCH", "/.dav/docs/._a.txt", strings.NewReader(``+ + `Wed, 01 Jan 2020 00:00:00 GMT`)) + expectStatus(t, resp, http.StatusMultiStatus) + if resp, _ := dav.do(t, "DELETE", "/.dav/docs/._a.txt"); resp.StatusCode != http.StatusNoContent { + t.Errorf("DELETE ._a.txt: %d", resp.StatusCode) + } + + // Users upload where they may. + user := davAs(t, f, userOfRole("user")) + if resp, _ := user.send(t, "PUT", "/.dav/docs/mine.txt", strings.NewReader("x")); resp.StatusCode != http.StatusForbidden { + t.Errorf("a reader's PUT: %d", resp.StatusCode) + } + if exists(filepath.Join(docs, "mine.txt")) { + t.Error("uploaded anyway") + } +} + +func TestDavGet(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "hello") + + resp, body := dav.do(t, "GET", "/.dav/docs/a.txt") + if resp.StatusCode != http.StatusOK || body != "hello" { + t.Errorf("GET: %d %q", resp.StatusCode, body) + } + resp, body = dav.do(t, "GET", "/.dav/docs/a.txt", "Range", "bytes=1-2") + if resp.StatusCode != http.StatusPartialContent || body != "el" { + t.Errorf("a range: %d %q", resp.StatusCode, body) + } + resp, body = dav.do(t, "HEAD", "/.dav/docs/a.txt") + if resp.StatusCode != http.StatusOK || body != "" || resp.Header.Get("Content-Length") != "5" { + t.Errorf("HEAD: %d %q %v", resp.StatusCode, body, resp.Header) + } + // Folders are for browsers to show. + resp, _ = dav.do(t, "GET", "/.dav/docs/") + if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/docs/" { + t.Errorf("a folder: %d %v", resp.StatusCode, resp.Header) + } + if resp, _ := dav.do(t, "GET", "/.dav/docs/none.txt"); resp.StatusCode != http.StatusNotFound { + t.Errorf("missing: %d", resp.StatusCode) + } +} + +func TestDavMkcol(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "a") + + resp, _ := dav.do(t, "MKCOL", "/.dav/docs/new") + expectStatus(t, resp, http.StatusCreated) + if info, err := os.Stat(filepath.Join(f.dir, "docs", "new")); err != nil || !info.IsDir() { + t.Errorf("made %v, %v", info, err) + } + for uri, want := range map[string]int{ + "/.dav/docs/new": http.StatusMethodNotAllowed, // there already + "/.dav/docs/a.txt": http.StatusMethodNotAllowed, + "/.dav/none/new": http.StatusConflict, // the folder is missing + "/.dav/": http.StatusMethodNotAllowed, + } { + if resp, _ := dav.do(t, "MKCOL", uri); resp.StatusCode != want { + t.Errorf("MKCOL %s: %d, want %d", uri, resp.StatusCode, want) + } + } + if resp, _ := dav.send(t, "MKCOL", "/.dav/docs/x", strings.NewReader("")); resp.StatusCode != http.StatusUnsupportedMediaType { + t.Errorf("with a body: %d", resp.StatusCode) + } + if resp, _ := dav.do(t, "MKCOL", "/.dav/docs/.git"); resp.StatusCode < 400 || exists(filepath.Join(f.dir, "docs", ".git")) { + t.Errorf("a hidden name: %d", resp.StatusCode) + } + user := davAs(t, f, userOfRole("user")) + if resp, _ := user.do(t, "MKCOL", "/.dav/docs/mine"); resp.StatusCode != http.StatusForbidden { + t.Errorf("a reader's MKCOL: %d", resp.StatusCode) + } +} + +func TestDavCopyMove(t *testing.T) { + f, dav := newDavFixture(t, withTrash) + f.write(t, "docs/a.txt", "a") + f.write(t, "docs/b.txt", "b") + f.write(t, "docs/sub/c.txt", "c") + docs := filepath.Join(f.dir, "docs") + + // Destinations are URLs; their host is not looked at. + resp, _ := dav.do(t, "MOVE", "/.dav/docs/a.txt", "Destination", "https://elsewhere.example/.dav/docs/moved.txt") + expectStatus(t, resp, http.StatusCreated) + if exists(filepath.Join(docs, "a.txt")) || readFile(t, f.dir, "docs/moved.txt") != "a" { + t.Error("not moved") + } + // What is there is kept with "Overwrite: F", and otherwise replaced, + // into the trash. + 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 { + t.Error("replaced anyway") + } + 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 { + t.Error("not replaced") + } + trashed := len(trashedOnDisk(t, f)) + + // Folders, with what they hold, or alone with "Depth: 0". + resp, _ = dav.do(t, "COPY", "/.dav/docs/sub/", "Destination", "/.dav/docs/sub2/") + expectStatus(t, resp, http.StatusCreated) + resp, _ = dav.do(t, "COPY", "/.dav/docs/sub/", "Destination", "/.dav/docs/empty/", "Depth", "0") + expectStatus(t, resp, http.StatusCreated) + if readFile(t, f.dir, "docs/sub2/c.txt") != "c" || !exists(filepath.Join(docs, "empty")) || exists(filepath.Join(docs, "empty", "c.txt")) { + t.Error("folders not copied") + } + resp, _ = dav.do(t, "MOVE", "/.dav/docs/sub2/", "Destination", "/.dav/sub3/") + expectStatus(t, resp, http.StatusCreated) + if readFile(t, f.dir, "sub3/c.txt") != "c" || exists(filepath.Join(docs, "sub2")) { + t.Error("folder not moved") + } + resp, _ = dav.do(t, "MOVE", "/.dav/docs/b.txt", "Destination", "/.dav/docs/na%C3%AFve%20b.txt") + expectStatus(t, resp, http.StatusCreated) + if readFile(t, f.dir, "docs/naïve b.txt") != "b" { + t.Error("not moved to an escaped name") + } + + for _, tc := range []struct { + method, uri, dest string + want int + }{ + {"MOVE", "/.dav/docs/", "/.dav/docs/sub/in/", http.StatusConflict}, // into itself + {"MOVE", "/.dav/docs/sub/c.txt", "/.dav/docs/", http.StatusConflict}, // over what holds it + {"COPY", "/.dav/docs/sub/c.txt", "/.dav/docs", http.StatusConflict}, // the same + {"MOVE", "/.dav/docs/moved.txt", "", http.StatusBadRequest}, // nowhere + {"MOVE", "/.dav/docs/moved.txt", "/docs/x.txt", http.StatusBadRequest}, // not WebDAV + {"MOVE", "/.dav/docs/moved.txt", "/.dav/none/x.txt", http.StatusConflict}, + {"MOVE", "/.dav/docs/moved.txt", "/.dav/docs/.x", http.StatusForbidden}, + {"MOVE", "/.dav/docs/moved.txt", "/.dav/docs/moved.txt", http.StatusForbidden}, + {"MOVE", "/.dav/docs/none.txt", "/.dav/docs/x.txt", http.StatusNotFound}, + } { + resp, body := dav.do(t, tc.method, tc.uri, "Destination", tc.dest) + if resp.StatusCode != tc.want { + t.Errorf("%s %s to %q: %d, want %d: %s", tc.method, tc.uri, tc.dest, resp.StatusCode, tc.want, body) + } + } + if !exists(filepath.Join(docs, "sub", "c.txt")) || len(trashedOnDisk(t, f)) != trashed { + t.Error("refused, but changed") + } + + // Users may not replace what they could not move. + user := davAs(t, f, userOfRole("user")) + resp, _ = user.do(t, "MOVE", "/.dav/docs/moved.txt", "Destination", "/.dav/docs/sub/c.txt") + expectStatus(t, resp, http.StatusForbidden) + if !exists(filepath.Join(docs, "moved.txt")) || !exists(filepath.Join(docs, "sub", "c.txt")) || len(trashedOnDisk(t, f)) != trashed { + t.Error("a reader moved") + } +} + +func TestDavDelete(t *testing.T) { + f, dav := newDavFixture(t, withTrash) + f.write(t, "docs/a.txt", "a") + f.write(t, "docs/sub/b.txt", "b") + + resp, _ := dav.do(t, "DELETE", "/.dav/docs/a.txt") + expectStatus(t, resp, http.StatusNoContent) + if exists(filepath.Join(f.dir, "docs", "a.txt")) || len(trashedOnDisk(t, f)) == 0 { + t.Error("not deleted into the trash") + } + user := davAs(t, f, userOfRole("user")) + for _, tc := range []struct { + dav *davClient + uri string + want int + }{ + {dav, "/.dav/docs/a.txt", http.StatusNotFound}, + {dav, "/.dav/", http.StatusForbidden}, + {user, "/.dav/docs/sub/", http.StatusForbidden}, + {dav, "/.dav/docs/sub/", http.StatusNoContent}, + } { + if resp, _ := tc.dav.do(t, "DELETE", tc.uri); resp.StatusCode != tc.want { + t.Errorf("DELETE %s: %d, want %d", tc.uri, resp.StatusCode, tc.want) + } + } + if exists(filepath.Join(f.dir, "docs", "sub")) { + t.Error("folder not deleted") + } +} + +func TestDavProppatch(t *testing.T) { + f, dav := newDavFixture(t) + f.write(t, "docs/a.txt", "a") + file := filepath.Join(f.dir, "docs", "a.txt") + proppatch := func(d *davClient, props string) davEntry { + t.Helper() + resp, body := d.send(t, "PROPPATCH", "/.dav/docs/a.txt", strings.NewReader(` +`+props+``), + "Content-Type", "application/xml") + expectStatus(t, resp, http.StatusMultiStatus) + return parseMultistatus(t, body)["/.dav/docs/a.txt"] + } + + // As Windows sets it after uploading: what goftp has no use for is + // taken anyway, what it cannot keep refused. + e := proppatch(dav, `Wed, 01 Jan 2020 00:00:00 GMT +Wed, 01 Jan 2020 00:00:00 GMT +00000020red`) + if !strings.Contains(e[200].Raw, "Win32LastModifiedTime") || !strings.Contains(e[200].Raw, "Win32FileAttributes") || + !strings.Contains(e[403].Raw, "color") || strings.Contains(e[200].Raw, "color") { + t.Errorf("Windows: %+v", e) + } + if got := mtimeOf(t, file); !got.Equal(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)) { + t.Errorf("kept %v", got) + } + e = proppatch(dav, `2021-02-03T04:05:06Z`) + if got := mtimeOf(t, file); !got.Equal(time.Date(2021, 2, 3, 4, 5, 6, 0, time.UTC)) || len(e) != 1 { + t.Errorf("RFC 3339: %v, %+v", got, e) + } + e = proppatch(dav, `yesterday`) + if !strings.Contains(e[409].Raw, "getlastmodified") || !mtimeOf(t, file).Equal(time.Date(2021, 2, 3, 4, 5, 6, 0, time.UTC)) { + t.Errorf("not a time: %+v", e) + } + // Only by those who may change the file. + e = proppatch(davAs(t, f, userOfRole("user")), `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 a reader: %+v", e) + } + if resp, _ := dav.send(t, "PROPPATCH", "/.dav/docs/a.txt", strings.NewReader("mailto:me@example.com`), + "Timeout", "Second-600", "Depth", depth) + } + + resp, body := lock(dav, "/.dav/docs/a.txt", "exclusive", "0") + expectStatus(t, resp, http.StatusOK) + token := strings.Trim(resp.Header.Get("Lock-Token"), "<>") + if !strings.HasPrefix(token, "opaquelocktoken:") || !strings.Contains(body, token) || !strings.Contains(body, "Second-600") || + !strings.Contains(body, "mailto:me@example.com") { + t.Errorf("locked: %s %s", token, body) + } + for _, tc := range []struct{ uri, scope, depth string }{ + {"/.dav/docs/a.txt", "exclusive", "0"}, + {"/.dav/docs/a.txt", "shared", "0"}, + {"/.dav/docs/", "exclusive", "infinity"}, // the folder, with the file in it + } { + if resp, body := lock(dav, tc.uri, tc.scope, tc.depth); resp.StatusCode != http.StatusLocked || !strings.Contains(body, "no-conflicting-lock") { + t.Errorf("%s lock of %s: %d %s", tc.scope, tc.uri, resp.StatusCode, body) + } + } + e := dav.propfind(t, "/.dav/docs/a.txt", "0", ``) + if locks := e["/.dav/docs/a.txt"][200].Locks; !slices.Equal(locks, []string{token}) { + t.Errorf("lockdiscovery: %v", locks) + } + + // Refreshed by its token. + resp, _ = dav.do(t, "LOCK", "/.dav/docs/a.txt", "If", "(<"+token+">)", "Timeout", "Second-60") + expectStatus(t, resp, http.StatusOK) + if resp, _ := dav.do(t, "LOCK", "/.dav/docs/a.txt", "If", "()"); resp.StatusCode != http.StatusPreconditionFailed { + t.Errorf("refreshed with another token: %d", resp.StatusCode) + } + // Ended by it. + if resp, _ := dav.do(t, "UNLOCK", "/.dav/docs/a.txt", "Lock-Token", ""); resp.StatusCode != http.StatusConflict { + t.Errorf("unlocked with another token: %d", resp.StatusCode) + } + resp, _ = dav.do(t, "UNLOCK", "/.dav/docs/a.txt", "Lock-Token", "<"+token+">") + expectStatus(t, resp, http.StatusNoContent) + resp, _ = lock(dav, "/.dav/docs/a.txt", "shared", "0") + expectStatus(t, resp, http.StatusOK) + // Deleting the file ends its locks. + resp, _ = dav.do(t, "DELETE", "/.dav/docs/a.txt") + expectStatus(t, resp, http.StatusNoContent) + if resp, _ := lock(dav, "/.dav/docs/a.txt", "exclusive", "0"); resp.StatusCode != http.StatusCreated { + t.Errorf("a new name: %d", resp.StatusCode) + } + if resp, _ := lock(davAs(t, f, userOfRole("user")), "/.dav/docs/b.txt", "exclusive", "0"); resp.StatusCode != http.StatusForbidden { + t.Errorf("by a reader: %d", resp.StatusCode) + } +} + +// WebDAV keeps homes as private, and as full, as the site does. +func TestDavHomes(t *testing.T) { + ta := newTestAuth(t) + f := newFixtureWith(t, ta, withHomes, func(cfg *config.Config) { cfg.Homes.Quota = 10 }) + ta.addUser(t, "dora", "user") + ta.addUser(t, "erin", "user") + dora, erin := davAs(t, f, "dora"), davAs(t, f, "erin") + + resp, _ := erin.send(t, "PUT", "/.dav/home/erin/e.txt", strings.NewReader("e")) + expectStatus(t, resp, http.StatusCreated) + if got := dora.propfind(t, "/.dav/", "1", ""); got["/.dav/home/"][200].Collection == nil { + t.Errorf("the root: %v", slices.Sorted(maps.Keys(got))) + } + if got := dora.propfind(t, "/.dav/home/", "1", ""); !slices.Equal(slices.Sorted(maps.Keys(got)), []string{"/.dav/home/", "/.dav/home/dora/"}) { + t.Errorf("homes: %v", slices.Sorted(maps.Keys(got))) + } + for _, tc := range []struct { + method, uri string + body io.Reader + }{ + {"PROPFIND", "/.dav/home/erin/", nil}, + {"GET", "/.dav/home/erin/e.txt", nil}, + {"PUT", "/.dav/home/erin/x.txt", strings.NewReader("x")}, + {"DELETE", "/.dav/home/erin/e.txt", nil}, + } { + if resp, _ := dora.send(t, tc.method, tc.uri, tc.body, "Depth", "0"); resp.StatusCode != http.StatusForbidden && + resp.StatusCode != http.StatusNotFound { + t.Errorf("%s %s: %d", tc.method, tc.uri, resp.StatusCode) + } + } + if readFile(t, f.dir, "home/erin/e.txt") != "e" || exists(filepath.Join(f.dir, "home", "erin", "x.txt")) { + t.Error("into someone else's home") + } + + quota := func() davProperties { + t.Helper() + return dora.propfind(t, "/.dav/home/dora/", "0", ``)["/.dav/home/dora/"][200] + } + if q := quota(); q.Used != "0" || q.Available != "10" { + t.Errorf("quota of an empty home: %+v", q) + } + resp, _ = dora.send(t, "PUT", "/.dav/home/dora/a.txt", strings.NewReader("123456")) + expectStatus(t, resp, http.StatusCreated) + resp, _ = dora.send(t, "PUT", "/.dav/home/dora/b.txt", strings.NewReader("12345")) + expectStatus(t, resp, http.StatusInsufficientStorage) + if q := quota(); q.Used != "6" || q.Available != "4" { + t.Errorf("quota: %+v", q) + } +} + +func TestSyncPage(t *testing.T) { + ta := newTestAuth(t) + f := newFixtureWith(t, ta, withHomes) + user, name := f.as("user"), userOfRole("user") + html := []string{"Accept", "text/html"} + syncForm := func(form string, headers ...string) (*http.Response, string) { + t.Helper() + return post(t, user, "/.sync/", form, headers...) + } + + resp, _ := f.as("").do(t, "GET", "/.sync/", html...) + if resp.StatusCode != http.StatusSeeOther || !strings.HasPrefix(resp.Header.Get("Location"), loginPath) { + t.Errorf("anonymous: %d %v", resp.StatusCode, resp.Header) + } + if _, body := user.do(t, "GET", "/", html...); !strings.Contains(body, `Sync`) { + t.Errorf("menu: %s", body) + } + resp, body := user.do(t, "GET", "/.sync/", html...) + expectStatus(t, resp, http.StatusOK) + home := "http://example.com/.dav/home/" + name + "/" + for _, want := range []string{ + `` + home + ``, + "The rest of the site is at http://example.com/.dav/", + "rclone config create goftp webdav url=" + home + " vendor=owncloud user=" + name + " pass=APP-PASSWORD --obscure", + "dav://example.com/.dav/home/" + name + "/", + "You have no app passwords", + "not served over HTTPS", + } { + if !strings.Contains(body, want) { + t.Errorf("no %q in %s", want, body) + } + } + + // A new app password is shown once, and signs in to WebDAV. + resp, body = syncForm("action=create&name=laptop", html...) + expectStatus(t, resp, http.StatusCreated) + m := regexp.MustCompile(`([a-z2-7]{4}(?:-[a-z2-7]{4}){5})`).FindStringSubmatch(body) + if m == nil || resp.Header.Get("Cache-Control") != "no-store" || !strings.Contains(body, "laptop") { + t.Fatalf("created: %v %s", resp.Header, body) + } + resp, _ = f.send(t, "PROPFIND", "/.dav/", nil, "Authorization", basicAuthHeader(name, m[1]), "Depth", "0") + expectStatus(t, resp, http.StatusMultiStatus) + _, body = user.do(t, "GET", "/.sync/", html...) + if !strings.Contains(body, "laptop") || strings.Contains(body, m[1]) || strings.Contains(body, `"faint">never`) { + t.Errorf("listed: %s", body) + } + + // Scripts get JSON. + resp, body = syncForm("action=create&name=phone") + expectStatus(t, resp, http.StatusCreated) + var created struct { + ID int64 + Name, Username, Password, URL string + } + if err := json.Unmarshal([]byte(body), &created); err != nil || created.Name != "phone" || created.Username != name || + created.URL != "http://example.com/.dav/" || len(created.Password) != 29 { + t.Errorf("JSON: %v %s", err, body) + } + if _, body := user.do(t, "GET", "/.sync/", html...); !strings.Contains(body, "phone") || !strings.Contains(body, `"faint">never`) { + t.Errorf("listed: %s", body) + } + + for _, tc := range []struct { + form string + headers []string + want int + says string + }{ + {"action=create&name=", html, http.StatusBadRequest, "Name the app password"}, + {"action=create&name=", nil, http.StatusBadRequest, "Name the app password"}, + {"action=revoke&id=99999", html, http.StatusNotFound, "no such app password"}, + {"action=revoke&id=x", nil, http.StatusBadRequest, ""}, + {"action=dance", nil, http.StatusBadRequest, ""}, + } { + if resp, body := syncForm(tc.form, tc.headers...); resp.StatusCode != tc.want || !strings.Contains(body, tc.says) { + t.Errorf("%s: %d %s", tc.form, resp.StatusCode, body) + } + } + + // Revoked, it stops working. + resp, _ = syncForm("action=revoke&id="+strconv.FormatInt(created.ID, 10), html...) + if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != "/.sync/?done=revoked" { + t.Errorf("revoked: %d %v", resp.StatusCode, resp.Header) + } + if _, body := user.do(t, "GET", "/.sync/?done=revoked", html...); !strings.Contains(body, "The app password was revoked") || + strings.Contains(body, "phone") { + t.Errorf("after revoking: %s", body) + } + resp, _ = f.send(t, "PROPFIND", "/.dav/", nil, "Authorization", basicAuthHeader(name, created.Password), "Depth", "0") + expectStatus(t, resp, http.StatusUnauthorized) + + // A user has only so many. + for i := 1; i < auth.MaxAppPasswords; i++ { + resp, _ := syncForm("action=create&name=app") + expectStatus(t, resp, http.StatusCreated) + } + if resp, body := syncForm("action=create&name=app", html...); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "revoke one") { + t.Errorf("too many: %d %s", resp.StatusCode, body) + } + + // Without a home, apps sync the site. + g := newFixtureWith(t, ta) + if _, body := g.as("user").do(t, "GET", "/.sync/", html...); !strings.Contains(body, `http://example.com/.dav/`) || + strings.Contains(body, "The rest of the site") { + t.Errorf("without homes: %s", body) + } +} diff --git a/internal/server/file.go b/internal/server/file.go index 4273bdf..e43f217 100644 --- a/internal/server/file.go +++ b/internal/server/file.go @@ -38,7 +38,7 @@ func (s *Server) serveFile(c fiber.Ctx, f *os.File, info fs.FileInfo, name strin func (s *Server) sendContent(c fiber.Ctx, f *os.File, info fs.FileInfo, name, ctype, inlinePolicy string, use func() error) error { size := info.Size() modTime := info.ModTime() - etag := `"` + strconv.FormatInt(modTime.UnixNano(), 16) + "-" + strconv.FormatInt(size, 16) + `"` + etag := etagOf(modTime, size) unchanged := notModified(c.Get(fiber.HeaderIfNoneMatch), c.Get(fiber.HeaderIfModifiedSince), etag, modTime) var ( ranges []byteRange @@ -101,6 +101,11 @@ func (s *Server) sendContent(c fiber.Ctx, f *os.File, info fs.FileInfo, name, ct } } +// etagOf is the entity tag of a file modified at mod, size bytes long. +func etagOf(mod time.Time, size int64) string { + return `"` + strconv.FormatInt(mod.UnixNano(), 16) + "-" + strconv.FormatInt(size, 16) + `"` +} + // 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/mkdir.go b/internal/server/mkdir.go index 7031b2b..dd1bce7 100644 --- a/internal/server/mkdir.go +++ b/internal/server/mkdir.go @@ -20,6 +20,15 @@ const badFolderName = "Folder names cannot be empty or very long, start with a d // field "folder" (see manage), e.g. curl -d folder=photos https://host/dir/ func (s *Server) mkdir(c fiber.Ctx, urlPath, name string) error { name = strings.TrimSpace(name) + if err := s.makeFolder(c, urlPath, name); err != nil { + return err + } + return s.done(c, urlPath, fiber.StatusCreated, escapePath(path.Join(urlPath, name)+"/"), "created="+url.QueryEscape(name)) +} + +// makeFolder creates the folder name in the directory at urlPath: 409 if +// something has that name, 404 if there is no such directory. +func (s *Server) makeFolder(c fiber.Ctx, urlPath, name string) error { if !validName(name) { return explain(fiber.ErrBadRequest, badFolderName) } @@ -55,5 +64,5 @@ func (s *Server) mkdir(c fiber.Ctx, urlPath, name string) error { syncDir(dir) s.log.Info("mkdir", zap.String("ip", c.IP()), zap.String("user", visitor(c)), zap.String("path", target)) - return s.done(c, urlPath, fiber.StatusCreated, escapePath(target+"/"), "created="+url.QueryEscape(name)) + return nil } diff --git a/internal/server/move.go b/internal/server/move.go index 1a2f103..7be951f 100644 --- a/internal/server/move.go +++ b/internal/server/move.go @@ -41,9 +41,9 @@ func (s *Server) relocate(c fiber.Ctx, dirPath string, names []string, into stri for _, name := range unique(names) { var err error if copying { - last, err = s.copyEntry(c, dirPath, name, dest) + last, err = s.copyEntry(c, dirPath, name, dest, name) } else { - last, err = s.moveEntry(c, dirPath, name, dest) + last, err = s.moveEntry(c, dirPath, name, dest, name) } if err != nil { return s.partialError(c, did, done, err) @@ -58,9 +58,13 @@ func (s *Server) relocate(c fiber.Ctx, dirPath string, names []string, into stri } // moveEntry moves the entry name of the folder at dirPath into the folder -// at dest, and returns its new URL path. The visitor needs the rights to -// read and delete it, everything in it as well, and to create it there. -func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest string) (string, error) { +// at dest, as newName, and returns its new URL path. The visitor needs the +// rights to read and delete it, everything in it as well, and to create it +// there. +func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest, newName string) (string, error) { + if !validName(newName) { + return "", explain(fiber.ErrBadRequest, badName) + } dir, realDir, info, err := s.entry(c, dirPath, name, auth.ActDelete) if err != nil { return "", err @@ -72,22 +76,25 @@ func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest string) (string, err } else if !ok { return "", fiber.ErrNotFound } - dst := path.Join(dest, name) + dst := path.Join(dest, newName) to, realTo, err := s.destination(c, dst, info.IsDir()) if err != nil { return "", err } defer to.Close() switch { - case realTo == realDir: + case realTo == realDir && newName == name: return "", explain(fiber.ErrConflict, name+" is in that folder already.") case info.IsDir() && within(realTo, realSrc): return "", explain(fiber.ErrConflict, "A folder cannot be moved into itself.") } - // The locks keep uploads of the name out of both folders meanwhile. - for _, d := range []*os.Root{dir, to} { - release, err := s.acquireLock(d, name, tempPrefix+rand.Text()+tempSuffix) + // The locks keep uploads of the names out of both folders meanwhile. + for _, l := range []struct { + dir *os.Root + name string + }{{dir, name}, {to, newName}} { + release, err := s.acquireLock(l.dir, l.name, tempPrefix+rand.Text()+tempSuffix) if err != nil { return "", err } @@ -100,22 +107,22 @@ func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest string) (string, err // may only move if all of it could be read and deleted here, and // created there. if info.IsDir() { - _, err := s.checkTreeTo(c, dir, name, src, realSrc, &treeDest{dst, path.Join(realTo, name)}, auth.ActRead, auth.ActDelete) + _, err := s.checkTreeTo(c, dir, name, src, realSrc, &treeDest{dst, path.Join(realTo, newName)}, auth.ActRead, auth.ActDelete) if err := s.treeError(err, "move"); err != nil { return "", err } } // Moving into a home takes room there. fromHome, _ := s.homeName(realSrc) - toHome, _ := s.homeName(path.Join(realTo, name)) + toHome, _ := s.homeName(path.Join(realTo, newName)) if toHome != "" && toHome != fromHome { if err := s.homeRoom(toHome, entryBytes(dir, name, info)); err != nil { return "", s.uploadError(nil, err) } } - switch err := moveNoReplace(dir, name, to, name, info); { + switch err := moveNoReplace(dir, name, to, newName, info); { case errors.Is(err, errExists): - return "", explain(fiber.ErrConflict, "Something named "+name+" is in that folder already.") + return "", explain(fiber.ErrConflict, "Something named "+newName+" is in that folder already.") case errors.Is(err, syscall.EXDEV): return "", explain(fiber.ErrConflict, "That folder is on another disk: copy it there, then delete it here.") case err != nil: @@ -132,13 +139,16 @@ func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest string) (string, err } // copyEntry copies the entry name of the folder at dirPath into the folder -// at dest, and returns the copy's URL path: a file, or a folder with what -// the visitor may read in it, as a zip of it would hold. The visitor needs -// the right to create all of the copy there. -func (s *Server) copyEntry(c fiber.Ctx, dirPath, name, dest string) (string, error) { +// at dest, as newName, and returns the copy's URL path: a file, or a folder +// with what the visitor may read in it, as a zip of it would hold. The +// visitor needs the right to create all of the copy there. +func (s *Server) copyEntry(c fiber.Ctx, dirPath, name, dest, newName string) (string, error) { if !entryName(name) { return "", fiber.ErrBadRequest } + if !validName(newName) { + return "", explain(fiber.ErrBadRequest, badName) + } src := path.Join(dirPath, name) // Checked before the entry is looked up, so that the answer does not // reveal what exists. @@ -153,7 +163,7 @@ func (s *Server) copyEntry(c fiber.Ctx, dirPath, name, dest string) (string, err return "", err } _ = f.Close() - dst := path.Join(dest, name) + dst := path.Join(dest, newName) to, realTo, err := s.destination(c, dst, info.IsDir()) if err != nil { return "", err @@ -165,13 +175,13 @@ func (s *Server) copyEntry(c fiber.Ctx, dirPath, name, dest string) (string, err if within(realTo, realSrc) { return "", explain(fiber.ErrConflict, "A folder cannot be copied into itself.") } - contents, err = s.copyFolder(to, name, src, realSrc, dst, path.Join(realTo, name), allow) + contents, err = s.copyFolder(to, newName, src, realSrc, dst, path.Join(realTo, newName), allow) } else { - err = s.copyFile(to, name, realSrc) + err = s.copyFile(to, newName, realSrc) } switch { case errors.Is(err, errExists): - return "", explain(fiber.ErrConflict, "Something named "+name+" is in that folder already.") + return "", explain(fiber.ErrConflict, "Something named "+newName+" is in that folder already.") case errors.Is(err, errDenied): return "", explain(fiber.ErrForbidden, "You may not create everything in it there.") case errors.Is(err, errTooMuch): diff --git a/internal/server/server.go b/internal/server/server.go index 63834c3..4a6683b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -9,6 +9,7 @@ import ( "net" "os" "path/filepath" + "slices" "strings" "sync" "sync/atomic" @@ -56,6 +57,9 @@ type Server struct { homeMu sync.Mutex // uses holds what homes hold, by name (see homeUse). uses sync.Map + // davLocks holds the WebDAV locks by token; davMu guards it. + davLocks map[string]*davLock + davMu sync.Mutex // fold is how rules compare paths (see checkNames), nil as spelled. fold func(string) string // proxyNoted is set once untrusted proxy headers have been logged. @@ -93,6 +97,7 @@ func New(cfg *config.Config, log *zap.Logger, authSvc *auth.Service) (*Server, e root: root, rootPath: rootPath, errEscape: escapeError(root), + davLocks: map[string]*davLock{}, } fold, err := s.checkNames() if err != nil { @@ -119,6 +124,7 @@ func New(cfg *config.Config, log *zap.Logger, authSvc *auth.Service) (*Server, e // Uploads are streamed to disk instead of buffered in memory. StreamRequestBody: true, DisablePreParseMultipartForm: true, + RequestMethods: append(slices.Clone(fiber.DefaultMethods), davMethods...), CaseSensitive: true, StrictRouting: true, // Forwarded headers (the proxy header, X-Forwarded-Proto) are only @@ -167,8 +173,12 @@ func New(cfg *config.Config, log *zap.Logger, authSvc *auth.Service) (*Server, e s.app.Get(strings.TrimSuffix(trashPath, "/"), func(c fiber.Ctx) error { return s.redirect(c, trashPath) }) s.app.Get(trashPath, s.trashPage) s.app.Post(trashPath, s.trashAction) + s.app.Get(strings.TrimSuffix(syncPath, "/"), func(c fiber.Ctx) error { return s.redirect(c, syncPath) }) + s.app.Get(syncPath, s.syncPage) + s.app.Post(syncPath, s.syncAction) s.app.Get(strings.TrimSuffix(searchPath, "/"), func(c fiber.Ctx) error { return s.redirect(c, searchPath) }) s.app.Get(searchPath, s.search) + s.davRoutes() s.app.Get(sharePrefix+"*", s.shared) s.app.Post(sharePrefix+"*", s.sharePost) s.app.Head(uploadsPrefix+"*", s.headUpload) diff --git a/internal/server/server_test.go b/internal/server/server_test.go index e3cc3a4..c29e2f5 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -470,7 +470,8 @@ func TestConditionalRequests(t *testing.T) { func TestMethodNotAllowed(t *testing.T) { f := newFixture(t) - for _, method := range []string{"OPTIONS", "PATCH"} { + // WebDAV's methods too, outside /.dav/. + for _, method := range []string{"OPTIONS", "PATCH", "PROPFIND"} { resp, _ := f.do(t, method, "/") expectStatus(t, resp, 405) if got := resp.Header.Get("Allow"); got != "GET, HEAD, PUT, POST, DELETE" { @@ -478,7 +479,7 @@ func TestMethodNotAllowed(t *testing.T) { } } // Methods unknown to Fiber. - resp, _ := f.do(t, "PROPFIND", "/") + resp, _ := f.do(t, "BREW", "/") expectStatus(t, resp, http.StatusNotImplemented) } @@ -490,7 +491,7 @@ func TestAccessLog(t *testing.T) { f.do(t, "HEAD", "/a.txt") f.as("").do(t, "GET", "/a.txt") f.do(t, "PATCH", "/a.txt") - f.do(t, "PROPFIND", "/a.txt") + f.do(t, "BREW", "/a.txt") f.do(t, "GET", "/missing") f.do(t, "GET", "/") @@ -503,7 +504,7 @@ func TestAccessLog(t *testing.T) { "HEAD /a.txt 200 0", "GET /a.txt 401 12", "PATCH /a.txt 405 18", - "PROPFIND /a.txt 501 15", + "BREW /a.txt 501 15", "GET /missing 404 9", } if len(got) != len(want)+1 || strings.Join(got[:len(want)], "|") != strings.Join(want, "|") { @@ -511,7 +512,7 @@ func TestAccessLog(t *testing.T) { } // Anyone can send unknown methods: that is no server error. for _, e := range f.logs.entries("request") { - if e["method"] == "PROPFIND" && e["level"] != "info" { + if e["method"] == "BREW" && e["level"] != "info" { t.Errorf("unknown method logged at level %v", e["level"]) } } diff --git a/internal/server/sync.go b/internal/server/sync.go new file mode 100644 index 0000000..b9d8ad1 --- /dev/null +++ b/internal/server/sync.go @@ -0,0 +1,147 @@ +package server + +import ( + "errors" + "strconv" + "strings" + "time" + + "github.com/gofiber/fiber/v3" + "go.uber.org/zap" + + "goftp/internal/auth" +) + +// syncPath is the page where users make app passwords for sync apps, and +// find how to set those up with goftp's WebDAV address (see dav.go). +const syncPath = "/.sync/" + +type syncData struct { + page + SiteURL string // the WebDAV address of the site + URL string // the one to sync: of the visitor's home, if they have one + Home bool + Path string // URL's path + // FileManagerURL is URL as file managers on Linux take it, e.g. + // davs://host/.dav/ + FileManagerURL string + Insecure bool // app passwords would be sent unencrypted + Username string + Apps []appRow + Max int + Created *createdApp + Notice string + Error string +} + +type appRow struct { + ID int64 + Name string + Created, CreatedISO string + LastUsed, LastUsedISO string // "" if never +} + +// createdApp is a new app password, shown once. +type createdApp struct{ Name, Password string } + +var syncNotices = map[string]string{"revoked": "The app password was revoked. Apps that used it can no longer sign in."} + +func (s *Server) syncPage(c fiber.Ctx) error { + if userOf(c) == nil { + return s.deny(c) + } + return s.renderSync(c, fiber.StatusOK, syncNotices[c.Query("done")], "", nil) +} + +func (s *Server) renderSync(c fiber.Ctx, status int, notice, failure string, created *createdApp) error { + u := userOf(c) + ctx, cancel := dbContext(c) + defer cancel() + apps, err := s.auth.AppPasswords(ctx, u) + if err != nil { + return err + } + data := syncData{page: s.page(c, "Sync"), SiteURL: c.BaseURL() + davPrefix + "/", Path: davPrefix + "/", + Insecure: !c.Secure(), Username: u.Username, Max: auth.MaxAppPasswords, Created: created, Notice: notice, Error: failure} + if home := s.homeOf(u); home != "" { + data.Home, data.Path = true, davHref(home, true) + } + data.URL = c.BaseURL() + data.Path + data.FileManagerURL = "dav" + strings.TrimPrefix(data.URL, "http") + for _, ap := range apps { + row := appRow{ID: ap.ID, Name: ap.Name, Created: ap.CreatedAt.UTC().Format("Jan 2, 2006 15:04") + " UTC", + CreatedISO: ap.CreatedAt.UTC().Format(time.RFC3339)} + if !ap.LastUsedAt.IsZero() { + row.LastUsed = ap.LastUsedAt.UTC().Format("Jan 2, 2006 15:04") + " UTC" + row.LastUsedISO = ap.LastUsedAt.UTC().Format(time.RFC3339) + } + data.Apps = append(data.Apps, row) + } + return s.render(c, status, "sync", data) +} + +// syncAction makes and revokes app passwords: +// +// action=create&name=NAME a new app password for the app NAME, shown once +// action=revoke&id=ID ends the app password ID +// +// e.g. curl -H "Authorization: Bearer $TOKEN" -d action=create -d name=rclone https://host/.sync/ +func (s *Server) syncAction(c fiber.Ctx) error { + u := userOf(c) + if u == nil { + return s.deny(c) + } + form, _, err := s.readForm(c) + if err != nil { + return err + } + html := strings.Contains(c.Get(fiber.HeaderAccept), fiber.MIMETextHTML) + ctx, cancel := dbContext(c) + defer cancel() + switch form["action"] { + case "create": + password, ap, err := s.auth.CreateAppPassword(ctx, u, form["name"]) + switch { + case errors.Is(err, auth.ErrInvalid): + return s.syncFailure(c, html, explain(fiber.ErrBadRequest, "Name the app password, in up to 64 characters, e.g. rclone on my laptop.")) + case errors.Is(err, auth.ErrTooMany): + return s.syncFailure(c, html, explain(fiber.ErrConflict, + "You have "+strconv.Itoa(auth.MaxAppPasswords)+" app passwords already: revoke one you no longer use first.")) + case err != nil: + return err + } + s.log.Info("app password created", zap.String("ip", c.IP()), zap.String("user", u.Username), zap.Int64("app", ap.ID), + zap.String("name", ap.Name)) + c.Set(fiber.HeaderCacheControl, "no-store") + if !html { + return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": ap.ID, "name": ap.Name, "username": u.Username, + "password": password, "url": c.BaseURL() + davPrefix + "/"}) + } + return s.renderSync(c, fiber.StatusCreated, "", "", &createdApp{Name: ap.Name, Password: password}) + case "revoke": + id, err := strconv.ParseInt(form["id"], 10, 64) + if err != nil { + return fiber.ErrBadRequest + } + if err := s.auth.RevokeAppPassword(ctx, u, id); errors.Is(err, auth.ErrNotFound) { + return s.syncFailure(c, html, explain(fiber.ErrNotFound, "There is no such app password.")) + } else if err != nil { + return err + } + s.log.Info("app password revoked", zap.String("ip", c.IP()), zap.String("user", u.Username), zap.Int64("app", id)) + if html { + return s.redirect(c, syncPath+"?done=revoked") + } + return c.SendStatus(fiber.StatusNoContent) + } + return fiber.ErrBadRequest +} + +// syncFailure shows why an action failed: on the page for browsers. +func (s *Server) syncFailure(c fiber.Ctx, html bool, err error) error { + var e *explained + if !html || !errors.As(err, &e) { + return err + } + return s.renderSync(c, e.err.Code, "", e.detail, nil) +} diff --git a/internal/server/templates/base.html b/internal/server/templates/base.html index eaba572..a029e66 100644 --- a/internal/server/templates/base.html +++ b/internal/server/templates/base.html @@ -497,6 +497,18 @@ user-select: all; } .onetime-note { font-size: 13px; opacity: 0.85; } + .app-cell { display: inline-flex; align-items: center; gap: 10px; font-weight: 500; overflow-wrap: anywhere; } + .app-cell .i { flex: none; color: var(--muted); } + .sync-details { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 10px 18px; align-items: baseline; margin: 0; font-size: 14px; } + .sync-details dt { font-size: 13px; font-weight: 600; color: var(--muted); } + .sync-details dd { margin: 0; overflow-wrap: anywhere; } + .sync-details dd.faint { grid-column: 2; margin-top: -6px; font-size: 13px; } + .sync-details .copyable code { background: var(--surface-2); font-size: 14px; letter-spacing: 0; overflow-wrap: anywhere; } + .sync-apps details { border-top: 1px solid var(--border); padding-top: 12px; } + .sync-apps summary { cursor: pointer; font-weight: 600; font-size: 14px; } + .sync-apps details p { margin-top: 8px; font-size: 13.5px; color: var(--muted); } + .sync-apps pre { margin: 10px 0 0; padding: 12px 14px; overflow-x: auto; border-radius: var(--radius-sm); background: var(--surface-2); font-size: 13px; line-height: 1.6; } + .sync-notes { margin: 4px 0 0; padding: 12px 0 0 18px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; color: var(--muted); } .manage { display: flex; flex-direction: column; gap: 14px; max-width: 420px; padding: 18px; margin-bottom: 16px; } .manage h2 { display: flex; align-items: center; gap: 10px; font-size: 16px; overflow-wrap: anywhere; } @@ -673,6 +685,9 @@ .files .num { width: auto; } .dz-hint { display: none; } .auth-card { padding: 24px 20px; } + .sync-details { grid-template-columns: minmax(0, 1fr); gap: 4px; } + .sync-details dt:not(:first-child) { margin-top: 8px; } + .sync-details dd.faint { grid-column: 1; margin-top: 0; } } @@ -699,6 +714,7 @@ @@ -716,6 +732,7 @@ + diff --git a/internal/server/templates/sync.html b/internal/server/templates/sync.html new file mode 100644 index 0000000..80d7580 --- /dev/null +++ b/internal/server/templates/sync.html @@ -0,0 +1,122 @@ +{{define "sync"}}{{template "head" .}} + +{{template "topbar" .}} +
+
+
+

Sync

+

Keep a folder on your computer or phone in sync with {{if .Home}}your files{{else}}goftp{{end}}, or open them in a file manager, through WebDAV. Apps sign in with an app password, never your own.

+
+
+ + {{with .Created}} + + {{end}} + {{with .Notice}}{{end}} + {{with .Error}}{{end}} + {{if .Insecure}}{{end}} + +
+

Connect

+
+
Server address
+
{{.URL}}
+ {{if .Home}}
Your home. The rest of the site is at {{.SiteURL}}
{{end}} +
Username
+
{{.Username}}
+
Password
+
An app password, made below: one for each app or device, so that you can revoke one without the others.
+
+
+ +
+

New app password

+
+ + + +
+
+ +
+ {{if .Apps}} + + + + {{range .Apps}} + + + + + + + {{end}} + +
App passwordCreatedLast usedRevoke
{{.Name}}Created Last used {{if .LastUsed}}{{else}}never{{end}} +
+ +
+ +

Revoke the app password for {{.Name}}? Apps using it can no longer sign in.

+
+
+
+
+ {{else}} +
+ +

You have no app passwords.

+

Create one for each app or device you sync with.

+
+ {{end}} +
+ +
+

Set up an app

+
+ rclone (Linux, macOS, Windows) +

Add goftp as a remote, then sync a folder both ways. The first run needs --resync; run the second line again whenever you like, e.g. from cron.

+
rclone config create goftp webdav url={{.URL}} vendor=owncloud user={{.Username}} pass=APP-PASSWORD --obscure
+rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**" --resync
+rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**"
+

+
+
+ FolderSync (Android) +

Under Accounts, add a WebDAV account with the server address, your username and an app password. Then add a folder pair of a folder there and one on the phone, with the sync type Two-way.

+
+
+ GoodSync (Windows, macOS) +

Choose WebDAV for one side of a job and enter the server address, your username and an app password; choose the folder to sync for the other side. Then analyze and sync.

+
+
+ Mountain Duck and Cyberduck (macOS, Windows) +

Make a bookmark or connection of the type WebDAV (HTTPS), paste the server address into Server, and enter your username and an app password. Mountain Duck shows goftp as a disk; its Smart Synchronization keeps the files you open on your computer.

+
+
+ Finder and other file managers +

In Finder, choose Go → Connect to Server and enter the server address. Most Linux file managers open {{.FileManagerURL}}.

+
+
    +
  • Apps see and change what you may on the site, no more{{if .Home}}, and what they put in your home counts toward its quota{{end}}.
  • + {{if .Trash}}
  • Files that a sync deletes go to the trash, where you can restore them for a while.
  • {{end}} +
  • Names starting with a dot, such as .git, are not synced: goftp hides them. macOS's .DS_Store and ._ files are taken and dropped.
  • +
  • Changing your password revokes all your app passwords.
  • +
+
+
+{{template "footer" .}} +{{template "icons"}} +{{script}} + + +{{end}} diff --git a/internal/server/upload.go b/internal/server/upload.go index 805dbc5..4b22237 100644 --- a/internal/server/upload.go +++ b/internal/server/upload.go @@ -70,64 +70,118 @@ func (s *Server) put(c fiber.Ctx) error { if err != nil || wantDir || urlPath == "/" { return fiber.ErrBadRequest } + // A chunked body cut off between chunks looks complete; only a declared + // length lets truncated uploads be detected. + length := int64(c.Request().Header.ContentLength()) + if length < 0 { + return fiber.ErrLengthRequired + } + created, err := s.putFile(c, urlPath, length, time.Time{}) + if err != nil { + return err + } + if created { + return c.SendStatus(fiber.StatusCreated) + } + return c.SendStatus(fiber.StatusNoContent) +} + +// putFile stores the request body, length bytes of it, as the file at the +// clean urlPath, and reports whether it created it. A body without a +// declared length has to be exactly that long. A non-zero mtime becomes +// the file's modification time. +func (s *Server) putFile(c fiber.Ctx, urlPath string, length int64, mtime time.Time) (created bool, err error) { + // A part of a file would replace all of it (RFC 9110, section 14.5). + if c.Get(fiber.HeaderContentRange) != "" { + return false, explain(fiber.ErrBadRequest, "Send the whole file: parts of files cannot be uploaded with PUT.") + } if hidden(urlPath) { - return fiber.ErrForbidden + return false, fiber.ErrForbidden } dirPath, name := path.Split(urlPath) if !validName(name) { - return fiber.ErrBadRequest + return false, fiber.ErrBadRequest } create, replace, err := s.uploadRights(c, urlPath) if err != nil { - return err + return false, err } if !create && !replace { - return s.deny(c) - } - // A chunked body cut off between chunks looks complete; only a declared - // length lets truncated uploads be detected. - length := int64(c.Request().Header.ContentLength()) - if length < 0 { - return fiber.ErrLengthRequired + return false, s.deny(c) } if limit := int64(s.cfg.Upload.MaxSize); limit > 0 && length > limit { - return fiber.ErrRequestEntityTooLarge + return false, fiber.ErrRequestEntityTooLarge } dir, realDir, err := s.uploadDir(dirPath) if errors.Is(err, fiber.ErrNotFound) { - return fiber.ErrConflict + return false, fiber.ErrConflict } if err != nil { - return err + return false, err } defer dir.Close() if create, replace, err = s.uploadRightsAt(c, urlPath, path.Join(realDir, name)); err != nil { - return err + return false, err } else if !create && !replace { - return s.deny(c) + return false, s.deny(c) } if !s.roomFor(dir, length) { - return s.uploadError(nil, errLowSpace) + return false, s.uploadError(nil, errLowSpace) } if err := s.homeRoom(s.homeAt(dir), length-sizeOf(dir, name)); err != nil { - return s.uploadError(nil, err) + return false, s.uploadError(nil, err) } body := s.requestBody(c) + var from io.Reader = body + if body.length < 0 { + from = &exactReader{r: body, left: length} + } rights := uploadRights{create: create, replace: replace} - created, size, err := s.receive(dir, name, body, c.Get(fiber.HeaderIfNoneMatch) != "*", rights) + created, size, err := s.receive(dir, name, from, c.Get(fiber.HeaderIfNoneMatch) != "*", rights) if errors.Is(err, errExists) { - return fiber.ErrPreconditionFailed + return false, fiber.ErrPreconditionFailed } if err != nil { - return s.uploadError(body, err) + return false, s.uploadError(body, err) + } + if !mtime.IsZero() { + _ = dir.Chtimes(name, time.Now(), mtime) } s.logUpload(c, urlPath, size, created) s.finishBody(c, body) - if created { - return c.SendStatus(fiber.StatusCreated) + return created, nil +} + +// errLength refuses a body that is not as long as it was said to be. +var errLength = errors.New("the body is not as long as was said") + +// exactReader reads left bytes of r, and fails if r ends before them or +// goes on after them. +type exactReader struct { + r io.Reader + left int64 +} + +func (e *exactReader) Read(p []byte) (int, error) { + if e.left <= 0 { + var one [1]byte + if n, _ := e.r.Read(one[:]); n > 0 { + return 0, errLength + } + return 0, io.EOF } - return c.SendStatus(fiber.StatusNoContent) + if int64(len(p)) > e.left { + p = p[:e.left] + } + n, err := e.r.Read(p) + e.left -= int64(n) + if errors.Is(err, io.EOF) && e.left > 0 { + err = errLength + } else if errors.Is(err, io.EOF) { + err = nil + } + return n, err } // postForm stores the files of a multipart/form-data upload (the listing @@ -641,6 +695,8 @@ func (s *Server) uploadError(body *requestBody, err error) error { return fiber.ErrRequestTimeout case berr != nil: return fiber.ErrBadRequest + case errors.Is(err, errLength): + return explain(fiber.ErrBadRequest, "The upload was not as long as it said it would be.") case errors.Is(err, errQuota): return explain(fiber.ErrInsufficientStorage, "There is no room for it in this home: delete something, and empty the trash, to make room.") case errors.Is(err, syscall.ENOSPC):