fix(db): read the checks a write relies on in the transaction that writes

On Postgres a check read before a transaction, or outside one, no longer
holds at the write that follows: the pool runs other requests' writes in
between. Moved into the unit of work that writes:

- the last-admin guard of an admin's role edit and of an account deletion
  (deleteUserCompletely takes a precondition run in its transaction)
- the login lockout guards of the auth settings save and the SSO settings
  save
- the first-account check of an SSO sign-up, which starts over as an
  ordinary sign-up when another account won
- the OAuth client caps, the token cap and the mail intake limits
- the year check before a vacay year is opened, and the label name check of
  a rename

A password sign-up that loses the last use of an invite is now refused, as
the SSO sign-up already was, and a reset link is burned by one conditional
update, so two resets with one link cannot both change the password.
This commit is contained in:
Maurice
2026-11-17 14:14:40 +01:00
parent c1bbe27ac6
commit af861e5816
16 changed files with 171 additions and 116 deletions
+13 -13
View File
@@ -260,19 +260,6 @@ export class AdminService {
}
const passwordHash = password ? bcrypt.hashSync(password, BCRYPT_COST) : null;
// Don't let the admin UI demote the last remaining admin — that would leave the
// instance with no one able to manage it (and on OIDC-only setups, no recovery). #1274
// SECURITY: plain JS comparisons, deliberately never folded into a
// repository method's WHERE clause (R4) — two repository reads, one
// service-level `if`.
if (role && role !== 'admin') {
const currentRole = await this.users.getRole(userId);
if (currentRole === 'admin') {
const adminCount = await this.users.countAdmins();
if (adminCount <= 1) throw new DomainError(400, 'Cannot remove the last admin');
}
}
// An admin sets a password for exactly one reason: the account is believed
// compromised. Without bumping password_version, verifyJwtAndLoadUser keeps
// accepting every cookie the intruder already holds, so the one action taken
@@ -299,6 +286,19 @@ export class AdminService {
// call, never split across separate un-transacted repository calls.
try {
await this.uow.transactional(async () => {
// Don't let the admin UI demote the last remaining admin — that would leave the
// instance with no one able to manage it (and on OIDC-only setups, no recovery). #1274
// SECURITY: plain JS comparisons, deliberately never folded into a
// repository method's WHERE clause (R4) — two repository reads, one
// service-level `if`. Read in the transaction that demotes, so two admins
// demoting each other at once cannot both see the other one still counted.
if (role && role !== 'admin') {
const currentRole = await this.users.getRole(userId);
if (currentRole === 'admin') {
const adminCount = await this.users.countAdmins();
if (adminCount <= 1) throw new DomainError(400, 'Cannot remove the last admin');
}
}
await this.users.applyAdminEdit(userId, patch);
if (password) {