Files
limitkit/test/store-http.test.js
T
75adef1e99 fix(http)!: key on the hop the proxy wrote, not the one the client sent (#2)
clientIp returned the FIRST x-forwarded-for hop, with a comment asserting that
was "what the proxy saw" and a test pinning it. Both were wrong in the same
direction.

A reverse proxy APPENDS to X-Forwarded-For. Caddy and nginx both do. So for a
request through one proxy the header reads `<whatever the client sent>, <what
the proxy actually saw>`, and the only entry a client cannot forge is the LAST.

The effect, in every adopter at once: send a random X-Forwarded-For with each
request and every request lands in a fresh bucket, so no bucket ever fills.
Not "weakened" — bypassed completely. A limiter that cannot be tripped is not a
limiter, and it is worse than none, because the route reads as protected.

Found in orangecat's payments audit (bitbaum/orangecat#563, finding 2), where
the same mistake is copied across three payment routes that each mint a real
Lightning invoice per request.

Now counts from the right. `trustedProxies` (default 1) says how many proxies
of your own sit in front; a CDN in front of Caddy is 2 and moves the answer one
further left. `trustedProxies: 0` means the server is exposed directly, so every
forwarded header is client-written and none is evidence — the result is
"unknown" rather than a number that looks like proof.

Verified against the fleet's actual topology rather than assumed: DNS resolves
straight to the box, no CDN, one Caddy 2.11.4 with no trusted_proxies
configured, so exactly one hop is appended.

BREAKING: the returned identity changes behind a proxy — which is the point.
v0.2.0. Adopters pinning #v0.1.0 keep the bypass until they move.


Claude-Session: https://claude.ai/code/session_018waGt1ieA9TjpscqrbrnGb

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 19:51:09 +02:00

89 lines
4.1 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { MemoryStore, slidingWindow, toHeaders, clientIp } from 'limitkit';
test('THE STORE IS BOUNDED — stranger keys cannot grow the process forever', () => {
// The bug this makes impossible: one replaced implementation kept a bare
// Map keyed by client IP with no eviction, so every stranger who ever hit
// the endpoint left an entry until the process died. Bounded means the
// worst case is a very old bucket forgetting, never a leak.
const store = new MemoryStore(100);
for (let i = 0; i < 1_000; i++) {
store.set(`ip:${i}`, { hits: [1] });
}
assert.ok(store.size <= 100, `store grew to ${store.size}`);
});
test('eviction is by recency of use, not insertion order', () => {
const store = new MemoryStore(2);
store.set('a', { hits: [1] });
store.set('b', { hits: [1] });
store.get('a'); // touch a — b becomes the eviction candidate
store.set('c', { hits: [1] });
assert.ok(store.get('a'), 'recently-used key evicted');
assert.equal(store.get('b'), undefined, 'least-recently-used key kept');
});
test('an emptied window releases its key entirely', () => {
const store = new MemoryStore();
store.set('k', { hits: [1, 2] });
store.set('k', { hits: [] });
assert.equal(store.size, 0, 'empty state must free the slot, not squat on it');
});
test('toHeaders emits the standard trio, Retry-After only on refusal', () => {
const rl = slidingWindow({ limit: 1, windowMs: 60_000 });
const ok = toHeaders(rl.check('k', 1_000_000));
assert.equal(ok['X-RateLimit-Limit'], '1');
assert.equal(ok['X-RateLimit-Remaining'], '0');
assert.equal(ok['Retry-After'], undefined, 'an allowed response must not tell anyone to wait');
const no = toHeaders(rl.check('k', 1_000_001));
assert.equal(no['Retry-After'], '60');
assert.equal(no['X-RateLimit-Reset'], String(Math.ceil((1_000_000 + 60_000) / 1000)));
});
const h = (map) => ({ get: (k) => map[k.toLowerCase()] ?? null });
test('clientIp: the hop the PROXY wrote, not the one the client sent', () => {
// A proxy APPENDS. So this header reads "<client-supplied>, <what Caddy saw>"
// and only the last entry is unforgeable. The previous version returned
// 9.9.9.9 here — the attacker's own value — and a test pinned it.
assert.equal(clientIp(h({ 'x-forwarded-for': '9.9.9.9, 10.0.0.1' })), '10.0.0.1');
// The bypass this closes: vary the header per request and every request is a
// new bucket, so no bucket ever fills. Same real client, one key.
const keys = new Set(
['1.1.1.1', '2.2.2.2', '3.3.3.3'].map((spoof) =>
clientIp(h({ 'x-forwarded-for': `${spoof}, 203.0.113.7` })),
),
);
assert.deepEqual([...keys], ['203.0.113.7'], 'a spoofed prefix must not mint new buckets');
// A single hop is the normal case: nothing was forged, the proxy wrote it.
assert.equal(clientIp(h({ 'x-forwarded-for': '203.0.113.7' })), '203.0.113.7');
});
test('clientIp: trustedProxies moves the hop, and 0 believes nothing', () => {
// Two of our own proxies (a CDN in front of Caddy) — the answer is one further left.
assert.equal(
clientIp(h({ 'x-forwarded-for': 'evil, 203.0.113.7, 10.0.0.1' }), { trustedProxies: 2 }),
'203.0.113.7',
);
// Directly exposed: every forwarded header is written by the client, so none
// is evidence. "unknown" is honest; a number here would look like proof.
assert.equal(clientIp(h({ 'x-forwarded-for': 'evil' }), { trustedProxies: 0 }), 'unknown');
// Fewer hops than configured — still the entry our own proxy wrote.
assert.equal(clientIp(h({ 'x-forwarded-for': '203.0.113.7' }), { trustedProxies: 3 }), '203.0.113.7');
});
test('clientIp: absence degrades to a shared bucket, never a throw', () => {
assert.equal(clientIp(h({ 'x-real-ip': '8.8.8.8' })), '8.8.8.8');
// "unknown" throttles anonymous traffic COLLECTIVELY — the right failure
// mode for abuse-blunting, and it must never throw.
assert.equal(clientIp(h({})), 'unknown');
assert.equal(clientIp(h({ 'x-forwarded-for': '' })), 'unknown', 'an empty header is not an identity');
assert.equal(clientIp(h({ 'x-forwarded-for': ' , , ' })), 'unknown', 'separators alone are not an identity');
});