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
22 changes: 22 additions & 0 deletions packages/mcp-core/src/__tests__/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
apiRequest,
ApiRequestError,
InvalidApiPathError,
UnauthorizedError,
UntrustedApiHostError,
} from '../api-client.js';
Expand Down Expand Up @@ -134,6 +135,27 @@ describe('apiRequest', () => {
expect(invalidateTokenMock).not.toHaveBeenCalled();
});

it.each([
'/orgs/acme/teams/..',
'/orgs/../subscriptions',
'/orgs/./acme',
'/orgs/acme/..?x=1',
'/orgs/%2e%2e/subscriptions',
'/orgs/.%2E/x',
'/orgs\\..\\subscriptions',
'/orgs/.\t./subscriptions',
'/orgs/%2e\n%2e/x',
])('rejects the dot segment in %s before fetching', async (path) => {
await expect(apiRequest(path, { token: 'secret' })).rejects.toBeInstanceOf(InvalidApiPathError);
expect(mockFetch).not.toHaveBeenCalled();
});

it('allows dots inside a segment and in the query string', async () => {
mockFetch.mockResolvedValue(makeResponse({}));
await apiRequest('/orgs/a.b/files/..hidden?next=../x');
expect(mockFetch).toHaveBeenCalledOnce();
});

it('refuses to send a token to a host outside the allowlist', async () => {
await expect(
apiRequest('/orgs', { token: 'secret', baseUrl: 'https://evil.example.com' }),
Expand Down
19 changes: 19 additions & 0 deletions packages/mcp-core/src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ export class UntrustedApiHostError extends Error {
}
}

export class InvalidApiPathError extends Error {
constructor(message: string) {
super(message);
this.name = 'InvalidApiPathError';
}
}

export function assertNoDotSegments(path: string): void {
const [pathname = ''] = path.split(/[?#]/, 1);
const segments = pathname
.replace(/[\t\n\r]/g, '')
.split(/[\\/]/)
.map((segment) => segment.replace(/%2e/gi, '.'));
if (segments.some((segment) => segment === '.' || segment === '..')) {
throw new InvalidApiPathError(`refusing a request path with a dot segment: ${path}`);
}
}

function allowedApiHosts(): string[] {
const extra = (process.env.FERRLABS_MCP_ALLOWED_API_HOSTS ?? '')
.split(',')
Expand Down Expand Up @@ -106,6 +124,7 @@ export function assertCredentialTarget(url: string): void {
export async function apiRequest<T>(path: string, options: RequestOptions = {}): Promise<T> {
const { method = 'GET', body, token, baseUrl, headers: extraHeaders } = options;
const base = baseUrl ?? DEFAULT_API_URL;
assertNoDotSegments(path);
const url = `${base}${path}`;

if (token) assertCredentialTarget(url);
Expand Down
1 change: 1 addition & 0 deletions packages/mcp-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
ApiRequestError,
UnauthorizedError,
UntrustedApiHostError,
InvalidApiPathError,
} from './api-client.js';
export { getToken, clearTokenCache, invalidateToken } from './auth/index.js';
export { runWithAuthContext, getRequestBearerToken } from './auth/context.js';
Expand Down
Loading