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:
co-authored by
Claude Opus 5.5
parent
fc70a34f3e
commit
15b5c6258d
@@ -27,6 +27,8 @@ export { applyPasswordRule, buildPasswordRuleHook, checkPassword, DEFAULT_PASSWO
|
||||
export type { PasswordRuleOptions } from './modules/access/passwordRule.js'
|
||||
export { isPasswordRuleHook, staffRuleOf } from './modules/access/ruleMark.js'
|
||||
export type { StaffRuleMark } from './modules/access/ruleMark.js'
|
||||
export { graphqlLoginName, lockedLooksWrong, withLockedAnswer } from './modules/access/lockedAnswer.js'
|
||||
export type { LockedAnswerOption } from './modules/access/lockedAnswer.js'
|
||||
export {
|
||||
adoptLateAuthCollections,
|
||||
applyFirstUserRoute,
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
import type { AfterErrorHook, Config, SanitizedCollectionConfig } from 'payload'
|
||||
|
||||
import { AuthenticationError, formatNames, toWords } from 'payload'
|
||||
|
||||
import { staffCollectionSlugs } from './predicates.js'
|
||||
|
||||
/**
|
||||
* A locked staff account answers its login exactly like a wrong password (`accounts.lockedAnswer`, 1.17.2).
|
||||
*
|
||||
* Payload checks the lock BEFORE the password: after five wrong passwords for `owner@site` any password gets
|
||||
* `401 "This user is locked due to having too many failed login attempts."` (GraphQL: `LockedAuth`), while an
|
||||
* address that has no account always gets `401 "The email or password provided is incorrect."`. Anybody could
|
||||
* tell which addresses have a staff account with no password at all (IAWL idea 6). This hook turns Payload's
|
||||
* `LockedAuth` on a staff collection's login into the `AuthenticationError` answer — same status, same body, same
|
||||
* GraphQL `extensions.name`. The price: the owner, while locked, is told "wrong e-mail or password" too.
|
||||
*
|
||||
* The lock itself is not touched (it still holds for `lockTime`). `accounts: { lockedAnswer: 'payload' }` keeps
|
||||
* Payload's message. The error is recognised by its `name` (`'LockedAuth'`, a string Payload sets — not a function
|
||||
* name, so a minified bundle keeps it) and by the collection: a staff collection only.
|
||||
*/
|
||||
export type LockedAnswerOption = 'payload' | 'wrong'
|
||||
|
||||
const isLocked = (error: unknown): boolean => (error as { name?: unknown } | null)?.name === 'LockedAuth'
|
||||
|
||||
/** The GraphQL login mutation's name of a collection, as @payloadcms/graphql builds it (`login${singularName}`). */
|
||||
export function graphqlLoginName(collection: Pick<SanitizedCollectionConfig, 'graphQL' | 'slug'>): null | string {
|
||||
const graphQL = collection.graphQL as { singularName?: string } | false | undefined
|
||||
if (graphQL === false) {return null}
|
||||
const singular = graphQL?.singularName ? toWords(graphQL.singularName, true) : formatNames(collection.slug).singular
|
||||
return `login${singular}`
|
||||
}
|
||||
|
||||
type T = ConstructorParameters<typeof AuthenticationError>[0]
|
||||
|
||||
/** The config-level `afterError` hook (REST and GraphQL). */
|
||||
export const lockedLooksWrong: AfterErrorHook = (args) => {
|
||||
const { collection, error, req } = args as { error: unknown; graphqlResult?: Record<string, unknown> } & Parameters<AfterErrorHook>[0]
|
||||
const payload = req?.payload
|
||||
if (!payload) {return}
|
||||
const staff = staffCollectionSlugs(payload)
|
||||
const wrong = new AuthenticationError(req.t as T)
|
||||
// GraphQL: the error is wrapped (`originalError`), the collection only shows in the mutation's name.
|
||||
const graphqlResult = (args as { graphqlResult?: { extensions?: Record<string, unknown>; path?: unknown[] } }).graphqlResult
|
||||
if (graphqlResult) {
|
||||
const original = (error as { originalError?: unknown }).originalError
|
||||
if (!isLocked(original)) {return}
|
||||
const field = Array.isArray(graphqlResult.path) ? graphqlResult.path[0] : undefined
|
||||
const names = new Set((payload.config.collections ?? []).filter((c) => staff.includes(c.slug)).map(graphqlLoginName))
|
||||
if (typeof field !== 'string' || !names.has(field)) {return}
|
||||
return {
|
||||
graphqlResult: {
|
||||
...graphqlResult,
|
||||
extensions: { ...graphqlResult.extensions, name: wrong.name, statusCode: wrong.status },
|
||||
message: wrong.message,
|
||||
},
|
||||
} as never
|
||||
}
|
||||
if (!isLocked(error) || !collection || !staff.includes(collection.slug)) {return}
|
||||
return { response: { errors: [{ message: wrong.message }] }, status: wrong.status } as never
|
||||
}
|
||||
|
||||
/** Adds the hook to a config (idempotent). */
|
||||
export function withLockedAnswer(config: Config): Config {
|
||||
const own = config.hooks?.afterError ?? []
|
||||
if (own.includes(lockedLooksWrong)) {return config}
|
||||
return { ...config, hooks: { ...config.hooks, afterError: [...own, lockedLooksWrong] } }
|
||||
}
|
||||
@@ -49,6 +49,13 @@ export type AccountsOptions = {
|
||||
* whose own `create` rule allows an anonymous request keeps its route.
|
||||
*/
|
||||
firstUserRoute?: 'all' | 'panel' | string[]
|
||||
/**
|
||||
* What a staff account that Payload has locked (too many wrong passwords) is told when it logs in.
|
||||
* Default (1.17.2): exactly what a wrong password gets — same status, message and GraphQL error name — so
|
||||
* nobody can find out which addresses have a staff account by locking them. The lock itself is unchanged.
|
||||
* `'wrong'` = the default, said out loud. `'payload'` = Payload's own "This user is locked due to having too many failed login attempts."
|
||||
*/
|
||||
lockedAnswer?: 'payload' | 'wrong'
|
||||
/**
|
||||
* The lowest role that may open the admin panel. Default `'editor'`: an
|
||||
* account that holds only `user` (the default role of a new account) sees
|
||||
|
||||
@@ -50,6 +50,9 @@ export function assertPluginSwitches(options: Record<string, unknown>): void {
|
||||
// `accounts: 0` / `''` dropped every account rule (staff-only reads, admin-only writes, the panel role,
|
||||
// the password rule, the closed first-user route).
|
||||
assertSwitchOrObject('accounts', options.accounts, { allowTrue: false })
|
||||
// 1.17.2: only 'payload' brings Payload's "locked" answer back (it tells which addresses have an account).
|
||||
const accounts = bag(options.accounts)
|
||||
if (accounts) {assertOneOf('accounts.lockedAnswer', accounts.lockedAnswer, ['payload', 'wrong'])}
|
||||
|
||||
// Opt-in features that add public endpoints or outbound calls: `'false'` switched them ON.
|
||||
for (const name of ['cookieScanner', 'stats', 'consentLog', 'adminMeta', 'whiteLabel'] as const) {
|
||||
|
||||
@@ -13,6 +13,7 @@ import { applyGlobalOverrides } from './globals/overrides.js'
|
||||
import { buildSiteIntegrations } from './globals/SiteIntegrations/index.js'
|
||||
import { buildSiteSettings } from './globals/SiteSettings/index.js'
|
||||
import { adminOnly, adminOrSelfIn, injectRoles, registerStaffCollections, staffCollectionSlugs } from './modules/access/index.js'
|
||||
import { withLockedAnswer } from './modules/access/lockedAnswer.js'
|
||||
import { applyPanelRole, countBelowPanelRole, resolvePanelRole } from './modules/access/panelRole.js'
|
||||
import { applyPasswordRule, buildPasswordRuleHook, resolvePasswordRule } from './modules/access/passwordRule.js'
|
||||
import { adoptLateAuthCollections, applyFirstUserRoute, applyStaffAccountAccess, markSeenAuthCollections, markStaffRules, staffCollectionsNobodyManages, unknownStaffCollections } from './modules/access/selfRegistration.js'
|
||||
@@ -258,6 +259,8 @@ export const iapl = (options: IaplOptions): Plugin => {
|
||||
const panelSlug = config.admin?.user ?? (config.collections ?? []).find((c) => c.auth)?.slug
|
||||
const staffSlugs = [...new Set([panelSlug, options.access?.authCollection, ...(options.access?.staffCollections ?? [])].filter((s): s is string => Boolean(s)))]
|
||||
config = applyStaffAccountAccess(config, staffSlugs, { ...accountAccess, pluginRoles: options.access?.authCollection ? [options.access.authCollection] : [] })
|
||||
// A locked staff account answers like a wrong password (accounts.lockedAnswer).
|
||||
if (accounts.lockedAnswer !== 'payload') {config = withLockedAnswer(config)}
|
||||
}
|
||||
if (options.twoFactor !== false) {
|
||||
const tf = options.twoFactor
|
||||
|
||||
Reference in New Issue
Block a user