From 7d9083b3cbd5194fb2d6396a42df61bd1ea0f901 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 19:53:55 +0000 Subject: [PATCH 1/7] test(plugin-auth): pin the lockout counter against an organization IP-list refusal Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .../account-lockout-resolved-account.test.ts | 86 ++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts b/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts index c14b262184d..34e715c4807 100644 --- a/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts +++ b/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts @@ -21,6 +21,11 @@ // The account events (`login_failed` / `account_locked`) name the account the // same resolution names, so the ledger and the lock never disagree. // +// Only a refused CREDENTIAL moves the counter. A sign-in whose password passed +// and that a policy then refused (the organization's allowed IP ranges, judged +// when the session is scoped to it) neither counts nor clears: the last +// `describe` below. +// // ## Why the server here is the real one // // "Which account does this identifier name" is answered by the credential @@ -74,13 +79,14 @@ function deployment() { return { engine, manager, events }; } -const post = (manager: AuthManager, path: string, body: unknown) => +const post = (manager: AuthManager, path: string, body: unknown, remoteAddress?: string) => manager.handleRequest( new Request(`${BASE}${path}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }), + remoteAddress ? { remoteAddress } : undefined, ); async function signUp(manager: AuthManager): Promise { @@ -226,3 +232,81 @@ describe('ADR-0069 D2: the lockout counts and holds the account the credential c expect((await signIn(manager, EMAIL, PASSWORD)).status).toBe(200); }); }); + +/** The organization the account belongs to, and the one network its own list admits. */ +const ORGANIZATION = 'org_lockout_policy'; +const INSIDE = '10.1.0.7'; +const OUTSIDE = '10.2.0.7'; +const NOT_ADMITTED = { status: 403, code: 'IP_NOT_ALLOWED' }; + +/** + * {@link deployment}, with the account a member of an organization whose own + * allowed IP ranges admit only 10.1.0.0/16. The deployment sets no list of its + * own, so the organization's list is judged where the sign-in's session is + * scoped to it — after the password passed. + */ +async function organizationListDeployment() { + const { engine, manager, events } = deployment(); + await signUp(manager); + const userId = String(accountRow(engine).id); + // Whatever membership the sign-up settled is replaced by the one under test. + engine.tables.set('sys_member', (engine.tables.get('sys_member') ?? []).filter((m: any) => m.user_id !== userId)); + await engine.insert('sys_member', { user_id: userId, organization_id: ORGANIZATION, role: 'member', created_at: new Date() }); + manager.setOrganizationAccessPolicySource({ + read: async (id) => (id === ORGANIZATION ? { allowed_ip_ranges: '10.1.0.0/16' } : {}), + anyOrganizationRequiresMfa: async () => false, + }); + return { engine, manager, events }; +} + +const signInFrom = (manager: AuthManager, password: string, remoteAddress: string) => + post(manager, '/sign-in/email', { email: EMAIL, password }, remoteAddress); + +describe('ADR-0069 D2: a correct password the organization\'s IP list refuses neither counts nor clears', () => { + it('refusals past the threshold lock nothing, and the account signs in from inside the list', async () => { + const { engine, manager, events } = await organizationListDeployment(); + events.length = 0; + + for (let i = 0; i < THRESHOLD; i++) { + expect(await outcome(await signInFrom(manager, PASSWORD, OUTSIDE)), `attempt ${i + 1}`).toEqual(NOT_ADMITTED); + expect(Number(accountRow(engine).failed_login_count ?? 0), `attempt ${i + 1}`).toBe(0); + } + expect(isLocked(accountRow(engine))).toBe(false); + expect(events.some((e) => e.action === 'account_locked')).toBe(false); + + const home = await signInFrom(manager, PASSWORD, INSIDE); + expect(home.status, await home.clone().text()).toBe(200); + expect(Number(accountRow(engine).failed_login_count ?? 0)).toBe(0); + }); + + it('the refusal leaves a count earned by wrong passwords where it stands', async () => { + const { engine, manager } = await organizationListDeployment(); + for (let i = 0; i < THRESHOLD - 1; i++) { + expect(await outcome(await signInFrom(manager, WRONG_PASSWORD, INSIDE))).toEqual(REFUSED); + } + expect(Number(accountRow(engine).failed_login_count)).toBe(THRESHOLD - 1); + + expect(await outcome(await signInFrom(manager, PASSWORD, OUTSIDE))).toEqual(NOT_ADMITTED); + expect(Number(accountRow(engine).failed_login_count)).toBe(THRESHOLD - 1); + expect(isLocked(accountRow(engine))).toBe(false); + }); + + it('a wrong password counts from either side of the list, and a correct one from inside clears', async () => { + const { engine, manager } = await organizationListDeployment(); + + // From outside the credential refuses first: the list is judged only once a session is created. + expect(await outcome(await signInFrom(manager, WRONG_PASSWORD, OUTSIDE))).toEqual(REFUSED); + expect(Number(accountRow(engine).failed_login_count)).toBe(1); + expect(await outcome(await signInFrom(manager, WRONG_PASSWORD, INSIDE))).toEqual(REFUSED); + expect(Number(accountRow(engine).failed_login_count)).toBe(2); + + expect((await signInFrom(manager, PASSWORD, INSIDE)).status).toBe(200); + expect(Number(accountRow(engine).failed_login_count)).toBe(0); + + for (let i = 0; i < THRESHOLD; i++) { + expect(await outcome(await signInFrom(manager, WRONG_PASSWORD, OUTSIDE))).toEqual(REFUSED); + } + expect(isLocked(accountRow(engine))).toBe(true); + expect(await outcome(await signInFrom(manager, PASSWORD, INSIDE))).toEqual(LOCKED); + }); +}); From 2f30c63c07d5ce9e912e2c30234716eb61f992c2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 19:59:03 +0000 Subject: [PATCH 2/7] fix(plugin-auth): an organization IP-list refusal after the password passed no longer moves the lockout counter Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .../src/auth-manager-after-hook.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts index 51904617301..fc4bf647fdb 100644 --- a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts +++ b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts @@ -42,8 +42,9 @@ import { codeSelfServiceRefusal } from './account-self-service-refusal-code.js'; import type { AuthManager } from './auth-manager.js'; -import { SIGN_IN_EMAIL_PATH } from './auth-session-audit.js'; +import { refusalCodeOf, SIGN_IN_EMAIL_PATH } from './auth-session-audit.js'; import { narrowDirectoryForExternalCaller } from './external-directory-door.js'; +import { SESSION_SCOPING_REFUSAL } from './organization-access-policy.js'; import { refuseIntrospectionForBannedSubject } from './oauth-token-read-ban-refusal.js'; import { rotateCallerBearerOnImpersonation } from './impersonation-bearer-rotation.js'; import { stampInvitationDeliveryOutcome } from './invitation-delivery-outcome.js'; @@ -89,6 +90,16 @@ function changeEmailFrom(user: unknown): { email: string; name?: string; id?: st }; } +/** + * ADR-0069 D2 — the `/sign-in/email` refusals that come AFTER the password + * passed, which the lockout counter neither counts nor clears: only a refused + * credential counts toward a lock. An organization's own allowed IP ranges are + * judged where the new session is scoped to it (`refuseUnadmittedScoping`, at + * session creation), which better-auth reaches only once the password has + * verified, so a wrong password never carries this code to the counter. + */ +const REFUSALS_AFTER_THE_CREDENTIAL: ReadonlySet = new Set([SESSION_SCOPING_REFUSAL.code]); + /** The body of `createAuthInstance`'s `hooks.after` arrow; the comments there and in it are its contract. */ export async function globalAfterHook(this: AfterHookHost, ctx: any) { // ── #8243: impersonation must actually take effect for a bearer ── @@ -149,6 +160,9 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { // better-auth catches an INVALID_EMAIL_OR_PASSWORD APIError and runs // the after-hook with it on `ctx.context.returned`; a success leaves // the session payload there. Count failures, reset on success. + // A refusal in REFUSALS_AFTER_THE_CREDENTIAL is neither: the password + // passed and a policy refused the session, so the counter is left as + // it stands. Its `login_failed` row is still written below. if (ctx?.path === SIGN_IN_EMAIL_PATH) { const email = typeof ctx?.body?.email === 'string' ? ctx.body.email : ''; if (email) { @@ -159,7 +173,8 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { } catch { succeeded = !(ctx?.context?.returned instanceof Error); } - const lockStamped = await this.recordSignInOutcome(email, succeeded); + const refusedAfterCredential = !succeeded && REFUSALS_AFTER_THE_CREDENTIAL.has(refusalCodeOf(ctx?.context?.returned) ?? ''); + const lockStamped = refusedAfterCredential ? undefined : await this.recordSignInOutcome(email, succeeded); // A refused sign-in writes `login_failed`, and the one that // stamped the lockout also writes `account_locked`. A successful // one is recorded as `login` by the session hook, unchanged. From 5df7a23486a2c47c20072211c0936582850778ab Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 19:59:28 +0000 Subject: [PATCH 3/7] fix(plugin-auth): judge the post-credential refusal at call time, past the module cycle Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .../plugin-auth/src/auth-manager-after-hook.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts index fc4bf647fdb..8070f4fc16d 100644 --- a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts +++ b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts @@ -97,8 +97,16 @@ function changeEmailFrom(user: unknown): { email: string; name?: string; id?: st * judged where the new session is scoped to it (`refuseUnadmittedScoping`, at * session creation), which better-auth reaches only once the password has * verified, so a wrong password never carries this code to the counter. + * + * A function, read when a sign-in is judged, rather than a set built when this + * module loads: `organization-access-policy.ts` imports `auth-manager.ts`, + * which reaches this module, so on that import order the constant is not yet + * initialised while this module evaluates. */ -const REFUSALS_AFTER_THE_CREDENTIAL: ReadonlySet = new Set([SESSION_SCOPING_REFUSAL.code]); +function refusedAfterTheCredential(returned: unknown): boolean { + const code = refusalCodeOf(returned); + return code !== undefined && [SESSION_SCOPING_REFUSAL.code].includes(code); +} /** The body of `createAuthInstance`'s `hooks.after` arrow; the comments there and in it are its contract. */ export async function globalAfterHook(this: AfterHookHost, ctx: any) { @@ -160,7 +168,7 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { // better-auth catches an INVALID_EMAIL_OR_PASSWORD APIError and runs // the after-hook with it on `ctx.context.returned`; a success leaves // the session payload there. Count failures, reset on success. - // A refusal in REFUSALS_AFTER_THE_CREDENTIAL is neither: the password + // A refusal `refusedAfterTheCredential` names is neither: the password // passed and a policy refused the session, so the counter is left as // it stands. Its `login_failed` row is still written below. if (ctx?.path === SIGN_IN_EMAIL_PATH) { @@ -173,8 +181,9 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { } catch { succeeded = !(ctx?.context?.returned instanceof Error); } - const refusedAfterCredential = !succeeded && REFUSALS_AFTER_THE_CREDENTIAL.has(refusalCodeOf(ctx?.context?.returned) ?? ''); - const lockStamped = refusedAfterCredential ? undefined : await this.recordSignInOutcome(email, succeeded); + const lockStamped = !succeeded && refusedAfterTheCredential(ctx?.context?.returned) + ? undefined + : await this.recordSignInOutcome(email, succeeded); // A refused sign-in writes `login_failed`, and the one that // stamped the lockout also writes `account_locked`. A successful // one is recorded as `login` by the session hook, unchanged. From d43cae7445fe5f8f42109a230349caec4a6b880c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:01:53 +0000 Subject: [PATCH 4/7] chore(plugin-auth): changeset for the lockout counter fix Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .changeset/31251-org-ip-refusal-lockout-counter.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .changeset/31251-org-ip-refusal-lockout-counter.md diff --git a/.changeset/31251-org-ip-refusal-lockout-counter.md b/.changeset/31251-org-ip-refusal-lockout-counter.md new file mode 100644 index 00000000000..7291f31a3d5 --- /dev/null +++ b/.changeset/31251-org-ip-refusal-lockout-counter.md @@ -0,0 +1,10 @@ +--- +'@objectstack/plugin-auth': patch +--- + +fix(plugin-auth): a correct-password sign-in that an organization's allowed IP ranges refuse no longer counts toward the account lockout + +Clause-②: no +Metadata-protocol: none + +The ADR-0069 D2 lockout counter read every refused `/sign-in/email` as a wrong password. An organization's own IP list is judged when the new session is scoped to it, which happens only after the password verified, so a member signing in from outside that list with the right password raised their own count and, at the threshold, locked their account: their next sign-in from inside the list was refused `ACCOUNT_LOCKED`. That refusal (`403 IP_NOT_ALLOWED`) now leaves the counter as it stands, neither counting nor clearing it. A wrong password still counts from either side of the list, and a successful sign-in still clears the count. The `403 IP_NOT_ALLOWED` answer and its `login_failed` audit row are unchanged. From c38cc4cfc5a7543fab34c48f3f0b3de2008d897e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:08:19 +0000 Subject: [PATCH 5/7] fix(plugin-auth): type the post-credential refusal list as strings Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- packages/plugins/plugin-auth/src/auth-manager-after-hook.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts index 8070f4fc16d..8364cadd5ff 100644 --- a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts +++ b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts @@ -104,8 +104,9 @@ function changeEmailFrom(user: unknown): { email: string; name?: string; id?: st * initialised while this module evaluates. */ function refusedAfterTheCredential(returned: unknown): boolean { + const after: readonly string[] = [SESSION_SCOPING_REFUSAL.code]; const code = refusalCodeOf(returned); - return code !== undefined && [SESSION_SCOPING_REFUSAL.code].includes(code); + return code !== undefined && after.includes(code); } /** The body of `createAuthInstance`'s `hooks.after` arrow; the comments there and in it are its contract. */ From d13e2cec5ac2231b1a1b36d1895c4551778a5ed6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:41:54 +0000 Subject: [PATCH 6/7] fix(plugin-auth): a ban or an unverified address refused after the password passed no longer moves the lockout counter Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .../account-lockout-resolved-account.test.ts | 55 ++++++++++++++++++- .../src/auth-manager-after-hook.ts | 47 ++++++++++++---- 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts b/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts index 34e715c4807..b54a540aa6f 100644 --- a/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts +++ b/packages/plugins/plugin-auth/src/account-lockout-resolved-account.test.ts @@ -23,8 +23,8 @@ // // Only a refused CREDENTIAL moves the counter. A sign-in whose password passed // and that a policy then refused (the organization's allowed IP ranges, judged -// when the session is scoped to it) neither counts nor clears: the last -// `describe` below. +// when the session is scoped to it; a ban; an address the deployment requires +// verified) neither counts nor clears: the last two `describe`s below. // // ## Why the server here is the real one // @@ -61,7 +61,8 @@ const PASSWORD = 'Corr3ct!Horse-Battery'; const WRONG_PASSWORD = 'Wr0ng!Guess-Zebra-7731'; const THRESHOLD = 3; -function deployment() { +/** `extra` is merged into the manager's config (a plugin switch, an e-mail rule). */ +function deployment(extra: Record = {}) { const engine = createMemoryEngine(); const events: AuthSessionAuditEventInput[] = []; const manager = new AuthManager({ @@ -70,6 +71,7 @@ function deployment() { dataEngine: engine, lockoutThreshold: THRESHOLD, lockoutDurationMinutes: 15, + ...extra, getAuditSink: () => ({ recordAuthEvent: async (event: AuthSessionAuditEventInput) => { events.push(event); @@ -310,3 +312,50 @@ describe('ADR-0069 D2: a correct password the organization\'s IP list refuses ne expect(await outcome(await signInFrom(manager, PASSWORD, INSIDE))).toEqual(LOCKED); }); }); + +describe('ADR-0069 D2: a correct password refused for a ban or an unverified address neither counts nor clears', () => { + it('a banned account: wrong passwords count, the correct one is refused BANNED_USER and moves nothing', async () => { + const { engine, manager, events } = deployment({ plugins: { admin: true } }); + await signUp(manager); + accountRow(engine).banned = true; + for (let i = 0; i < THRESHOLD - 1; i++) { + expect(await outcome(await signIn(manager, EMAIL, WRONG_PASSWORD))).toEqual(REFUSED); + } + expect(Number(accountRow(engine).failed_login_count)).toBe(THRESHOLD - 1); + events.length = 0; + + for (let i = 0; i < THRESHOLD; i++) { + expect(await outcome(await signIn(manager, EMAIL, PASSWORD)), `attempt ${i + 1}`) + .toEqual({ status: 403, code: 'BANNED_USER' }); + expect(Number(accountRow(engine).failed_login_count), `attempt ${i + 1}`).toBe(THRESHOLD - 1); + } + expect(isLocked(accountRow(engine))).toBe(false); + expect(events.some((e) => e.action === 'account_locked')).toBe(false); + + // Lifted, the ban leaves no lock behind: the correct password signs in and clears the count. + accountRow(engine).banned = false; + expect((await signIn(manager, EMAIL, PASSWORD)).status).toBe(200); + expect(Number(accountRow(engine).failed_login_count)).toBe(0); + }); + + it('an unverified address: wrong passwords count, the correct one is refused EMAIL_NOT_VERIFIED and moves nothing', async () => { + const { engine, manager } = deployment({ emailAndPassword: { enabled: true, requireEmailVerification: true } }); + await signUp(manager); + accountRow(engine).email_verified = false; + for (let i = 0; i < THRESHOLD - 1; i++) { + expect(await outcome(await signIn(manager, EMAIL, WRONG_PASSWORD))).toEqual(REFUSED); + } + expect(Number(accountRow(engine).failed_login_count)).toBe(THRESHOLD - 1); + + for (let i = 0; i < THRESHOLD; i++) { + expect(await outcome(await signIn(manager, EMAIL, PASSWORD)), `attempt ${i + 1}`) + .toEqual({ status: 403, code: 'EMAIL_NOT_VERIFIED' }); + expect(Number(accountRow(engine).failed_login_count), `attempt ${i + 1}`).toBe(THRESHOLD - 1); + } + expect(isLocked(accountRow(engine))).toBe(false); + + // One more wrong password still reaches the threshold and locks. + expect(await outcome(await signIn(manager, EMAIL, WRONG_PASSWORD))).toEqual(REFUSED); + expect(isLocked(accountRow(engine))).toBe(true); + }); +}); diff --git a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts index 8364cadd5ff..e06b13de2fc 100644 --- a/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts +++ b/packages/plugins/plugin-auth/src/auth-manager-after-hook.ts @@ -90,23 +90,46 @@ function changeEmailFrom(user: unknown): { email: string; name?: string; id?: st }; } +/** + * better-auth's admin plugin refuses a banned user's new session with this + * code, from its `session.create.before` hook (`ADMIN_ERROR_CODES.BANNED_USER` + * in `better-auth/dist/plugins/admin/error-codes.mjs`). The package exports no + * public path to that table, so the code is named here, once. + */ +const VENDOR_BANNED_USER_CODE = 'BANNED_USER'; + /** * ADR-0069 D2 — the `/sign-in/email` refusals that come AFTER the password * passed, which the lockout counter neither counts nor clears: only a refused - * credential counts toward a lock. An organization's own allowed IP ranges are - * judged where the new session is scoped to it (`refuseUnadmittedScoping`, at - * session creation), which better-auth reaches only once the password has - * verified, so a wrong password never carries this code to the counter. + * credential counts toward a lock. better-auth's `signInEmail` throws + * `INVALID_EMAIL_OR_PASSWORD` for a wrong password before it reaches either + * place these are raised, so a wrong password never carries one of them to + * the counter: + * + * - `EMAIL_NOT_VERIFIED` (`BASE_ERROR_CODES`), thrown by `signInEmail` itself + * right after the password verified, when the deployment requires a + * verified address; + * - the organization's own allowed IP ranges (`SESSION_SCOPING_REFUSAL`), + * judged where the new session is scoped to it (`refuseUnadmittedScoping`, + * at session creation); + * - a ban ({@link VENDOR_BANNED_USER_CODE}), judged at session creation too. * * A function, read when a sign-in is judged, rather than a set built when this * module loads: `organization-access-policy.ts` imports `auth-manager.ts`, - * which reaches this module, so on that import order the constant is not yet - * initialised while this module evaluates. + * which reaches this module, so on that import order its constant is not yet + * initialised while this module evaluates; and better-auth is loaded lazily, + * as everywhere in this package. */ -function refusedAfterTheCredential(returned: unknown): boolean { - const after: readonly string[] = [SESSION_SCOPING_REFUSAL.code]; +async function refusedAfterTheCredential(returned: unknown): Promise { const code = refusalCodeOf(returned); - return code !== undefined && after.includes(code); + if (code === undefined) return false; + const { BASE_ERROR_CODES } = await import('@better-auth/core/error'); + const after: readonly string[] = [ + BASE_ERROR_CODES.EMAIL_NOT_VERIFIED.code, + SESSION_SCOPING_REFUSAL.code, + VENDOR_BANNED_USER_CODE, + ]; + return after.includes(code); } /** The body of `createAuthInstance`'s `hooks.after` arrow; the comments there and in it are its contract. */ @@ -170,8 +193,8 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { // the after-hook with it on `ctx.context.returned`; a success leaves // the session payload there. Count failures, reset on success. // A refusal `refusedAfterTheCredential` names is neither: the password - // passed and a policy refused the session, so the counter is left as - // it stands. Its `login_failed` row is still written below. + // passed and a policy then refused the sign-in, so the counter is left + // as it stands. Its `login_failed` row is still written below. if (ctx?.path === SIGN_IN_EMAIL_PATH) { const email = typeof ctx?.body?.email === 'string' ? ctx.body.email : ''; if (email) { @@ -182,7 +205,7 @@ export async function globalAfterHook(this: AfterHookHost, ctx: any) { } catch { succeeded = !(ctx?.context?.returned instanceof Error); } - const lockStamped = !succeeded && refusedAfterTheCredential(ctx?.context?.returned) + const lockStamped = !succeeded && await refusedAfterTheCredential(ctx?.context?.returned) ? undefined : await this.recordSignInOutcome(email, succeeded); // A refused sign-in writes `login_failed`, and the one that From f2bb01d5b4e7a7a9ae663165c40c690ffeb02263 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:49:03 +0000 Subject: [PATCH 7/7] chore(plugin-auth): changeset names the three post-credential refusals Claude-Session: https://claude.ai/code/session_01FEkQ5mSU6a4NFvNfFHukWv Co-Authored-By: Claude --- .changeset/31251-org-ip-refusal-lockout-counter.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/31251-org-ip-refusal-lockout-counter.md b/.changeset/31251-org-ip-refusal-lockout-counter.md index 7291f31a3d5..21e4a246777 100644 --- a/.changeset/31251-org-ip-refusal-lockout-counter.md +++ b/.changeset/31251-org-ip-refusal-lockout-counter.md @@ -2,9 +2,9 @@ '@objectstack/plugin-auth': patch --- -fix(plugin-auth): a correct-password sign-in that an organization's allowed IP ranges refuse no longer counts toward the account lockout +fix(plugin-auth): a correct-password sign-in refused afterwards, by an organization's allowed IP ranges, a ban or an unverified address, no longer counts toward the account lockout Clause-②: no Metadata-protocol: none -The ADR-0069 D2 lockout counter read every refused `/sign-in/email` as a wrong password. An organization's own IP list is judged when the new session is scoped to it, which happens only after the password verified, so a member signing in from outside that list with the right password raised their own count and, at the threshold, locked their account: their next sign-in from inside the list was refused `ACCOUNT_LOCKED`. That refusal (`403 IP_NOT_ALLOWED`) now leaves the counter as it stands, neither counting nor clearing it. A wrong password still counts from either side of the list, and a successful sign-in still clears the count. The `403 IP_NOT_ALLOWED` answer and its `login_failed` audit row are unchanged. +The ADR-0069 D2 lockout counter read every refused `/sign-in/email` as a wrong password. Three refusals come only after the password verified: an organization's own IP list (`403 IP_NOT_ALLOWED`, judged when the new session is scoped to it), a ban (`403 BANNED_USER`, judged when the session is created) and an address the deployment requires verified (`403 EMAIL_NOT_VERIFIED`). Each raised the count, so an account holder with the right password could lock their own account: a member signing in from outside the organization's network was then refused `ACCOUNT_LOCKED` from inside it, a lifted ban left a lock behind, and a person waiting for their verification mail was locked out once it arrived. These three refusals now leave the counter as it stands, neither counting nor clearing it. A wrong password still counts in every case, and a successful sign-in still clears the count. The refusals' status, code and message and their `login_failed` audit rows are unchanged.