diff --git a/README.md b/README.md index 31fa060..a71d0bb 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,7 @@ A library path must be an absolute, existing folder, never the filesystem root, ## Security notes - Sonda listens on localhost only, unless you change `server.listen`. To use it from other machines, put it behind a reverse proxy that terminates HTTPS. -- Passwords are hashed with Argon2id. Refresh tokens are stored only as hashes. +- Passwords are hashed with Argon2id. Refresh tokens are stored only as hashes. An access token is checked against the database on every request, so it stops working as soon as its account is deleted, even if the same user id is given to a new account. - Music folders are read-only to Sonda. Streaming opens files through `os.Root`, so a path can never leave its library, even if a symlink is swapped in later. - File paths are never returned by the catalog, and audio file paths are not logged. - Provider API keys are write-only through the API and never logged. They sit in the database, so protect your backups. diff --git a/internal/api/http/middleware.go b/internal/api/http/middleware.go index 3359f5c..0e41a92 100644 --- a/internal/api/http/middleware.go +++ b/internal/api/http/middleware.go @@ -16,6 +16,7 @@ import ( const ( ctxUserID = "user_id" ctxUsername = "username" + ctxAccount = "account" // the auth.User that requireAuth already looked up ) // limitBody caps the request body size. @@ -63,14 +64,20 @@ type userLookup interface { // of when the access token expires. func requireAdmin(users userLookup) gin.HandlerFunc { return func(c *gin.Context) { - user, err := users.User(c.Request.Context(), currentUserID(c)) + // requireAuth just read the account in this same request: use it, do not + // read it twice. + account, known := accountOf(c) + var err error + if !known { + account, err = users.User(c.Request.Context(), currentUserID(c)) + } switch { case errors.Is(err, auth.ErrUserNotFound): writeError(c, http.StatusUnauthorized, "unauthorized", "account no longer exists") case err != nil: _ = c.Error(err) writeError(c, http.StatusInternalServerError, "internal", "internal error") - case !user.IsAdmin: + case !account.IsAdmin: writeError(c, http.StatusForbidden, "admin_required", "this action needs an admin account") default: c.Next() @@ -78,12 +85,27 @@ func requireAdmin(users userLookup) gin.HandlerFunc { } } -// requireAuth checks the access token. It reads "Authorization: Bearer "; +// accountOf returns the account that requireAuth looked up for this request. +func accountOf(c *gin.Context) (auth.User, bool) { + v, ok := c.Get(ctxAccount) + u, isUser := v.(auth.User) + return u, ok && isUser +} + +// requireAuth checks the access token, then that it was issued for the account +// that has this id now. User ids can be given again after an account is deleted +// (SQLite reuses the highest one), and a token lives for minutes: without this check +// the token of a deleted user would work as whoever gets the id next. The account +// must exist, have the same name, and not have been created after the token was signed +// (both times are whole seconds from the server's clock; a clock stepped back can make +// a new account look newer than its token for the length of the step). +// +// It reads "Authorization: Bearer "; // when allowQuery is true and the header is absent it also accepts the // access_token query parameter. That is for routes loaded by an