A locked staff account answers its login like a wrong password (accounts.lockedAnswer)

Payload checks the lock before the password, so after five wrong passwords
a staff address answered "This user is locked…" to any password while an
address with no account always answered "The email or password provided is
incorrect.": anybody could find out which addresses have a staff account
(IAWL idea 6, owner's Q1). A config-level afterError hook now answers
LockedAuth on a staff collection's login exactly like AuthenticationError:
REST status and body, GraphQL message and extensions.name. The lock is
unchanged; visitor collections keep Payload's answer.
accounts: { lockedAnswer: 'payload' } keeps Payload's message; any other
value stops the start. Building blocks lockedLooksWrong, withLockedAnswer,
graphqlLoginName.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 10:39:40 +01:00
co-authored by Claude Opus 5.5
parent fc70a34f3e
commit 15b5c6258d
14 changed files with 262 additions and 2 deletions
+35
View File
@@ -0,0 +1,35 @@
/** A locked staff account answers like a wrong password, REST and GraphQL (lockedAnswerSuite.ts). 2FA on. */
import { describe, expect, test, vi } from 'vitest'
import { lockedAnswerSite, PASSWORD } from './lockedAnswerSuite.js'
vi.mock('server-only', () => ({}))
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
const { gqlLogin, lock, restLogin, state } = lockedAnswerSite()
describe('accounts.lockedAnswer (default): a locked staff account looks like a wrong password', () => {
test('REST: the locked owner, with any password, gets exactly what an invented address gets', async () => {
await lock('users', '[email protected]')
const invented = await restLogin('users', '[email protected]', 'a-wrong-password')
expect(invented.status).toBe(401)
expect(await restLogin('users', '[email protected]', 'a-wrong-password')).toEqual(invented)
expect(await restLogin('users', '[email protected]', PASSWORD)).toEqual(invented)
expect(invented.body).not.toMatch(/locked/i)
// the lock itself is still there
const stored = (await state.payload.find({ collection: 'users' as never, overrideAccess: true, showHiddenFields: true, where: { email: { equals: '[email protected]' } } })).docs[0] as { lockUntil?: string }
expect(new Date(String(stored.lockUntil)).getTime()).toBeGreaterThan(Date.now())
})
test('GraphQL: the same message, status and error name', async () => {
const invented = await gqlLogin('loginUser', '[email protected]', 'a-wrong-password')
expect(invented.name).toBe('AuthenticationError')
expect(await gqlLogin('loginUser', '[email protected]', PASSWORD)).toEqual(invented)
})
test('a visitor collection keeps Payload\'s answer (not staff)', async () => {
await lock('customers', '[email protected]', 3)
expect((await restLogin('customers', '[email protected]', PASSWORD)).body).toMatch(/locked/i)
expect((await gqlLogin('loginCustomer', '[email protected]', PASSWORD)).name).toBe('LockedAuth')
})
})
+16
View File
@@ -0,0 +1,16 @@
/** A locked staff account answers like a wrong password with 2FA off too (lockedAnswerSuite.ts). */
import { describe, expect, test, vi } from 'vitest'
import { lockedAnswerSite, PASSWORD } from './lockedAnswerSuite.js'
vi.mock('server-only', () => ({}))
const { gqlLogin, lock, restLogin } = lockedAnswerSite({ twoFactor: false })
describe('accounts.lockedAnswer with twoFactor: false', () => {
test('REST and GraphQL answer the locked owner like an invented address', async () => {
await lock('users', '[email protected]')
expect(await restLogin('users', '[email protected]', PASSWORD)).toEqual(await restLogin('users', '[email protected]', 'x-wrong-password-1'))
expect(await gqlLogin('loginUser', '[email protected]', PASSWORD)).toEqual(await gqlLogin('loginUser', '[email protected]', 'x-wrong-password-1'))
})
})
+23
View File
@@ -0,0 +1,23 @@
/** accounts.lockedAnswer: 'payload' keeps Payload's "locked" answer (lockedAnswerSuite.ts). */
import { describe, expect, test, vi } from 'vitest'
import { iapl } from '../src/plugin.js'
import { lockedAnswerSite, PASSWORD } from './lockedAnswerSuite.js'
vi.mock('server-only', () => ({}))
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
const { gqlLogin, lock, restLogin } = lockedAnswerSite({ lockedAnswer: 'payload' })
describe("accounts.lockedAnswer: 'payload'", () => {
test('the locked owner is told it is locked, REST and GraphQL', async () => {
await lock('users', '[email protected]')
expect((await restLogin('users', '[email protected]', PASSWORD)).body).toMatch(/locked/i)
expect((await gqlLogin('loginUser', '[email protected]', PASSWORD)).name).toBe('LockedAuth')
})
test('a value nobody understands stops the start', async () => {
const config = { collections: [], secret: 'x' } as never
await expect((async () => iapl({ accounts: { lockedAnswer: 'Payload' as never }, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] } } as never)(config))()).rejects.toThrow(/accounts\.lockedAnswer must be one of/)
})
})
+79
View File
@@ -0,0 +1,79 @@
/**
* A locked staff account answers its login exactly like a wrong password (accounts.lockedAnswer, 1.17.2; IAWL
* idea 6). Shared by lockedAnswer.test.ts (default, 2FA on), lockedAnswer2fa-off.test.ts (2FA off) and
* lockedAnswerPayload.test.ts (`lockedAnswer: 'payload'`): one Payload per file.
* Database: SQLite in memory, or IPAL_TEST_POSTGRES_URI / IPAL_TEST_MONGO_URI (test/http/db.ts).
*/
import type { Payload } from 'payload'
import { payloadTotp } from '@clocklimited/payload-2fa'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import { getPayload } from 'payload'
import { afterAll, beforeAll } from 'vitest'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
import { testDatabase } from './http/db.js'
import { startPayloadServer, type TestServer } from './http/server.js'
export const PASSWORD = 'a-long-test-password-1'
export type Answer = { body: string; status: number }
export function lockedAnswerSite({ lockedAnswer, twoFactor = true }: { lockedAnswer?: 'payload'; twoFactor?: boolean } = {}) {
const state = {} as { payload: Payload; server: TestServer }
beforeAll(async () => {
const config = buildTestConfig({
admin: { user: 'users' },
collections: [
// a visitor collection with Payload's own lock: not staff, not changed
{ slug: 'customers', access: { create: () => true }, auth: { maxLoginAttempts: 3 }, fields: [] },
{ slug: 'media', fields: [], upload: { staticDir: '/tmp/iapl-locked-answer-media' } },
],
db: testDatabase('lockedanswer'),
editor: lexicalEditor(),
plugins: [
iapl({
access: { authCollection: 'users' },
...(lockedAnswer ? { accounts: { lockedAnswer } } : {}),
i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] },
twoFactor: twoFactor ? { issuer: 'Example', plugin: payloadTotp } : false,
}),
],
secret: 'integration-test-secret-0123456789',
telemetry: false,
})
state.payload = await getPayload({ config })
state.server = await startPayloadServer(config)
await state.payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['admin'] } as never })
await state.payload.create({ collection: 'customers' as never, data: { email: '[email protected]', password: PASSWORD } as never })
}, 120_000)
afterAll(async () => {
await state.server?.close()
await state.payload?.destroy?.()
})
const restLogin = async (collection: string, email: string, password: string): Promise<Answer> => {
const res = await fetch(`${state.server.url}/api/${collection}/login`, {
body: JSON.stringify({ email, password }),
headers: { 'content-type': 'application/json' },
method: 'POST',
})
return { body: await res.text(), status: res.status }
}
const gqlLogin = async (mutation: string, email: string, password: string) => {
const res = await fetch(`${state.server.url}/api/graphql`, {
body: JSON.stringify({ query: `mutation { ${mutation}(email: ${JSON.stringify(email)}, password: ${JSON.stringify(password)}) { token } }` }),
headers: { 'content-type': 'application/json' },
method: 'POST',
})
const body = (await res.json()) as { errors?: { extensions?: { name?: string; statusCode?: number }; message: string }[] }
const e = body.errors?.[0]
return { name: e?.extensions?.name, message: e?.message, status: res.status, statusCode: e?.extensions?.statusCode }
}
/** Locks `email` with wrong passwords (Payload's default: 5). */
const lock = async (collection: string, email: string, times = 5) => {
for (let i = 0; i < times; i++) {await restLogin(collection, email, 'a-wrong-password')}
}
return { gqlLogin, lock, restLogin, state }
}