From 910fbd75cf518ae1a974917f7a4ec089f9324293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Tue, 1 Dec 2026 10:52:25 +0100 Subject: [PATCH] IPv6 grouping without node:net (the limiter also reaches non-Node bundles; webpack build); reference index entry; export order Co-Authored-By: Claude Opus 5.5 --- docs/agent/reference-site/INDEX.md | 1 + scripts/reference-index.mjs | 1 + src/index.ts | 8 ++++---- src/modules/shared/rateLimit.ts | 30 ++++++++++++++++++------------ test/rateLimitIpv6.test.ts | 3 +++ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/docs/agent/reference-site/INDEX.md b/docs/agent/reference-site/INDEX.md index 4b5895c..b564f34 100644 --- a/docs/agent/reference-site/INDEX.md +++ b/docs/agent/reference-site/INDEX.md @@ -1175,6 +1175,7 @@ Rules: AGENT.md §8a. | `e2e/mail-relay.spec.ts` | the mail service on a built site: the state block in the panel, the first e-mail through the public confirm route, no token in anything the browser receives | | `e2e/routes.snapshot.json` | every public route of the site (REST, GraphQL, Next route files) with one line saying who may use it and why — the judged list `routes:check` compares with | | `e2e/routes.ts` | `routes:check`: the site's public routes against e2e/routes.snapshot.json (`pnpm payload run e2e/routes.ts [update]`; from the repository `pnpm routes:check`) (below) | +| `e2e/sign-in-answers.spec.ts` | built-site test (pnpm e2e:built, 1.17.2): a locked staff account gets the wrong-password answer over REST and GraphQL (accounts.lockedAnswer); the plugin's panel actions refuse another origin and take the site's own (adminSameOrigin) | | `e2e/sign-in-look.spec.ts` | browser test of the sign-in look (whiteLabel.signIn): the panel comes with the form, sits above it on a phone in both themes, is on forgot / reset / logout too, the language links work, the 2FA code screen keeps its boxes (now named) | | `e2e/smoke.spec.ts` | the smoke tests every release must pass: pages, showcase, forms (also a file sent by e-mail), sitemap, SEO/GEO, headers, consent, 2FA | | `e2e/smtp-sink.ts` | a local mail sink for the smoke tests: reads what the site really sent (recipients, attachments) | diff --git a/scripts/reference-index.mjs b/scripts/reference-index.mjs index 67cb639..6866ced 100644 --- a/scripts/reference-index.mjs +++ b/scripts/reference-index.mjs @@ -71,6 +71,7 @@ export const DESCRIPTIONS = { 'e2e/mail-relay-setup.ts': 'for the mail-service spec: empties the SMTP fields and the stored token, so the next e-mail enrols and goes through the mail service (local database only)', 'e2e/mail-relay.spec.ts': 'the mail service on a built site: the state block in the panel, the first e-mail through the public confirm route, no token in anything the browser receives', 'e2e/smtp-sink.ts': 'a local mail sink for the smoke tests: reads what the site really sent (recipients, attachments)', + 'e2e/sign-in-answers.spec.ts': 'built-site test (pnpm e2e:built, 1.17.2): a locked staff account gets the wrong-password answer over REST and GraphQL (accounts.lockedAnswer); the plugin\'s panel actions refuse another origin and take the site\'s own (adminSameOrigin)', 'e2e/sign-in-look.spec.ts': 'browser test of the sign-in look (whiteLabel.signIn): the panel comes with the form, sits above it on a phone in both themes, is on forgot / reset / logout too, the language links work, the 2FA code screen keeps its boxes (now named)', 'e2e/two-factor-back.spec.ts': 'browser test of the 2FA screens\' `back` link (twoFactor.safeBack): a prepared link — script or another host — ends on the admin root, an ordinary one is followed', 'e2e/routes.snapshot.json': 'every public route of the site (REST, GraphQL, Next route files) with one line saying who may use it and why — the judged list `routes:check` compares with', diff --git a/src/index.ts b/src/index.ts index dd5b336..9ad6a5e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,15 +22,13 @@ export { staffCollectionSlugs, userMeetsRole, } from './modules/access/index.js' +export { graphqlLoginName, lockedLooksWrong, withLockedAnswer } from './modules/access/lockedAnswer.js' +export type { LockedAnswerOption } from './modules/access/lockedAnswer.js' export { applyPanelRole, countBelowPanelRole, DEFAULT_PANEL_ROLE, panelAccess, resolvePanelRole } from './modules/access/panelRole.js' export { applyPasswordRule, buildPasswordRuleHook, checkPassword, DEFAULT_PASSWORD_MIN_LENGTH, resolvePasswordRule } from './modules/access/passwordRule.js' 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 { ADMIN_POST_ENDPOINTS, guardAdminEndpoints, requireOwnOrigin } from './modules/security/adminSameOrigin.js' -export type { AdminSameOriginOption } from './modules/security/adminSameOrigin.js' -export type { LockedAnswerOption } from './modules/access/lockedAnswer.js' export { adoptLateAuthCollections, applyFirstUserRoute, @@ -458,6 +456,8 @@ export { writeOnlySecrets, } from './modules/secrets/writeOnly.js' export type { WriteOnlySecretOptions } from './modules/secrets/writeOnly.js' +export { ADMIN_POST_ENDPOINTS, guardAdminEndpoints, requireOwnOrigin } from './modules/security/adminSameOrigin.js' +export type { AdminSameOriginOption } from './modules/security/adminSameOrigin.js' export { adminClientHintSources, buildAdminCsp, diff --git a/src/modules/shared/rateLimit.ts b/src/modules/shared/rateLimit.ts index 091d412..efa527e 100644 --- a/src/modules/shared/rateLimit.ts +++ b/src/modules/shared/rateLimit.ts @@ -1,5 +1,3 @@ -import { isIPv4, isIPv6 } from 'node:net' - import { readEnv } from './envName.js' type Bucket = { count: number; resetAt: number } @@ -131,22 +129,30 @@ export function setIpv6Grouping(next: Ipv6Grouping = {}): void { ipv6 = { prefix, wide, wideFactor } } -/** The 8 groups of an IPv6 address as numbers, or null. */ +/** A dotted IPv4 address (no `node:net`: this module also reaches bundles that are not Node's). */ +function isIPv4(value: string): boolean { + const parts = value.split('.') + return parts.length === 4 && parts.every((p) => /^\d{1,3}$/.test(p) && Number(p) <= 255) +} + +/** The 8 groups of an IPv6 address as numbers (brackets and a zone id allowed), or null when it is not one. */ function ipv6Groups(address: string): null | number[] { - const bare = address.replace(/^\[|\]$/g, '').split('%')[0] - if (!isIPv6(bare)) {return null} - let text = bare - const v4 = /(\d+\.\d+\.\d+\.\d+)$/.exec(text) + let text = address.replace(/^\[|\]$/g, '').split('%')[0] + if (!text.includes(':') || !/^[0-9a-f:.]+$/i.test(text) || text.split('::').length > 2) {return null} + const v4 = /(?:^|:)(\d+\.\d+\.\d+\.\d+)$/.exec(text) if (v4) { + if (!isIPv4(v4[1])) {return null} const [a, b, c, d] = v4[1].split('.').map(Number) text = text.slice(0, -v4[1].length) + `${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}` - } - const [head, tail] = text.includes('::') ? text.split('::') : [text, undefined] + } else if (text.includes('.')) {return null} + const compressed = text.includes('::') + const [head, tail = ''] = compressed ? text.split('::') : [text] const left = head ? head.split(':') : [] const right = tail ? tail.split(':') : [] - const fill = tail === undefined ? [] : Array(8 - left.length - right.length).fill('0') - const groups = [...left, ...fill, ...right].map((g) => parseInt(g, 16)) - return groups.length === 8 && groups.every((g) => Number.isInteger(g) && g >= 0 && g <= 0xffff) ? groups : null + if ([...left, ...right].some((g) => !/^[0-9a-f]{1,4}$/i.test(g))) {return null} + const missing = 8 - left.length - right.length + if (compressed ? missing < 1 : missing !== 0) {return null} + return [...left, ...Array(compressed ? missing : 0).fill('0'), ...right].map((g) => parseInt(g, 16)) } /** `2001:db8:1:2::/64` for any address of that network (`bits` = prefix length). */ diff --git a/test/rateLimitIpv6.test.ts b/test/rateLimitIpv6.test.ts index 1bd0ce2..d26e4ae 100644 --- a/test/rateLimitIpv6.test.ts +++ b/test/rateLimitIpv6.test.ts @@ -32,6 +32,9 @@ describe('ipRateKey', () => { expect(ipRateKey('::ffff:c633:6407')).toBe('198.51.100.7') expect(ipRateKey('')).toBe('') expect(ipRateKey('not-an-ip')).toBe('not-an-ip') + for (const bad of ['1::2::3', '2001:db8:1:2:3:4:5:6:7', '12345::1', '::ffff:300.1.1.1', 'to:abcdef', 'a:b']) {expect([bad, ipRateKey(bad)]).toEqual([bad, bad])} + expect(ipRateKey('::')).toBe('0:0:0:0:0:0:0:0/64') + expect(ipRateKey('256.1.1.1')).toBe('256.1.1.1') }) test('setIpv6Grouping: per address again, another prefix; nonsense is refused', () => {