mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 10:58:10 +00:00
94 lines
3.2 KiB
TypeScript
94 lines
3.2 KiB
TypeScript
import { PUSH_LIMITS } from '@orca-cloud/push-contract'
|
|
import { Hono } from 'hono'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { ClientIpRateLimiter, clientIpRateLimit } from './client-ip-rate-limit.js'
|
|
|
|
const CAPACITY = PUSH_LIMITS.unauthenticatedRequestsPerMinutePerIp
|
|
|
|
function limiterApp(limiter: ClientIpRateLimiter, trustedProxyHops = 0): Hono {
|
|
const app = new Hono()
|
|
app.post('/probe', clientIpRateLimit(limiter, { trustedProxyHops }), (context) =>
|
|
context.json({ ok: true })
|
|
)
|
|
return app
|
|
}
|
|
|
|
describe('client ip rate limiter', () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('buckets on the last forwarded hop, the only one the platform appended', async () => {
|
|
const app = limiterApp(new ClientIpRateLimiter({ now: () => 1_000 }))
|
|
for (let index = 0; index < CAPACITY; index++) {
|
|
await app.request('/probe', {
|
|
method: 'POST',
|
|
headers: { 'x-forwarded-for': `10.0.0.${index}, 203.0.113.7` }
|
|
})
|
|
}
|
|
const sameClient = await app.request('/probe', {
|
|
method: 'POST',
|
|
headers: { 'x-forwarded-for': '10.9.9.9, 203.0.113.7' }
|
|
})
|
|
expect(sameClient.status).toBe(429)
|
|
const otherClient = await app.request('/probe', {
|
|
method: 'POST',
|
|
headers: { 'x-forwarded-for': '10.0.0.1, 198.51.100.9' }
|
|
})
|
|
expect(otherClient.status).toBe(200)
|
|
})
|
|
|
|
it('skips the configured trusted proxies when counting from the right', async () => {
|
|
const app = limiterApp(new ClientIpRateLimiter({ now: () => 1_000, capacity: 1 }), 1)
|
|
// <client>, <cloud run>, <load balancer>: one trusted hop after the client.
|
|
const headers = { 'x-forwarded-for': '203.0.113.7, 10.0.0.1' }
|
|
expect((await app.request('/probe', { method: 'POST', headers })).status).toBe(200)
|
|
expect((await app.request('/probe', { method: 'POST', headers })).status).toBe(429)
|
|
expect(
|
|
(
|
|
await app.request('/probe', {
|
|
method: 'POST',
|
|
headers: { 'x-forwarded-for': '198.51.100.9, 10.0.0.1' }
|
|
})
|
|
).status
|
|
).toBe(200)
|
|
})
|
|
|
|
it('trusts nothing when the header is shorter than the configured depth', async () => {
|
|
const app = limiterApp(new ClientIpRateLimiter({ now: () => 1_000, capacity: 1 }), 1)
|
|
// Only one hop, so the client value the depth points at does not exist.
|
|
const headers = { 'x-forwarded-for': '203.0.113.7' }
|
|
expect((await app.request('/probe', { method: 'POST', headers })).status).toBe(200)
|
|
expect(
|
|
(
|
|
await app.request('/probe', {
|
|
method: 'POST',
|
|
headers: { 'x-forwarded-for': '198.51.100.9' }
|
|
})
|
|
).status
|
|
).toBe(429)
|
|
})
|
|
|
|
it('ignores spoofable x-real-ip and uses a single shared bucket', async () => {
|
|
const app = limiterApp(new ClientIpRateLimiter({ now: () => 1_000, capacity: 1 }))
|
|
expect(
|
|
(await app.request('/probe', { method: 'POST', headers: { 'x-real-ip': '198.51.100.9' } }))
|
|
.status
|
|
).toBe(200)
|
|
expect(
|
|
(await app.request('/probe', { method: 'POST', headers: { 'x-real-ip': '203.0.113.7' } }))
|
|
.status
|
|
).toBe(429)
|
|
expect((await app.request('/probe', { method: 'POST' })).status).toBe(429)
|
|
expect((await app.request('/probe', { method: 'POST' })).status).toBe(429)
|
|
})
|
|
})
|