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 61ea6b648c
commit 0eeee1e884
16 changed files with 171 additions and 116 deletions
@@ -125,6 +125,16 @@ describe('PasswordResetTokensRepository', () => {
expect((await rawRow(target)).consumed_at).not.toBeNull();
expect((await rawRow(other)).consumed_at).toBeNull();
});
it('PWDRESETREPO-014: burns a token once: a second call finds it used and leaves the first stamp', async () => {
const { user } = createUser(testDb);
const target = await seedToken(user.id, hash('once'));
expect(await tokens.markConsumed(target)).toBe(true);
const stamped = (await rawRow(target)).consumed_at;
expect(await tokens.markConsumed(target)).toBe(false);
expect((await rawRow(target)).consumed_at).toBe(stamped);
});
});
describe('consumeAllLiveForUser', () => {
@@ -134,6 +134,7 @@ beforeAll(async () => {
await createTestMcpTokensRepo(testDb),
await createTestUsersRepo(testDb),
new EphemeralTokenService(),
await createTestUnitOfWork(testDb),
);
svc = new AuthService(
new PermissionsService(await createTestAppSettingsRepo(testDb), await createTestUnitOfWork(testDb)),
@@ -14,6 +14,7 @@ import { AuthController } from '../../../src/nest/auth/auth.controller';
import type { AuthService } from '../../../src/nest/auth/auth.service';
import type { UserProfileService } from '../../../src/nest/auth/user-profile.service';
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
import type { UnitOfWork } from '../../../src/nest/database/unit-of-work';
import { ApiTokenGuard } from '../../../src/nest/public-api/api-token.guard';
import { grantedScopes, narrowToGrant, requireScope } from '../../../src/nest/public-api/public-api-request';
import { PublicApiController } from '../../../src/nest/public-api/public-api.controller';
@@ -24,7 +25,12 @@ import type { User } from '../../../src/types';
import { createUser } from '../../helpers/factories';
import { findRow, updateRows } from '../../helpers/factories/rows';
import { resetTestDb } from '../../helpers/test-db';
import { createTestMcpTokensRepo, createTestUsersRepo, sharedTestOrm } from '../../helpers/test-uow';
import {
createTestMcpTokensRepo,
createTestUnitOfWork,
createTestUsersRepo,
sharedTestOrm,
} from '../../helpers/test-uow';
import { HttpException } from '@nestjs/common';
import type { ExecutionContext } from '@nestjs/common';
import {
@@ -398,6 +404,7 @@ describe('TokenService — storing and resolving a grant', () => {
await createTestMcpTokensRepo(testDb),
await createTestUsersRepo(testDb),
new EphemeralTokenService(),
await createTestUnitOfWork(testDb),
);
});
@@ -472,6 +479,7 @@ describe('TokenService — storing and resolving a grant', () => {
} as unknown as McpTokensRepository,
{} as UsersRepository,
new EphemeralTokenService(),
{} as UnitOfWork,
);
expect((await noColumns.verifyApiTokenWithGrant('trek_legacy'))?.grant).toEqual({
mode: 'all',
+2 -1
View File
@@ -14,7 +14,7 @@ import { createUser } from '../../helpers/factories';
import { countRows, findRow, insertRow, updateRows } from '../../helpers/factories/rows';
import { expectRegisteredProvider } from '../../helpers/module-providers';
import { resetTestDb } from '../../helpers/test-db';
import { createTestMcpTokensRepo, createTestUsersRepo } from '../../helpers/test-uow';
import { createTestMcpTokensRepo, createTestUnitOfWork, createTestUsersRepo } from '../../helpers/test-uow';
import { sharedTestOrm } from '../../helpers/test-uow';
import { describe, it, expect, beforeAll, beforeEach, afterAll, vi } from 'vitest';
@@ -64,6 +64,7 @@ beforeAll(async () => {
await createTestMcpTokensRepo(testDb),
await createTestUsersRepo(testDb),
new EphemeralTokenService(),
await createTestUnitOfWork(testDb),
);
});
+2 -1
View File
@@ -18,7 +18,7 @@ import { deleteRows, updateRows } from '../helpers/factories/rows';
import { setAppSetting } from '../helpers/factories/settings';
import { readUser } from '../helpers/factories/users';
import { resetTestDb, resetRateLimits } from '../helpers/test-db';
import { createTestMcpTokensRepo, createTestUsersRepo } from '../helpers/test-uow';
import { createTestMcpTokensRepo, createTestUnitOfWork, createTestUsersRepo } from '../helpers/test-uow';
import { MikroORM } from '@mikro-orm/core';
import type { INestApplication } from '@nestjs/common';
@@ -55,6 +55,7 @@ beforeAll(async () => {
await createTestMcpTokensRepo(testDb),
await createTestUsersRepo(testDb),
new EphemeralTokenService(),
await createTestUnitOfWork(testDb),
);
// Real WebSocket against the unified NestJS app (Express is gone). buildApp owns
// the same composition production uses; we attach the real ws server to it.