Unverified Commit 7985900a authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Fix MCP middleware DNS rebinding (#99480)

https://github.com/vercel/next-js-mirror/pull/201Co-authored-by: 's avatarKarim Rahal <karim@karimrahal.com>
parent c7c666be
...@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http' ...@@ -3,6 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
import { parseUrl } from '../../../lib/url' import { parseUrl } from '../../../lib/url'
import { warnOnce } from '../../../build/output/log' import { warnOnce } from '../../../build/output/log'
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection' import { isCsrfOriginAllowed } from '../../app-render/csrf-protection'
import { parsePath } from '../../../shared/lib/router/utils/parse-path'
const allowedDevOriginsDocs = const allowedDevOriginsDocs =
'https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins' 'https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins'
...@@ -96,21 +97,19 @@ function parseHostnameFromHeader( ...@@ -96,21 +97,19 @@ function parseHostnameFromHeader(
function isInternalEndpoint(req: IncomingMessage): boolean { function isInternalEndpoint(req: IncomingMessage): boolean {
if (!req.url) return false if (!req.url) return false
try { const { pathname } = parsePath(req.url)
// TODO: We should standardize on a single prefix for this
const isMiddlewareRequest = req.url.includes('/__nextjs') // TODO: We should standardize on a single prefix for this
const isInternalAsset = req.url.includes('/_next') const isMiddlewareRequest = pathname.includes('/__nextjs')
// Static media requests are excluded, as they might be loaded via CSS and would fail const isInternalAsset = pathname.includes('/_next')
// CORS checks. // Static media requests are excluded, as they might be loaded via CSS and would fail
const isIgnoredRequest = // CORS checks.
req.url.includes('/_next/image') || const isIgnoredRequest =
req.url.includes('/_next/static/media') || pathname.includes('/_next/image') ||
req.url.includes('/_next/static/immutable/media') pathname.includes('/_next/static/media') ||
pathname.includes('/_next/static/immutable/media')
return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest)
} catch (err) { return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest)
return false
}
} }
export const blockCrossSiteDEV = ( export const blockCrossSiteDEV = (
......
...@@ -4,6 +4,8 @@ import { ...@@ -4,6 +4,8 @@ import {
type McpServerOptions, type McpServerOptions,
} from './get-or-create-mcp-server' } from './get-or-create-mcp-server'
import { parseBody } from '../api-utils/node/parse-body' import { parseBody } from '../api-utils/node/parse-body'
import { parsePath } from '../../shared/lib/router/utils/parse-path'
import { removeTrailingSlash } from '../../shared/lib/router/utils/remove-trailing-slash'
import { StreamableHTTPServerTransport } from 'next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp' import { StreamableHTTPServerTransport } from 'next/dist/compiled/@modelcontextprotocol/sdk/server/streamableHttp'
export function getMcpMiddleware(options: McpServerOptions) { export function getMcpMiddleware(options: McpServerOptions) {
...@@ -12,8 +14,8 @@ export function getMcpMiddleware(options: McpServerOptions) { ...@@ -12,8 +14,8 @@ export function getMcpMiddleware(options: McpServerOptions) {
res: ServerResponse, res: ServerResponse,
next: () => void next: () => void
): Promise<void> { ): Promise<void> {
const { pathname } = new URL(req.url || '', 'http://n') const { pathname } = parsePath(req.url || '')
if (!pathname.startsWith('/_next/mcp')) { if (removeTrailingSlash(pathname) !== '/_next/mcp') {
return next() return next()
} }
const mcpServer = getOrCreateMcpServer(options) const mcpServer = getOrCreateMcpServer(options)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment