Skip to content

Commit aab314e

Browse files
authored
refactor(proxy): move blocked client check from FIM route to middleware (#1778)
The isFimClientBlocked guard was only blocking buggy client versions on the FIM endpoint. Move this to the Next.js proxy middleware so blocked clients are rejected before hitting any route.
1 parent 6b98ef6 commit aab314e

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/app/api/fim/completions/route.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
extractFraudAndProjectHeaders,
1616
invalidRequestResponse,
1717
temporarilyUnavailableResponse,
18-
upgradeRequiredResponse,
1918
wrapInSafeNextResponse,
2019
captureProxyError,
2120
extractHeaderAndLimitLength,
@@ -30,18 +29,6 @@ const MISTRAL_FIM_URL = 'https://api.mistral.ai/v1/fim/completions';
3029
const INCEPTION_FIM_URL = 'https://api.inceptionlabs.ai/v1/fim/completions';
3130
const FIM_MAX_TOKENS_LIMIT = 1000;
3231

33-
// These client versions had a bug that caused excessive FIM endpoint requests.
34-
// Block them and require users to upgrade.
35-
const BLOCKED_FIM_USER_AGENT_REGEX = /^kilo\/7\.0\.[0-9]+$/;
36-
const BLOCKED_FIM_USER_AGENTS = ['kilo/7.1.0', 'kilo/7.1.1', 'kilo/7.1.2', 'kilo/7.1.3'];
37-
38-
function isFimClientBlocked(userAgent: string | null): boolean {
39-
if (!userAgent) return false;
40-
return (
41-
BLOCKED_FIM_USER_AGENT_REGEX.test(userAgent) || BLOCKED_FIM_USER_AGENTS.includes(userAgent)
42-
);
43-
}
44-
4532
type FimProvider = 'mistral' | 'inception';
4633

4734
function resolveFimProvider(model: string): {
@@ -89,10 +76,6 @@ const FIMRequestBody = z.object({
8976
type FIMRequestBody = z.infer<typeof FIMRequestBody>;
9077

9178
export async function POST(request: NextRequest) {
92-
if (isFimClientBlocked(request.headers.get('user-agent'))) {
93-
return upgradeRequiredResponse();
94-
}
95-
9679
const requestStartedAt = performance.now();
9780
const requesBodyTextPromise = request.text();
9881

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { type NextFetchEvent, NextResponse } from 'next/server';
2+
import type { MiddlewareFactory } from '@/middleware/types';
3+
import type { NextMiddlewareWithAuth, NextRequestWithAuth } from 'next-auth/middleware';
4+
5+
// These client versions had a bug that caused excessive requests.
6+
// Block them at the middleware level so they never reach the app.
7+
const BLOCKED_USER_AGENT_REGEX = /^kilo\/7\.0\.[0-9]+$/;
8+
const BLOCKED_USER_AGENTS = new Set(['kilo/7.1.0', 'kilo/7.1.1', 'kilo/7.1.2', 'kilo/7.1.3']);
9+
10+
function isClientBlocked(userAgent: string | null): boolean {
11+
if (!userAgent) return false;
12+
return BLOCKED_USER_AGENT_REGEX.test(userAgent) || BLOCKED_USER_AGENTS.has(userAgent);
13+
}
14+
15+
export const withBlockedClients: MiddlewareFactory = (nextMiddleware: NextMiddlewareWithAuth) => {
16+
return async (request: NextRequestWithAuth, nextFetchEvent: NextFetchEvent) => {
17+
if (isClientBlocked(request.headers.get('user-agent'))) {
18+
return NextResponse.json(
19+
{
20+
error: 'upgrade_required',
21+
message: 'Please upgrade your Kilo extension to the latest version.',
22+
},
23+
{ status: 426 }
24+
);
25+
}
26+
return nextMiddleware(request, nextFetchEvent);
27+
};
28+
};

src/proxy.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { NextRequestWithAuth } from 'next-auth/middleware';
22
import { NextResponse } from 'next/server';
33
import { withAuthenticatedAdminApiRoutes } from './middleware/withAuthenticatedAdminApiRoutes';
4+
import { withBlockedClients } from './middleware/withBlockedClients';
45
import { withKiloEditorCookie } from './middleware/withKiloEditorCookie';
56

67
function baseProxy(request: NextRequestWithAuth) {
@@ -13,7 +14,9 @@ function baseProxy(request: NextRequestWithAuth) {
1314
});
1415
}
1516

16-
export const proxy = withAuthenticatedAdminApiRoutes(withKiloEditorCookie(baseProxy));
17+
export const proxy = withBlockedClients(
18+
withAuthenticatedAdminApiRoutes(withKiloEditorCookie(baseProxy))
19+
);
1720

1821
export const config = {
1922
/*

0 commit comments

Comments
 (0)