MCP PHP SDK

DnsRebindingProtectionMiddleware implements MiddlewareInterface

FinalYes

Protects local MCP servers against DNS rebinding attacks.

The Host header is always validated against allowedHosts, ignoring case and port. If present, Origin must match the request URI origin or an entry in allowedOrigins, including scheme and port, unless allowedOrigins contains '*'. By default, only localhost hosts with same-origin requests or requests without an Origin header are allowed.

Tags
see
https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#security-warning
author

Volodymyr Panivko sveneld300@gmail.com

Table of Contents

Interfaces

MiddlewareInterface

Properties

$allowedHosts  : array<int, string>
$allowedOrigins  : array<int, string>
$responseFactory  : ResponseFactoryInterface
$streamFactory  : StreamFactoryInterface

Methods

__construct()  : mixed
process()  : ResponseInterface
createForbiddenResponse()  : ResponseInterface
isAllowedHost()  : bool
isSameOrigin()  : bool
normalizeOrigin()  : string

Properties

Methods

__construct()

public __construct([array<int, string> $allowedHosts = ['localhost', '127.0.0.1', '[::1]'] ][, array<int, string> $allowedOrigins = [] ][, ResponseFactoryInterface|null $responseFactory = null ][, StreamFactoryInterface|null $streamFactory = null ]) : mixed
Parameters
$allowedHosts : array<int, string> = ['localhost', '127.0.0.1', '[::1]']

Hostnames (without port) that are permitted. Defaults to localhost variants. IPv6 addresses must be bracketed (e.g. [::1]). Use ['*'] to allow any non-empty Host header.

$allowedOrigins : array<int, string> = []

Origins (scheme, host and optional port) that are permitted, compared case-insensitively. Same-origin requests are always permitted. Use ['*'] to allow any Origin header.

$responseFactory : ResponseFactoryInterface|null = null

PSR-17 response factory (auto-discovered if null)

$streamFactory : StreamFactoryInterface|null = null

PSR-17 stream factory (auto-discovered if null)

process()

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface

createForbiddenResponse()

private createForbiddenResponse(string $message) : ResponseInterface
Parameters
$message : string
Return values
ResponseInterface

isSameOrigin()

private isSameOrigin(ServerRequestInterface $request) : bool
Parameters
$request : ServerRequestInterface
Return values
bool
On this page

Search results