From 52d1828b468127bc2e10fc3a0942e0c95442f875 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Wed, 11 Nov 2026 19:25:09 +0100 Subject: [PATCH 1/2] fix(auth): a token stops working when its account is deleted SQLite gives the highest user id again after that user is deleted, and an access token lives for minutes, so the token of a deleted user worked as whoever got the id next (settings, taste, listens, account). requireAuth now looks the account up after checking the token and refuses it when the account is gone, has another name, or was created after the token was signed. The token carries its issue time for this. Tests: a unit test of every case, and the real scenario on SQLite (delete the highest id, create another user, the old token gets 401, the new one works). Closes #30 --- README.md | 2 +- internal/api/http/middleware.go | 23 +++++- internal/api/http/middleware_test.go | 108 +++++++++++++++++++++++++++ internal/api/http/router.go | 4 +- internal/auth/token.go | 9 ++- 5 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 internal/api/http/middleware_test.go 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..0f27cc5 100644 --- a/internal/api/http/middleware.go +++ b/internal/api/http/middleware.go @@ -78,12 +78,18 @@ func requireAdmin(users userLookup) gin.HandlerFunc { } } -// requireAuth checks the access token. It reads "Authorization: Bearer "; +// 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. +// +// 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