Files
ipal-kit/test/securityHttpBeta1.test.ts
T
Radosław SmolińskiandClaude Fable 5.2 8fa71238ce M1 (1): stored secrets are write-only
smtpPassword and turnstileSecretKey in the Site integrations global are returned
as '********' to every read that goes through access control (REST, GraphQL, the
panel's form state); the Local API with access control off still gets the value.
A save that sends nothing, an empty value or the mask keeps the stored one;
'[[clear]]' removes it. MaskedField says that a value is set and offers to
remove it. Off: secrets: { writeOnly: false }. writeOnlySecret(s) and the hooks
are exported for a project's own fields.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 00:39:12 +01:00

93 lines
4.6 KiB
TypeScript

/**
* The same guarantees with the oldest supported 2FA package, @clocklimited/payload-2fa 3.0.0-beta.1 (installed
* here under the alias `payload-2fa-beta1`). The other HTTP tests run on the version in devDependencies, which is
* the one client sites use.
*/
import type { Payload } from 'payload'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { getPayload } from 'payload'
import { payloadTotp } from 'payload-2fa-beta1'
import { afterAll, beforeAll, expect, test, vi } from 'vitest'
import { ipalKit } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
import { testDatabase } from './http/db.js'
import { cookiesOf, startPayloadServer, type TestServer } from './http/server.js'
import { totp } from './http/totp.js'
vi.mock('server-only', () => ({}))
vi.mock('next/headers.js', async () => (await import('./http/server.js')).nextHeadersMock)
const SECRET = 'JBSWY3DPEHPK3PXP'
const PASSWORD = 'a-long-test-password-1'
let server: TestServer
let payload: Payload
const post = (pathname: string, body: unknown, headers: Record<string, string> = {}) =>
fetch(`${server.url}/api${pathname}`, { body: JSON.stringify(body), headers: { 'content-type': 'application/json', ...headers }, method: 'POST' })
const get = (pathname: string, headers: Record<string, string>) => fetch(`${server.url}/api${pathname}`, { headers })
beforeAll(async () => {
const config = buildTestConfig({
admin: { user: 'users' },
collections: [
{ slug: 'customers', access: { create: () => true }, auth: true, fields: [] },
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
],
db: testDatabase('sechttp_b1'),
editor: lexicalEditor(),
plugins: [
ipalKit({
access: { authCollection: 'users' },
forms: {},
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
twoFactor: { issuer: 'Example', plugin: payloadTotp as never },
}),
],
secret: 'integration-test-secret-0123456789',
telemetry: false,
})
payload = await getPayload({ config })
server = await startPayloadServer(config)
await payload.updateGlobal({ slug: 'site-integrations' as never, data: { smtpPassword: 'S3CRET' } as never })
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['admin'], totpSecret: SECRET } as never })
await payload.create({ collection: 'customers' as never, data: { email: '[email protected]', password: PASSWORD } as never })
}, 120_000)
afterAll(async () => {
await server?.close()
await payload?.destroy?.()
})
test('beta.1: password only → refused; password and code → allowed (REST and GraphQL)', async () => {
const login = await post('/users/login', { email: '[email protected]', password: PASSWORD })
const cookie = cookiesOf(login).join('; ')
for (const p of ['/globals/site-integrations', '/form-submissions', '/users', '/globals/site-settings']) {
expect([p, (await get(p, { cookie })).status]).toEqual([p, 403])
}
const before = (await (await post('/graphql', { query: '{ SiteIntegration { smtpPassword } }' }, { cookie })).json()) as { errors?: unknown[] }
expect(before.errors?.length).toBeGreaterThan(0)
const verify = await post('/verify-totp', { token: totp(SECRET) }, { cookie })
expect(((await verify.json()) as { ok: boolean }).ok).toBe(true)
const full = [cookie, ...cookiesOf(verify)].join('; ')
const integrations = await get('/globals/site-integrations', { cookie: full })
expect(integrations.status).toBe(200)
expect(((await integrations.json()) as { smtpPassword: string }).smtpPassword).toBe('********') // set, never returned (write-only secrets)
expect((await get('/form-submissions', { cookie: full })).status).toBe(200)
})
test('beta.1: a customers account cannot use the 2FA endpoints or reach staff data', async () => {
const login = (await (await post('/customers/login', { email: '[email protected]', password: PASSWORD })).json()) as { token: string }
const authorization = `JWT ${login.token}`
const ATTACKER = 'KRSXG5CTMVRXEZLU'
expect((await post('/setup-totp', { secret: ATTACKER, token: totp(ATTACKER) }, { authorization })).status).toBe(403)
const admin = await payload.find({ collection: 'users' as never, showHiddenFields: true })
expect((admin.docs[0] as { totpSecret?: string }).totpSecret).toBe(SECRET)
expect((await get('/globals/site-integrations', { authorization })).status).toBe(403)
expect((await get('/form-submissions', { authorization })).status).toBe(403)
})