From 558eff561f3368ac5cb64b3465471d0c26985fc5 Mon Sep 17 00:00:00 2001 From: ssenerg Date: Sun, 25 Oct 2026 23:39:33 +0330 Subject: [PATCH] Fix the high and medium findings of the security review - Count wrong two-factor codes per account, wherever they come from: after 5 in a row, each next code waits a minute, then twice as long after each wrong one, up to a day. The client's budget alone let someone with the password guess codes from many addresses. - Ask for the password to make an app password, or to set up two-factor sign-in, so that a stolen session cannot become a lasting WebDAV login or lock its user out. App passwords and passkeys made while the password changes are refused, as sessions already were. - Give an upload link's use back only for an upload really forgotten, and forget a stored upload before removing its temp file: a HEAD at that moment gave the use and its bytes back. - Count folders against home quotas (4 KiB each), and check the free space, and the last 1% of the disk's inodes, before folders and empty files are made, which no write checked before. - Have links follow what they lead to when it is renamed or moved, and end when it is deleted, so that none leads to what is made in its place. - Keep the last 50 MB of each container's log. - Also: re-check an entry after locking it for deletion, keep tokens out of the log line of unknown methods, and copy the passkey host name out of the request buffer. Co-Authored-By: Claude Opus 5.5 --- README.md | 52 +++++---- docker-compose.yml | 12 ++ internal/auth/apppass.go | 19 +++- internal/auth/apppass_test.go | 28 +++-- internal/auth/authtest/store.go | 23 +++- internal/auth/export_test.go | 12 ++ internal/auth/passkey.go | 7 +- internal/auth/passkey_test.go | 2 +- internal/auth/postgres.go | 33 ++++-- internal/auth/service.go | 1 + internal/auth/share.go | 24 ++++ internal/auth/store.go | 15 ++- internal/auth/twofactor.go | 134 ++++++++++++++++++++--- internal/auth/twofactor_test.go | 107 +++++++++++++++++- internal/server/auth.go | 9 +- internal/server/dav.go | 10 +- internal/server/dav_test.go | 25 +++-- internal/server/link_test.go | 63 +++++++++++ internal/server/manage.go | 11 +- internal/server/mkdir.go | 3 + internal/server/move.go | 10 +- internal/server/passkey.go | 8 +- internal/server/passkey_test.go | 2 +- internal/server/people.go | 18 ++- internal/server/people_test.go | 6 +- internal/server/quota.go | 37 ++++++- internal/server/quota_test.go | 26 +++++ internal/server/resumable.go | 20 ++-- internal/server/resumable_test.go | 32 ++++++ internal/server/space.go | 20 ++-- internal/server/space_other.go | 2 + internal/server/space_statfs.go | 14 ++- internal/server/sync.go | 26 ++++- internal/server/templates/sync.html | 4 + internal/server/templates/twofactor.html | 6 +- internal/server/twofactor.go | 57 +++++++--- internal/server/twofactor_test.go | 61 +++++++++-- internal/server/upload.go | 2 +- internal/server/upload_test.go | 15 +++ 39 files changed, 805 insertions(+), 151 deletions(-) diff --git a/README.md b/README.md index fb253b8..513cc1f 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,8 @@ The port is only published on 127.0.0.1: put a TLS-terminating reverse proxy in front and set `GOFTP_PROXY_HEADER=X-Forwarded-For` and `GOFTP_TRUSTED_PROXIES` in `.env` (see [`.env.example`](.env.example)), or let goftp serve HTTPS itself with `GOFTP_TLS_DIR`, `GOFTP_TLS_CERT` and -`GOFTP_TLS_KEY`, then set `GOFTP_BIND=0.0.0.0`. +`GOFTP_TLS_KEY`, then set `GOFTP_BIND=0.0.0.0`. Docker keeps the last +50 MB of each container's log. ## Users and roles @@ -108,13 +109,16 @@ to turn it on. That signs them out everywhere else, and shows 10 recovery codes once, each of which works once instead of a code, for a lost phone; new ones replace them, given the password. Codes are TOTP (RFC 6238: 6 digits, 30 seconds, SHA-1, as apps expect), accepted 30 seconds early or -late, and none works twice. Turning it off takes the password too. Wrong -codes count against the client's sign-in budget, and a sign-in waits for -its code 5 minutes, 5 tries at most. For someone who lost both, a superadmin -turns it off under **Users** (marked *two-factor*), or with -`goftp user two-factor-off NAME`. App passwords still sign apps in over -WebDAV without a code: they are made by a user already signed in, for one -app each, and are revoked alone. +late, and none works twice. Setting it up and turning it off take the +password too, so that a stolen session cannot lock its user out. Wrong +codes count against the client's sign-in budget, and against the user's +wherever they come from: after 5 in a row, the next waits a minute, then +twice as long after each wrong one, up to a day, until one is right. A +sign-in waits for its code 5 minutes, 5 tries at most. For someone who lost +both, a superadmin turns it off under **Users** (marked *two-factor*), or +with `goftp user two-factor-off NAME`. App passwords still sign apps in +over WebDAV without a code: they are made by a user already signed in, +with their password, for one app each, and are revoked alone. | Role | May | |--------------|-----------------------------------------------------------------------------------------| @@ -189,8 +193,9 @@ A home may hold up to a quota: `homes.quota` (`GOFTP_HOMES_QUOTA`, e.g. its user, under **Users** or with `goftp user quota NAME 10GiB` (`none` for no limit, `default` for `homes.quota`). What its user deleted from it counts too while it is in the trash, so deleting makes room once the trash -is emptied. Uploads, copies and moves that would go past it, also through -upload links into the home, get `507 Insufficient Storage`. **My files** +is emptied, and each folder counts 4 KiB, as it takes that much of most +disks. Uploads, new folders, copies and moves that would go past it, also +through upload links into the home, get `507 Insufficient Storage`. **My files** shows how much the home holds, with a bar when it has a quota. Deleting a user moves their home to the trash, and removes the rules for @@ -267,8 +272,9 @@ With two-factor sign-in, add `"code"` (from the app, or a recovery code). Without it, the answer is `401` with `{"two_factor":true,"challenge":"..."}`; send `{"challenge":"...","code":"..."}` within 5 minutes instead of the -password. Scripts turn it on with `action=start` (which answers with the -`secret` and `otpauth://` `uri` for the app) and then `action=confirm` +password. Scripts turn it on with `action=start` and `password` (which +answers with the `secret` and `otpauth://` `uri` for the app) and then +`action=confirm` with `code` (answering with the `recovery_codes` and a new `token`), and off with `action=off` and `password`, all on `POST /.auth/two-factor`. @@ -295,7 +301,9 @@ token. `POST /.auth/logout` ends the session. Sessions last The target directory must exist, and `upload.max_size` caps each file. Uploads stop short of the disk's last `upload.min_free` bytes (`GOFTP_UPLOAD_MIN_FREE`; 1 GiB by default, `0` turns the check off), which stay for the database and -everything else on that disk: the upload gets `507 Insufficient Storage`. +everything else on that disk, and of the last 1% of its inodes, where it +counts them, which every file and folder takes: the upload, or the new +folder, gets `507 Insufficient Storage`. Users who may upload new files into a folder (`write`) can also create folders in it, with the "New folder" button or `curl -d folder=photos -H "Authorization: Bearer $TOKEN" https://host/dir/`. @@ -460,8 +468,11 @@ length when it starts, so one that does not fit is refused before it is sent. **Shared** shows how much was sent through each. A link never gives more than its creator may still read and share: it -stops working when they lose those rights, or when what it leads to is -moved or deleted. **Shared**, next to **Files** in the top bar, lists your +stops working when they lose those rights. It follows what it leads to +when that is renamed or moved, and ends when that is deleted, even if it +is put back from the trash, so that it never leads to something made in +its place; a file uploaded again keeps its links. **Shared**, next to +**Files** in the top bar, lists your links, and for superadmins everyone's, with how often each was used and when it was last opened, to revoke them. Deleting a user, or giving them a new temporary password, ends their links too. Scripts: @@ -487,13 +498,14 @@ 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 the others. Making one takes your password, so that a stolen +session cannot become an app password. 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/ +curl -H "Authorization: Bearer $TOKEN" -d action=create -d name=laptop --data-urlencode password=... 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/ ``` @@ -586,9 +598,9 @@ Wrong app passwords count against the client's sign-in budget with someone. A superadmin can still change the rules, and whoever runs the server can read its files. - Sharing a folder with people tells whether a username exists, to any - user with a home. A folder deleted by other means than goftp (on the - server's disk, say) keeps its shares, for a folder made in its place, - until its owner stops them. + user with a home. A file or folder deleted by other means than goftp + (on the server's disk, say) keeps its links and shares, for one made in + its place, until they are revoked. - Upload links let whoever has them fill a folder, with files of up to `upload.max_size` each, until the disk is down to `upload.min_free`: give them a short life, a password, a limit of files or of size. Into a diff --git a/docker-compose.yml b/docker-compose.yml index c83f00f..9eec57d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,10 +2,21 @@ # cp .env.example .env && chmod 600 .env # then set POSTGRES_PASSWORD # docker compose up -d --build # docker compose exec goftp goftp user add alice --role superadmin + +# Docker keeps all of a container's log unless told otherwise, and goftp +# logs every request: each keeps its last 50 MB, so that requests cannot +# fill the disk. +x-logging: &logging + driver: json-file + options: + max-size: 10m + max-file: "5" + services: postgres: image: postgres:18-alpine restart: unless-stopped + logging: *logging environment: POSTGRES_DB: goftp POSTGRES_USER: goftp @@ -24,6 +35,7 @@ services: build: . image: goftp restart: unless-stopped + logging: *logging depends_on: postgres: condition: service_healthy diff --git a/internal/auth/apppass.go b/internal/auth/apppass.go index 4be70f1..b96e1c9 100644 --- a/internal/auth/apppass.go +++ b/internal/auth/apppass.go @@ -3,6 +3,7 @@ package auth import ( "context" "crypto/rand" + "errors" "fmt" "strings" "time" @@ -30,20 +31,30 @@ const maxAppPasswordName = 64 // 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) { +// CreateAppPassword makes an app password named name for u, after checking +// their password, and returns it: it is not kept, so it can only be shown +// now. The password keeps a stolen session from becoming an app password, +// which would outlive it. +func (s *Service) CreateAppPassword(ctx context.Context, u *User, name, password 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) } + if err := s.checkPasswordOf(ctx, u, password); err != nil { + return "", nil, err + } 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 { + err := s.store.CreateAppPassword(ctx, tokenHash(raw), ap, u.PasswordHash) + if errors.Is(err, ErrNotFound) { + // The password changed meanwhile, ending what it made. + return "", nil, errChangedMeanwhile + } + if err != nil { return "", nil, err } return strings.Join(groups, "-"), ap, nil diff --git a/internal/auth/apppass_test.go b/internal/auth/apppass_test.go index 8d28103..7d39580 100644 --- a/internal/auth/apppass_test.go +++ b/internal/auth/apppass_test.go @@ -18,17 +18,24 @@ func TestAppPasswords(t *testing.T) { if err != nil { t.Fatal(err) } - if _, err := svc.CreateUser(ctx, "erin", "user"); err != nil { + erinTemp, err := svc.CreateUser(ctx, "erin", "user") + if 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) { + if _, _, err := svc.CreateAppPassword(ctx, dora, name, temp); !errors.Is(err, auth.ErrInvalid) { t.Errorf("CreateAppPassword(%q): %v", name, err) } } - password, ap, err := svc.CreateAppPassword(ctx, dora, " rclone on my laptop ") + // It takes the password: a stolen session alone makes none. + for _, wrong := range []string{"", "wrong password", erinTemp} { + if _, _, err := svc.CreateAppPassword(ctx, dora, "laptop", wrong); !errors.Is(err, auth.ErrInvalidCredentials) { + t.Errorf("password %q: %v", wrong, err) + } + } + password, ap, err := svc.CreateAppPassword(ctx, dora, " rclone on my laptop ", temp) if err != nil { t.Fatal(err) } @@ -51,7 +58,7 @@ func TestAppPasswords(t *testing.T) { t.Errorf("AppPasswordUser(%q, %q): %v, %v", tc.username, tc.password, u, err) } } - other, _, err := svc.CreateAppPassword(ctx, erin, "phone") + other, _, err := svc.CreateAppPassword(ctx, erin, "phone", erinTemp) if err != nil { t.Fatal(err) } @@ -70,7 +77,7 @@ func TestAppPasswords(t *testing.T) { } // Listed newest first, with when they were last used. - phone, _, err := svc.CreateAppPassword(ctx, dora, "phone") + phone, _, err := svc.CreateAppPassword(ctx, dora, "phone", temp) if err != nil { t.Fatal(err) } @@ -102,11 +109,11 @@ func TestAppPasswords(t *testing.T) { // A user has only so many. for i := 1; i < auth.MaxAppPasswords; i++ { - if _, _, err := svc.CreateAppPassword(ctx, dora, "app"); err != nil { + if _, _, err := svc.CreateAppPassword(ctx, dora, "app", temp); err != nil { t.Fatal(i, err) } } - if _, _, err := svc.CreateAppPassword(ctx, dora, "one too many"); !errors.Is(err, auth.ErrTooMany) { + if _, _, err := svc.CreateAppPassword(ctx, dora, "one too many", temp); !errors.Is(err, auth.ErrTooMany) { t.Errorf("over the limit: %v", err) } @@ -135,7 +142,12 @@ func TestAppPasswords(t *testing.T) { 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") + // The password checked must still be the password when it is made. + if _, _, err := svc.CreateAppPassword(ctx, dora, "stale", temp); !errors.Is(err, auth.ErrInvalidCredentials) { + t.Errorf("with the password before the change: %v", err) + } + dora = userNamed(t, svc, "dora") + last, _, err := svc.CreateAppPassword(ctx, dora, "last", "a long new password") if err != nil { t.Fatal(err) } diff --git a/internal/auth/authtest/store.go b/internal/auth/authtest/store.go index 75d74ae..f039a99 100644 --- a/internal/auth/authtest/store.go +++ b/internal/auth/authtest/store.go @@ -142,10 +142,10 @@ func (s *Store) UserByID(_ context.Context, id int64) (*auth.User, error) { return &c, nil } -func (s *Store) CreatePasskey(_ context.Context, pk *auth.Passkey) error { +func (s *Store) CreatePasskey(_ context.Context, pk *auth.Passkey, passwordHash string) error { s.mu.Lock() defer s.mu.Unlock() - if _, ok := s.users[pk.UserID]; !ok { + if u, ok := s.users[pk.UserID]; !ok || u.PasswordHash != passwordHash { return auth.ErrNotFound } n := 0 @@ -438,10 +438,10 @@ func (s *Store) deleteApps(userID int64) { } } -func (s *Store) CreateAppPassword(_ context.Context, tokenHash []byte, ap *auth.AppPassword) error { +func (s *Store) CreateAppPassword(_ context.Context, tokenHash []byte, ap *auth.AppPassword, passwordHash string) error { s.mu.Lock() defer s.mu.Unlock() - if _, ok := s.users[ap.UserID]; !ok { + if u, ok := s.users[ap.UserID]; !ok || u.PasswordHash != passwordHash { return auth.ErrNotFound } n := 0 @@ -682,6 +682,18 @@ func (s *Store) DeleteShare(_ context.Context, id, userID int64) error { return auth.ErrNotFound } +func (s *Store) MoveShare(_ context.Context, id int64, p string) error { + s.mu.Lock() + defer s.mu.Unlock() + for _, sh := range s.shares { + if sh.ID == id { + sh.Path = p + return nil + } + } + return auth.ErrNotFound +} + func (s *Store) deleteShares(userID int64) { for k, sh := range s.shares { if sh.CreatedBy == userID { @@ -775,9 +787,10 @@ func (s *Store) DeleteUpload(_ context.Context, id int64) error { for k, up := range s.uploads { if up.ID == id { delete(s.uploads, k) + return nil } } - return nil + return auth.ErrNotFound } func (s *Store) TakeExpiredUploads(_ context.Context, limit int) ([]auth.Upload, error) { diff --git a/internal/auth/export_test.go b/internal/auth/export_test.go index 9448a9f..bcca8c0 100644 --- a/internal/auth/export_test.go +++ b/internal/auth/export_test.go @@ -1,4 +1,16 @@ package auth +import "time" + // TOTPCode is the code of secret for the time step, for tests. var TOTPCode = totpCode + +// AgeWrongCodes makes the wrong two-factor codes so far as old as if they +// came d earlier, for tests. +func (s *Service) AgeWrongCodes(d time.Duration) { + s.wrongCodes.mu.Lock() + defer s.wrongCodes.mu.Unlock() + for _, w := range s.wrongCodes.m { + w.last = w.last.Add(-d) + } +} diff --git a/internal/auth/passkey.go b/internal/auth/passkey.go index 0d90e39..24c36e1 100644 --- a/internal/auth/passkey.go +++ b/internal/auth/passkey.go @@ -253,7 +253,12 @@ func (s *Service) FinishPasskey(ctx context.Context, u *User, token, name string return nil, err } pk := &Passkey{UserID: u.ID, Name: name, CredentialID: cred.ID, Credential: raw} - if err := s.store.CreatePasskey(ctx, pk); err != nil { + err = s.store.CreatePasskey(ctx, pk, u.PasswordHash) + if errors.Is(err, ErrNotFound) { + // The password changed meanwhile, ending what it made. + return nil, ErrCeremonyGone + } + if err != nil { return nil, err } return pk, nil diff --git a/internal/auth/passkey_test.go b/internal/auth/passkey_test.go index 4a9a540..044b2ec 100644 --- a/internal/auth/passkey_test.go +++ b/internal/auth/passkey_test.go @@ -122,7 +122,7 @@ func TestPasskeys(t *testing.T) { // With two-factor sign-in, no code after it: a passkey is one in // itself. - secret, err := svc.StartTwoFactor(ctx, dora) + secret, err := svc.StartTwoFactor(ctx, dora, password) if err != nil { t.Fatal(err) } diff --git a/internal/auth/postgres.go b/internal/auth/postgres.go index 393c534..88136c8 100644 --- a/internal/auth/postgres.go +++ b/internal/auth/postgres.go @@ -91,12 +91,13 @@ func (s *PgStore) SetPassword(ctx context.Context, userID int64, passwordHash st }) } -func (s *PgStore) CreateAppPassword(ctx context.Context, tokenHash []byte, ap *AppPassword) error { +func (s *PgStore) CreateAppPassword(ctx context.Context, tokenHash []byte, ap *AppPassword, passwordHash string) 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. + // exceed the limit together, and orders this against SetPassword, + // as in CreateSession. var id int64 - err := tx.QueryRow(ctx, "SELECT id FROM users WHERE id = $1 FOR UPDATE", ap.UserID).Scan(&id) + err := tx.QueryRow(ctx, "SELECT id FROM users WHERE id = $1 AND password_hash = $2 FOR UPDATE", ap.UserID, passwordHash).Scan(&id) if errors.Is(err, pgx.ErrNoRows) { return ErrNotFound } @@ -347,13 +348,18 @@ func setRecoveryCodes(ctx context.Context, tx pgx.Tx, userID int64, hashes [][]b return err } -func (s *PgStore) CreatePasskey(ctx context.Context, pk *Passkey) error { +func (s *PgStore) CreatePasskey(ctx context.Context, pk *Passkey, passwordHash string) error { return pgx.BeginFunc(ctx, s.db, func(tx pgx.Tx) error { // The row lock serializes a user's passkeys, so they cannot exceed - // the limit together. - if _, err := tx.Exec(ctx, "SELECT 1 FROM users WHERE id = $1 FOR UPDATE", pk.UserID); err != nil { + // the limit together, and orders this against SetPassword, as in + // CreateSession. + tag, err := tx.Exec(ctx, "SELECT 1 FROM users WHERE id = $1 AND password_hash = $2 FOR UPDATE", pk.UserID, passwordHash) + if err != nil { return err } + if tag.RowsAffected() == 0 { + return ErrNotFound + } var n int if err := tx.QueryRow(ctx, "SELECT count(*) FROM passkeys WHERE user_id = $1", pk.UserID).Scan(&n); err != nil { return err @@ -361,7 +367,7 @@ func (s *PgStore) CreatePasskey(ctx context.Context, pk *Passkey) error { if n >= MaxPasskeys { return ErrTooMany } - err := tx.QueryRow(ctx, `INSERT INTO passkeys (user_id, name, credential_id, credential) VALUES ($1, $2, $3, $4) + err = tx.QueryRow(ctx, `INSERT INTO passkeys (user_id, name, credential_id, credential) VALUES ($1, $2, $3, $4) ON CONFLICT (credential_id) DO NOTHING RETURNING id, created_at`, pk.UserID, pk.Name, pk.CredentialID, pk.Credential). Scan(&pk.ID, &pk.CreatedAt) if errors.Is(err, pgx.ErrNoRows) { @@ -567,6 +573,14 @@ func (s *PgStore) DeleteShare(ctx context.Context, id, userID int64) error { return err } +func (s *PgStore) MoveShare(ctx context.Context, id int64, p string) error { + tag, err := s.db.Exec(ctx, "UPDATE shares SET path = $2 WHERE id = $1", id, p) + if err == nil && tag.RowsAffected() == 0 { + err = ErrNotFound + } + return err +} + func (s *PgStore) DeleteExpiredShares(ctx context.Context) error { if _, err := s.db.Exec(ctx, "DELETE FROM shares WHERE expires_at <= now()"); err != nil { return err @@ -625,7 +639,10 @@ func (s *PgStore) ReleaseUpload(ctx context.Context, id int64, writer string, ke } func (s *PgStore) DeleteUpload(ctx context.Context, id int64) error { - _, err := s.db.Exec(ctx, "DELETE FROM uploads WHERE id = $1", id) + tag, err := s.db.Exec(ctx, "DELETE FROM uploads WHERE id = $1", id) + if err == nil && tag.RowsAffected() == 0 { + err = ErrNotFound + } return err } diff --git a/internal/auth/service.go b/internal/auth/service.go index ed217c4..47a768f 100644 --- a/internal/auth/service.go +++ b/internal/auth/service.go @@ -46,6 +46,7 @@ type Service struct { fold atomic.Value // how rules compare paths (see SetFold) challenges challenges // sign-ins that wait for a two-factor code + wrongCodes wrongCodes // users' wrong two-factor codes in a row ceremonies ceremonies // passkeys being made or used } diff --git a/internal/auth/share.go b/internal/auth/share.go index 0d232cf..65e1cf0 100644 --- a/internal/auth/share.go +++ b/internal/auth/share.go @@ -5,6 +5,7 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/base64" + "errors" "fmt" "strconv" "strings" @@ -190,6 +191,29 @@ func (s *Service) RevokeShare(ctx context.Context, u *User, id int64, all bool) return s.store.DeleteShare(ctx, id, u.ID) } +// MoveShares has the links to the file or folder at the URL path from, and +// to what is in it, follow it to to, where it moved; they end with to "", +// as it was deleted. Links left at to by what is gone end as well: none +// may reach what takes the place of what it led to. +func (s *Service) MoveShares(ctx context.Context, from, to string) error { + links, err := s.store.Shares(ctx, 0) + if err != nil { + return err + } + for _, sh := range links { + switch { + case s.within(sh.Path, from) && to != "": + err = s.store.MoveShare(ctx, sh.ID, to+below(sh.Path, from)) + case s.within(sh.Path, from), to != "" && s.within(sh.Path, to): + err = s.store.DeleteShare(ctx, sh.ID, 0) + } + if err != nil && !errors.Is(err, ErrNotFound) { + return err + } + } + return nil +} + // MayShare reports whether username may create links anywhere at all. func (s *Service) MayShare(username string) bool { return s.mayAnywhere(username, ActShare) } diff --git a/internal/auth/store.go b/internal/auth/store.go index 648b6a2..ef4f53b 100644 --- a/internal/auth/store.go +++ b/internal/auth/store.go @@ -100,8 +100,9 @@ type Store interface { // 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 + // such user, or their password hash is no longer passwordHash (see + // SetPassword). + CreateAppPassword(ctx context.Context, tokenHash []byte, ap *AppPassword, passwordHash string) 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. @@ -153,8 +154,9 @@ type Store interface { // CreatePasskey stores pk and sets its ID and CreatedAt; ErrTooMany if // its user has MaxPasskeys, ErrExists if its credential is stored - // already. - CreatePasskey(ctx context.Context, pk *Passkey) error + // already, ErrNotFound if there is no such user, or their password hash + // is no longer passwordHash (see SetPassword). + CreatePasskey(ctx context.Context, pk *Passkey, passwordHash string) error // Passkeys lists userID's passkeys, oldest first. Passkeys(ctx context.Context, userID int64) ([]Passkey, error) // UsePasskey notes that the passkey id was used, now with credential. @@ -199,6 +201,9 @@ type Store interface { // DeleteShare deletes the link id if userID created it, or whoever did // if userID is 0. DeleteShare(ctx context.Context, id, userID int64) error + // MoveShare gives the link id the path p, where what it leads to + // moved. + MoveShare(ctx context.Context, id int64, p string) error // DeleteExpiredShares deletes the links and folder shares that // expired. DeleteExpiredShares(ctx context.Context) error @@ -217,6 +222,8 @@ type Store interface { // ReleaseUpload ends writer's claim and keeps the upload for keep from // now. ReleaseUpload(ctx context.Context, id int64, writer string, keep time.Duration) error + // DeleteUpload forgets the upload id; ErrNotFound if it is gone + // already. DeleteUpload(ctx context.Context, id int64) error // TakeExpiredUploads deletes up to limit expired uploads that no // writer claims, and returns them. diff --git a/internal/auth/twofactor.go b/internal/auth/twofactor.go index 98a44cc..9730d88 100644 --- a/internal/auth/twofactor.go +++ b/internal/auth/twofactor.go @@ -36,6 +36,13 @@ const ( challengeTTL = 5 * time.Minute challengeTries = 5 maxChallenges = 10_000 + // Wrong codes count for their user, wherever they come from: after + // freeWrongCodes in a row, each next code waits, twice as long each + // time, up to maxCodeWait. Guessing then takes years, even with the + // password and many addresses. + freeWrongCodes = 5 + firstCodeWait = time.Minute + maxCodeWait = 24 * time.Hour ) var ( @@ -53,6 +60,70 @@ type CodeNeeded struct{ Challenge string } func (e *CodeNeeded) Error() string { return "a two-factor code is needed" } +// CodesRefused is the error of a two-factor code that came too soon after +// too many wrong ones: it was not checked, and the next may come after +// Wait. +type CodesRefused struct{ Wait time.Duration } + +func (e *CodesRefused) Error() string { return "too many wrong two-factor codes" } + +// wrongCodes counts the wrong two-factor codes in a row of each user, by ID. +type wrongCodes struct { + mu sync.Mutex + m map[int64]*wrongStreak +} + +type wrongStreak struct { + n int + last time.Time +} + +// next is when the next code may come. +func (w *wrongStreak) next() time.Time { + if w.n < freeWrongCodes { + return w.last + } + return w.last.Add(min(firstCodeWait< 0 { + w.n-- + } +} + // challenge is a sign-in that waits for a code. type challenge struct { user *User @@ -171,8 +242,35 @@ func (s *Service) loginSession(ctx context.Context, u *User, recovery bool) (*Se // checkCode checks code, from u's app or one of their recovery codes, and // uses it up. It reports whether it was a recovery code. Without two-factor -// sign-in, as it was turned off meanwhile, any code will do. +// sign-in, as it was turned off meanwhile, any code will do. After too many +// wrong codes, it refuses them for a while (*CodesRefused); what cannot be +// a code of either kind, such as one cut short, is no guess and does not +// count. func (s *Service) checkCode(ctx context.Context, u *User, code string) (bool, error) { + code = strings.Map(func(r rune) rune { + if r == ' ' || r == '-' { + return -1 + } + return r + }, strings.ToLower(strings.TrimSpace(code))) + if !totpShaped(code) && !recoveryShaped(code) { + return false, ErrWrongCode + } + if wait := s.wrongCodes.take(u.ID, time.Now()); wait > 0 { + return false, &CodesRefused{Wait: wait} + } + recovery, err := s.matchCode(ctx, u, code) + switch { + case err == nil: + s.wrongCodes.right(u.ID) + case !errors.Is(err, ErrWrongCode): + s.wrongCodes.refund(u.ID) + } + return recovery, err +} + +// matchCode does checkCode's checking. +func (s *Service) matchCode(ctx context.Context, u *User, code string) (bool, error) { tf, err := s.store.TwoFactor(ctx, u.ID) if err != nil { return false, err @@ -180,12 +278,6 @@ func (s *Service) checkCode(ctx context.Context, u *User, code string) (bool, er if tf.Secret == nil { return false, nil } - code = strings.Map(func(r rune) rune { - if r == ' ' || r == '-' { - return -1 - } - return r - }, strings.ToLower(strings.TrimSpace(code))) if step, ok := matchTOTP(tf.Secret, code, time.Now(), tf.Step); ok { used, err := s.store.UseTwoFactorStep(ctx, u.ID, step) if err != nil || !used { @@ -193,7 +285,7 @@ func (s *Service) checkCode(ctx context.Context, u *User, code string) (bool, er } return false, nil } - if len(code) != recoveryCodeLength || strings.ContainsFunc(code, func(r rune) bool { return !('a' <= r && r <= 'z' || '2' <= r && r <= '7') }) { + if !recoveryShaped(code) { return false, ErrWrongCode } used, err := s.store.UseRecoveryCode(ctx, u.ID, tokenHash(code)) @@ -203,6 +295,12 @@ func (s *Service) checkCode(ctx context.Context, u *User, code string) (bool, er return true, nil } +// recoveryShaped reports whether code has the letters and digits of a +// recovery code. +func recoveryShaped(code string) bool { + return len(code) == recoveryCodeLength && !strings.ContainsFunc(code, func(r rune) bool { return !('a' <= r && r <= 'z' || '2' <= r && r <= '7') }) +} + // cmpErr returns err, or otherwise if err is nil. func cmpErr(err, otherwise error) error { if err != nil { @@ -214,7 +312,7 @@ func cmpErr(err, otherwise error) error { // matchTOTP returns the time step around now for which code is the code of // secret, if one is, after the step last. func matchTOTP(secret []byte, code string, now time.Time, last int64) (int64, bool) { - if len(code) != totpDigits || strings.ContainsFunc(code, func(r rune) bool { return r < '0' || r > '9' }) { + if !totpShaped(code) { return 0, false } at := now.Unix() / totpPeriod @@ -226,6 +324,11 @@ func matchTOTP(secret []byte, code string, now time.Time, last int64) (int64, bo return 0, false } +// totpShaped reports whether code has the digits of an app's code. +func totpShaped(code string) bool { + return len(code) == totpDigits && !strings.ContainsFunc(code, func(r rune) bool { return r < '0' || r > '9' }) +} + // totpCode is the code of secret for the time step (RFC 4226, 5.3). func totpCode(secret []byte, step int64) string { mac := hmac.New(sha1.New, secret) @@ -261,10 +364,15 @@ func (s *Service) TwoFactorOf(ctx context.Context, u *User) (*TwoFactorState, er return st, nil } -// StartTwoFactor makes a new secret for u to add to their app, and returns -// it in base32: two-factor sign-in is on once ConfirmTwoFactor gets a code -// of it. ErrInvalid if it is on already. -func (s *Service) StartTwoFactor(ctx context.Context, u *User) (string, error) { +// StartTwoFactor makes a new secret for u to add to their app, after +// checking their password, and returns it in base32: two-factor sign-in is +// on once ConfirmTwoFactor gets a code of it. ErrInvalid if it is on +// already. The password keeps a stolen session from turning it on with a +// secret of its own, which would lock u out. +func (s *Service) StartTwoFactor(ctx context.Context, u *User, password string) (string, error) { + if err := s.checkPasswordOf(ctx, u, password); err != nil { + return "", err + } tf, err := s.store.TwoFactor(ctx, u.ID) if err != nil { return "", err diff --git a/internal/auth/twofactor_test.go b/internal/auth/twofactor_test.go index 393cf00..e82b9a6 100644 --- a/internal/auth/twofactor_test.go +++ b/internal/auth/twofactor_test.go @@ -55,7 +55,10 @@ func TestTwoFactor(t *testing.T) { if _, _, err := svc.ConfirmTwoFactor(ctx, dora, "123456"); !errors.Is(err, auth.ErrNotFound) { t.Errorf("confirmed nothing: %v", err) } - secret, err := svc.StartTwoFactor(ctx, dora) + if _, err := svc.StartTwoFactor(ctx, dora, "wrong password"); !errors.Is(err, auth.ErrInvalidCredentials) { + t.Errorf("set up with a wrong password: %v", err) + } + secret, err := svc.StartTwoFactor(ctx, dora, password) if err != nil { t.Fatal(err) } @@ -92,7 +95,7 @@ func TestTwoFactor(t *testing.T) { if st, err := svc.TwoFactorOf(ctx, dora); err != nil || !st.On || st.Pending != "" || st.RecoveryCodes != 10 { t.Errorf("on: %+v, %v", st, err) } - if _, err := svc.StartTwoFactor(ctx, dora); !errors.Is(err, auth.ErrInvalid) { + if _, err := svc.StartTwoFactor(ctx, dora, password); !errors.Is(err, auth.ErrInvalid) { t.Errorf("set up again: %v", err) } dora = userNamed(t, svc, "dora") @@ -158,6 +161,16 @@ func TestTwoFactor(t *testing.T) { if _, err := svc.ContinueLogin(ctx, "nope", codes[2]); !errors.Is(err, auth.ErrChallengeGone) { t.Errorf("no such sign-in: %v", err) } + // Those were five wrong codes in a row: the next waits (see + // TestWrongCodes). + _, err = svc.LoginWithCode(ctx, "dora", password, codes[2]) + if refused := (*auth.CodesRefused)(nil); !errors.As(err, &refused) { + t.Errorf("after five wrong codes: %v", err) + } + svc.AgeWrongCodes(time.Minute) + if _, err := svc.LoginWithCode(ctx, "dora", password, codes[2]); err != nil { + t.Errorf("a minute later: %v", err) + } // New recovery codes, and turning it off, take the password. if _, err := svc.NewRecoveryCodes(ctx, dora, "wrong password"); !errors.Is(err, auth.ErrInvalidCredentials) { @@ -190,7 +203,7 @@ func TestTwoFactor(t *testing.T) { } // Administrators turn it off for those who lost their app. - secret, err = svc.StartTwoFactor(ctx, dora) + secret, err = svc.StartTwoFactor(ctx, dora, password) if err != nil { t.Fatal(err) } @@ -212,6 +225,94 @@ func TestTwoFactor(t *testing.T) { }) } +func TestWrongCodes(t *testing.T) { + services(t, func(t *testing.T, svc *auth.Service) { + ctx := context.Background() + password, err := svc.CreateUser(ctx, "dora", "user") + if err != nil { + t.Fatal(err) + } + dora := userNamed(t, svc, "dora") + secret, err := svc.StartTwoFactor(ctx, dora, password) + if err != nil { + t.Fatal(err) + } + if _, _, err := svc.ConfirmTwoFactor(ctx, dora, codeOf(t, secret, 0)); err != nil { + t.Fatal(err) + } + code := codeOf(t, secret, 1) + wrong := otherCode(code) + refused := func(what string, err error, atMost time.Duration) { + t.Helper() + var r *auth.CodesRefused + if !errors.As(err, &r) || r.Wait <= atMost/2 || r.Wait > atMost { + t.Errorf("%s: %v (%+v)", what, err, r) + } + } + withChallenge := func(code string) error { + t.Helper() + _, err := svc.Login(ctx, "dora", password) + var needed *auth.CodeNeeded + if !errors.As(err, &needed) { + t.Fatal(err) + } + _, err = svc.ContinueLogin(ctx, needed.Challenge, code) + return err + } + + // Five wrong codes in a row go, however they come; what cannot be a + // code does not count. + for i := range 5 { + var err error + if i%2 == 0 { + _, err = svc.LoginWithCode(ctx, "dora", password, wrong) + } else { + err = withChallenge(wrong) + } + if !errors.Is(err, auth.ErrWrongCode) { + t.Errorf("wrong code %d: %v", i, err) + } + if _, err := svc.LoginWithCode(ctx, "dora", password, "12"); !errors.Is(err, auth.ErrWrongCode) { + t.Errorf("no code: %v", err) + } + } + // The next waits a minute, unchecked: even the right one. + _, err = svc.LoginWithCode(ctx, "dora", password, code) + refused("the sixth", err, time.Minute) + refused("the sixth, waiting for it", withChallenge(code), time.Minute) + // Then twice as long after each wrong one. + for _, wait := range []time.Duration{2 * time.Minute, 4 * time.Minute} { + svc.AgeWrongCodes(wait / 2) + if _, err := svc.LoginWithCode(ctx, "dora", password, wrong); !errors.Is(err, auth.ErrWrongCode) { + t.Errorf("after waiting: %v", err) + } + _, err = svc.LoginWithCode(ctx, "dora", password, code) + refused("then", err, wait) + } + // Up to a day. + for range 20 { + svc.AgeWrongCodes(24 * time.Hour) + if _, err := svc.LoginWithCode(ctx, "dora", password, wrong); !errors.Is(err, auth.ErrWrongCode) { + t.Fatalf("after a day: %v", err) + } + } + _, err = svc.LoginWithCode(ctx, "dora", password, code) + refused("at most", err, 24*time.Hour) + + // The right code ends the streak; made now, as checking codes takes + // a while. + svc.AgeWrongCodes(24 * time.Hour) + if _, err := svc.LoginWithCode(ctx, "dora", password, codeOf(t, secret, 1)); err != nil { + t.Fatal(err) + } + for range 5 { + if _, err := svc.LoginWithCode(ctx, "dora", password, wrong); !errors.Is(err, auth.ErrWrongCode) { + t.Errorf("after the right code: %v", err) + } + } + }) +} + func TestOTPAuthURI(t *testing.T) { got := auth.OTPAuthURI("files.example.com", "dora", "ABCDEF") want := "otpauth://totp/files.example.com:dora?algorithm=SHA1&digits=6&issuer=files.example.com&period=30&secret=ABCDEF" diff --git a/internal/server/auth.go b/internal/server/auth.go index 589a357..fba4ff9 100644 --- a/internal/server/auth.go +++ b/internal/server/auth.go @@ -391,11 +391,18 @@ func (s *Server) login(c fiber.Ctx) error { ctx, cancel := dbContext(c) defer cancel() sess, err := s.auth.LoginWithCode(ctx, form["username"], form["password"], form["code"]) - var needed *auth.CodeNeeded + var ( + needed *auth.CodeNeeded + refused *auth.CodesRefused + ) switch { case errors.As(err, &needed): check.succeeded() return s.askCode(c, isJSON, needed.Challenge, next) + case errors.As(err, &refused): + s.log.Warn("login codes refused: too many wrong ones", zap.String("ip", c.IP()), zap.String("user", auth.NormalizeUsername(form["username"]))) + retryAfter(c, refused.Wait) + return fail(fiber.StatusTooManyRequests, tooManyCodes(refused.Wait)) case errors.Is(err, auth.ErrWrongCode): // The password was right; the failure still counts. s.log.Warn("login code wrong", zap.String("ip", c.IP()), zap.String("user", auth.NormalizeUsername(form["username"]))) diff --git a/internal/server/dav.go b/internal/server/dav.go index 98b0e93..e9303db 100644 --- a/internal/server/dav.go +++ b/internal/server/dav.go @@ -699,17 +699,19 @@ func (s *Server) replacing(c fiber.Ctx, dst string, isDir bool, do func() error) if err != nil { return err } - // The shares of a folder stay with it, wherever it ends up. + // The shares of a folder stay with it, wherever it ends up. Those of + // a file stay with its name, as when it is uploaded again: apps save + // that way. asidePath := path.Join(realDir, aside) if isDir { - s.foldersMoved(realPath, asidePath) - defer s.foldersMoved(asidePath, "") + s.sharesMoved(realPath, asidePath, true) + defer s.sharesMoved(asidePath, "", true) } if err := do(); err != nil { perr := renameNoReplace(dir, aside, name, info) switch { case perr == nil && isDir: - s.foldersMoved(asidePath, realPath) + s.sharesMoved(asidePath, realPath, true) case perr != nil: // Something took the name meanwhile. if _, terr := s.toTrash(c, dir, aside, realPath, info); terr != nil { diff --git a/internal/server/dav_test.go b/internal/server/dav_test.go index 65f4e7e..7e71799 100644 --- a/internal/server/dav_test.go +++ b/internal/server/dav_test.go @@ -48,7 +48,7 @@ func davAs(t *testing.T, f *fixture, name string) *davClient { if err != nil { t.Fatal(err) } - password, _, err := f.auth.svc.CreateAppPassword(t.Context(), u, "test") + password, _, err := f.auth.svc.CreateAppPassword(t.Context(), u, "test", testPassword(name)) if err != nil { t.Fatal(err) } @@ -165,7 +165,7 @@ func TestDavSignIn(t *testing.T) { if err != nil { t.Fatal(err) } - password, _, err := svc.CreateAppPassword(ctx, u, "laptop") + password, _, err := svc.CreateAppPassword(ctx, u, "laptop", testPassword(name)) if err != nil { t.Fatal(err) } @@ -211,7 +211,7 @@ func TestDavSignIn(t *testing.T) { } // Revoked ones stop working, and so do all once the password changes. - revoked, ap, err := svc.CreateAppPassword(ctx, u, "phone") + revoked, ap, err := svc.CreateAppPassword(ctx, u, "phone", testPassword(name)) if err != nil { t.Fatal(err) } @@ -234,7 +234,10 @@ func TestDavSignIn(t *testing.T) { } // Failures count against the client as wrong passwords do. - fresh, _, err := svc.CreateAppPassword(ctx, u, "new laptop") + if u, err = svc.UserNamed(ctx, name); err != nil { + t.Fatal(err) + } + fresh, _, err := svc.CreateAppPassword(ctx, u, "new laptop", "another long password") if err != nil { t.Fatal(err) } @@ -775,7 +778,8 @@ func TestSyncPage(t *testing.T) { } // A new app password is shown once, and signs in to WebDAV. - resp, body = syncForm("action=create&name=laptop", html...) + pw := "&password=" + url.QueryEscape(testPassword(name)) + resp, body = syncForm("action=create&name=laptop"+pw, 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") { @@ -789,7 +793,7 @@ func TestSyncPage(t *testing.T) { } // Scripts get JSON. - resp, body = syncForm("action=create&name=phone") + resp, body = syncForm("action=create&name=phone" + pw) expectStatus(t, resp, http.StatusCreated) var created struct { ID int64 @@ -809,8 +813,11 @@ func TestSyncPage(t *testing.T) { want int says string }{ - {"action=create&name=", html, http.StatusBadRequest, "Name the app password"}, + {"action=create&name=" + pw, html, http.StatusBadRequest, "Name the app password"}, {"action=create&name=", nil, http.StatusBadRequest, "Name the app password"}, + // It takes the password: a stolen session alone makes none. + {"action=create&name=tablet", html, http.StatusForbidden, "The password is wrong."}, + {"action=create&name=tablet&password=wrong", nil, http.StatusForbidden, "The password is wrong."}, {"action=revoke&id=99999", html, http.StatusNotFound, "no such app password"}, {"action=revoke&id=x", nil, http.StatusBadRequest, ""}, {"action=dance", nil, http.StatusBadRequest, ""}, @@ -834,10 +841,10 @@ func TestSyncPage(t *testing.T) { // A user has only so many. for i := 1; i < auth.MaxAppPasswords; i++ { - resp, _ := syncForm("action=create&name=app") + resp, _ := syncForm("action=create&name=app" + pw) expectStatus(t, resp, http.StatusCreated) } - if resp, body := syncForm("action=create&name=app", html...); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "revoke one") { + if resp, body := syncForm("action=create&name=app"+pw, html...); resp.StatusCode != http.StatusConflict || !strings.Contains(body, "revoke one") { t.Errorf("too many: %d %s", resp.StatusCode, body) } diff --git a/internal/server/link_test.go b/internal/server/link_test.go index 0553e99..5e73d8b 100644 --- a/internal/server/link_test.go +++ b/internal/server/link_test.go @@ -2,6 +2,7 @@ package server import ( "encoding/json" + "errors" "net/http" "net/url" "os" @@ -359,3 +360,65 @@ func TestLinkUploadNames(t *testing.T) { t.Errorf("%d entries, %d with a random number", len(entries), random) } } + +// A link follows what it leads to when that is renamed or moved, and ends +// when it is deleted: it must not lead to what takes its place. +func TestLinkFollowsItsEntry(t *testing.T) { + f := newFixture(t) + f.write(t, "docs/a.txt", "alpha") + f.write(t, "docs/sub/b.txt", "beta") + f.write(t, "docs/d.txt", "one") + f.write(t, "archive/.keep", "") + admin, anon := f.as("admin"), f.as("") + file, fileInfo := createLink(t, admin, url.Values{"path": {"/docs/a.txt"}}) + folder, folderInfo := createLink(t, admin, url.Values{"path": {"/docs/sub"}}) + other, otherInfo := createLink(t, admin, url.Values{"path": {"/docs/d.txt"}}) + reads := func(p, want string) { + t.Helper() + if resp, body := anon.do(t, "GET", p); resp.StatusCode != http.StatusOK || body != want { + t.Errorf("%s: %d %q, want %q", p, resp.StatusCode, body, want) + } + } + gone := func(p string) { + t.Helper() + if resp, body := anon.do(t, "GET", p); resp.StatusCode == http.StatusOK || strings.Contains(body, "secret") { + t.Errorf("%s: %d %q", p, resp.StatusCode, body) + } + } + + // Renamed and moved, with the folder it is in too. + resp, _ := post(t, admin, "/docs/", "rename=a.txt&to=c.txt") + expectStatus(t, resp, http.StatusCreated) + reads(file+"/c.txt", "alpha") + resp, _ = post(t, admin, "/docs/", "move=sub&into=/archive") + expectStatus(t, resp, http.StatusCreated) + reads(folder+"/b.txt", "beta") + resp, _ = post(t, admin, "/", "rename=docs&to=papers") + expectStatus(t, resp, http.StatusCreated) + reads(file+"/c.txt", "alpha") + + // Uploaded again, a file keeps its link: apps save that way. + resp, _ = admin.send(t, "PUT", "/papers/d.txt", strings.NewReader("two")) + expectStatus(t, resp, http.StatusNoContent) + reads(other+"/d.txt", "two") + + // Deleted, it ends, even once something else takes the name. + resp, _ = admin.do(t, "DELETE", "/papers/c.txt") + expectStatus(t, resp, http.StatusNoContent) + f.write(t, "papers/c.txt", "secret") + gone(file + "/c.txt") + resp, _ = admin.do(t, "DELETE", "/archive/sub") + expectStatus(t, resp, http.StatusNoContent) + f.write(t, "archive/sub/b.txt", "secret") + gone(folder + "/b.txt") + // And so do the links to what was in a folder deleted. + resp, _ = admin.do(t, "DELETE", "/papers") + expectStatus(t, resp, http.StatusNoContent) + f.write(t, "papers/d.txt", "secret") + gone(other + "/d.txt") + for _, info := range []map[string]any{fileInfo, folderInfo, otherInfo} { + if sh, err := f.auth.svc.ShareByID(t.Context(), int64(info["id"].(float64))); !errors.Is(err, auth.ErrNotFound) { + t.Errorf("a link left: %+v, %v", sh, err) + } + } +} diff --git a/internal/server/manage.go b/internal/server/manage.go index ed7d3ee..2f3202c 100644 --- a/internal/server/manage.go +++ b/internal/server/manage.go @@ -200,6 +200,9 @@ func (s *Server) remove(c fiber.Ctx, dirPath, name string, permanent bool) (stri return "", err } defer release() + if err := sameEntry(dir, name, info); err != nil { + return "", err + } target := path.Join(dirPath, name) contents := 0 @@ -227,9 +230,7 @@ func (s *Server) remove(c fiber.Ctx, dirPath, name string, permanent bool) (stri syncDir(dir) // What its home holds moved to the trash, or left. s.homeChanged(path.Join(realDir, name)) - if info.IsDir() { - s.foldersMoved(path.Join(realDir, name), "") - } + s.sharesMoved(path.Join(realDir, name), "", info.IsDir()) s.log.Info("delete", zap.String("ip", c.IP()), zap.String("user", visitor(c)), zap.String("path", target), zap.Bool("folder", info.IsDir()), zap.Int("contents", contents), zap.Bool("trash", id != "")) return id, nil @@ -373,9 +374,7 @@ func (s *Server) rename(c fiber.Ctx, dirPath, from, to string) error { return s.openError(err) } syncDir(dir) - if info.IsDir() { - s.foldersMoved(path.Join(realDir, from), path.Join(realDir, to)) - } + s.sharesMoved(path.Join(realDir, from), path.Join(realDir, to), info.IsDir()) s.log.Info("rename", zap.String("ip", c.IP()), zap.String("user", visitor(c)), zap.String("path", src), zap.String("to", dst)) } return s.done(c, dirPath, fiber.StatusCreated, escapePath(object(dst, info.IsDir())), "renamed="+url.QueryEscape(to)) diff --git a/internal/server/mkdir.go b/internal/server/mkdir.go index dd1bce7..f5686e9 100644 --- a/internal/server/mkdir.go +++ b/internal/server/mkdir.go @@ -56,6 +56,9 @@ func (s *Server) makeFolder(c fiber.Ctx, urlPath, name string) error { return err } defer release() + if err := s.folderRoom(dir); err != nil { + return s.uploadError(&requestBody{}, err) + } if err := dir.Mkdir(name, 0o755); errors.Is(err, fs.ErrExist) { return fiber.ErrConflict } else if err != nil { diff --git a/internal/server/move.go b/internal/server/move.go index b040821..182a0cc 100644 --- a/internal/server/move.go +++ b/internal/server/move.go @@ -168,9 +168,7 @@ func (s *Server) moveEntry(c fiber.Ctx, dirPath, name, dest, newName string, rep } syncDir(dir) syncDir(to) - if info.IsDir() { - s.foldersMoved(realSrc, path.Join(realTo, newName)) - } + s.sharesMoved(realSrc, path.Join(realTo, newName), info.IsDir()) if toHome != fromHome { s.recount(fromHome) s.recount(toHome) @@ -358,6 +356,9 @@ func (s *Server) copyFolder(dst *os.Root, name, urlPath, realPath, dstPath, real } else if !errors.Is(err, fs.ErrNotExist) { return 0, err } + if err := s.folderRoom(dst); err != nil { + return 0, err + } if err := dst.Mkdir(tmp, 0o755); err != nil { return 0, err } @@ -421,6 +422,9 @@ func (s *Server) copyTree(dst *os.Root, urlPath, realPath, dstPath, realDst stri } var sub *os.Root if dst != nil { + if err := s.folderRoom(dst); err != nil { + return n, err + } if err := dst.Mkdir(it.Name, 0o755); errors.Is(err, fs.ErrExist) { return n, errExists } else if err != nil { diff --git a/internal/server/passkey.go b/internal/server/passkey.go index 3979561..f2e6a1c 100644 --- a/internal/server/passkey.go +++ b/internal/server/passkey.go @@ -37,8 +37,12 @@ type passkeyRequest struct { Credential json.RawMessage `json:"credential"` } -// siteOf is where the visitor of c makes and uses passkeys. -func siteOf(c fiber.Ctx) auth.Site { return auth.Site{Host: c.Hostname(), Origin: c.BaseURL()} } +// siteOf is where the visitor of c makes and uses passkeys. It is kept +// past the request, so the host name is copied out of its buffer, which +// the next request on the connection reuses. +func siteOf(c fiber.Ctx) auth.Site { + return auth.Site{Host: strings.Clone(c.Hostname()), Origin: strings.Clone(c.BaseURL())} +} // readJSON reads a small JSON body into v. func (s *Server) readJSON(c fiber.Ctx, v any) error { diff --git a/internal/server/passkey_test.go b/internal/server/passkey_test.go index ad9fe57..798c504 100644 --- a/internal/server/passkey_test.go +++ b/internal/server/passkey_test.go @@ -129,7 +129,7 @@ func TestPasskeySignIn(t *testing.T) { } // With two-factor sign-in, no code after it. - _, _, dora = setUpTwoFactor(t, dora) + _, _, dora = setUpTwoFactor(t, dora, "dora") if resp, _ := passkeySignIn(t, f, a, cred, "/"); resp.StatusCode != http.StatusOK { t.Errorf("with two-factor sign-in: %d", resp.StatusCode) } diff --git a/internal/server/people.go b/internal/server/people.go index fed9cf3..cdee41c 100644 --- a/internal/server/people.go +++ b/internal/server/people.go @@ -148,13 +148,21 @@ func (s *Server) davWays(c fiber.Ctx, urlPath string) []string { return ways } -// foldersMoved has the folder shares of the folder at the real URL path -// from, and of those in it, follow it to to, where it moved; they end with -// to "", as it was deleted, or once it left its home. A folder deleted by -// other means than goftp keeps its shares, until its owner stops them. -func (s *Server) foldersMoved(from, to string) { +// sharesMoved has the shares of the file or folder (dir) at the real URL +// path from, and of what is in it, follow it to to, where it moved, or end +// with to "", as it was deleted: its links (see auth.MoveShares) and, of a +// folder, who it is shared with (see auth.MoveFolderShares). What is +// deleted by other means than goftp keeps its shares, until they are +// stopped. +func (s *Server) sharesMoved(from, to string, dir bool) { ctx, cancel := context.WithTimeout(context.Background(), dbTimeout) defer cancel() + if err := s.auth.MoveShares(ctx, from, to); err != nil { + s.log.Error("move links", zap.String("path", from), zap.String("to", to), zap.Error(err)) + } + if !dir { + return + } if err := s.auth.MoveFolderShares(ctx, from, to); err != nil { s.log.Error("move folder shares", zap.String("path", from), zap.String("to", to), zap.Error(err)) } diff --git a/internal/server/people_test.go b/internal/server/people_test.go index 2f8c374..cbf80c0 100644 --- a/internal/server/people_test.go +++ b/internal/server/people_test.go @@ -43,7 +43,7 @@ func grant(t *testing.T, owner *fixture, p, user, access string) int64 { func TestFolderSharing(t *testing.T) { ta := newTestAuth(t) - f := newFixtureWith(t, ta, withHomes, func(cfg *config.Config) { cfg.Homes.Quota = 1000 }) + f := newFixtureWith(t, ta, withHomes, func(cfg *config.Config) { cfg.Homes.Quota = 64 << 10 }) dora := f.signedIn(ta.addUser(t, "dora", "user")) erin := f.signedIn(ta.addUser(t, "erin", "user")) finn := f.signedIn(ta.addUser(t, "finn", "user")) @@ -160,9 +160,9 @@ func TestFolderSharing(t *testing.T) { } } // What they add counts against the owner's home. - resp, _ = erin.send(t, "PUT", "/home/dora/docs/big.bin", strings.NewReader(strings.Repeat("x", 1200))) + resp, _ = erin.send(t, "PUT", "/home/dora/docs/big.bin", strings.NewReader(strings.Repeat("x", 60_000))) expectStatus(t, resp, http.StatusInsufficientStorage) - resp, _ = erin.send(t, "PUT", "/home/erin/big.bin", strings.NewReader(strings.Repeat("x", 900))) + resp, _ = erin.send(t, "PUT", "/home/erin/big.bin", strings.NewReader(strings.Repeat("x", 60_000))) expectStatus(t, resp, http.StatusCreated) // The Shared pages of both, and of a superadmin, who sees nothing of diff --git a/internal/server/quota.go b/internal/server/quota.go index 42eb59f..114d614 100644 --- a/internal/server/quota.go +++ b/internal/server/quota.go @@ -80,14 +80,22 @@ func (s *Server) countHome(name string, u *homeUse) error { return nil } +// folderBytes is what a folder counts for in a home, its share of the disk +// on most file systems: empty, it would count for nothing, and folders +// could be made without end. +const folderBytes = 4096 + // treeBytes adds up the files in the folder dir and the folders in it, // hidden ones too, such as unfinished uploads: they take space as well, but // for goftp's notes (whose a home is, what a trash entry is) and locks. A -// file with more names counts once. +// file with more names counts once; each folder in dir counts folderBytes. func treeBytes(dir *os.Root) int64 { var n int64 seen := map[[2]uint64]bool{} _ = fs.WalkDir(dir.FS(), ".", func(p string, d fs.DirEntry, err error) error { + if err == nil && d.IsDir() && p != "." { + n += folderBytes + } if err != nil || !d.Type().IsRegular() || p == homeOwner || p == trashInfo { return nil } @@ -170,6 +178,29 @@ func (s *Server) homeRoom(name string, n int64) error { return nil } +// folderRoom checks, before a folder is made in dir, that the disk is not +// down to upload.min_free, and that the folder fits in the home dir is in, +// if any, where it then counts as held. +func (s *Server) folderRoom(dir *os.Root) error { + if !s.roomFor(dir, folderBytes) { + return errLowSpace + } + name := s.homeAt(dir) + if name == "" { + return nil + } + u, err := s.homeUse(name) + if err != nil { + return err + } + defer u.mu.Unlock() + if u.quota > 0 && u.used+folderBytes > u.quota { + return errQuota + } + u.used += folderBytes + return nil +} + // homeAt returns the name of the home that the folder dir is in, if any. func (s *Server) homeAt(dir *os.Root) string { if s.cfg.Homes.Path == "" { @@ -211,6 +242,10 @@ func (s *Server) dirPath(dir *os.Root) (string, bool) { // file is made. func (s *Server) writeGuard(dir *os.Root, w io.Writer, credit int64) (io.Writer, error) { g := &guardWriter{w: w, space: s.spaceGuard(dir), credit: credit} + // Before anything is written, for files that stay empty. + if g.space != nil && !s.roomFor(dir, 0) { + return nil, errLowSpace + } if name := s.homeAt(dir); name != "" { u, err := s.homeUse(name) if err != nil { diff --git a/internal/server/quota_test.go b/internal/server/quota_test.go index e8fe09d..6f55251 100644 --- a/internal/server/quota_test.go +++ b/internal/server/quota_test.go @@ -156,3 +156,29 @@ func TestLinkMaxSize(t *testing.T) { t.Errorf("into a full home: %d", resp.StatusCode) } } + +// Folders count against a home's quota too: empty, they could otherwise be +// made without end. +func TestHomeQuotaFolders(t *testing.T) { + ta := newTestAuth(t) + f := newFixtureWith(t, ta, withHomes, func(cfg *config.Config) { cfg.Homes.Quota = 3 * folderBytes }) + dora := f.signedIn(ta.addUser(t, "dora", "user")) + for _, name := range []string{"a", "b", "c"} { + resp, _ := post(t, dora, "/home/dora/", "folder="+name) + expectStatus(t, resp, http.StatusCreated) + } + if resp, body := post(t, dora, "/home/dora/", "folder=d"); resp.StatusCode != http.StatusInsufficientStorage || !strings.Contains(body, "no room for it in this home") { + t.Errorf("one too many: %d %s", resp.StatusCode, body) + } + // Counted again, they still count; copies of them take room too. + f.srv.recountAll() + if used, _, _, err := f.srv.homeUsage("dora"); err != nil || used != 3*folderBytes { + t.Errorf("counted: %d, %v", used, err) + } + if resp, _ := post(t, dora, "/home/dora/", "copy=a&into=/home/dora/b"); resp.StatusCode != http.StatusInsufficientStorage { + t.Errorf("a copy: %d", resp.StatusCode) + } + if exists(filepath.Join(f.dir, "home", "dora", "b", "a")) || len(leftovers(filepath.Join(f.dir, "home", "dora", "b"))) != 0 { + t.Error("copied anyway") + } +} diff --git a/internal/server/resumable.go b/internal/server/resumable.go index 405c15c..98a502d 100644 --- a/internal/server/resumable.go +++ b/internal/server/resumable.go @@ -462,6 +462,14 @@ func (s *Server) completeUpload(c fiber.Ctx, dir *os.Root, up *auth.Upload, righ s.dropUpload(up) return err } + // Forgotten before its temp file goes: a HEAD that finds that gone + // drops the upload (see dropUpload), and must not give its use of a + // link back. + ctx, cancel := context.WithTimeout(context.Background(), dbTimeout) + defer cancel() + if err := s.auth.DeleteUpload(ctx, up.ID); err != nil && !errors.Is(err, auth.ErrNotFound) { + s.log.Warn("forget upload", zap.Int64("upload", up.ID), zap.Error(err)) + } // Linking leaves the temp file behind. _ = dir.Remove(up.Temp) if !up.Mtime.IsZero() { @@ -469,11 +477,6 @@ func (s *Server) completeUpload(c fiber.Ctx, dir *os.Root, up *auth.Upload, righ s.log.Warn("keep modification time", zap.String("path", path.Join(up.Dir, name)), zap.Error(err)) } } - ctx, cancel := context.WithTimeout(context.Background(), dbTimeout) - defer cancel() - if err := s.auth.DeleteUpload(ctx, up.ID); err != nil { - s.log.Warn("forget upload", zap.Int64("upload", up.ID), zap.Error(err)) - } s.logUpload(c, path.Join(up.Dir, name), up.Length, created) return nil } @@ -540,13 +543,16 @@ func (s *Server) releaseUpload(up *auth.Upload, writer string) { } // dropUpload forgets an upload and removes what arrived. An upload through -// a link gives its use of the link back. +// a link gives its use of the link back, unless it was forgotten already: +// it may have been stored meanwhile. func (s *Server) dropUpload(up *auth.Upload) { s.removePartial(up) ctx, cancel := context.WithTimeout(context.Background(), dbTimeout) defer cancel() if err := s.auth.DeleteUpload(ctx, up.ID); err != nil { - s.log.Warn("forget upload", zap.Int64("upload", up.ID), zap.Error(err)) + if !errors.Is(err, auth.ErrNotFound) { + s.log.Warn("forget upload", zap.Int64("upload", up.ID), zap.Error(err)) + } return } s.unuseLink(up.ShareID, up.Length) diff --git a/internal/server/resumable_test.go b/internal/server/resumable_test.go index bfd5f3c..8b8ba88 100644 --- a/internal/server/resumable_test.go +++ b/internal/server/resumable_test.go @@ -9,6 +9,7 @@ import ( "io" "net" "net/http" + "net/url" "os" "path/filepath" "strconv" @@ -262,6 +263,37 @@ func TestResumableCancel(t *testing.T) { } } +// A stored upload keeps its use of a link, even when something that found +// it unfinished drops it afterwards, as a HEAD does that finds its temp file +// gone the moment it is stored. +func TestResumableLinkUseKept(t *testing.T) { + f := newFixture(t) + f.write(t, "inbox/.keep", "") + anon := f.as("") + drop, info := createLink(t, f.as("admin"), url.Values{"path": {"/inbox"}, "kind": {"upload"}, "max_uses": {"2"}, "max_size": {"8"}}) + loc := startResumable(t, anon, drop+"/", "one.txt", 4) + up, err := f.auth.svc.UploadByToken(t.Context(), strings.TrimPrefix(loc, uploadsPrefix)) + if err != nil { + t.Fatal(err) + } + expectStatus(t, patchAt(t, anon, loc, 0, "1234"), http.StatusNoContent) + f.srv.dropUpload(up) + sh, err := f.auth.svc.ShareByID(t.Context(), int64(info["id"].(float64))) + if err != nil || sh.Uses != 1 || sh.Bytes != 4 { + t.Fatalf("after the drop: %+v, %v", sh, err) + } + if readFile(t, f.dir, "inbox/one.txt") != "1234" { + t.Error("the stored file went") + } + // Dropped before it was stored, it gives its use back. + loc = startResumable(t, anon, drop+"/", "two.txt", 4) + resp, _ := tus(t, anon, "DELETE", loc, "") + expectStatus(t, resp, http.StatusNoContent) + if sh, err := f.auth.svc.ShareByID(t.Context(), sh.ID); err != nil || sh.Uses != 1 || sh.Bytes != 4 { + t.Errorf("after a cancelled upload: %+v, %v", sh, err) + } +} + // Once all data is there, the file is stored as other uploads are. func TestResumableCompletion(t *testing.T) { ta := newTestAuth(t) diff --git a/internal/server/space.go b/internal/server/space.go index 16502ed..0b6dca2 100644 --- a/internal/server/space.go +++ b/internal/server/space.go @@ -7,22 +7,28 @@ import ( ) // errLowSpace stops writing short of the disk's last upload.min_free bytes, -// which are kept for the database and everything else on it. Like a full -// disk, it answers 507. -var errLowSpace = fmt.Errorf("less than upload.min_free left on the disk: %w", syscall.ENOSPC) +// which are kept for the database and everything else on it, and of its +// last inodes (see inodeReserve). Like a full disk, it answers 507. +var errLowSpace = fmt.Errorf("the disk is down to its last upload.min_free bytes or inodes: %w", syscall.ENOSPC) // spaceCheckEvery is how much is written between looks at the free space. const spaceCheckEvery = 8 << 20 -// roomFor reports whether n more bytes fit in the folder dir above -// upload.min_free; true where the free space cannot be known. +// With upload.min_free, the last 1/inodeReserve of a disk's inodes are +// kept too, where it counts them: each file and folder takes one, and +// empty ones take no bytes. +const inodeReserve = 100 + +// roomFor reports whether n more bytes, and a file or folder, fit in the +// folder dir above upload.min_free; true where the free space cannot be +// known. func (s *Server) roomFor(dir *os.Root, n int64) bool { min := int64(s.cfg.Upload.MinFree) if min <= 0 { return true } - free, ok := freeSpace(dir) - return !ok || free-n >= min + free, fewInodes, ok := diskSpace(dir) + return !ok || free-n >= min && !fewInodes } // spaceGuard returns what checks, for writes into the folder dir, that the diff --git a/internal/server/space_other.go b/internal/server/space_other.go index e3a7108..ad87c51 100644 --- a/internal/server/space_other.go +++ b/internal/server/space_other.go @@ -5,3 +5,5 @@ package server import "os" func freeSpace(*os.Root) (int64, bool) { return 0, false } + +func diskSpace(*os.Root) (int64, bool, bool) { return 0, false, false } diff --git a/internal/server/space_statfs.go b/internal/server/space_statfs.go index 5a6dd0e..646346a 100644 --- a/internal/server/space_statfs.go +++ b/internal/server/space_statfs.go @@ -11,14 +11,22 @@ import ( // freeSpace returns the bytes that unprivileged writers can still add to // the file system of the folder dir. func freeSpace(dir *os.Root) (int64, bool) { + free, _, ok := diskSpace(dir) + return free, ok +} + +// diskSpace returns freeSpace, and whether the file system's inodes, if it +// counts them, are down to their last 1/inodeReserve. +func diskSpace(dir *os.Root) (free int64, fewInodes, ok bool) { f, err := dir.Open(".") if err != nil { - return 0, false + return 0, false, false } defer f.Close() var st unix.Statfs_t if err := unix.Fstatfs(int(f.Fd()), &st); err != nil { - return 0, false + return 0, false, false } - return int64(st.Bavail) * int64(st.Bsize), true + files, left := int64(st.Files), int64(st.Ffree) + return int64(st.Bavail) * int64(st.Bsize), files > 0 && left < files/inodeReserve, true } diff --git a/internal/server/sync.go b/internal/server/sync.go index 76cbf3c..7688509 100644 --- a/internal/server/sync.go +++ b/internal/server/sync.go @@ -4,6 +4,7 @@ import ( "errors" "strconv" "strings" + "time" "github.com/gofiber/fiber/v3" "go.uber.org/zap" @@ -81,10 +82,11 @@ func (s *Server) renderSync(c fiber.Ctx, status int, notice, failure string, cre // 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 +// action=create&name=NAME&password=PW 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/ +// e.g. curl -H "Authorization: Bearer $TOKEN" -d action=create -d name=rclone -d password=... https://host/.sync/ func (s *Server) syncAction(c fiber.Ctx) error { u := userOf(c) if u == nil { @@ -99,16 +101,32 @@ func (s *Server) syncAction(c fiber.Ctx) error { defer cancel() switch form["action"] { case "create": - password, ap, err := s.auth.CreateAppPassword(ctx, u, form["name"]) + check, wait := s.checkPassword(c, u.Username) + if wait > 0 { + retryAfter(c, wait) + return s.syncFailure(c, html, explain(fiber.ErrTooManyRequests, tooMany)) + } + password, ap, err := s.auth.CreateAppPassword(ctx, u, form["name"], form["password"]) switch { case errors.Is(err, auth.ErrInvalid): + check.refund() 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.ErrInvalidCredentials): + s.log.Warn("app password refused: wrong password", zap.String("ip", c.IP()), zap.String("user", u.Username)) + return s.syncFailure(c, html, explain(fiber.ErrForbidden, "The password is wrong.")) case errors.Is(err, auth.ErrTooMany): + check.succeeded() 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 errors.Is(err, auth.ErrBusy): + check.refund() + retryAfter(c, time.Second) + return s.syncFailure(c, html, explain(fiber.ErrServiceUnavailable, busy)) case err != nil: + check.refund() return err } + check.succeeded() 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") diff --git a/internal/server/templates/sync.html b/internal/server/templates/sync.html index 2a3b443..5605c0b 100644 --- a/internal/server/templates/sync.html +++ b/internal/server/templates/sync.html @@ -40,9 +40,13 @@

Connect

New app password

+ +
diff --git a/internal/server/templates/twofactor.html b/internal/server/templates/twofactor.html index 3e4f8b3..6580648 100644 --- a/internal/server/templates/twofactor.html +++ b/internal/server/templates/twofactor.html @@ -74,8 +74,12 @@

Add this site to your authenticator app

Turn on

Signing in will then take, after your password, a 6-digit code from an authenticator app on your phone, which changes every 30 seconds: a stolen password alone will not do. Sync apps keep signing in with their app passwords.

-
+ + +
diff --git a/internal/server/twofactor.go b/internal/server/twofactor.go index 9de73b1..d1013a2 100644 --- a/internal/server/twofactor.go +++ b/internal/server/twofactor.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "html/template" + "math" "strings" "time" @@ -108,7 +109,7 @@ func qrSVG(code *qr.Code) template.HTML { // twoFactorAction sets up two-factor sign-in, sent URL-encoded or as JSON: // -// action=start makes a new secret for the visitor's app: +// action=start&password=PW makes a new secret for the visitor's app: // {"secret": BASE32, "uri": "otpauth://..."} // action=confirm&code=CODE turns it on, given a code of the secret, and // answers with recovery codes, shown once; it @@ -139,18 +140,6 @@ func (s *Server) twoFactorAction(c fiber.Ctx) error { c.Set(fiber.HeaderCacheControl, "no-store") switch form["action"] { - case "start": - secret, err := s.auth.StartTwoFactor(ctx, u) - if errors.Is(err, auth.ErrInvalid) { - return fail(fiber.StatusConflict, "Two-factor sign-in is on already.") - } - if err != nil { - return err - } - if isJSON { - return c.JSON(fiber.Map{"secret": secret, "uri": auth.OTPAuthURI(c.Hostname(), u.Username, secret)}) - } - return s.redirect(c, twoFactorPath) case "confirm": codes, changed, err := s.auth.ConfirmTwoFactor(ctx, u, form["code"]) switch { @@ -173,27 +162,36 @@ func (s *Server) twoFactorAction(c fiber.Ctx) error { c.Locals(sessionKey, &session{user: changed.User, token: changed.Token, cookie: sess.cookie}) return s.renderTwoFactor(c, fiber.StatusOK, twoFactorPage{Codes: codes, Notice: "Two-factor sign-in is on. You were signed out everywhere else."}) - case "codes", "off": + case "start", "codes", "off": default: return fiber.ErrBadRequest } - // Both take the password. + // These take the password. check, wait := s.checkPassword(c, u.Username) if wait > 0 { retryAfter(c, wait) return fail(fiber.StatusTooManyRequests, tooMany) } - var codes []string - if form["action"] == "codes" { + var ( + secret string + codes []string + ) + switch form["action"] { + case "start": + secret, err = s.auth.StartTwoFactor(ctx, u, form["password"]) + case "codes": codes, err = s.auth.NewRecoveryCodes(ctx, u, form["password"]) - } else { + default: err = s.auth.DisableTwoFactor(ctx, u, form["password"]) } switch { case errors.Is(err, auth.ErrInvalidCredentials): s.log.Warn("two-factor change refused: wrong password", zap.String("ip", c.IP()), zap.String("user", u.Username)) return fail(fiber.StatusForbidden, "The password is wrong.") + case errors.Is(err, auth.ErrInvalid) && form["action"] == "start": + check.succeeded() + return fail(fiber.StatusConflict, "Two-factor sign-in is on already.") case errors.Is(err, auth.ErrInvalid): check.succeeded() return fail(fiber.StatusConflict, "Two-factor sign-in is off.") @@ -206,6 +204,12 @@ func (s *Server) twoFactorAction(c fiber.Ctx) error { return err } check.succeeded() + if form["action"] == "start" { + if isJSON { + return c.JSON(fiber.Map{"secret": secret, "uri": auth.OTPAuthURI(c.Hostname(), u.Username, secret)}) + } + return s.redirect(c, twoFactorPath) + } if form["action"] == "off" { s.log.Info("two-factor sign-in off", zap.String("ip", c.IP()), zap.String("user", u.Username)) if isJSON { @@ -256,7 +260,12 @@ func (s *Server) loginCode(c fiber.Ctx, form map[string]string, isJSON bool, nex ctx, cancel := dbContext(c) defer cancel() sess, err := s.auth.ContinueLogin(ctx, challenge, form["code"]) + var refused *auth.CodesRefused switch { + case errors.As(err, &refused): + s.log.Warn("login codes refused: too many wrong ones", zap.String("ip", c.IP())) + retryAfter(c, refused.Wait) + return fail(fiber.StatusTooManyRequests, tooManyCodes(refused.Wait), false) case errors.Is(err, auth.ErrWrongCode): s.log.Warn("login code wrong", zap.String("ip", c.IP())) return fail(fiber.StatusUnauthorized, "Wrong code. Enter the one your app shows now, or a recovery code.", false) @@ -273,6 +282,18 @@ func (s *Server) loginCode(c fiber.Ctx, form map[string]string, isJSON bool, nex return s.signedIn(c, sess, isJSON, next) } +// tooManyCodes says to wait before the next two-factor code. +func tooManyCodes(wait time.Duration) string { + n, unit := int(math.Ceil(wait.Minutes())), "minute" + if n > 90 { + n, unit = int(math.Ceil(wait.Hours())), "hour" + } + if n != 1 { + unit += "s" + } + return fmt.Sprintf("Too many wrong codes: try again in %d %s, or sign in with a passkey.", n, unit) +} + // askCode answers a sign-in that needs a two-factor code, with challenge to // send back with it. func (s *Server) askCode(c fiber.Ctx, isJSON bool, challenge, next string) error { diff --git a/internal/server/twofactor_test.go b/internal/server/twofactor_test.go index d7b756e..94be189 100644 --- a/internal/server/twofactor_test.go +++ b/internal/server/twofactor_test.go @@ -9,6 +9,7 @@ import ( "fmt" "html" "net/http" + "net/url" "regexp" "strings" "testing" @@ -38,12 +39,12 @@ var ( challengeSent = regexp.MustCompile(`name="challenge" value="([A-Za-z0-9_-]{43})"`) ) -// setUpTwoFactor turns two-factor sign-in on for the user of f from the -// page, and returns their secret, recovery codes and new session. -func setUpTwoFactor(t *testing.T, f *fixture) (string, []string, *fixture) { +// setUpTwoFactor turns two-factor sign-in on for the user name of f from +// the page, and returns their secret, recovery codes and new session. +func setUpTwoFactor(t *testing.T, f *fixture, name string) (string, []string, *fixture) { t.Helper() html := []string{"Accept", "text/html"} - resp, _ := post(t, f, twoFactorPath, "action=start", html...) + resp, _ := post(t, f, twoFactorPath, "action=start&password="+url.QueryEscape(testPassword(name)), html...) if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != twoFactorPath { t.Fatalf("start: %d %v", resp.StatusCode, resp.Header) } @@ -88,7 +89,15 @@ func TestTwoFactorSignIn(t *testing.T) { if !strings.Contains(body, "Off: your password alone signs you in.") || !strings.Contains(body, `value="start"`) { t.Errorf("off: %s", body) } - secret, codes, dora := setUpTwoFactor(t, dora) + // Setting it up takes the password, which a stolen session lacks. + resp, body := post(t, dora, twoFactorPath, "action=start&password=wrong", html...) + if resp.StatusCode != http.StatusForbidden || !strings.Contains(body, "The password is wrong.") || secretShown.MatchString(body) { + t.Errorf("start, wrong password: %d %s", resp.StatusCode, body) + } + if resp, _ := post(t, dora, twoFactorPath, "action=start"); resp.StatusCode != http.StatusForbidden { + t.Errorf("start, no password: %d", resp.StatusCode) + } + secret, codes, dora := setUpTwoFactor(t, dora, "dora") _, body = dora.do(t, "GET", twoFactorPath, html...) if !strings.Contains(body, "On: after your password") || !strings.Contains(body, "10 of 10 left.") { t.Errorf("on: %s", body) @@ -103,7 +112,7 @@ func TestTwoFactorSignIn(t *testing.T) { r, k, v := formBody(kv...) return f.as("").send(t, "POST", loginPath, r, k, v) } - resp, body := form("username", "dora", "password", testPassword("dora"), "next", "/docs/") + resp, body = form("username", "dora", "password", testPassword("dora"), "next", "/docs/") m := challengeSent.FindStringSubmatch(body) if resp.StatusCode != http.StatusOK || m == nil || !strings.Contains(body, "

Two-factor sign-in

") || cookieOf(resp, cookieName) != nil { t.Fatalf("after the password: %d %s", resp.StatusCode, body) @@ -180,7 +189,7 @@ func TestTwoFactorSignIn(t *testing.T) { } // Superadmins turn it off for those who lost their app. - _, _, dora = setUpTwoFactor(t, dora) + _, _, dora = setUpTwoFactor(t, dora, "dora") resp, _ = post(t, f.as("superadmin"), usersPath, "action=two-factor-off&user=dora", html...) if resp.StatusCode != http.StatusSeeOther || resp.Header.Get("Location") != usersPath+"?done=two-factor" { t.Errorf("reset: %d %v", resp.StatusCode, resp.Header) @@ -197,7 +206,7 @@ func TestTwoFactorSignIn(t *testing.T) { func TestTwoFactorLimiter(t *testing.T) { ta := newTestAuth(t) f := newFixtureWith(t, ta, func(c *config.Config) { c.Limiter = config.LimiterConfig{MaxFailures: 3, Window: time.Minute} }) - _, codes, _ := setUpTwoFactor(t, f.signedIn(ta.addUser(t, "dora", "user"))) + _, codes, _ := setUpTwoFactor(t, f.signedIn(ta.addUser(t, "dora", "user")), "dora") form := func(kv ...string) (*http.Response, string) { t.Helper() r, k, v := formBody(kv...) @@ -217,3 +226,39 @@ func TestTwoFactorLimiter(t *testing.T) { t.Errorf("after the budget: %d", resp.StatusCode) } } + +// Wrong codes count for the account as well, wherever they come from: with +// the password, many addresses could otherwise guess codes for long. +func TestTwoFactorCodeLimit(t *testing.T) { + ta := newTestAuth(t) + f := newFixtureWith(t, ta) + secret, _, _ := setUpTwoFactor(t, f.signedIn(ta.addUser(t, "dora", "user")), "dora") + form := func(kv ...string) (*http.Response, string) { + t.Helper() + r, k, v := formBody(kv...) + return f.as("").send(t, "POST", loginPath, r, k, v) + } + wrong := otherTOTP(totpOf(t, secret, 1)) + for i := range 5 { + if resp, _ := form("username", "dora", "password", testPassword("dora"), "code", wrong); resp.StatusCode != http.StatusUnauthorized { + t.Errorf("wrong code %d: %d", i, resp.StatusCode) + } + } + resp, body := form("username", "dora", "password", testPassword("dora"), "code", totpOf(t, secret, 1)) + if resp.StatusCode != http.StatusTooManyRequests || resp.Header.Get("Retry-After") != "60" || + !strings.Contains(body, "Too many wrong codes: try again in 1 minute, or sign in with a passkey.") { + t.Errorf("the sixth: %d %v %s", resp.StatusCode, resp.Header, body) + } + // And so after a challenge. + _, body = form("username", "dora", "password", testPassword("dora")) + m := challengeSent.FindStringSubmatch(body) + if m == nil { + t.Fatal(body) + } + if resp, body := form("challenge", m[1], "code", totpOf(t, secret, 1)); resp.StatusCode != http.StatusTooManyRequests || !strings.Contains(body, m[1]) { + t.Errorf("with a challenge: %d %s", resp.StatusCode, body) + } + if n := len(f.logs.entries("login codes refused: too many wrong ones")); n != 2 { + t.Errorf("%d refusals logged", n) + } +} diff --git a/internal/server/upload.go b/internal/server/upload.go index cc2ec3b..a942933 100644 --- a/internal/server/upload.go +++ b/internal/server/upload.go @@ -819,7 +819,7 @@ func (s *Server) wrapHandler(next fasthttp.RequestHandler) fasthttp.RequestHandl s.writeAccess(access{ ip: ctx.RemoteIP().String(), method: string(ctx.Method()), - path: string(ctx.URI().PathOriginal()), + path: logPath(string(ctx.URI().PathOriginal())), status: ctx.Response.StatusCode(), start: ctx.Time(), }, int64(len(ctx.Response.Body())), nil) diff --git a/internal/server/upload_test.go b/internal/server/upload_test.go index 7de93e3..89f8507 100644 --- a/internal/server/upload_test.go +++ b/internal/server/upload_test.go @@ -485,6 +485,21 @@ func TestMinFree(t *testing.T) { if resp, _ := op.send(t, "POST", "/inbox/", body, "Content-Type", ctype); resp.StatusCode != http.StatusInsufficientStorage { t.Errorf("form: %d", resp.StatusCode) } + // So do empty files and folders, which take no bytes but an inode. + if resp, _ := op.send(t, "PUT", "/inbox/empty.txt", strings.NewReader("")); resp.StatusCode != http.StatusInsufficientStorage { + t.Errorf("an empty file: %d", resp.StatusCode) + } + body, ctype = multipartForm(t, formPart{field: "file", filename: "empty.txt"}) + if resp, _ := op.send(t, "POST", "/inbox/", body, "Content-Type", ctype); resp.StatusCode != http.StatusInsufficientStorage { + t.Errorf("an empty file in a form: %d", resp.StatusCode) + } + if resp, _ := post(t, op, "/inbox/", "folder=sub"); resp.StatusCode != http.StatusInsufficientStorage { + t.Errorf("a folder: %d", resp.StatusCode) + } + f.write(t, "docs/sub/.keep", "") + if resp, _ := post(t, op, "/", "copy=docs&into=/inbox"); resp.StatusCode != http.StatusInsufficientStorage { + t.Errorf("a folder copied: %d", resp.StatusCode) + } if got := leftovers(filepath.Join(f.dir, "inbox")); len(got) != 0 { t.Errorf("left behind: %v", got) }