Repository navigation
feat(admin): manage users from the Administration page and log security events - #68
Merged
Merged
Conversation
POST /users/admin/create always made an administrator, had no validation or error handling, and only looked for the email. PUT /users/:id accepted any username or email (a duplicate was a generic 500, and login takes either as the identifier) and a password of any length, and could demote the last administrator. DELETE /users/:id let an administrator delete their own account, and GET /users/:id returned the whole document, password hash and encrypted Discogs token included. Create takes isAdmin from the body (default false) and answers with the new user. Create and update trim the fields, require a valid email and at least 6 characters of password like the profile route, and refuse a username or email another user has. The last administrator cannot be demoted, and nobody can delete their own account (which also keeps the last administrator, since only an administrator can call the route). A user that does not exist, or an id that is not an id, is a 404 instead of a 500.
The page could toggle the role, reset a password and delete, but there was no way to add a user, or to change a username or email, short of signing up or editing MongoDB by hand. An "Add user" button opens a dialog with username, email, password and an administrator checkbox; a pencil on each row opens the same dialog for that user, sending only what changed (a blank password keeps the current one) and keeping the role fixed on your own account. The server's message is shown when it refuses (a taken username or email, the last administrator). New strings in all 7 locales.
The check only wanted something@something, so testuser@known was accepted. The domain now needs at least one dot with text on both sides of every dot.
Self-hosted instances can use admin@localhost. It is the one domain without a dot that the admin email check accepts; other single-label hosts such as user@known are still refused.
Failed logins left one line without an address, admin actions left none, and the rate limiter answered 429 silently. A small audit() helper now writes one structured line per security event: `audit <action> <outcome> key="value"` for grep, fail2ban or a log shipper, with the same fields under `audit` in the JSON. Recorded: login (success, unknown user, wrong password), signup (success, closed, email taken), throttled attempts, user create, update (the changed field names, never values) and delete, a non-administrator calling an admin route, and a manual task run. Never recorded: passwords, tokens, or what was typed as the identifier. Names are quoted so a line break cannot forge a second line. The address is req.ip, so behind a reverse proxy it needs TRUST_PROXY.
Production always trusts one proxy hop; only development behind a proxy needs TRUST_PROXY=true.
changed=role did not tell a promotion from a demotion. An edit that changes the role now logs role=user->admin or role=admin->user, and a new account logs its starting role. The other changed fields are still names only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Administration page can toggle a role, reset a password and delete a user, but there is no way to add a user, or to change a username or email, short of signing up or editing MongoDB by hand.
POST /api/users/admin/createexists but nothing calls it, and the admin routes behind it trust their input. This adds the missing UI and tightens the routes it relies on. Opened as a pull request straight away rather than a discussion issue; happy to rework or drop any part.Changes
fix(users): validate and protect the admin user routesisAdmin: truefor everyone, had no validation or error handling, and only looked for the email. It now takesisAdminfrom the body (default false), trims the fields, requires a valid email (with a top-level domain, souser@hostis refused;localhostis the one exception, for self-hosted setups) and the same 6-character password minimum as the profile route, and refuses a username or email another user has. It answers 201 with the new user.GET /users/:id) returned the whole document, including the password hash and the encrypted Discogs token. It now leaves both out.feat(admin): add and edit users from the Administration page: an "Add user" button opens a dialog (username, email, password, administrator), and a pencil on each row opens the same dialog for that user. Editing sends only what changed, a blank password keeps the current one, and the role is fixed on your own account (the page already blocked that). The server's message is shown when it refuses. 8 new strings in all 7 locales.feat(audit): log logins, admin user actions and refused admin requests: security events as one structured line each, so an instance that is reachable from the internet can see who tried what. A smallaudit()helper writesaudit <action> <outcome> key="value" ... ip=...(grep, fail2ban or a log shipper can pick it out) and the same fields underauditin the JSON. It records: login (success, unknown user, wrong password), signup (success, closed, email taken), throttled attempts (the rate limiter used to answer 429 silently), user create, edit (the names of the changed fields, never their values; a role change is shown asrole=user->adminorrole=admin->user, since a role is not a secret and who was promoted matters) and delete, a non-administrator calling an admin route, and a manual task run. Never recorded: passwords, tokens, or what was typed as the identifier (people sometimes type a password there); a failed login says only whether the user exists, and names the real account when it does. Names are quoted so a line break cannot forge a second line. The address isreq.ip: right behind the bundled Nginx (production trusts one proxy hop), and in development behind a proxy it needsTRUST_PROXY=true. Nothing is stored or shown in the UI. If you would like an Audit view in the Administration page, this is the one place to also write to the database, and I am happy to build it.Not changed: deleting a user does not remove their collection items (that is how it behaved before and is a separate question), and the existing reset-password prompt and role toggle stay as they are.
How it was verified
cd backend && npm run build && npm run test:run: pass, 259 tests. 48 new tests for the four user handlers (users.controller.test.ts), most of which fail against the previous controller, and 23 for the audit events (the helper, login and signup, the admin guard, a task run).cd frontend && npm run lint && npm run test:run && npm run build: 0 lint errors, 179 tests pass. 17 new tests: the dialog (create, administrator, required fields, short password, server message, edit sending only changes, own role locked, cancel) and the page wiring (Add user opens an empty form and reloads the list after saving, the edit button opens the user, own role disabled).GET /users/:idhas nopasswordordiscogs.tokenEncrypted; editing a username works, and an email that is root's answers 400; a sole administrator demoting themselves answers 400 and deleting your own account answers 400; with a second administrator the demotion works, and the second administrator can delete the first.Audit lines, on a throwaway backend with an empty database: a login, an unknown user, a wrong password, a user create, an edit, a login as that user, that user calling
GET /api/users, and a delete each print the expected line (the typed identifier of the failed login is not in it), and 101 login attempts end in arate_limitedline and a 429. The task-run line is covered by a unit test only. While throttled, every rejected request writes a line, so a flood fills the log at the rate of the flood.Tried by hand in the browser on a local dev server: adding and editing a user work. Not checked: phone width. The tests use the translation keys, not the translated text.
Checklist
develop(notmain)fix(discogs): …CHANGELOG.mdunder## [Unreleased]npm run lint,npm run test:runandnpm run buildpassnpm run buildpassesbackend/src/scripts/migration-runner.ts(none needed)