/** Sign-in rules of the staff collections, 2FA on (signInRulesSuite.ts). */ import { describe, expect, test, vi } from 'vitest' import { foldSignInName, passwordMatches } from '../src/modules/access/signInRules.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, gqlLogin, idOf, login, session, state, stored } = signInSite() describe('accounts.signInLimit: a stranger cannot lock a staff account out', () => { test('five wrong passwords from one IP: that IP gets 429 for the address; the owner, elsewhere, signs in', async () => { for (let i = 0; i < 5; i++) {expect((await login('owner@example.com', 'a-wrong-password', '198.51.100.66')).status).toBe(401)} const refused = await login('owner@example.com', PASSWORD, '198.51.100.66') expect(refused.status).toBe(429) expect(refused.body).toMatch(/Too many failed sign-ins/) expect((await login('owner@example.com', PASSWORD, '203.0.113.7')).status).toBe(200) expect((await stored('owner@example.com'))?.lockUntil ?? null).toBeNull() }) test('the answer for "too many" is the same for a real and an invented address', async () => { for (let i = 0; i < 5; i++) {await login('nobody@example.com', 'a-wrong-password', '198.51.100.67')} for (let i = 0; i < 5; i++) {await login('editor2@example.com', 'a-wrong-password', '198.51.100.68')} const invented = await login('nobody@example.com', 'a-wrong-password', '198.51.100.67') const real = await login('editor2@example.com', 'a-wrong-password', '198.51.100.68') expect(invented.status).toBe(429) expect(real).toEqual(invented) }) test('correct sign-ins never count; spellings of one address share one count (case, width)', async () => { for (let i = 0; i < 8; i++) {expect((await login('office@example.com', PASSWORD, '198.51.100.69')).status).toBe(200)} for (const spelling of ['FOLD@example.com', 'fold@example.com', ' fold@EXAMPLE.com', 'fold@example.com', 'Fold@Example.com']) { await login(spelling, 'a-wrong-password', '198.51.100.70') } expect((await login('fold@example.com', PASSWORD, '198.51.100.70')).status).toBe(429) expect(foldSignInName('fold@Example.COM ')).toBe('fold@example.com') }) test('GraphQL counts and answers the same way', async () => { for (let i = 0; i < 5; i++) {await gqlLogin('editor@example.com', 'a-wrong-password', '198.51.100.71')} const refused = await gqlLogin('editor@example.com', PASSWORD, '198.51.100.71') expect(refused.status).toBe(429) expect(refused.message).toMatch(/Too many failed sign-ins/) }) test("Payload's per-account lock stays as the net, at 50 failures for an hour", () => { const auth = state.payload.collections['users' as never].config.auth as { lockTime: number; maxLoginAttempts: number } expect([auth.maxLoginAttempts, auth.lockTime]).toEqual([50, 3_600_000]) const customers = state.payload.collections['customers' as never].config.auth as { maxLoginAttempts: number } expect(customers.maxLoginAttempts).toBe(5) // not staff: Payload's default }) }) describe('accounts.signInSameOrigin: staff sign-in only from the site\'s own origin', () => { test('login, forgot password and reset password from a foreign page: 403, nothing counted or sent', async () => { const before = state.mails.length expect((await login('admin2@example.com', PASSWORD, '198.51.100.80', FOREIGN)).status).toBe(403) expect((await call('/users/forgot-password', { email: 'admin2@example.com' }, { 'sec-fetch-site': 'cross-site' })).status).toBe(403) expect((await call('/users/reset-password', { password: 'a-new-password-123', token: 'x'.repeat(40) }, FOREIGN)).status).toBe(403) expect((await gqlLogin('admin2@example.com', PASSWORD, '198.51.100.80', FOREIGN)).status).toBe(403) expect(state.mails.length).toBe(before) // the site's own page and a script without browser headers go through expect((await login('admin2@example.com', PASSWORD, '198.51.100.80', { origin: state.server.url, 'sec-fetch-site': 'same-origin' })).status).toBe(200) expect((await login('admin2@example.com', PASSWORD, '198.51.100.80')).status).toBe(200) expect((await call('/users/forgot-password', { email: 'admin2@example.com' })).status).toBe(200) }) test('a visitor collection is not staff: its login from another origin is untouched', async () => { expect((await call('/customers/login', { email: 'client@example.com', password: PASSWORD }, FOREIGN)).status).toBe(200) }) }) describe('accounts.confirmOwnChanges: one\'s own e-mail or password needs the current password', () => { test('REST: without it 400, wrong 400, right 200; never read back', async () => { const cookie = await session('editor@example.com', PASSWORD, '203.0.113.30') const id = await idOf('editor@example.com') const patch = (data: object) => call(`/users/${id}`, data, { cookie, 'x-real-ip': '203.0.113.30' }, 'PATCH') const none = await patch({ email: 'editor-new@example.com' }) expect([none.status, none.body]).toEqual([400, expect.stringMatching(/Enter your current password/)]) const wrong = await patch({ currentPassword: 'not-my-password', email: 'editor-new@example.com' }) expect([wrong.status, wrong.body]).toEqual([400, expect.stringMatching(/current password is not right/)]) const right = await patch({ currentPassword: PASSWORD, email: 'editor-new@example.com' }) expect(right.status).toBe(200) expect(right.body).not.toMatch(/a-long-test-password/) expect(await idOf('editor-new@example.com')).toBe(id) // other fields of one's own account need nothing expect((await call(`/users/${id}`, { roles: ['editor'] }, { cookie }, 'PATCH')).status).not.toBe(400) }) test('GraphQL: the same rule; the password too', async () => { const cookie = await session('editor-new@example.com', PASSWORD, '203.0.113.31') const id = await idOf('editor-new@example.com') const gql = async (data: string) => JSON.parse((await call('/graphql', { query: `mutation { updateUser(id: ${JSON.stringify(id)}, data: { ${data} }) { email } }` }, { cookie })).body) as { data?: { updateUser?: unknown }; errors?: { message: string }[] } expect((await gql('password: "a-new-password-456"')).errors?.[0]?.message).toBeTruthy() expect((await gql(`password: "a-new-password-456", currentPassword: "${PASSWORD}"`)).data?.updateUser).toBeTruthy() expect((await login('editor-new@example.com', 'a-new-password-456', '203.0.113.31')).status).toBe(200) }) test('an admin changing ANOTHER account is not asked', async () => { const cookie = await session('owner@example.com', PASSWORD, '203.0.113.32') const id = await idOf('editor2@example.com') expect((await call(`/users/${id}`, { email: 'editor2-renamed@example.com' }, { cookie }, 'PATCH')).status).toBe(200) }) test('wrong current passwords count like failed sign-ins (429 after five)', async () => { const cookie = await session('office@example.com', PASSWORD, '203.0.113.33') const id = await idOf('office@example.com') for (let i = 0; i < 5; i++) { expect((await call(`/users/${id}`, { currentPassword: `wrong-${i}`, password: 'another-password-1' }, { cookie, 'x-real-ip': '203.0.113.33' }, 'PATCH')).status).toBe(400) } expect((await call(`/users/${id}`, { currentPassword: PASSWORD, password: 'another-password-1' }, { cookie, 'x-real-ip': '203.0.113.33' }, 'PATCH')).status).toBe(429) }) test("passwordMatches: Payload's hash formats; an unknown prefix is refused", async () => { const doc = (await state.payload.db.findOne({ collection: 'users', where: { email: { equals: 'owner@example.com' } } })) as unknown as { hash: string; salt: string } expect(await passwordMatches(doc, PASSWORD)).toBe(true) expect(await passwordMatches(doc, 'nope')).toBe(false) expect(await passwordMatches({ hash: `pbkdf2-sha512-v9:${doc.hash.split(':')[1]}`, salt: doc.salt }, PASSWORD)).toBe(false) }) })