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
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { submitForm } from '../src/modules/forms/submitForm.js'
|
||||
import { UNKNOWN_IP_FACTOR } from '../src/modules/shared/rateLimit.js'
|
||||
import { fakePayload } from './helpers.js'
|
||||
|
||||
vi.mock('server-only', () => ({}))
|
||||
@@ -82,6 +83,19 @@ describe('submitForm — Turnstile', () => {
|
||||
expect(create).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
test('turnstileHostnames: a token solved on another host is rejected', async () => {
|
||||
mockSiteverify({ hostname: 'evil.example', success: true })
|
||||
const { create, payload } = fakePayload({ forms, integrations: keys })
|
||||
const base = { data, formId: 'contact', payload, turnstileToken: 'tok' }
|
||||
expect(await submitForm({ ...base, ip: nextIp(), turnstileHostnames: ['www.firma.pl'] })).toEqual({
|
||||
reason: 'turnstile',
|
||||
success: false,
|
||||
})
|
||||
expect(create).not.toHaveBeenCalled()
|
||||
mockSiteverify({ hostname: 'WWW.firma.pl', success: true })
|
||||
expect((await submitForm({ ...base, ip: nextIp(), turnstileHostnames: ['www.firma.pl'] })).success).toBe(true)
|
||||
})
|
||||
|
||||
test('configured + Cloudflare rejects OUR secret → allowed by default, refused with reject', async () => {
|
||||
mockSiteverify({ 'error-codes': ['invalid-input-secret'], success: false })
|
||||
const { payload } = fakePayload({ forms, integrations: keys })
|
||||
@@ -97,6 +111,15 @@ describe('submitForm — Turnstile', () => {
|
||||
describe('submitForm — other gates', () => {
|
||||
afterEach(() => vi.restoreAllMocks())
|
||||
|
||||
test('no client IP is not a free pass — it shares one capped bucket', async () => {
|
||||
const { payload } = fakePayload({ forms })
|
||||
let accepted = 0
|
||||
for (let i = 0; i < 50; i++) {
|
||||
if ((await submitForm({ data, formId: 'contact', maxPerMinute: 1, payload })).success) {accepted++}
|
||||
}
|
||||
expect(accepted).toBe(UNKNOWN_IP_FACTOR)
|
||||
})
|
||||
|
||||
test('rate limit per IP', async () => {
|
||||
const { payload } = fakePayload({ forms })
|
||||
const ip = nextIp()
|
||||
|
||||
Reference in New Issue
Block a user