Skip to content

user: keep a username unique within its group name - #99

Merged
msimerson merged 1 commit into
NicTool:mainfrom
aberoham:user-unique-name
Nov 20, 2026
Merged

msimerson merged 1 commit into
NicTool:mainfrom
aberoham:user-unique-name

Conversation

@aberoham

Copy link
Copy Markdown
Contributor

Two POST /user calls with the same username and gid both answer 201. A PUT can also rename a user onto a name its group already has. Login takes username@groupname, and with two matching rows it signs in whichever one the password fits. That name no longer identifies one user.

v2 refused this in User/Sanity.pm _username_exists. A username had to be unique among live users in every live group with that group's name. This PR applies the same rule in the mysql and file stores. User create, rename, move and restore check it. So do group rename, restore, and create with an explicit id, since a destroyed group leaves its users behind. A clash answers 409 with username <name>@<group> is not unique.

A user now needs a group that exists, or the store answers 400. Without that, two users could share a login in a gid that a later group takes.

On mysql each check runs in a transaction under a GET_LOCK on the group name, as zone names already do. The lock key drops trailing spaces because mysql compares names that way. A new test starts four creates of one login at once and expects one winner. Without the lock, two of them won. The group's usable_ns is now written inside the same transaction, so a refused rename leaves it alone. A restore through delete({ deleted: false }) goes through put and is checked too.

The file store tests in lib/store/file.test.js now seed group 1 before creating users in it.

v2's t/06_users.t checks this at lines 236 and 306. With this change v3 refuses both. The later failures at 395 and 408 go away.

Login takes username@groupname, so two live users with one username in
live groups of one name made it ambiguous. User create, rename, move and
restore now refuse that with a conflict, as v2 did. So do group rename
and restore. A user needs a group that exists. On mysql a named lock on
the group name serialises the check against concurrent writers.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are blocking approval.

No open issues 🎉

What changed in this PR

This PR restores v2’s username-uniqueness rule in the MySQL and file stores, preventing ambiguous logins across live groups that share a name.

Changes:

  • Check user and group creation, updates, and restoration for username conflicts; require users to reference an existing group.
  • Protect MySQL uniqueness checks with group-name locks and keep group permission updates in the same transaction.
  • Add conflict and concurrency tests, and seed required groups in file-store fixtures.
File Description
routes/​user.test.js Verify duplicate user creation and renaming return HTTP 409.
lib/​user/​test/​mysql.js Test concurrent user creation and group-renaming conflicts.
lib/​user/​test/​index.js Cover uniqueness, missing groups, moves, and restoration.
lib/​user/​store/​mysql.js Add transactional username checks, group-name locks, and group validation.
lib/​user/​store/​file.js Validate username availability and group existence during user writes.
lib/​user/​store/​base.js Add shared conflict errors and file-store uniqueness helpers.
lib/​store/​file.test.js Seed groups required by user fixtures.
lib/​group/​store/​mysql.js Check group-name transitions and update permissions transactionally.
lib/​group/​store/​file.js Check group creation, renaming, and restoration for username conflicts.

Review effort: Balanced

@msimerson
msimerson merged commit f90d8e8 into NicTool:main Nov 20, 2026
11 checks passed
@aberoham
aberoham deleted the user-unique-name branch November 20, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants