Rate limit: no IP shares a capped bucket; optional Turnstile hostname check
A request without a usable client IP used to skip every limiter. It now counts against one shared 'unknown-ip' bucket at 20x the limit. submitForm, createSubmitAction and verifyTurnstile accept turnstileHostnames/hostnames to reject tokens solved on another domain. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
10da62ea4c
commit
8e10232765
@@ -69,6 +69,20 @@ export type RateLimitArgs = {
|
||||
|
||||
type CheckArgs = { cost?: number } & RateLimitArgs
|
||||
|
||||
/**
|
||||
* Requests with no usable client IP (a proxy that strips the header, a
|
||||
* misconfigured IPAL_IP_HEADER) share one bucket with a roomier limit. Letting
|
||||
* them through unlimited meant a client that simply omits the header walked
|
||||
* past every limiter; one shared bucket keeps real visitors flowing while
|
||||
* capping the flood.
|
||||
*/
|
||||
export const UNKNOWN_IP_KEY = 'unknown-ip'
|
||||
export const UNKNOWN_IP_FACTOR = 20
|
||||
|
||||
function resolveKey(key: string, max: number): { key: string; max: number } {
|
||||
return key ? { key, max } : { key: UNKNOWN_IP_KEY, max: max * UNKNOWN_IP_FACTOR }
|
||||
}
|
||||
|
||||
function hitMemory(id: string, cost: number, windowMs: number): Bucket {
|
||||
const now = Date.now()
|
||||
sweep(now)
|
||||
@@ -87,11 +101,11 @@ function hitMemory(id: string, cost: number, windowMs: number): Bucket {
|
||||
* backwards compatibility; the plugin's own routes use consumeRateLimit.
|
||||
*
|
||||
* Returns true when the request is within the limit, false when it should be
|
||||
* rejected. A missing key (no IP) is allowed through — better to accept a
|
||||
* submission than to block everyone behind a proxy that strips the header.
|
||||
* rejected. A missing key (no IP) counts against one shared bucket with
|
||||
* UNKNOWN_IP_FACTOR × max.
|
||||
*/
|
||||
export function checkRateLimit({ cost = 1, key, max = 5, scope = 'form', windowMs = 60_000 }: CheckArgs): boolean {
|
||||
if (!key) {return true}
|
||||
export function checkRateLimit({ cost = 1, key: rawKey, max: rawMax = 5, scope = 'form', windowMs = 60_000 }: CheckArgs): boolean {
|
||||
const { key, max } = resolveKey(rawKey, rawMax)
|
||||
const id = `${scope}:${key}`
|
||||
const current = buckets.get(id)
|
||||
const used = current && current.resetAt > Date.now() ? current.count : 0
|
||||
@@ -108,12 +122,12 @@ export function checkRateLimit({ cost = 1, key, max = 5, scope = 'form', windowM
|
||||
*/
|
||||
export async function consumeRateLimit({
|
||||
cost = 1,
|
||||
key,
|
||||
max = 5,
|
||||
key: rawKey,
|
||||
max: rawMax = 5,
|
||||
scope = 'form',
|
||||
windowMs = 60_000,
|
||||
}: CheckArgs): Promise<{ ok: boolean; retryAfter: number }> {
|
||||
if (!key) {return { ok: true, retryAfter: 0 }}
|
||||
const { key, max } = resolveKey(rawKey, rawMax)
|
||||
const id = `${scope}:${key}`
|
||||
|
||||
let bucket: Bucket
|
||||
@@ -164,8 +178,8 @@ export type ClientIpOptions = {
|
||||
* and walk past the rate limit; each proxy appends the address it saw, so the
|
||||
* rightmost entries are the trustworthy ones.
|
||||
*
|
||||
* Empty string when nothing usable is present — the limiter then lets the
|
||||
* request through.
|
||||
* Empty string when nothing usable is present — the limiter then counts it in
|
||||
* the shared UNKNOWN_IP_KEY bucket.
|
||||
*/
|
||||
let warnedUnpinned = false
|
||||
|
||||
|
||||
Reference in New Issue
Block a user