Skip to content

Commit 1bd5a9a

Browse files
fix(httpHeaders): catch malformed base64 in decodeMcpHeaderValue (return raw so mismatch path applies, not 500)
1 parent 5ea86f8 commit 1bd5a9a

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

packages/core/src/shared/httpHeaders.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ export function encodeMcpHeaderValue(value: string): string {
3939
export function decodeMcpHeaderValue(value: string): string {
4040
const m = /^=\?base64\?(.+)\?=$/.exec(value);
4141
if (!m) return value;
42-
// atob output is one Latin-1 char per byte; charCodeAt gives the byte value back.
43-
// eslint-disable-next-line unicorn/prefer-code-point
44-
const bytes = Uint8Array.from(atob(m[1]!), c => c.charCodeAt(0));
45-
return new TextDecoder().decode(bytes);
42+
try {
43+
// atob output is one Latin-1 char per byte; charCodeAt gives the byte value back.
44+
// eslint-disable-next-line unicorn/prefer-code-point
45+
const bytes = Uint8Array.from(atob(m[1]!), c => c.charCodeAt(0));
46+
return new TextDecoder().decode(bytes);
47+
} catch {
48+
// Malformed base64 from a misbehaving client. Return the raw value so
49+
// validateMcpHeaders falls through to the 400/-32001 mismatch path
50+
// instead of bubbling a DOMException up to the transport's outer catch.
51+
return value;
52+
}
4653
}
4754

4855
/**

0 commit comments

Comments
 (0)