Rate limits per user, and coalesce live-event reloads (#10)
CI / Lint (eslint) (pull_request) Successful in 23s
CI / Test (jest) (pull_request) Successful in 23s
CI / Security (npm audit) (pull_request) Successful in 1m17s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m42s
CI / Integration (api) (pull_request) Successful in 4m23s
CI / UI (playwright) (pull_request) Successful in 2m35s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m13s
CI / Lint (eslint) (pull_request) Successful in 23s
CI / Test (jest) (pull_request) Successful in 23s
CI / Security (npm audit) (pull_request) Successful in 1m17s
CI / Scan (secrets, workflows, shell) (pull_request) Successful in 1m42s
CI / Integration (api) (pull_request) Successful in 4m23s
CI / UI (playwright) (pull_request) Successful in 2m35s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m13s
Production refused 1,052 of the UI's requests with 429 in one day: every user arrives through the reverse proxy, so the per-IP limiter put the whole team in one bucket, and each open page reloaded its whole list on every live event (about two per Gandalf command). - lib/ratelimit.js keys both limiters: a trusted SSO identity gets user:<name>, a request with the valid Gandalf key gets 'gandalf' (an invalid key falls back to the IP, so nobody can drain Gandalf's bucket), everything else the IP. The API default goes from 300 to 1000 per 15 min per bucket. - Pulse.util.throttle: live-event reloads on Executions, Dashboard and Workers run at most once per 2 s (at once when idle, then once at the end of the window). User actions still reload immediately. - Unit tests for the keys. No visual change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a01c28e06
commit
6ffe230c34
7 files changed
+98
-9
No files matched your search
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
process.env.TRUSTED_PROXIES = '192.0.2.10';
|
||||
const { rateLimitKey } = require('../lib/ratelimit');
|
||||
|
||||
const PROXY = '192.0.2.10';
|
||||
const req = (remoteAddress, headers = {}) => ({ headers, ip: remoteAddress, socket: { remoteAddress } });
|
||||
const env = { GANDALF_API_KEY: 'g-key' };
|
||||
|
||||
beforeAll(() => jest.spyOn(console, 'warn').mockImplementation(() => {}));
|
||||
afterAll(() => jest.restoreAllMocks());
|
||||
|
||||
test('each signed-in user behind the proxy gets their own bucket', () => {
|
||||
expect(rateLimitKey(req(PROXY, { 'remote-user': 'alice' }), env)).toBe('user:alice');
|
||||
expect(rateLimitKey(req(PROXY, { 'remote-user': 'bob' }), env)).toBe('user:bob');
|
||||
});
|
||||
|
||||
test('identity headers from anywhere but the proxy are ignored (keyed by IP)', () => {
|
||||
expect(rateLimitKey(req('198.51.100.7', { 'remote-user': 'alice' }), env)).toBe('ip:198.51.100.7');
|
||||
});
|
||||
|
||||
test('Gandalf gets its own bucket only with the valid key', () => {
|
||||
expect(rateLimitKey(req('10.10.10.61', { 'x-gandalf-api-key': 'g-key' }), env)).toBe('gandalf');
|
||||
expect(rateLimitKey(req('10.10.10.61', { 'x-gandalf-api-key': 'wrong' }), env)).toBe('ip:10.10.10.61');
|
||||
expect(rateLimitKey(req('10.10.10.61', { 'x-gandalf-api-key': 'g-key' }), {})).toBe('ip:10.10.10.61');
|
||||
});
|
||||
|
||||
test('anything else is keyed by IP (IPv6 grouped by subnet)', () => {
|
||||
expect(rateLimitKey(req('10.10.10.8'), env)).toBe('ip:10.10.10.8');
|
||||
expect(rateLimitKey(req('2001:db8::1'), env)).toBe(rateLimitKey(req('2001:db8::2'), env));
|
||||
});
|
||||
Reference in new issue
Block a user