IAPL rename: the old names keep working, and a second cookie name and counter key for new sites

- exports: ipalKit, IpalOptions, ipalEnvRules, IpalEnvFeatures, ipalT, ipalVar, IPAL_TOKENS,
  IPAL_BREAKPOINTS, IpalTokenName, IpalGlobalSlug are aliases of the iapl names
- env: IAPL_* is read first, IPAL_* as fallback (readEnv; EnvRule.aliases for assertEnv);
  the crawler service reads IPAL_CRAWLER_SITES too
- doctor: reads ipal-doctor.json when it is the only one, the old pointer block, ipal-audit-skip
  and old skill folders; new rule legacy-names (info) and storage-names (warning)
- iapl-skills: installs iapl-*, removes the 16 old folders by exact name, rewrites the old
  pointer block in place; a project's own ipal-* skill stays
- storageNames: 'ipal' (default) | 'iapl': the cookie the scanner declares and the 2FA counter's
  kv prefix; <PageStats visitorCookie>, IAPL_VISITOR_COOKIE, verifyLimit.keyPrefix
- test/legacyNames.test.ts: 24 tests for all of the above

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 17:31:13 +01:00
co-authored by Claude Fable 5.2
parent 0a4f001797
commit 425fac14fb
43 changed files with 683 additions and 82 deletions
+10 -2
View File
@@ -27,6 +27,13 @@ import { serverText } from '../../integrations/texts.js'
* Requests a browser marks as coming from another origin never get here (sameOrigin.ts).
*/
export type VerifyLimitOptions = {
/**
* First part of the count's key in `payload.kv`: `<keyPrefix>:2fa-verify:<collection>:<id>`.
* Default `'ipal'`, the key every site made before 1.17.0 has; `iapl({ storageNames: 'iapl' })`
* (new sites) makes it `'iapl'`. One prefix per site: changing it on a live site starts the
* running counts from zero once.
*/
keyPrefix?: string
/** Attempts per account in the window. Default 5. */
max?: number
/** Where the count is kept besides the instance's memory. Default `'database'`; `'memory'` = this instance only. */
@@ -47,7 +54,7 @@ type KV = { get: (key: string) => Promise<unknown>; set: (key: string, value: ob
let warnedStore = false
/** Key of an account's stored count in `payload.kv`. */
export const verifyLimitKey = (collection: string, id: string) => `ipal:2fa-verify:${collection}:${id}`
export const verifyLimitKey = (collection: string, id: string, keyPrefix = 'ipal') => `${keyPrefix}:2fa-verify:${collection}:${id}`
/** Counts one attempt in the database and returns the stored bucket; null when there is no store or it fails. */
async function hitStored(payload: { kv?: unknown; logger?: { warn: (m: string) => void } }, key: string, windowMs: number, now: number): Promise<Bucket | null> {
@@ -72,6 +79,7 @@ export function buildVerifyLimit(collection: string, options: VerifyLimitOptions
const max = Number.isFinite(options.max) && options.max! > 0 ? Math.floor(options.max!) : 5
const windowMs = (Number.isFinite(options.windowMinutes) && options.windowMinutes! > 0 ? options.windowMinutes! : 10) * 60_000
const stored = options.store !== 'memory'
const keyPrefix = typeof options.keyPrefix === 'string' && options.keyPrefix.trim() ? options.keyPrefix.trim() : 'ipal'
return async (req: PayloadRequest): Promise<null | Response> => {
// No session: the package refuses by itself, and there is no account to count against.
@@ -94,7 +102,7 @@ export function buildVerifyLimit(collection: string, options: VerifyLimitOptions
// This attempt's own number, taken now: the bucket is shared and keeps growing while we wait below.
const mine: Bucket = { count: bucket.count, resetAt: bucket.resetAt }
// …and in the database, so a restart or another instance does not start from zero. The higher count decides.
const kept = stored ? await hitStored(req.payload as never, verifyLimitKey(collection, id), windowMs, now) : null
const kept = stored ? await hitStored(req.payload as never, verifyLimitKey(collection, id, keyPrefix), windowMs, now) : null
const worst = kept && kept.count > mine.count ? kept : mine
if (worst.count <= max) {return null}
const retryAfter = Math.max(1, Math.ceil((worst.resetAt - now) / 1000))