Skip to content
Closed
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
25 changes: 25 additions & 0 deletions packages/mcp-gateway/src/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const hopByHopHeaders = new Set([
'upgrade',
]);

const framingHeader = 'content-length';

const headerNamePattern = /^[A-Za-z0-9!#$%&'*+.^_`|~-]+$/;

const blockedExactHeaders = new Set([
Expand Down Expand Up @@ -102,6 +104,13 @@ const staticHeadersSchema = z.record(z.string(), headerValueSchema).superRefine(
path: [name],
});
}
if (normalized === framingHeader) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Static header cannot be a framing header',
path: [name],
});
}
}
});

Expand All @@ -124,6 +133,20 @@ const auxiliaryHeadersSchema = z
path: [name],
});
}
if (isTransportIdentityHeader(normalized)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Auxiliary header cannot be transport identity metadata',
path: [name],
});
}
if (normalized === framingHeader) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Auxiliary header cannot be a framing header',
path: [name],
});
}
}
});

Expand Down Expand Up @@ -164,6 +187,8 @@ export function buildUpstreamHeaders(params: {
if (
hopByHopHeaders.has(normalized) ||
isCredentialLikeHeader(normalized) ||
isTransportIdentityHeader(normalized) ||
normalized === framingHeader ||
blockedStaticNames.has(normalized)
)
continue;
Expand Down
48 changes: 48 additions & 0 deletions packages/mcp-gateway/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
buildScopedConnectRootPath,
buildUpstreamHeaders,
parseScopedConnectPath,
parseAuxiliaryHeaders,
parseStaticHeaders,
isPublicIp,
ProviderGrantBundleSchema,
Expand Down Expand Up @@ -118,6 +119,50 @@ describe('provider grant schema', () => {
});
});

describe('auxiliary headers', () => {
test('rejects transport identity and framing headers', () => {
expect(() => parseAuxiliaryHeaders({ Host: 'example.com' })).toThrow(
'Auxiliary header cannot be transport identity metadata'
);
expect(() => parseAuxiliaryHeaders({ Forwarded: 'for=192.0.2.1' })).toThrow(
'Auxiliary header cannot be transport identity metadata'
);
expect(() => parseAuxiliaryHeaders({ 'X-Forwarded-For': '192.0.2.1' })).toThrow(
'Auxiliary header cannot be transport identity metadata'
);
expect(() => parseAuxiliaryHeaders({ 'X-Forwarded-Host': 'example.com' })).toThrow(
'Auxiliary header cannot be transport identity metadata'
);
expect(() => parseAuxiliaryHeaders({ 'CF-Connecting-IP': '192.0.2.1' })).toThrow(
'Auxiliary header cannot be transport identity metadata'
);
expect(() => parseAuxiliaryHeaders({ 'Content-Length': '10' })).toThrow(
'Auxiliary header cannot be a framing header'
);
expect(parseAuxiliaryHeaders({ 'X-Request-Id': 'request-1' })).toEqual({
'X-Request-Id': 'request-1',
});
});

test('buildUpstreamHeaders drops transport identity and framing auxiliary headers', () => {
const headers = buildUpstreamHeaders({
source: new Headers({ Accept: 'application/json' }),
auxiliaryHeaders: [
['Host', 'attacker.example'],
['X-Forwarded-For', '192.0.2.1'],
['Content-Length', '10'],
['X-Request-Id', 'request-1'],
],
});

expect(headers.get('host')).toBeNull();
expect(headers.get('x-forwarded-for')).toBeNull();
expect(headers.get('content-length')).toBeNull();
expect(headers.get('x-request-id')).toBe('request-1');
expect(headers.get('accept')).toBe('application/json');
});
});

describe('static headers', () => {
test('allows authorization but rejects transport identity headers', () => {
expect(parseStaticHeaders({ Authorization: 'Bearer test' })).toEqual({
Expand All @@ -132,6 +177,9 @@ describe('static headers', () => {
expect(() => parseStaticHeaders({ 'CF-Connecting-IP': '203.0.113.1' })).toThrow(
'Static header cannot be transport identity metadata'
);
expect(() => parseStaticHeaders({ 'Content-Length': '10' })).toThrow(
'Static header cannot be a framing header'
);
expect(
parseStaticHeaders({
'CF-Access-Client-Id': 'client-id',
Expand Down