Skip to content

Commit 30f9855

Browse files
committed
fix: update authorization header handling to prevent empty values and show validation error
1 parent 17ace55 commit 30f9855

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

client/src/App.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,8 @@ const App = () => {
162162
return migrateFromLegacyAuth(legacyToken, legacyHeaderName);
163163
}
164164

165-
// Default to Authorization: Bearer as the most common case
166-
return [
167-
{
168-
name: "Authorization",
169-
value: "Bearer ",
170-
enabled: true,
171-
},
172-
];
165+
// Default to empty array
166+
return [];
173167
});
174168

175169
const [pendingSampleRequests, setPendingSampleRequests] = useState<

client/src/lib/hooks/__tests__/useConnection.test.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ jest.mock("@modelcontextprotocol/sdk/client/auth.js", () => ({
9797
}));
9898

9999
// Mock the toast hook
100+
const mockToast = jest.fn();
100101
jest.mock("@/lib/hooks/useToast", () => ({
101102
useToast: () => ({
102-
toast: jest.fn(),
103+
toast: mockToast,
103104
}),
104105
}));
105106

@@ -940,6 +941,13 @@ describe("useConnection", () => {
940941
expect(headers).toHaveProperty("Authorization", "Bearer mock-token");
941942
// Should not have the x-custom-auth-headers since Authorization is standard
942943
expect(headers).not.toHaveProperty("x-custom-auth-headers");
944+
945+
// Should show toast notification for empty Authorization header
946+
expect(mockToast).toHaveBeenCalledWith({
947+
title: "Invalid Authorization Header",
948+
description: expect.any(String),
949+
variant: "destructive",
950+
});
943951
});
944952

945953
test("prioritizes custom headers over legacy auth", async () => {

client/src/lib/hooks/useConnection.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,23 @@ export function useConnection({
400400
// Use custom headers (migration is handled in App.tsx)
401401
let finalHeaders: CustomHeaders = customHeaders || [];
402402

403+
// Check for empty Authorization headers and show validation error
404+
const hasEmptyAuthHeader = finalHeaders.some(
405+
(header) =>
406+
header.enabled &&
407+
header.name.trim().toLowerCase() === "authorization" &&
408+
(!header.value.trim() || header.value.trim() === "Bearer"),
409+
);
410+
411+
if (hasEmptyAuthHeader) {
412+
toast({
413+
title: "Invalid Authorization Header",
414+
description:
415+
"Authorization header is enabled but empty. Please add a token or disable the header. It will be added automatically.",
416+
variant: "destructive",
417+
});
418+
}
419+
403420
// Check if we need to inject OAuth token
404421
// This handles both empty headers and default "Bearer " placeholder headers
405422
const isEmptyAuthHeader = (header: CustomHeaders[number]) =>

0 commit comments

Comments
 (0)