DnsRebindingProtectionMiddleware implements MiddlewareInterface
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
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
$allowedHosts read-only
private
array<int, string>
$allowedHosts
$allowedOrigins read-only
private
array<int, string>
$allowedOrigins
$responseFactory
private
ResponseFactoryInterface
$responseFactory
$streamFactory
private
StreamFactoryInterface
$streamFactory
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
ResponseInterfacecreateForbiddenResponse()
private
createForbiddenResponse(string $message) : ResponseInterface
Parameters
- $message : string
Return values
ResponseInterfaceisAllowedHost()
private
isAllowedHost(string $host) : bool
Parameters
- $host : string
Return values
boolisSameOrigin()
private
isSameOrigin(ServerRequestInterface $request) : bool
Parameters
- $request : ServerRequestInterface
Return values
boolnormalizeOrigin()
private
normalizeOrigin(string $origin) : string
Parameters
- $origin : string