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
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Postgres whose access is decided by [Casbin](https://casbin.org) policies.
- Names can be searched across folders (see [Search](#search)), and links
can share files and folders, or take uploads like a drop box (see
[Share links](#share-links)).
- Sync apps and file managers reach the same files over WebDAV, signed in
with app passwords (see [Syncing with WebDAV](#syncing-with-webdav)).
- Dotfiles are never listed or served. Paths are resolved with `os.Root`, so
requests and symlinks cannot escape the served directory.

Expand Down Expand Up @@ -390,6 +392,58 @@ Creating links needs the `share` action on what is shared, and for
upload links `write` too. Admins have it; on upgrade, it is added only if
the admin rule is still the default one. To let others share, e.g. `goftp policy add user:bob '/bob/*' share`.

## Syncing with WebDAV

goftp serves its files over WebDAV at `/.dav/`, so that sync apps can keep
a folder on a computer or phone in sync with it, and file managers can
open it: `https://host/.dav/PATH` is `PATH`, with the same rules, homes,
quotas and trash as the site. **Sync**, in the account menu, shows the
address (your home, if you have one) and makes app passwords.

Apps sign in with your username and an **app password**, never your own
password: make one for each app or device, so that you can revoke one
without the others. It is shown once (only a hash is stored), may be typed
without its dashes, and stops working when revoked, when you change your
password, or when you are given a new temporary one. Each user may have
20. Scripts:

```sh
curl -H "Authorization: Bearer $TOKEN" -d action=create -d name=laptop https://host/.sync/
# {"id":1,"name":"laptop","username":"bob","password":"abcd-efgh-...","url":"https://host/.dav/"}
curl -H "Authorization: Bearer $TOKEN" -d action=revoke -d id=1 https://host/.sync/
```

With [rclone](https://rclone.org), which keeps modification times with
`vendor=owncloud`, a folder syncs both ways like this; the first run needs
`--resync`, and the second line can then run from cron:

```sh
rclone config create goftp webdav url=https://host/.dav/home/bob/ vendor=owncloud user=bob pass=APP-PASSWORD --obscure
rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**" --resync
rclone bisync ~/goftp goftp: --exclude ".*" --exclude ".*/**"
```

FolderSync (Android), GoodSync, Mountain Duck and Cyberduck take the same
address, username and app password as a WebDAV account; Finder opens it
with Go → Connect to Server. goftp keeps the modification times that
apps send with uploads (`X-OC-Mtime`) or set afterwards (`PROPPATCH` of
`getlastmodified`, or Windows' `Win32LastModifiedTime`), and reports how
much room a home has left (RFC 4331). Limits:

- Names that start with a dot are not synced, as goftp hides them: exclude
them in the app. macOS's `.DS_Store` and `._` files are taken and
dropped.
- Other properties than modification times are refused. Locks are
advisory: they keep other WebDAV clients that ask from taking the same
lock, and nothing else.
- WebDAV sends each file in one request, where the browser sends large
ones in pieces: behind a proxy that limits the size of requests
(Cloudflare: 100 MB on its Free and Pro plans), larger files cannot be
uploaded over WebDAV.

Wrong app passwords count against the client's sign-in budget
(`limiter.*`).

## Security notes

- Serve over HTTPS: passwords and session tokens travel with every sign-in
Expand Down Expand Up @@ -418,6 +472,9 @@ the admin rule is still the default one. To let others share, e.g. `goftp policy
- Symlinks are followed only when they use relative targets that stay inside
the served directory, do not lead into a dotfile or dot-directory, and
lead where the visitor may go anyway.
- App passwords carry 120 bits, and only their SHA-256 hashes are stored.
They sign in to WebDAV alone, with HTTP Basic authentication, which
sends them with every request: serve WebDAV over HTTPS only.
- Share links carry 256-bit tokens, which are kept out of the logs. Link
passwords are hashed with Argon2id; wrong ones count against the
client's sign-in budget, and each link takes at most 20 password checks
Expand Down
77 changes: 77 additions & 0 deletions internal/auth/apppass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package auth

import (
"context"
"crypto/rand"
"fmt"
"strings"
"time"
"unicode"
"unicode/utf8"
)

// AppPassword lets an app, such as a sync app, sign in as its user over
// WebDAV, with a password of its own that can be revoked alone.
type AppPassword struct {
ID int64
UserID int64
Name string // what it is for, e.g. "rclone on my laptop"
CreatedAt time.Time
LastUsedAt time.Time // zero if never
}

// MaxAppPasswords bounds the app passwords of one user.
const MaxAppPasswords = 20

// maxAppPasswordName bounds the length of an app password's name.
const maxAppPasswordName = 64

// appPasswordLength is the number of letters and digits of an app password,
// 120 bits of it; it is shown in groups of four, which may be left out.
const appPasswordLength = 24

// CreateAppPassword makes an app password named name for u, and returns it:
// it is not kept, so it can only be shown now.
func (s *Service) CreateAppPassword(ctx context.Context, u *User, name string) (string, *AppPassword, error) {
name = strings.TrimSpace(name)
if name == "" || utf8.RuneCountInString(name) > maxAppPasswordName || strings.ContainsFunc(name, unicode.IsControl) {
return "", nil, fmt.Errorf("%w: name it in up to %d characters", ErrInvalid, maxAppPasswordName)
}
raw := strings.ToLower(rand.Text()[:appPasswordLength])
var groups []string
for i := 0; i < len(raw); i += 4 {
groups = append(groups, raw[i:i+4])
}
ap := &AppPassword{UserID: u.ID, Name: name}
if err := s.store.CreateAppPassword(ctx, tokenHash(raw), ap); err != nil {
return "", nil, err
}
return strings.Join(groups, "-"), ap, nil
}

// AppPasswords lists u's app passwords, newest first.
func (s *Service) AppPasswords(ctx context.Context, u *User) ([]AppPassword, error) {
return s.store.AppPasswords(ctx, u.ID)
}

// RevokeAppPassword ends u's app password id (ErrNotFound if u has none
// of that ID).
func (s *Service) RevokeAppPassword(ctx context.Context, u *User, id int64) error {
return s.store.DeleteAppPassword(ctx, u.ID, id)
}

// AppPasswordUser returns the user username, if password is one of their
// app passwords (ErrNotFound otherwise). Case and the dashes between groups
// do not matter.
func (s *Service) AppPasswordUser(ctx context.Context, username, password string) (*User, error) {
raw := strings.ToLower(strings.ReplaceAll(strings.TrimSpace(password), "-", ""))
if len(raw) != appPasswordLength {
return nil, ErrNotFound
}
for _, c := range raw {
if !('a' <= c && c <= 'z' || '2' <= c && c <= '7') {
return nil, ErrNotFound
}
}
return s.store.AppPasswordUser(ctx, NormalizeUsername(username), tokenHash(raw))
}
149 changes: 149 additions & 0 deletions internal/auth/apppass_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package auth_test

import (
"context"
"errors"
"regexp"
"strings"
"testing"
"time"

"goftp/internal/auth"
)

func TestAppPasswords(t *testing.T) {
services(t, func(t *testing.T, svc *auth.Service) {
ctx := context.Background()
temp, err := svc.CreateUser(ctx, "dora", "user")
if err != nil {
t.Fatal(err)
}
if _, err := svc.CreateUser(ctx, "erin", "user"); err != nil {
t.Fatal(err)
}
dora, erin := userNamed(t, svc, "dora"), userNamed(t, svc, "erin")

for _, name := range []string{"", " ", strings.Repeat("x", 65), "a\nb"} {
if _, _, err := svc.CreateAppPassword(ctx, dora, name); !errors.Is(err, auth.ErrInvalid) {
t.Errorf("CreateAppPassword(%q): %v", name, err)
}
}
password, ap, err := svc.CreateAppPassword(ctx, dora, " rclone on my laptop ")
if err != nil {
t.Fatal(err)
}
if !regexp.MustCompile(`^[a-z2-7]{4}(-[a-z2-7]{4}){5}$`).MatchString(password) {
t.Errorf("password %q", password)
}
if ap.ID == 0 || ap.Name != "rclone on my laptop" || ap.CreatedAt.IsZero() || !ap.LastUsedAt.IsZero() {
t.Errorf("app password %+v", ap)
}

// It signs in as dora, written as people may copy it.
for _, tc := range []struct{ username, password string }{
{"dora", password},
{"Dora", strings.ToUpper(password)},
{"dora", strings.ReplaceAll(password, "-", "")},
{"dora", " " + password + "\n"},
} {
u, err := svc.AppPasswordUser(ctx, tc.username, tc.password)
if err != nil || u.ID != dora.ID {
t.Errorf("AppPasswordUser(%q, %q): %v, %v", tc.username, tc.password, u, err)
}
}
other, _, err := svc.CreateAppPassword(ctx, erin, "phone")
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct{ username, password string }{
{"erin", password}, // someone else's
{"dora", other}, // and the other way round
{"dora", temp}, // the account's password
{"dora", password[:len(password)-1] + "1"}, // not base32
{"dora", password + "a"},
{"nobody", password},
{"dora", ""},
} {
if _, err := svc.AppPasswordUser(ctx, tc.username, tc.password); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("AppPasswordUser(%q, %q): %v", tc.username, tc.password, err)
}
}

// Listed newest first, with when they were last used.
phone, _, err := svc.CreateAppPassword(ctx, dora, "phone")
if err != nil {
t.Fatal(err)
}
apps, err := svc.AppPasswords(ctx, dora)
if err != nil {
t.Fatal(err)
}
if len(apps) != 2 || apps[0].Name != "phone" || apps[1].ID != ap.ID || time.Since(apps[1].LastUsedAt) > time.Minute ||
!apps[0].LastUsedAt.IsZero() {
t.Errorf("listed %+v", apps)
}

// Revoked, one stops working and the others do not.
if err := svc.RevokeAppPassword(ctx, erin, ap.ID); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("revoked by someone else: %v", err)
}
if err := svc.RevokeAppPassword(ctx, dora, ap.ID); err != nil {
t.Fatal(err)
}
if err := svc.RevokeAppPassword(ctx, dora, ap.ID); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("revoked twice: %v", err)
}
if _, err := svc.AppPasswordUser(ctx, "dora", password); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("revoked app password: %v", err)
}
if _, err := svc.AppPasswordUser(ctx, "dora", phone); err != nil {
t.Errorf("the other app password: %v", err)
}

// A user has only so many.
for i := 1; i < auth.MaxAppPasswords; i++ {
if _, _, err := svc.CreateAppPassword(ctx, dora, "app"); err != nil {
t.Fatal(i, err)
}
}
if _, _, err := svc.CreateAppPassword(ctx, dora, "one too many"); !errors.Is(err, auth.ErrTooMany) {
t.Errorf("over the limit: %v", err)
}

// A new password ends them all: they may have been made by
// whoever learned the old one.
sess, err := svc.Login(ctx, "dora", temp)
if err != nil {
t.Fatal(err)
}
if _, err := svc.ChangePassword(ctx, sess.User, temp, "a long new password"); err != nil {
t.Fatal(err)
}
if _, err := svc.AppPasswordUser(ctx, "dora", phone); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("after a password change: %v", err)
}
if apps, err := svc.AppPasswords(ctx, dora); err != nil || len(apps) != 0 {
t.Errorf("after a password change: %v, %v", apps, err)
}
if _, err := svc.AppPasswordUser(ctx, "erin", other); err != nil {
t.Errorf("another user's: %v", err)
}
// So does a reset by an administrator, and deleting the user.
if _, err := svc.ResetPassword(ctx, "erin"); err != nil {
t.Fatal(err)
}
if _, err := svc.AppPasswordUser(ctx, "erin", other); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("after a reset: %v", err)
}
last, _, err := svc.CreateAppPassword(ctx, dora, "last")
if err != nil {
t.Fatal(err)
}
if err := svc.DeleteUser(ctx, "dora"); err != nil {
t.Fatal(err)
}
if _, err := svc.AppPasswordUser(ctx, "dora", last); !errors.Is(err, auth.ErrNotFound) {
t.Errorf("of a deleted user: %v", err)
}
})
}
Loading
Loading