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'
import { parseUrl } from '../../../lib/url'
import { warnOnce } from '../../../build/output/log'
import { isCsrfOriginAllowed } from '../../app-render/csrf-protection'
import { parsePath } from '../../../shared/lib/router/utils/parse-path'
const allowedDevOriginsDocs =
'https://nextjs.org/docs/app/api-reference/config/next-config-js/allowedDevOrigins'
......@@ -96,21 +97,19 @@ function parseHostnameFromHeader(
function isInternalEndpoint(req: IncomingMessage): boolean {
if (!req.url) return false
try {
// TODO: We should standardize on a single prefix for this
const isMiddlewareRequest = req.url.includes('/__nextjs')
const isInternalAsset = req.url.includes('/_next')
// Static media requests are excluded, as they might be loaded via CSS and would fail
// CORS checks.
const isIgnoredRequest =
req.url.includes('/_next/image') ||
req.url.includes('/_next/static/media') ||
req.url.includes('/_next/static/immutable/media')
return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest)
} catch (err) {
return false
}
const { pathname } = parsePath(req.url)
// TODO: We should standardize on a single prefix for this
const isMiddlewareRequest = pathname.includes('/__nextjs')
const isInternalAsset = pathname.includes('/_next')
// Static media requests are excluded, as they might be loaded via CSS and would fail
// CORS checks.
const isIgnoredRequest =
pathname.includes('/_next/image') ||
pathname.includes('/_next/static/media') ||
pathname.includes('/_next/static/immutable/media')
return !isIgnoredRequest && (isInternalAsset || isMiddlewareRequest)
}
export const blockCrossSiteDEV = (
......
......@@ -4,6 +4,8 @@ import {
type McpServerOptions,
} from './get-or-create-mcp-server'
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'
export function getMcpMiddleware(options: McpServerOptions) {
......@@ -12,8 +14,8 @@ export function getMcpMiddleware(options: McpServerOptions) {
res: ServerResponse,
next: () => void
): Promise<void> {
const { pathname } = new URL(req.url || '', 'http://n')
if (!pathname.startsWith('/_next/mcp')) {
const { pathname } = parsePath(req.url || '')
if (removeTrailingSlash(pathname) !== '/_next/mcp') {
return next()
}
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