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 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0f67a6884c
commit
910fbd75cf
+4
-4
@@ -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,
|
||||
|
||||
@@ -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). */
|
||||
|
||||
Reference in New Issue
Block a user