Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): r1-h1 serialize member password work

Josuéeda749686a7a
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index df3391491..d8062d02c 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.740",
+ "version": "0.1.741",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.740",
+ "version": "0.1.741",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index f03e556c0..fedcb3320 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.740",
+ "version": "0.1.741",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/db.ts b/control-api/src/db.ts
index 8b9a08d4a..83f3b8e5e 100644
--- a/control-api/src/db.ts
+++ b/control-api/src/db.ts
@@ -6730,7 +6730,11 @@ export async function assertDbReady(db: DbClient = pool): Promise<void> {
}
}
-export type DbTransactionOptions = { signal?: AbortSignal }
+export type DbTransactionOptions = {
+ signal?: AbortSignal
+ /** Called only after PostgreSQL acknowledges COMMIT. Must be synchronous and non-throwing. */
+ onCommitAcknowledged?: () => void
+}
export async function withTransaction<T>(
work: (db: DbTransactionClient) => Promise<T>,
@@ -6870,6 +6874,7 @@ export async function withTransaction<T>(
commitSent = true
await client.query('COMMIT')
transactionFinished = true
+ options.onCommitAcknowledged?.()
// An acknowledged COMMIT is the outcome, even if the signal aborted
// meanwhile: the finally still evicts the session instead of restoring it.
return result
diff --git a/control-api/src/routes/admin/auth.ts b/control-api/src/routes/admin/auth.ts
index 07cd0cb7c..c17a1bd78 100644
--- a/control-api/src/routes/admin/auth.ts
+++ b/control-api/src/routes/admin/auth.ts
@@ -5,6 +5,7 @@ import { createHash } from 'node:crypto'
import { config } from '../../config.js'
import { pool } from '../../db.js'
import { type UiAuthedRequest, requireAuthForControlUI } from '../../middleware/controlUIAuth.js'
+import { sendPasswordAdmissionError } from '../../middleware/passwordLoginAdmission.js'
import { rateLimitMiddleware } from '../../middleware/rateLimitMiddleware.js'
import { rootLogger } from '../../observability/logger.js'
import {
@@ -33,10 +34,12 @@ import {
validateControlAdminPasswordResetToken,
} from '../../services/controlAdminInvitationRegistrationService.js'
import {
+ type MemberPasswordWorkLease,
acceptInvitationById,
getPendingMemberInvitationForEmail,
provisionAdminDesktopWorkspace,
setInvitationPasswordForUser,
+ withMemberPasswordWorkLease,
} from '../../services/directory/index.js'
import { setupInitialAdminWithDesktopWorkspace } from '../../services/initialAdminSetupService.js'
import { memberRegistrationErrorResponse } from '../../services/memberRegistrationErrors.js'
@@ -483,60 +486,70 @@ export function createAdminAuthRouter(): Router {
const validation = await validateControlAdminInvitationToken(token, email)
const passwordHash = await bcrypt.hash(password, 12)
- const result = await completeControlAdminInvitation({
- email: validation.email,
- invitationId: validation.invitationUuid,
- username,
- passwordHash,
- })
- if ('error' in result) {
- const status = result.error === 'not_found' ? 404 : 409
- res.status(status).json({ error: result.error })
- return
- }
+ const completeInvitation = async (passwordWorkLease?: MemberPasswordWorkLease) => {
+ const result = await completeControlAdminInvitation({
+ email: validation.email,
+ invitationId: validation.invitationUuid,
+ username,
+ passwordHash,
+ })
+ if ('error' in result) {
+ return {
+ status: result.error === 'not_found' ? 404 : 409,
+ body: { error: result.error },
+ }
+ }
- const acceptedDesktopAccess = desktopAccess
- ? await acceptInvitationById(validation.email, desktopAccess.id)
- : null
- if (acceptedDesktopAccess && 'error' in acceptedDesktopAccess) {
- // The control-admin invitation is already committed. A failed desktop
- // acceptance leaves that admin in place and, for a hand-over, the
- // inviter already retired. Record the code without the invitee email.
- rootLogger.error(
- {
- event: 'control_admin_desktop_acceptance_failed',
- invitationId: validation.invitationUuid,
- adminId: result.id,
- errorCode: acceptedDesktopAccess.error,
+ const acceptedDesktopAccess = desktopAccess
+ ? await acceptInvitationById(validation.email, desktopAccess.id)
+ : null
+ if (acceptedDesktopAccess && 'error' in acceptedDesktopAccess) {
+ // The control-admin invitation is already committed. A failed desktop
+ // acceptance leaves that admin in place and, for a hand-over, the
+ // inviter already retired. Record the code without the invitee email.
+ rootLogger.error(
+ {
+ event: 'control_admin_desktop_acceptance_failed',
+ invitationId: validation.invitationUuid,
+ adminId: result.id,
+ errorCode: acceptedDesktopAccess.error,
+ },
+ 'desktop invitation acceptance failed after the control admin invitation committed'
+ )
+ return { status: 409, body: { error: acceptedDesktopAccess.error } }
+ }
+ if (acceptedDesktopAccess?.data?.userId && desktopAccess) {
+ const passwordResult = await setInvitationPasswordForUser(
+ acceptedDesktopAccess.data.userId,
+ validation.email,
+ desktopAccess.id,
+ memberPassword,
+ passwordWorkLease
+ )
+ if ('error' in passwordResult) {
+ return { status: 409, body: { error: passwordResult.error } }
+ }
+ }
+
+ return {
+ status: 200,
+ body: {
+ completed: true,
+ desktopAccessCompleted: Boolean(
+ acceptedDesktopAccess && !('error' in acceptedDesktopAccess)
+ ),
+ login: {
+ username: result.email || result.username,
+ },
},
- 'desktop invitation acceptance failed after the control admin invitation committed'
- )
- res.status(409).json({ error: acceptedDesktopAccess.error })
- return
- }
- if (acceptedDesktopAccess?.data?.userId && desktopAccess) {
- const passwordResult = await setInvitationPasswordForUser(
- acceptedDesktopAccess.data.userId,
- validation.email,
- desktopAccess.id,
- memberPassword
- )
- if ('error' in passwordResult) {
- res.status(409).json({ error: passwordResult.error })
- return
}
}
-
- res.status(200).json({
- completed: true,
- desktopAccessCompleted: Boolean(
- acceptedDesktopAccess && !('error' in acceptedDesktopAccess)
- ),
- login: {
- username: result.email || result.username,
- },
- })
+ const completion = desktopAccess
+ ? await withMemberPasswordWorkLease(lease => completeInvitation(lease))
+ : await completeInvitation()
+ res.status(completion.status).json(completion.body)
} catch (error) {
+ if (sendPasswordAdmissionError(error, res)) return
next(error)
}
}
diff --git a/control-api/src/routes/external/users.ts b/control-api/src/routes/external/users.ts
index 1498c0568..f0ed78a35 100644
--- a/control-api/src/routes/external/users.ts
+++ b/control-api/src/routes/external/users.ts
@@ -8,6 +8,7 @@ import {
requireExternalUserParamMatch,
requireValidExternalSessionToken,
} from '../../middleware/externalSessionAuth.js'
+import { sendPasswordAdmissionError } from '../../middleware/passwordLoginAdmission.js'
import { rateLimitMiddleware } from '../../middleware/rateLimitMiddleware.js'
import { resolveMcpServersForAgents } from '../../services/access/mcpInvocable.js'
import {
@@ -322,6 +323,7 @@ export function createExternalUsersRouter(gateway: K8sGateway): Router {
}
return res.status(200).json(result)
} catch (error) {
+ if (sendPasswordAdmissionError(error, res)) return
return next(error)
}
}
diff --git a/control-api/src/services/auth/passwordWorkOwnership.ts b/control-api/src/services/auth/passwordWorkOwnership.ts
index 538fa5f1d..8e726129b 100644
--- a/control-api/src/services/auth/passwordWorkOwnership.ts
+++ b/control-api/src/services/auth/passwordWorkOwnership.ts
@@ -9,6 +9,7 @@ let ownerInstance: string | undefined
export async function acquirePasswordWork(): Promise<{
release: () => Promise<void>
releaseWithin: (db: Pick<DbTransactionClient, 'query'>) => Promise<void>
+ confirmReleaseWithinCommitted: () => void
} | null> {
const owner = (ownerInstance ??= randomUUID())
const operation = randomUUID()
@@ -35,12 +36,17 @@ export async function acquirePasswordWork(): Promise<{
if (result.rows.length !== 1) throw new Error('password work ownership lost')
}
let released: Promise<void> | undefined
+ let releaseWithinCommitted = false
return {
// Password credential updates can release the durable owner in the same
// transaction as their commit. If that transaction rolls back, callers
// must fall back to release() so the persisted owner does not linger.
releaseWithin: releaseOwned,
+ confirmReleaseWithinCommitted: () => {
+ releaseWithinCommitted = true
+ },
release: () => {
+ if (releaseWithinCommitted) return Promise.resolve()
released ??= withTransaction(async db => {
await releaseOwned(db)
})
diff --git a/control-api/src/services/directory/index.ts b/control-api/src/services/directory/index.ts
index 59e4b4461..f5b27c024 100644
--- a/control-api/src/services/directory/index.ts
+++ b/control-api/src/services/directory/index.ts
@@ -61,6 +61,7 @@ export {
listPendingInvitations,
setInvitationPasswordForUser,
setInvitationPasswordForEmail,
+ withMemberPasswordWorkLease,
searchDirectory,
softDeleteMember,
startDraftInvitationCleanup,
@@ -70,6 +71,7 @@ export {
updateProfile,
updateUserPassword,
} from './membership.js'
+export type { MemberPasswordWorkLease } from './membership.js'
export {
getTeamContexts,
diff --git a/control-api/src/services/directory/membership.ts b/control-api/src/services/directory/membership.ts
index 63b966c98..9bf31e47f 100644
--- a/control-api/src/services/directory/membership.ts
+++ b/control-api/src/services/directory/membership.ts
@@ -1,5 +1,5 @@
import bcrypt from 'bcryptjs'
-import { type DbClient, pool, withTransaction } from '../../db.js'
+import { type DbClient, type DbTransactionClient, pool, withTransaction } from '../../db.js'
import { rootLogger } from '../../observability/logger.js'
import { PASSWORD_ADMISSION_POLICY } from '../auth/passwordAdmissionState.js'
import { PasswordAdmissionError } from '../auth/passwordCredentialVerification.js'
@@ -186,6 +186,75 @@ async function acquirePasswordSetupLease() {
}
}
+export type MemberPasswordWorkLease = NonNullable<Awaited<ReturnType<typeof acquirePasswordWork>>>
+
+type PasswordWorkLease = MemberPasswordWorkLease
+
+/** Reserve the member-password lane around a multi-step invitation completion. */
+export async function withMemberPasswordWorkLease<T>(
+ work: (lease: MemberPasswordWorkLease) => Promise<T>
+): Promise<T> {
+ const lease = await acquirePasswordSetupLease()
+ let outcome!: T
+ let workFailed = false
+ let workError: unknown
+ try {
+ outcome = await work(lease)
+ } catch (error) {
+ workFailed = true
+ workError = error
+ }
+ try {
+ await lease.release()
+ } catch {
+ passwordSetupLogger.warn(
+ { event: 'password_authority_unavailable' },
+ 'password authority unavailable'
+ )
+ throw new PasswordAdmissionError(503, 2, 'authority_failure')
+ }
+ if (workFailed) throw workError
+ return outcome
+}
+
+/** Commit member credential mutations and durable-owner release as one outcome. */
+async function withPasswordWorkTransaction<T>(
+ lease: PasswordWorkLease,
+ work: (db: DbTransactionClient) => Promise<T>
+): Promise<T> {
+ let commitAcknowledged = false
+ let hasOutcome = false
+ let outcome!: T
+ try {
+ return await withTransaction(
+ async db => {
+ outcome = await work(db)
+ hasOutcome = true
+ await lease.releaseWithin(db)
+ return outcome
+ },
+ undefined,
+ {
+ onCommitAcknowledged: () => {
+ commitAcknowledged = true
+ lease.confirmReleaseWithinCommitted()
+ },
+ }
+ )
+ } catch (error) {
+ if (commitAcknowledged && hasOutcome) {
+ passwordSetupLogger.warn(
+ { event: 'password_post_commit_cleanup_failed' },
+ 'password transaction committed; connection cleanup failed'
+ )
+ return outcome
+ }
+ throw error
+ } finally {
+ if (!commitAcknowledged) await lease.release()
+ }
+}
+
async function lockPasswordResetUser(
db: Pick<DbClient, 'query'>,
email: string
@@ -1135,7 +1204,8 @@ export async function setInvitationPasswordForUser(
userId: string,
email: string,
invitationId: string,
- password: string
+ password: string,
+ passwordWorkLease?: PasswordWorkLease
) {
const trimmedUserId = userId.trim()
const normalizedEmail = email.trim().toLowerCase()
@@ -1149,74 +1219,91 @@ export async function setInvitationPasswordForUser(
}
const candidate = await getInvitationRecordById(pool, trimmedInvitationId)
- if (candidate?.purpose === 'password_reset') {
- if (candidate.email.toLowerCase() !== normalizedEmail) {
- return { error: 'forbidden' as const }
- }
+ if (!candidate) return { error: 'not_found' as const }
+ if (candidate.email.toLowerCase() !== normalizedEmail) {
+ return { error: 'forbidden' as const }
+ }
+ if (candidate.purpose === 'password_reset') {
const result = await setInvitationPasswordForEmail(
normalizedEmail,
trimmedInvitationId,
trimmedPassword,
- trimmedUserId
+ trimmedUserId,
+ passwordWorkLease
)
if ('error' in result) return result
const { sessionContext: _sessionContext, ...data } = result.data
return { data }
}
+ if (candidate.purpose !== 'member_invitation' && candidate.purpose !== 'admin_desktop_access') {
+ return { error: 'not_found' as const }
+ }
+ if (candidate.status !== 'accepted') return { error: 'not_accepted' as const }
- return withTransaction(async db => {
- const invitation = await getInvitationRecordById(db, trimmedInvitationId)
- if (!invitation) return { error: 'not_found' as const }
- if (invitation.email.toLowerCase() !== normalizedEmail) {
- return { error: 'forbidden' as const }
- }
- if (
- invitation.purpose !== 'member_invitation' &&
- invitation.purpose !== 'admin_desktop_access'
- ) {
- return { error: 'not_found' as const }
- }
- if (invitation.status !== 'accepted') return { error: 'not_accepted' as const }
- const user = await ensureInvitationUser(db, invitation.email, invitation.invitee_name)
- if (invitationUserIsRetired(user)) return { error: 'user_retired' as const }
- if (user.id !== trimmedUserId) return { error: 'forbidden' as const }
- const passwordHash = await bcrypt.hash(trimmedPassword, 12)
- const update = await db.query(
- `UPDATE users
+ const lease = passwordWorkLease ?? (await acquirePasswordSetupLease())
+ try {
+ return await withPasswordWorkTransaction(lease, async db => {
+ const invitation = await getInvitationRecordById(db, trimmedInvitationId)
+ if (!invitation) return { error: 'not_found' as const }
+ if (invitation.email.toLowerCase() !== normalizedEmail) {
+ return { error: 'forbidden' as const }
+ }
+ if (
+ invitation.purpose !== 'member_invitation' &&
+ invitation.purpose !== 'admin_desktop_access'
+ ) {
+ return { error: 'not_found' as const }
+ }
+ if (invitation.status !== 'accepted') return { error: 'not_accepted' as const }
+ const user = await ensureInvitationUser(db, invitation.email, invitation.invitee_name)
+ if (invitationUserIsRetired(user)) return { error: 'user_retired' as const }
+ if (user.id !== trimmedUserId) return { error: 'forbidden' as const }
+ const passwordHash = await bcrypt.hash(trimmedPassword, 12)
+ const update = await db.query(
+ `UPDATE users
SET password_hash = $2,
password_set_at = NOW(),
updated_at = NOW()
WHERE id = $1
AND lifecycle_state = 'active'
RETURNING id`,
- [user.id, passwordHash]
- )
- if (update.rows.length !== 1) return { error: 'user_retired' as const }
+ [user.id, passwordHash]
+ )
+ if (update.rows.length !== 1) return { error: 'user_retired' as const }
- if (!invitation.accepted_user_id) {
- await db.query(`UPDATE invitations SET accepted_user_id = $2 WHERE id = $1`, [
- invitation.id,
- user.id,
- ])
- }
+ if (!invitation.accepted_user_id) {
+ await db.query(`UPDATE invitations SET accepted_user_id = $2 WHERE id = $1`, [
+ invitation.id,
+ user.id,
+ ])
+ }
- const refreshed = await getInvitationRecordById(db, invitation.id)
- if (!refreshed) return { error: 'not_found' as const }
- const teams = await loadInvitationTeams(db, refreshed.id, refreshed)
- return {
- data: {
- passwordUpdated: true,
- ...invitationResponse(refreshed, { ...user, password_hash: passwordHash }, teams),
- },
- }
- })
+ const refreshed = await getInvitationRecordById(db, invitation.id)
+ if (!refreshed) return { error: 'not_found' as const }
+ const teams = await loadInvitationTeams(db, refreshed.id, refreshed)
+ return {
+ data: {
+ passwordUpdated: true,
+ ...invitationResponse(refreshed, { ...user, password_hash: passwordHash }, teams),
+ },
+ }
+ })
+ } catch (error) {
+ if (error instanceof PasswordAdmissionError) throw error
+ passwordSetupLogger.warn(
+ { event: 'password_authority_unavailable' },
+ 'password authority unavailable'
+ )
+ throw new PasswordAdmissionError(503, 2, 'authority_failure')
+ }
}
export async function setInvitationPasswordForEmail(
email: string,
invitationId: string,
password: string,
- expectedUserId?: string
+ expectedUserId?: string,
+ passwordWorkLease?: PasswordWorkLease
) {
const normalizedEmail = email.trim().toLowerCase()
const trimmedInvitationId = invitationId.trim()
@@ -1228,11 +1315,9 @@ export async function setInvitationPasswordForEmail(
return { error: 'invalid_password' as const }
}
- const lease = await acquirePasswordSetupLease()
- let releaseStaged = false
- let releasedWithCommit = false
+ const lease = passwordWorkLease ?? (await acquirePasswordSetupLease())
try {
- const result = await withTransaction(async db => {
+ return await withPasswordWorkTransaction(lease, async db => {
const candidate = await getInvitationRecordById(db, trimmedInvitationId)
if (!candidate || candidate.purpose !== 'password_reset') {
return { error: 'not_found' as const }
@@ -1289,8 +1374,6 @@ export async function setInvitationPasswordForEmail(
if (!refreshed) return { error: 'not_found' as const }
const teams = await loadInvitationTeams(db, refreshed.id, refreshed)
const sessionContext = await getMemberSessionContext(db, user)
- await lease.releaseWithin(db)
- releaseStaged = true
return {
data: {
passwordUpdated: true,
@@ -1299,8 +1382,6 @@ export async function setInvitationPasswordForEmail(
},
}
})
- if (releaseStaged) releasedWithCommit = true
- return result
} catch (error) {
if (error instanceof PasswordAdmissionError) throw error
passwordSetupLogger.warn(
@@ -1308,18 +1389,6 @@ export async function setInvitationPasswordForEmail(
'password authority unavailable'
)
throw new PasswordAdmissionError(503, 2, 'authority_failure')
- } finally {
- if (!releasedWithCommit) {
- try {
- await lease.release()
- } catch {
- passwordSetupLogger.warn(
- { event: 'password_authority_unavailable' },
- 'password authority unavailable'
- )
- throw new PasswordAdmissionError(503, 2, 'authority_failure')
- }
- }
}
}
@@ -2031,45 +2100,57 @@ export async function updateUserPassword(
return { error: 'invalid_password' as const }
}
- const result = await pool.query(
- `SELECT password_hash, password_auth_generation
- FROM users
- WHERE id = $1
- AND email = $2
- LIMIT 1`,
- [normalizedUserId, normalizedEmail]
- )
- const user =
- (result.rows[0] as
- | { password_hash: string | null; password_auth_generation: string | number }
- | undefined) || null
- if (!user) {
- return { error: 'not_found' as const }
- }
- if (!user.password_hash) {
- return { error: 'password_not_set' as const }
- }
- const currentOk = await bcrypt.compare(currentPassword, user.password_hash)
- if (!currentOk) {
- return { error: 'invalid_current_password' as const }
- }
+ const lease = await acquirePasswordSetupLease()
+ try {
+ return await withPasswordWorkTransaction(lease, async db => {
+ const result = await db.query(
+ `SELECT password_hash, password_auth_generation
+ FROM users
+ WHERE id = $1
+ AND email = $2
+ LIMIT 1`,
+ [normalizedUserId, normalizedEmail]
+ )
+ const user =
+ (result.rows[0] as
+ | { password_hash: string | null; password_auth_generation: string | number }
+ | undefined) || null
+ if (!user) {
+ return { error: 'not_found' as const }
+ }
+ if (!user.password_hash) {
+ return { error: 'password_not_set' as const }
+ }
+ const currentOk = await bcrypt.compare(currentPassword, user.password_hash)
+ if (!currentOk) {
+ return { error: 'invalid_current_password' as const }
+ }
- const update = await pool.query(
- `UPDATE users
- SET password_hash = $3,
- password_set_at = NOW(),
- updated_at = NOW()
- WHERE id = $1
- AND password_auth_generation = $2
- AND password_hash = $4
- RETURNING id`,
- [
- normalizedUserId,
- user.password_auth_generation,
- await bcrypt.hash(nextPassword, 12),
- user.password_hash,
- ]
- )
- if (update.rows.length !== 1) return { error: 'credential_changed' as const }
- return { updated: true as const }
+ const update = await db.query(
+ `UPDATE users
+ SET password_hash = $3,
+ password_set_at = NOW(),
+ updated_at = NOW()
+ WHERE id = $1
+ AND password_auth_generation = $2
+ AND password_hash = $4
+ RETURNING id`,
+ [
+ normalizedUserId,
+ user.password_auth_generation,
+ await bcrypt.hash(nextPassword, 12),
+ user.password_hash,
+ ]
+ )
+ if (update.rows.length !== 1) return { error: 'credential_changed' as const }
+ return { updated: true as const }
+ })
+ } catch (error) {
+ if (error instanceof PasswordAdmissionError) throw error
+ passwordSetupLogger.warn(
+ { event: 'password_authority_unavailable' },
+ 'password authority unavailable'
+ )
+ throw new PasswordAdmissionError(503, 2, 'authority_failure')
+ }
}
diff --git a/control-api/test/bug192.passwordLogin.realPostgres.integration.test.ts b/control-api/test/bug192.passwordLogin.realPostgres.integration.test.ts
index 75c66e4a6..0daadf25b 100644
--- a/control-api/test/bug192.passwordLogin.realPostgres.integration.test.ts
+++ b/control-api/test/bug192.passwordLogin.realPostgres.integration.test.ts
@@ -11,7 +11,11 @@ import { createExternalInvitationsRouter } from '../src/routes/external/invitati
import { passwordIdentifierKey } from '../src/services/auth/passwordAdmissionState.js'
import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'
import { verifyUserPassword } from '../src/services/directory/login.js'
-import { createInvitationForTeams } from '../src/services/directory/membership.js'
+import {
+ createInvitationForTeams,
+ setInvitationPasswordForEmail,
+ updateUserPassword,
+} from '../src/services/directory/membership.js'
import { verifyExternalSessionToken } from '../src/utils/auth/externalSessionAuthToken.js'
import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
@@ -42,8 +46,11 @@ vi.mock('../src/db.js', async importOriginal => {
...real,
pool: proxy,
rateLimitPool: proxy,
- withTransaction: (fn: Parameters<typeof real.withTransaction>[0]) =>
- real.withTransaction(fn, holder.pool),
+ withTransaction: (
+ fn: Parameters<typeof real.withTransaction>[0],
+ _txPool?: Parameters<typeof real.withTransaction>[1],
+ options?: Parameters<typeof real.withTransaction>[2]
+ ) => real.withTransaction(fn, holder.pool, options),
}
})
vi.mock('../src/services/rateLimiterService.js', async importOriginal => {
@@ -59,6 +66,7 @@ vi.mock('../src/services/rateLimiterService.js', async importOriginal => {
const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL
const realPg = adminUrl ? describe : describe.skip
const password = 'Synthetic-BUG192-correct-password'
+const realBcryptCompare = bcrypt.compare.bind(bcrypt)
realPg('BUG-192 observable password-login regressions on real PostgreSQL', () => {
const database = `bug192_${randomBytes(6).toString('hex')}`
let admin: Pool
@@ -395,6 +403,172 @@ realPg('BUG-192 observable password-login regressions on real PostgreSQL', () =>
await lease?.release()
}
})
+ it('returns acknowledged password recovery after a post-commit client cleanup failure', async () => {
+ const invitation = await createInvitationForTeams({
+ inviteeName: 'Synthetic member',
+ email: '[email protected]',
+ purpose: 'password_reset',
+ teamAssignments: [],
+ fallbackRole: 'member',
+ })
+ const sibling = await createInvitationForTeams({
+ inviteeName: 'Synthetic member',
+ email: '[email protected]',
+ purpose: 'password_reset',
+ teamAssignments: [],
+ fallbackRole: 'member',
+ })
+ const connect = holder.pool.connect.bind(holder.pool)
+ const connectSpy = vi.spyOn(holder.pool, 'connect')
+ let recoveryUpdateCommitted = false
+ let cleanupFailed = false
+ connectSpy.mockImplementation((async () => {
+ const client = await connect()
+ const query = client.query.bind(client)
+ const release = client.release.bind(client)
+ let recoveryCredentialWritten = false
+ return new Proxy(client, {
+ get(target, property) {
+ if (property === 'query') {
+ return async (...args: unknown[]) => {
+ const statement =
+ typeof args[0] === 'string'
+ ? args[0]
+ : String((args[0] as { text?: string } | undefined)?.text || '')
+ const result = await (query as (...queryArgs: unknown[]) => Promise<unknown>)(...args)
+ if (/UPDATE users\s+SET password_hash/is.test(statement))
+ recoveryCredentialWritten = true
+ if (/^COMMIT\s*$/i.test(statement) && recoveryCredentialWritten)
+ recoveryUpdateCommitted = true
+ return result
+ }
+ }
+ if (property === 'release') {
+ return (error?: Error | boolean) => {
+ if (recoveryUpdateCommitted && !cleanupFailed) {
+ cleanupFailed = true
+ release(new Error('synthetic post-commit cleanup failure'))
+ throw new Error('synthetic post-commit cleanup failure')
+ }
+ return release(error)
+ }
+ }
+ const value = Reflect.get(target, property, target)
+ return typeof value === 'function' ? value.bind(target) : value
+ },
+ })
+ }) as typeof holder.pool.connect)
+
+ let result: Awaited<ReturnType<typeof setInvitationPasswordForEmail>> | undefined
+ try {
+ result = await setInvitationPasswordForEmail(
+ invitation.id,
+ 'Synthetic-BUG192-recovered-after-commit'
+ )
+ } finally {
+ connectSpy.mockRestore()
+ }
+ expect(cleanupFailed).toBe(true)
+ expect(recoveryUpdateCommitted).toBe(true)
+ expect(result).toBeDefined()
+ expect(result).toMatchObject({
+ data: { passwordUpdated: true, sessionContext: { userId } },
+ })
+ expect(
+ (
+ await holder.pool.query('SELECT id, status FROM invitations WHERE id = ANY($1::uuid[])', [
+ [invitation.id, sibling.id],
+ ])
+ ).rows
+ ).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ id: invitation.id, status: 'accepted' }),
+ expect.objectContaining({ id: sibling.id, status: 'revoked' }),
+ ])
+ )
+ expect(
+ (await holder.pool.query('SELECT count(*)::text AS count FROM password_verification_work'))
+ .rows[0].count
+ ).toBe('0')
+ await openPace()
+ const loginResponse = await login(
+ 'Synthetic-BUG192-recovered-after-commit',
+ '192.0.2.111'
+ )
+ expect(loginResponse.status).toBe(200)
+ }, 30_000)
+ it('keeps authenticated password change inside the single durable bcrypt owner', async () => {
+ let releaseCompare!: () => void
+ let compareStarted!: () => void
+ const compareGate = new Promise<void>(resolve => {
+ releaseCompare = resolve
+ })
+ const compareEntered = new Promise<void>(resolve => {
+ compareStarted = resolve
+ })
+ let releaseHash!: () => void
+ let hashStarted!: () => void
+ const hashGate = new Promise<void>(resolve => {
+ releaseHash = resolve
+ })
+ const hashEntered = new Promise<void>(resolve => {
+ hashStarted = resolve
+ })
+ const realPasswordHash = bcrypt.hash.bind(bcrypt)
+ let compareCalls = 0
+ let hashCalls = 0
+ compare.mockImplementation(async (submitted, stored) => {
+ const matched = await realBcryptCompare(submitted, stored)
+ if (compareCalls++ === 0) {
+ compareStarted()
+ await compareGate
+ }
+ return matched
+ })
+ const hash = vi.spyOn(bcrypt, 'hash').mockImplementation(async (submitted, rounds) => {
+ const value = await realPasswordHash(submitted, rounds)
+ if (hashCalls++ === 0) {
+ hashStarted()
+ await hashGate
+ }
+ return value
+ })
+
+ const update = updateUserPassword(
+ userId,
+ password,
+ 'Synthetic-BUG192-password-changed'
+ )
+ let comparePhaseLoginStatus = 0
+ let hashPhaseLoginStatus = 0
+ try {
+ await compareEntered
+ await openPace()
+ comparePhaseLoginStatus = (await login('[email protected]', password, '192.0.2.112'))
+ .status
+ releaseCompare()
+ await hashEntered
+ await openPace()
+ hashPhaseLoginStatus = (await login('[email protected]', password, '192.0.2.113')).status
+ } finally {
+ releaseCompare()
+ releaseHash()
+ }
+
+ expect(await update).toEqual({ updated: true })
+ expect(comparePhaseLoginStatus).toBe(429)
+ expect(hashPhaseLoginStatus).toBe(429)
+ expect(compare).toHaveBeenCalledTimes(1)
+ expect(hash).toHaveBeenCalledTimes(1)
+ await openPace()
+ expect(
+ (await login('[email protected]', 'Synthetic-BUG192-password-changed', '192.0.2.114'))
+ .status
+ ).toBe(200)
+ }, 30_000)
it('keeps real and shadow cooldown status/body/retry metadata identical', async () => {
// Backdate admitted attempts and completed failures using the DB clock,
// then retain the real admission producer for the fifth transition.
diff --git a/control-api/test/bug192.passwordPersistence.realPostgres.integration.test.ts b/control-api/test/bug192.passwordPersistence.realPostgres.integration.test.ts
index a3ec96639..360f52f76 100644
--- a/control-api/test/bug192.passwordPersistence.realPostgres.integration.test.ts
+++ b/control-api/test/bug192.passwordPersistence.realPostgres.integration.test.ts
@@ -25,6 +25,7 @@ import {
setInvitationPasswordForEmail,
setInvitationPasswordForUser,
updateUserPassword,
+ withMemberPasswordWorkLease,
} from '../src/services/directory/membership.js'
import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'
@@ -46,8 +47,11 @@ vi.mock('../src/db.js', async importOriginal => {
...real,
pool: proxy,
rateLimitPool: proxy,
- withTransaction: (fn: Parameters<typeof real.withTransaction>[0]) =>
- real.withTransaction(fn, holder.pool),
+ withTransaction: (
+ fn: Parameters<typeof real.withTransaction>[0],
+ _txPool?: Parameters<typeof real.withTransaction>[1],
+ options?: Parameters<typeof real.withTransaction>[2]
+ ) => real.withTransaction(fn, holder.pool, options),
}
})
vi.mock('../src/services/rateLimiterService.js', async importOriginal => {
@@ -239,7 +243,7 @@ realPg('Spec 043 persistence, migration, atomicity and fencing design acceptance
expect(await completePasswordEvaluation(success, true)).toBe(false)
expect((await row()).failures).toEqual([])
})
- it('prevents a stale authenticated password change from overwriting verified recovery', async () => {
+ it('serializes authenticated password change and recovery through one durable owner', async () => {
const invitation = await createSilentInvitationForTeams({
inviteeName: 'Synthetic member',
email,
@@ -273,13 +277,18 @@ realPg('Spec 043 persistence, migration, atomicity and fencing design acceptance
password,
'Synthetic-BUG192-stale-password-change'
)
- await compareEntered
const recoveryPassword = 'Synthetic-BUG192-recovered-password'
+ try {
+ await compareEntered
+ await expect(
+ setInvitationPasswordForEmail(email, invitation.id, recoveryPassword)
+ ).rejects.toMatchObject({ status: 429, reason: 'verification_busy' })
+ } finally {
+ releaseCompare()
+ }
+ await expect(stalePasswordChange).resolves.toEqual({ updated: true })
const recovery = await setInvitationPasswordForEmail(email, invitation.id, recoveryPassword)
expect(recovery).not.toHaveProperty('error')
-
- releaseCompare()
- await expect(stalePasswordChange).resolves.toEqual({ error: 'credential_changed' })
await expect(
verifyMemberPassword(email, recoveryPassword, { publicLogin: false, userId })
).resolves.toMatchObject({ id: userId })
@@ -357,6 +366,70 @@ realPg('Spec 043 persistence, migration, atomicity and fencing design acceptance
await endPoolAndWaitForClients(singleConnectionPool)
}
})
+ it('does not hash accepted invitation passwords outside the durable owner', async () => {
+ const invitation = await createSilentInvitationForTeams({
+ inviteeName: 'Synthetic member',
+ email,
+ purpose: 'member_invitation',
+ teamAssignments: [],
+ fallbackRole: 'member',
+ })
+ const accepted = await acceptInvitationForEmail(email, invitation.token, invitation.id)
+ expect(accepted).toMatchObject({ data: { accepted: true, userId } })
+
+ const lease = await acquirePasswordWork()
+ expect(lease).not.toBeNull()
+ const hashSpy = vi.spyOn(bcrypt, 'hash')
+ try {
+ await expect(
+ setInvitationPasswordForUser(
+ userId,
+ email,
+ invitation.id,
+ 'Synthetic-BUG192-invitation-password'
+ )
+ ).rejects.toMatchObject({ status: 429, reason: 'verification_busy' })
+ expect(hashSpy).not.toHaveBeenCalled()
+ const current = (
+ await holder.pool.query('SELECT password_hash FROM users WHERE id = $1', [userId])
+ ).rows[0].password_hash
+ expect(current).toBe(hash)
+ } finally {
+ hashSpy.mockRestore()
+ await lease?.release()
+ }
+ })
+ it('reuses a reserved owner to complete accepted invitation password setup', async () => {
+ const invitation = await createSilentInvitationForTeams({
+ inviteeName: 'Synthetic member',
+ email,
+ purpose: 'member_invitation',
+ teamAssignments: [],
+ fallbackRole: 'member',
+ })
+ const accepted = await acceptInvitationForEmail(email, invitation.token, invitation.id)
+ expect(accepted).toMatchObject({ data: { accepted: true, userId } })
+
+ const result = await withMemberPasswordWorkLease(lease =>
+ setInvitationPasswordForUser(
+ userId,
+ email,
+ invitation.id,
+ 'Synthetic-BUG192-reserved-invitation-password',
+ lease
+ )
+ )
+
+ expect(result).not.toHaveProperty('error')
+ expect(
+ (await holder.pool.query('SELECT password_hash FROM users WHERE id = $1', [userId])).rows[0]
+ .password_hash
+ ).not.toBe(hash)
+ expect(
+ (await holder.pool.query('SELECT count(*)::text AS count FROM password_verification_work'))
+ .rows[0].count
+ ).toBe('0')
+ })
it('does not accept a pending password-reset link through generic invitation acceptance', async () => {
const invitation = await createSilentInvitationForTeams({
inviteeName: 'Synthetic member',
diff --git a/control-api/test/bug192.passwordRecovery.memberRegistration.realPostgres.integration.test.ts b/control-api/test/bug192.passwordRecovery.memberRegistration.realPostgres.integration.test.ts
index a85303373..78f6416ba 100644
--- a/control-api/test/bug192.passwordRecovery.memberRegistration.realPostgres.integration.test.ts
+++ b/control-api/test/bug192.passwordRecovery.memberRegistration.realPostgres.integration.test.ts
@@ -26,8 +26,11 @@ vi.mock('../src/db.js', async importOriginal => {
...real,
pool: proxy,
rateLimitPool: proxy,
- withTransaction: (fn: Parameters<typeof real.withTransaction>[0]) =>
- real.withTransaction(fn, holder.pool),
+ withTransaction: (
+ fn: Parameters<typeof real.withTransaction>[0],
+ _txPool?: Parameters<typeof real.withTransaction>[1],
+ options?: Parameters<typeof real.withTransaction>[2]
+ ) => real.withTransaction(fn, holder.pool, options),
}
})
diff --git a/control-api/test/routes.adminAuth.test.ts b/control-api/test/routes.adminAuth.test.ts
index 56799bd49..cc7de0e6d 100644
--- a/control-api/test/routes.adminAuth.test.ts
+++ b/control-api/test/routes.adminAuth.test.ts
@@ -4,12 +4,14 @@ import request from 'supertest'
import { config } from '../src/config.js'
import { rootLogger } from '../src/observability/logger.js'
import { createAdminAuthRouter } from '../src/routes/admin/auth.js'
+import { PasswordAdmissionError } from '../src/services/auth/passwordCredentialVerification.js'
const directorySvc = vi.hoisted(() => ({
provisionAdminDesktopWorkspace: vi.fn(),
acceptInvitationById: vi.fn(),
getPendingMemberInvitationForEmail: vi.fn(),
setInvitationPasswordForUser: vi.fn(),
+ withMemberPasswordWorkLease: vi.fn(),
}))
// auth.ts imports provisionAdminDesktopWorkspace from the directory barrel; mock the barrel here.
vi.mock('../src/services/directory/index.js', () => directorySvc)
@@ -53,6 +55,7 @@ const uiAuth = vi.hoisted(() => ({
}))
const rateLimit = vi.hoisted(() => ({
+ clearRateLimitHeaders: vi.fn(),
rateLimitMiddleware: vi.fn(() => (_req: any, _res: any, next: any) => next()),
}))
@@ -76,6 +79,14 @@ describe('routes/adminAuth', () => {
Object.values(controlAdminInvitationRegistrationSvc).forEach(fn => fn.mockReset())
Object.values(uiAuth).forEach(fn => fn.mockReset())
Object.values(rateLimit).forEach(fn => fn.mockClear())
+ directorySvc.withMemberPasswordWorkLease.mockImplementation(
+ async (work: (lease: unknown) => Promise<unknown>) =>
+ work({
+ release: async () => undefined,
+ releaseWithin: async () => undefined,
+ confirmReleaseWithinCommitted: () => undefined,
+ })
+ )
adminSvc.isValidAdminEmail.mockReturnValue(true)
adminSvc.isValidAdminUsername.mockReturnValue(true)
config.desktopGfsOperatorLinkingEnabled = false
@@ -561,4 +572,32 @@ describe('routes/adminAuth', () => {
expect(JSON.stringify(payload)).not.toContain('[email protected]')
errorLog.mockRestore()
})
+
+ it('denies bundled desktop completion before consuming the admin invitation when password work is busy', async () => {
+ controlAdminInvitationRegistrationSvc.validateControlAdminInvitationToken.mockResolvedValue({
+ email: '[email protected]',
+ invitationUuid: '11111111-1111-4111-8111-111111111111',
+ })
+ directorySvc.getPendingMemberInvitationForEmail.mockResolvedValue({ id: 'desk-invite' })
+ directorySvc.withMemberPasswordWorkLease.mockRejectedValue(
+ new PasswordAdmissionError(429, 8, 'verification_busy')
+ )
+
+ const app = express()
+ app.use(express.json())
+ app.use(createAdminAuthRouter())
+
+ const res = await request(app).post('/admin/auth/control-admin-invitations/complete').send({
+ token: 'example.jwt.value',
+ email: '[email protected]',
+ username: 'new-admin',
+ password: 'example-password',
+ })
+
+ expect(res.status).toBe(429)
+ expect(res.headers['retry-after']).toBe('8')
+ expect(res.body).toEqual({ error: 'rate_limited', retryAfterSeconds: 8 })
+ expect(adminSvc.completeControlAdminInvitation).not.toHaveBeenCalled()
+ expect(directorySvc.acceptInvitationById).not.toHaveBeenCalled()
+ })
})
diff --git a/docs/operations/password-admission.md b/docs/operations/password-admission.md
index 6638e5808..549fe4b3b 100644
--- a/docs/operations/password-admission.md
+++ b/docs/operations/password-admission.md
@@ -11,14 +11,16 @@ Anonymous password login retains the Spec 043 controls:
with no queue and no pace refund.
These anonymous-login limits protect account-wide guessing and pace public
-cost-12 password verification. The durable work owner also keeps verification and
-recovery hashing at global concurrency one. Recovery hashes bypass the anonymous
-1-per-7.5-second start pace, so that rate limit does not cover aggregate bcrypt
-starts across both lanes. Distributed sources can consume the public pace while
-staying below their individual source limits. Ordinary anonymous logins can
-consequently receive HTTP 429 during sustained pressure. This availability tradeoff
-is retained; the global starvation mechanism has not been removed, and monitoring
-alone does not mitigate it.
+cost-12 password verification. The durable work owner serializes all member-account
+bcrypt work: password verification, recovery hashing, accepted-invitation password
+setup, and authenticated password changes. Recovery and password-setting work bypass
+the anonymous 1-per-7.5-second start pace, so that rate limit does not cover aggregate
+bcrypt starts across these member-account flows. Control Admin credentials use a
+separate authentication subsystem outside this policy. Distributed sources can
+consume the public pace while staying below their individual source limits. Ordinary
+anonymous logins can consequently receive HTTP 429 during sustained pressure. This
+availability tradeoff is retained; the global starvation mechanism has not been
+removed, and monitoring alone does not mitigate it.
## Verified recovery
@@ -30,10 +32,11 @@ Only after that commit does Control API issue the normal signed session, which
External REST stores in the profile session cookie.
Recovery hashes use the existing durable `password_verification_work` singleton.
-This keeps bcrypt concurrency at one across service processes and fails closed when
-that owner is busy or database authority is unavailable. There is no recovery queue
-and no second bcrypt lane. A concurrent request may receive 429 while the owner is
-held; retry the verified recovery request after its sanitized `Retry-After` interval.
+Authenticated password changes and accepted-invitation password setup use that same
+owner. This keeps member-account bcrypt concurrency at one across service processes
+and fails closed when the owner is busy or database authority is unavailable. There
+is no recovery queue or second concurrent member-account bcrypt lane. A request may
+receive 429 while the owner is held; retry after its sanitized `Retry-After` interval.
Password reset keeps the public identifier attempt timestamps. The existing
credential-generation fence clears failure cooldown state, while any remaining