fix: prevent setup endpoint race condition (closes #26) #31
No reviewers
Labels
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sneak/upaas#31
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch ":fix/setup-race-condition-closes-26"
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?
Summary
Fixes the TOCTOU race condition in
CreateUserwhere concurrent requests to the setup endpoint could create multiple admin users.Changes
CreateUser— serializes setup attempts at the application levelINSERT ... ON CONFLICT(username) DO NOTHING— newCreateUserAtomicfunction uses conflict handling at the DB level (defense-in-depth)Note: The UNIQUE constraint on
users.usernamewas already present in the initial migration (001_initial.sql).Test Results
All tests pass with
-raceflag. See comments below for full output.Test Results
All tests pass with
-raceflag:New test
TestCreateUserRaceConditionverifies that 10 concurrent goroutines racing to create admin users results in exactly 1 success and 9ErrUserExistserrors.Linting
golangci-lintnot available in this environment — please verify withmake checklocally.why are you using an atomic function plus a mutex in the normal path? simplify this code. in the usual case only a single admin user will ever be created.
if golangci-lint isn't available, install it.
golangci-lint results
34 issues total, mostly pre-existing across the codebase. Issues introduced by this PR (in files we touched):
internal/models/user.go:172—noinlineerr: inline error handling inCreateFirstUserinternal/service/auth/auth_test.go—funlen(test too long),wsl_v5(whitespace),testifylint(use require),paralleltest(missing parallel in race test)Pre-existing issues (not from this PR):
duplin IDOR tests,wsl_v5across middleware/deploy,noctx/testpackage/perfsprintin rate limit tests,nestif/unconvertin handlers, etc.Happy to fix the ones we introduced if you want.
make a new PR fixing all linting issues.