express.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.createMcpExpressApp = createMcpExpressApp;
  7. const express_1 = __importDefault(require("express"));
  8. const hostHeaderValidation_js_1 = require("./middleware/hostHeaderValidation.js");
  9. /**
  10. * Creates an Express application pre-configured for MCP servers.
  11. *
  12. * When the host is '127.0.0.1', 'localhost', or '::1' (the default is '127.0.0.1'),
  13. * DNS rebinding protection middleware is automatically applied to protect against
  14. * DNS rebinding attacks on localhost servers.
  15. *
  16. * @param options - Configuration options
  17. * @returns A configured Express application
  18. *
  19. * @example
  20. * ```typescript
  21. * // Basic usage - defaults to 127.0.0.1 with DNS rebinding protection
  22. * const app = createMcpExpressApp();
  23. *
  24. * // Custom host - DNS rebinding protection only applied for localhost hosts
  25. * const app = createMcpExpressApp({ host: '0.0.0.0' }); // No automatic DNS rebinding protection
  26. * const app = createMcpExpressApp({ host: 'localhost' }); // DNS rebinding protection enabled
  27. *
  28. * // Custom allowed hosts for non-localhost binding
  29. * const app = createMcpExpressApp({ host: '0.0.0.0', allowedHosts: ['myapp.local', 'localhost'] });
  30. * ```
  31. */
  32. function createMcpExpressApp(options = {}) {
  33. const { host = '127.0.0.1', allowedHosts } = options;
  34. const app = (0, express_1.default)();
  35. app.use(express_1.default.json());
  36. // If allowedHosts is explicitly provided, use that for validation
  37. if (allowedHosts) {
  38. app.use((0, hostHeaderValidation_js_1.hostHeaderValidation)(allowedHosts));
  39. }
  40. else {
  41. // Apply DNS rebinding protection automatically for localhost hosts
  42. const localhostHosts = ['127.0.0.1', 'localhost', '::1'];
  43. if (localhostHosts.includes(host)) {
  44. app.use((0, hostHeaderValidation_js_1.localhostHostValidation)());
  45. }
  46. else if (host === '0.0.0.0' || host === '::') {
  47. // Warn when binding to all interfaces without DNS rebinding protection
  48. // eslint-disable-next-line no-console
  49. console.warn(`Warning: Server is binding to ${host} without DNS rebinding protection. ` +
  50. 'Consider using the allowedHosts option to restrict allowed hosts, ' +
  51. 'or use authentication to protect your server.');
  52. }
  53. }
  54. return app;
  55. }
  56. //# sourceMappingURL=express.js.map