Security review fixes: staff collection first everywhere, 2FA package endpoints guarded, per-entity opt-out, hot reload
F1: roles count only for accounts of a staff collection, in every exported predicate and access builder (isAdmin, isEditor, hasMinimumRole, adminOnly, adminOrEditor, requireRole, adminOrSelf, adminOrSelfIn, the Field variants, the roles field) and first in userMeetsRole. An account of another auth collection with its own roles: ['admin'] gets nothing. Staff collections: admin.user, access.authCollection, access.staffCollections. Predicates take req.payload as an optional last argument and otherwise use the staff collections of the config ipalKit built in this process. F2: every endpoint @clocklimited/payload-2fa registers answers accounts of the 2FA collection only (setup-totp wrote the secret into the 2FA collection under the caller's id, whatever collection the caller came from); remove and the admin reset/remove endpoints also need the second factor. F3: per-entity opt-out honoured, operation by operation: the package's custom.totp.disableAccessWrapper and ipal-kit's custom.ipal.secondFactor (passed on to the package); the package's global disableAccessWrapper too. F4: a no-user auth strategy on the 2FA collection guards the live config at the start of each request, so entities from plugins listed after ipalKit stay guarded after a dev hot reload and with disableOnInit. Tests through HTTP for all four; the 2FA tests now run on payload-2fa 3.0.0-beta.7 (what client sites use), a second file on beta.1 (alias), and the HTTP files can run against Postgres (IPAL_TEST_POSTGRES_URI). Docs, agent guide, skills and changelog updated; the not-yet-enrolled limitation is stated plainly. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
97ee1289aa
commit
32faaa5e30
@@ -9,8 +9,8 @@ import { describe, expect, test } from 'vitest'
|
||||
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
||||
|
||||
import { adminOrSelfIn } from '../src/modules/access/access.js'
|
||||
import { staffCollectionSlugs, userMeetsRole } from '../src/modules/access/predicates.js'
|
||||
import { guardPayloadAccess, requireSecondFactor, secondFactorPassed } from '../src/modules/security/totp/secondFactor.js'
|
||||
import { isAdmin, staffCollectionSlugs, userMeetsRole } from '../src/modules/access/predicates.js'
|
||||
import { guardPayloadAccess, guardTotpEndpoint, requireSecondFactor, secondFactorPassed } from '../src/modules/security/totp/secondFactor.js'
|
||||
import { ipalKit } from '../src/plugin.js'
|
||||
import { withTotpParts } from './helpers.js'
|
||||
|
||||
@@ -42,6 +42,9 @@ describe('userMeetsRole: who is staff', () => {
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers' } }, 'user')).toBe(false)
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers' } }, 'admin')).toBe(false)
|
||||
expect(userMeetsRole({ payload, user: { collection: 'no-such-collection' } }, 'user')).toBe(false)
|
||||
// a role value on an account of a non-staff collection counts for nothing
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers', roles: ['admin'] } }, 'user')).toBe(false)
|
||||
expect(userMeetsRole({ payload, user: { roles: ['admin'] } }, 'user')).toBe(false) // no collection at all
|
||||
expect(userMeetsRole({ payload, user: null }, 'user')).toBe(false)
|
||||
})
|
||||
|
||||
@@ -58,12 +61,17 @@ describe('userMeetsRole: who is staff', () => {
|
||||
const payload = payloadOf(config)
|
||||
expect(staffCollectionSlugs(payload)).toEqual(['users', 'partners'])
|
||||
expect(userMeetsRole({ payload, user: { collection: 'partners' } }, 'admin')).toBe(true) // no roles field there: all equal
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers', roles: ['admin'] } }, 'admin')).toBe(false)
|
||||
expect(userMeetsRole({ payload, user: { collection: 'customers' } }, 'admin')).toBe(false)
|
||||
})
|
||||
|
||||
test('without a payload only a held role counts', () => {
|
||||
test('without a payload: the staff collections of the config ipalKit built last; only a held role counts', async () => {
|
||||
await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ ...twoAuth(), admin: { user: 'users' } } as Config)
|
||||
expect(userMeetsRole({ user: { collection: 'users' } }, 'user')).toBe(false)
|
||||
expect(userMeetsRole({ user: { collection: 'users', roles: ['admin'] } }, 'admin')).toBe(true)
|
||||
expect(userMeetsRole({ user: { collection: 'customers', roles: ['admin'] } }, 'admin')).toBe(false)
|
||||
expect(isAdmin({ collection: 'customers', roles: ['admin'] })).toBe(false) // isAdmin(req.user) in a hook
|
||||
expect(isAdmin({ collection: 'users', roles: ['admin'] })).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -154,6 +162,54 @@ describe('ipalKit and the 2FA plugin', () => {
|
||||
expect((config.custom as { ipal?: { twoFactor?: unknown } } | undefined)?.ipal?.twoFactor).toBeUndefined()
|
||||
})
|
||||
|
||||
test('per-entity opt-out: the package key and ipal-kit\'s key, operation by operation', async () => {
|
||||
const config = await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: fakeTotp as never } })({
|
||||
collections: [
|
||||
{ slug: 'a', custom: { totp: { disableAccessWrapper: { read: true } } }, fields: [] },
|
||||
{ slug: 'b', custom: { ipal: { secondFactor: false } }, fields: [] },
|
||||
],
|
||||
globals: [{ slug: 'g', custom: { ipal: { secondFactor: { read: false } } }, fields: [] }],
|
||||
secret: 'x',
|
||||
} as unknown as Config)
|
||||
const payload = payloadOf(config)
|
||||
// an operation without a function = Payload's default: any logged-in account
|
||||
const call = (access: unknown, op: string) => {
|
||||
const fn = (access as Record<string, ((a: unknown) => unknown) | undefined> | undefined)?.[op]
|
||||
return fn ? fn({ req: { payload, user: passwordOnly } }) : true
|
||||
}
|
||||
const col = (slug: string) => config.collections!.find((c) => c.slug === slug)!.access
|
||||
expect(call(col('a'), 'read')).toBe(true)
|
||||
expect(call(col('a'), 'update')).toBe(false)
|
||||
// opted out of everything and no rule of its own: left exactly as the project wrote it (Payload's default applies)
|
||||
expect(col('b')).toBeUndefined()
|
||||
const g = config.globals!.find((x) => x.slug === 'g')!.access
|
||||
expect(call(g, 'read')).toBe(true)
|
||||
expect(call(g, 'update')).toBe(false)
|
||||
// ipal-kit's key reaches the package in its own key, so the package leaves the operation alone too
|
||||
expect((config.globals!.find((x) => x.slug === 'g')!.custom as { totp: unknown }).totp).toEqual({ disableAccessWrapper: { read: true } })
|
||||
})
|
||||
|
||||
test("the 2FA package's endpoints: accounts of the 2FA collection only; management needs the second factor", async () => {
|
||||
const payload = { config: { custom: { ipal: { twoFactor: { collection: 'users' } } } } }
|
||||
const inner = () => Promise.resolve(Response.json({ ok: true }))
|
||||
const call = async (path: string, user: unknown) => {
|
||||
const endpoint = guardTotpEndpoint({ handler: inner as (req: never) => Promise<Response>, path }, 'users')
|
||||
return (await endpoint.handler({ payload, user } as never)).status
|
||||
}
|
||||
const customer = { id: 1, collection: 'customers' }
|
||||
const newcomer = { id: 2, _strategy: 'local-jwt', collection: 'users', hasTotp: false }
|
||||
for (const path of ['/setup-totp', '/verify-totp', '/remove-totp', '/admin/reset-user-totp', '/some-future-endpoint']) {
|
||||
expect([path, await call(path, customer)]).toEqual([path, 403])
|
||||
expect([path, await call(path, null)]).toEqual([path, 200]) // no user: the package answers itself
|
||||
}
|
||||
expect(await call('/setup-totp', newcomer)).toBe(200)
|
||||
expect(await call('/verify-totp', passwordOnly)).toBe(200)
|
||||
expect(await call('/remove-totp', passwordOnly)).toBe(403)
|
||||
expect(await call('/admin/reset-user-totp', passwordOnly)).toBe(403)
|
||||
expect(await call('/some-future-endpoint', passwordOnly)).toBe(403)
|
||||
expect(await call('/admin/reset-user-totp', verified)).toBe(200)
|
||||
})
|
||||
|
||||
test('twoFactor: false — no guard, a password is enough (as documented)', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })({ collections: [], secret: 'x' } as unknown as Config)
|
||||
const payload = payloadOf(config)
|
||||
|
||||
Reference in New Issue
Block a user