class TrustedProxiesMiddleware implements MiddlewareInterface (View source)

Middleware that checks request headers against a configured list of trusted proxy IP addresses.

Constants

HEADER_CLIENT_IP

HEADER_HOST

HEADER_PORT

HEADER_PROTOCOL

FOR_PATTERN

PROTO_PATTERN

HOST_PATTERN

Properties

protected array $settings

Methods

void
injectSettings(array $settings)

Injects the configuration settings

ResponseInterface
process(ServerRequestInterface $request, RequestHandlerInterface $handler)

No description

array
unquoteArray(array $array)

No description

array|null
getForwardedHeader(string $type, array $headerValues)

No description

getTrustedProxyHeaderValues(string $type, ServerRequestInterface $request)

Get the values of trusted proxy header.

mixed|null
getFirstTrustedProxyHeaderValue(string $type, ServerRequestInterface $request)

Convenience getter for the first value of a given trusted proxy header.

bool
ipIsTrustedProxy(string $ipAddress)

Check if the given IP address is from a trusted proxy.

bool
isFromTrustedProxy(ServerRequestInterface $request)

Check if the given request is from a trusted proxy.

string|bool
getTrustedClientIpAddress(ServerRequestInterface $request)

Get the most trusted client's IP address.

Details

void injectSettings(array $settings)

Injects the configuration settings

Parameters

array $settings

Return Value

void

Exceptions

InvalidConfigurationException

ResponseInterface process(ServerRequestInterface $request, RequestHandlerInterface $handler)

No description

Parameters

ServerRequestInterface $request
RequestHandlerInterface $handler

Return Value

ResponseInterface

protected array unquoteArray(array $array)

No description

Parameters

array $array

Return Value

array

protected array|null getForwardedHeader(string $type, array $headerValues)

No description

Parameters

string $type

The header value type to retrieve from the Forwarded header value. One of the HEADER_* constants.

array $headerValues

The Forwarded header value, e.g. "for=192.168.178.5; host=www.acme.org:8080"

Return Value

array|null

The array of values for the header type or null if the header

protected Iterator getTrustedProxyHeaderValues(string $type, ServerRequestInterface $request)

Get the values of trusted proxy header.

Parameters

string $type

One of the HEADER_* constants

ServerRequestInterface $request

The request to get the trusted proxy header from

Return Value

Iterator

An array of the values for this header type or NULL if this header type should not be trusted

protected mixed|null getFirstTrustedProxyHeaderValue(string $type, ServerRequestInterface $request)

Convenience getter for the first value of a given trusted proxy header.

Parameters

string $type

One of the HEADER_* constants

ServerRequestInterface $request

The request to get the trusted proxy header from

Return Value

mixed|null

The first value of this header type or NULL if this header type should not be trusted

protected bool ipIsTrustedProxy(string $ipAddress)

Check if the given IP address is from a trusted proxy.

Parameters

string $ipAddress

Return Value

bool

protected bool isFromTrustedProxy(ServerRequestInterface $request)

Check if the given request is from a trusted proxy.

Parameters

ServerRequestInterface $request

Return Value

bool

If the server REMOTE_ADDR is from a trusted proxy

protected string|bool getTrustedClientIpAddress(ServerRequestInterface $request)

Get the most trusted client's IP address.

This is the right-most address in the trusted client IP header, that is not a trusted proxy address. If all proxies are trusted, this is the left-most address in the header. If no proxies are trusted or no client IP header is trusted, this is the remote address of the machine directly connected to the server.

Parameters

ServerRequestInterface $request

Return Value

string|bool

The most trusted client's IP address or false if no remote address can be found