Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { timingSafeEqual } from '@kilocode/encryption';
import { extractBearerToken } from '@kilocode/worker-utils/extract-bearer-token';
import {
AUTO_DECIDER_MAX_COST_USD,
AUTO_DECIDER_MIN_COST_USD,
listAutoRoutingDeciderCandidates,
} from '@/lib/model-stats/auto-routing-decider-candidates';
import { INTERNAL_API_SECRET } from '@/lib/config.server';

function extractBearerToken(authHeader: string | null): string | null {
if (!authHeader) return null;
const trimmed = authHeader.trim();
if (trimmed.slice(0, 7).toLowerCase() !== 'bearer ') return null;
return trimmed.slice(7).trim() || null;
}

function parseCostBound(value: string | null, fallback: number): number {
if (value === null) return fallback;
const parsed = Number(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { timingSafeEqual } from '@kilocode/encryption';
import { extractBearerToken } from '@kilocode/worker-utils/extract-bearer-token';
import { z } from 'zod';
import { and, eq } from 'drizzle-orm';
import { kilocode_users, organization_memberships } from '@kilocode/db/schema';
Expand All @@ -38,16 +39,6 @@ const RequestSchema = z.object({

const SIX_HOURS_IN_SECONDS = 6 * 60 * 60;

// Inline bearer extraction (case-insensitive prefix, RFC 6750 §2.1). Kept local
// to avoid importing @kilocode/worker-utils, whose transitive `jose` ESM import
// breaks under jest's CJS transform.
function extractBearerToken(authHeader: string | null): string | null {
if (!authHeader) return null;
const trimmed = authHeader.trim();
if (trimmed.slice(0, 7).toLowerCase() !== 'bearer ') return null;
return trimmed.slice(7).trim() || null;
}

export async function POST(req: NextRequest) {
const token = extractBearerToken(req.headers.get('authorization'));
if (!INTERNAL_API_SECRET || !token || !timingSafeEqual(token, INTERNAL_API_SECRET)) {
Expand Down
6 changes: 2 additions & 4 deletions apps/web/src/lib/mcp-gateway/http.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'server-only';
import { NextResponse } from 'next/server';
import { GatewayError } from '@kilocode/mcp-gateway';
import { extractBearerToken as extractBearerTokenFromHeader } from '@kilocode/worker-utils/extract-bearer-token';

export function gatewayErrorResponse(error: unknown) {
if (error instanceof GatewayError) {
Expand All @@ -16,8 +17,5 @@ export function gatewayErrorResponse(error: unknown) {
}

export function extractBearerToken(headers: Headers): string | null {
const authorization = headers.get('authorization');
if (!authorization?.toLowerCase().startsWith('bearer ')) return null;
const token = authorization.slice(7).trim();
return token.length > 0 ? token : null;
return extractBearerTokenFromHeader(headers.get('authorization'));
}
1 change: 1 addition & 0 deletions packages/worker-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"./cloud-agent-next-client": "./src/cloud-agent-next-client.ts",
"./cloud-agent-session-access": "./src/cloud-agent-session-access.ts",
"./kilo-model-id": "./src/kilo-model-id.ts",
"./extract-bearer-token": "./src/extract-bearer-token.ts",
"./cloud-agent-queue-report": "./src/cloud-agent-queue-report.ts",
"./cloud-agent-failure": "./src/cloud-agent-failure.ts",
"./security-auto-analysis-policy": "./src/security-auto-analysis-policy.ts",
Expand Down
14 changes: 13 additions & 1 deletion services/db-proxy/src/utils/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ describe('auth utilities', () => {

const token = extractBearerToken(c);

expect(token).toBe('');
expect(token).toBeNull();
});

it('extracts token case-insensitively per RFC 6750', () => {
const c = createMockContext();
(c.req.header as jest.Mock).mockImplementation((name: string) => {
if (name === 'Authorization') return 'bearer my-token-123';
return undefined;
});

const token = extractBearerToken(c);

expect(token).toBe('my-token-123');
});
});

Expand Down
7 changes: 2 additions & 5 deletions services/db-proxy/src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import type { Context } from 'hono';
import type { ContentfulStatusCode } from 'hono/utils/http-status';
import { timingSafeEqual } from '@kilocode/encryption';
import { extractBearerToken as extractBearerTokenFromHeader } from '@kilocode/worker-utils/extract-bearer-token';
import type { Env, ErrorCode } from '../types';

/**
* Extract bearer token from Authorization header
*/
export function extractBearerToken(c: Context<{ Bindings: Env }>): string | null {
const authHeader = c.req.header('Authorization');
if (!authHeader?.startsWith('Bearer ')) {
return null;
}
return authHeader.slice(7);
return extractBearerTokenFromHeader(c.req.header('Authorization'));
}

/**
Expand Down