MEDIUM: Setup endpoint race condition — multiple admin users can be created #26
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Bug
Files:
internal/handlers/setup.go,HandleSetupPOST()internal/service/auth/auth.go,CreateUser()internal/middleware/middleware.go,SetupRequired()Severity: MEDIUM — Authentication bypass
Description
There is a TOCTOU (Time-of-Check-Time-of-Use) race condition in the setup flow:
SetupRequired()middleware checks if any users existHandleSetupPOST()processes the formCreateUser()checksUserExists()again, then insertsIf two requests arrive simultaneously (e.g., automated attack while the setup page is accessible), both can pass the
UserExists()check before either has inserted a user. The second insert may succeed (SQLite doesn't have a UNIQUE constraint on username based on the migration), creating a second admin account.Even if the second
CreateUserreturnsErrUserExists, the middleware check at step 1 happens independently for each request.Suggested Fix
users.usernamein the database schemaCreateUseror use an INSERT with conflict handling:CreateUserfunction should check row count after insert to detect conflictsimplement 1 and 2 and give me a PR