Files
ipal-kit/test/signInRulesOff.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 52710021ef Staff sign-in rules: failures per IP and address, own origin, current password for own changes (1.18.0)
Q2 (IAWL idea 16): Payload locks a staff account after 5 wrong passwords
from anybody, so a stranger kept the owner out without a password. Failed
sign-ins are now counted per client IP and typed address (folded: NFKC,
lower case, trim): 5 in 15 min, then 429 for that IP and address only;
only failures count (a place taken before the compare, given back on
success or refusal; "try again in a moment" while others are being
checked). Payload's own lock stays as the net at 50 failures for an hour
where the project set none. The mail service's account-mail limit now
counts per address per visitor (5) with 20 per address in all, so one
stranger no longer blocks the owner's own reset mail.
Q3 (idea 7): staff login, forgot and reset password refuse a request a
browser marks as coming from another origin (403 before anything is
counted or sent); visitor collections untouched.
Q5 (idea 29): an account that changes its own e-mail, username or password
must send its current password (panel field on the account page via the
plugin's MaskedField, no import-map change; REST and GraphQL). Admins
changing others, server code and API keys are not asked; wrong ones count
like failed sign-ins.
Options accounts.signInLimit / signInSameOrigin / confirmOwnChanges, all
validated at start. Tests: 15 HTTP tests (2FA on/off, all off), fail
before on 1.17.1; built-site spec own-account.spec.ts in e2e:built.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-01 11:12:40 +01:00

32 lines
2.0 KiB
TypeScript

/** accounts: { signInLimit: false, signInSameOrigin: false, confirmOwnChanges: false } — Payload's behaviour as before 1.18.0. */
import { describe, expect, test, vi } from 'vitest'
import { iapl } from '../src/plugin.js'
import { FOREIGN, PASSWORD, signInSite } from './signInRulesSuite.js'
vi.mock('server-only', () => ({}))
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
const { call, idOf, login, session, state } = signInSite({ off: true })
describe('the sign-in rules switched off', () => {
test("Payload's lock (5 from anywhere), foreign sign-in taken, own e-mail without a question", async () => {
expect((await login('[email protected]', PASSWORD, '203.0.113.1', FOREIGN)).status).toBe(200)
for (let i = 0; i < 5; i++) {await login('[email protected]', 'a-wrong-password', '198.51.100.95')}
const locked = await login('[email protected]', PASSWORD, '203.0.113.95')
expect(locked.status).toBe(401)
expect((state.payload.collections['users' as never].config.auth as { maxLoginAttempts: number }).maxLoginAttempts).toBe(5)
const cookie = await session('[email protected]', PASSWORD, '203.0.113.96')
expect((await call(`/users/${await idOf('[email protected]')}`, { email: '[email protected]' }, { cookie }, 'PATCH')).status).toBe(200)
})
test('values nobody understands stop the start', async () => {
const i18n = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
const start = (accounts: unknown) => (async () => iapl({ accounts, i18n } as never)({ collections: [], secret: 'x' } as never))()
await expect(start({ signInLimit: 0 })).rejects.toThrow(/accounts\.signInLimit must be/)
await expect(start({ signInLimit: { perAddress: 0 } })).rejects.toThrow(/perAddress must be/)
await expect(start({ signInSameOrigin: 'false' })).rejects.toThrow(/signInSameOrigin must be/)
await expect(start({ confirmOwnChanges: 'no' })).rejects.toThrow(/confirmOwnChanges must be/)
})
})