Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 |
|--------------|-----------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`.

Expand All @@ -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/`.
Expand Down Expand Up @@ -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:
Expand All @@ -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/
```
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +35,7 @@ services:
build: .
image: goftp
restart: unless-stopped
logging: *logging
depends_on:
postgres:
condition: service_healthy
Expand Down
19 changes: 15 additions & 4 deletions internal/auth/apppass.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"context"
"crypto/rand"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down
28 changes: 20 additions & 8 deletions internal/auth/apppass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 18 additions & 5 deletions internal/auth/authtest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions internal/auth/export_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 6 additions & 1 deletion internal/auth/passkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/passkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading
Loading