|
| 1 | +import type { FastifyInstance } from 'fastify'; |
| 2 | +import Fastify from 'fastify'; |
| 3 | + |
| 4 | +import { hostHeaderValidation, localhostHostValidation } from './middleware/hostHeaderValidation.js'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Options for creating an MCP Fastify application. |
| 8 | + */ |
| 9 | +export interface CreateMcpFastifyAppOptions { |
| 10 | + /** |
| 11 | + * The hostname to bind to. Defaults to `'127.0.0.1'`. |
| 12 | + * When set to `'127.0.0.1'`, `'localhost'`, or `'::1'`, DNS rebinding protection is automatically enabled. |
| 13 | + */ |
| 14 | + host?: string; |
| 15 | + |
| 16 | + /** |
| 17 | + * List of allowed hostnames for DNS rebinding protection. |
| 18 | + * If provided, host header validation will be applied using this list. |
| 19 | + * For IPv6, provide addresses with brackets (e.g., `'[::1]'`). |
| 20 | + * |
| 21 | + * This is useful when binding to `'0.0.0.0'` or `'::'` but still wanting |
| 22 | + * to restrict which hostnames are allowed. |
| 23 | + */ |
| 24 | + allowedHosts?: string[]; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Creates a Fastify application pre-configured for MCP servers. |
| 29 | + * |
| 30 | + * When the host is `'127.0.0.1'`, `'localhost'`, or `'::1'` (the default is `'127.0.0.1'`), |
| 31 | + * DNS rebinding protection is automatically applied via an onRequest hook to protect against |
| 32 | + * DNS rebinding attacks on localhost servers. |
| 33 | + * |
| 34 | + * Fastify parses JSON request bodies by default, so no additional middleware is required |
| 35 | + * for MCP Streamable HTTP endpoints. |
| 36 | + * |
| 37 | + * @param options - Configuration options |
| 38 | + * @returns A configured Fastify application |
| 39 | + * |
| 40 | + * @example Basic usage - defaults to 127.0.0.1 with DNS rebinding protection |
| 41 | + * ```ts source="./fastify.examples.ts#createMcpFastifyApp_default" |
| 42 | + * const app = createMcpFastifyApp(); |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * @example Custom host - DNS rebinding protection only applied for localhost hosts |
| 46 | + * ```ts source="./fastify.examples.ts#createMcpFastifyApp_customHost" |
| 47 | + * const appOpen = createMcpFastifyApp({ host: '0.0.0.0' }); // No automatic DNS rebinding protection |
| 48 | + * const appLocal = createMcpFastifyApp({ host: 'localhost' }); // DNS rebinding protection enabled |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * @example Custom allowed hosts for non-localhost binding |
| 52 | + * ```ts source="./fastify.examples.ts#createMcpFastifyApp_allowedHosts" |
| 53 | + * const app = createMcpFastifyApp({ host: '0.0.0.0', allowedHosts: ['myapp.local', 'localhost'] }); |
| 54 | + * ``` |
| 55 | + */ |
| 56 | +export function createMcpFastifyApp(options: CreateMcpFastifyAppOptions = {}): FastifyInstance { |
| 57 | + const { host = '127.0.0.1', allowedHosts } = options; |
| 58 | + |
| 59 | + const app = Fastify(); |
| 60 | + |
| 61 | + // Fastify parses JSON by default - no middleware needed |
| 62 | + |
| 63 | + // If allowedHosts is explicitly provided, use that for validation |
| 64 | + if (allowedHosts) { |
| 65 | + app.addHook('onRequest', hostHeaderValidation(allowedHosts)); |
| 66 | + } else { |
| 67 | + // Apply DNS rebinding protection automatically for localhost hosts |
| 68 | + const localhostHosts = ['127.0.0.1', 'localhost', '::1']; |
| 69 | + if (localhostHosts.includes(host)) { |
| 70 | + app.addHook('onRequest', localhostHostValidation()); |
| 71 | + } else if (host === '0.0.0.0' || host === '::') { |
| 72 | + // Warn when binding to all interfaces without DNS rebinding protection |
| 73 | + app.log.warn( |
| 74 | + `Server is binding to ${host} without DNS rebinding protection. ` + |
| 75 | + 'Consider using the allowedHosts option to restrict allowed hosts, ' + |
| 76 | + 'or use authentication to protect your server.' |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return app; |
| 82 | +} |
0 commit comments