Remove rate limiting from OAuth registration and MCP endpoints
Drop the per-IP registration limiter and the per-user MCP limiter along with the shared in-memory/Redis helper. Request throttling is an infrastructure concern (reverse proxy, CDN) rather than something each endpoint should reimplement.
This commit is contained in:
1 parent
d2b4aa174e
commit
162487eca1
7 files changed
-304
No files matched your search
@@ -1,70 +0,0 @@
|
||||
import { beforeEach, expect, test, vi } from 'vitest';
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
parseRequest: vi.fn(),
|
||||
createOauthClient: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/request', () => ({
|
||||
parseRequest: mocks.parseRequest,
|
||||
}));
|
||||
|
||||
vi.mock('@/queries/prisma/oauth', () => ({
|
||||
createOauthClient: mocks.createOauthClient,
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/crypto', () => ({
|
||||
uuid: () => 'client-id',
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/redis', () => ({
|
||||
default: { enabled: false, client: {} },
|
||||
}));
|
||||
|
||||
import { resetRateLimits } from '@/lib/rate-limit';
|
||||
import { POST } from './route';
|
||||
|
||||
const body = {
|
||||
client_name: 'Test client',
|
||||
redirect_uris: ['http://127.0.0.1:3000/callback'],
|
||||
};
|
||||
|
||||
function register(ip: string) {
|
||||
return POST(
|
||||
new Request('https://umami.example/api/oauth/register', {
|
||||
method: 'POST',
|
||||
headers: { 'x-forwarded-for': ip, 'content-type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
resetRateLimits();
|
||||
delete process.env.OAUTH_DISABLED;
|
||||
delete process.env.OAUTH_DISABLE_DCR;
|
||||
delete process.env.DISABLE_LOGIN;
|
||||
|
||||
mocks.parseRequest.mockReset();
|
||||
mocks.createOauthClient.mockReset();
|
||||
|
||||
mocks.parseRequest.mockResolvedValue({ body });
|
||||
mocks.createOauthClient.mockImplementation(async input => ({
|
||||
...input,
|
||||
createdAt: new Date(),
|
||||
}));
|
||||
});
|
||||
|
||||
test('limits registrations per source address without affecting other sources', async () => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
expect((await register('203.0.113.1')).status).toBe(201);
|
||||
}
|
||||
|
||||
const limited = await register('203.0.113.1');
|
||||
|
||||
expect(limited.status).toBe(429);
|
||||
await expect(limited.json()).resolves.toMatchObject({ error: 'temporarily_unavailable' });
|
||||
|
||||
expect((await register('203.0.113.2')).status).toBe(201);
|
||||
expect(mocks.createOauthClient).toHaveBeenCalledTimes(21);
|
||||
});
|
||||
@@ -1,18 +1,13 @@
|
||||
import { uuid } from '@/lib/crypto';
|
||||
import { getIpAddress } from '@/lib/ip';
|
||||
import { isDynamicRegistrationEnabled } from '@/lib/oauth/config';
|
||||
import { OAuthError, oauthErrorResponse, oauthJson } from '@/lib/oauth/errors';
|
||||
import { corsPreflight } from '@/lib/oauth/metadata';
|
||||
import { isAcceptableRedirectUri } from '@/lib/oauth/redirect';
|
||||
import { checkRateLimit } from '@/lib/rate-limit';
|
||||
import { parseRequest } from '@/lib/request';
|
||||
import { notFound } from '@/lib/response';
|
||||
import { createOauthClient } from '@/queries/prisma/oauth';
|
||||
import { clientRegistrationRequestSchema } from '../schema';
|
||||
|
||||
const REGISTRATIONS_PER_HOUR = 20;
|
||||
const REGISTRATION_WINDOW_SECONDS = 60 * 60;
|
||||
|
||||
export function OPTIONS() {
|
||||
return corsPreflight();
|
||||
}
|
||||
@@ -38,20 +33,6 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
try {
|
||||
// Budget is per source address so one requester cannot exhaust registration for every other
|
||||
// client. Enforced via Redis when available, otherwise a bounded per-key in-process window.
|
||||
const allowed = await checkRateLimit(
|
||||
`oauth:register:${getIpAddress(request.headers) ?? 'unknown'}`,
|
||||
REGISTRATIONS_PER_HOUR,
|
||||
REGISTRATION_WINDOW_SECONDS,
|
||||
);
|
||||
|
||||
if (!allowed) {
|
||||
throw new OAuthError('temporarily_unavailable', 'Too many registrations. Try again later.', {
|
||||
status: 429,
|
||||
});
|
||||
}
|
||||
|
||||
if (!body.redirect_uris.every(isAcceptableRedirectUri)) {
|
||||
throw new OAuthError(
|
||||
'invalid_redirect_uri',
|
||||
|
||||
Reference in new issue
Block a user