2017 © Pedro Peláez
 

library ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

image

akrabat/ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

  • Tuesday, July 3, 2018
  • by akrabat
  • Repository
  • 7 Watchers
  • 88 Stars
  • 21,925 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 17 Forks
  • 0 Open issues
  • 10 Versions
  • 121 % Grown

The README.md

Client IP address middleware

PSR-15 Middleware that determines the client IP address and stores it as an ServerRequest attribute called ip_address. It optionally checks various common proxy headers and then falls back to $_SERVER['REMOTE_ADDR']., (*1)

![Build status][Master image], (*2)

Configuration

The constructor takes 4 parameters which can be used to configure this middleware., (*3)

Check proxy headers, (*4)

Note that the proxy headers are only checked if the first parameter to the constructor is set to true. If it is set to false, then only $_SERVER['REMOTE_ADDR'] is used., (*5)

Trusted Proxies, (*6)

If you configure to check the proxy headers (first parameter is true), you have to provide an array of trusted proxies as the second parameter. When the array is empty, the proxy headers will always be evaluated which is not recommended. If the array is not empty, it must contain strings with IP addresses (wildcard * is allowed in any given part) or networks in CIDR-notation. One of them must match the $_SERVER['REMOTE_ADDR'] variable in order to allow evaluating the proxy headers - otherwise the REMOTE_ADDR itself is returned., (*7)

Attribute name, (*8)

By default, the name of the attribute is 'ip_address'. This can be changed by the third constructor parameter., (*9)

Headers to inspect, (*10)

By default, this middleware checks the 'Forwarded', 'X-Forwarded-For', 'X-Forwarded', 'X-Cluster-Client-Ip' and 'Client-Ip' headers. You can replace this list with your own using the fourth constructor parameter., (*11)

If you use the nginx, set_real_ip_from directive, then you should probably set this to:, (*12)

$headersToInspect = [
    'X-Real-IP',
    'Forwarded',
    'X-Forwarded-For',
    'X-Forwarded',
    'X-Cluster-Client-Ip',
    'Client-Ip',
];

If you use CloudFlare, then according to the documentation you should probably set this to:, (*13)

$headersToInspect = [
    'CF-Connecting-IP',
    'True-Client-IP',
    'Forwarded',
    'X-Forwarded-For',
    'X-Forwarded',
    'X-Cluster-Client-Ip',
    'Client-Ip',
];

Security considerations

A malicious client may send any header to your proxy, including any proxy headers, containing any IP address. If your proxy simply adds another IP address to the header, an attacker can send a fake IP. Make sure to setup your proxy in a way that removes any sent (and possibly faked) headers from the original request and replaces them with correct values (i.e. the currently used REMOTE_ADDR on the proxy server)., (*14)

This library cannot by design ensure you get correct and trustworthy results if your network environment isn't setup properly., (*15)

Installation

composer require akrabat/ip-address-middleware, (*16)

In Mezzio, copy Mezzio/config/ip_address.global.php.dist into your Mezzio Application config/autoload directory as ip_address.global.php, (*17)

Usage

In Slim 3:, (*18)

$checkProxyHeaders = true; // Note: Never trust the IP address for security processes!
$trustedProxies = ['10.0.0.1', '10.0.0.2']; // Note: Never trust the IP address for security processes!
$app->add(new RKA\Middleware\IpAddress($checkProxyHeaders, $trustedProxies));

$app->get('/', function ($request, $response, $args) {
    $ipAddress = $request->getAttribute('ip_address');

    return $response;
});

In Laminas or Mezzio, add to your pipeline.php config at the correct stage, usually just before the DispatchMiddleware:, (*19)

# config/pipeline.php
# using default config
$app->add(RKA\Middleware\IpAddress::class);

If required, update your .env file with the environmental variables found in /config/autoload/ip_address.global.php., (*20)

Testing

  • Code style: $ vendor/bin/phpcs
  • Unit tests: $ vendor/bin/phpunit
  • Code coverage: $ vendor/bin/phpunit --coverage-html ./build

The Versions

03/07 2018

dev-master

9999999-dev http://github.com/akrabat/rka-ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

03/07 2018

1.0.1

1.0.1.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

18/06 2018

dev-remove-port-from-ipaddress

dev-remove-port-from-ipaddress http://github.com/akrabat/rka-ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

18/02 2018

1.0

1.0.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-15 middleware that determines the client IP address and stores it as a ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

18/02 2018

0.6

0.6.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

13/11 2016

0.5

0.5.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

06/11 2015

0.4

0.4.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

02/11 2015

0.3

0.3.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

31/10 2015

0.2

0.2.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip

31/10 2015

0.1

0.1.0.0 http://github.com/akrabat/rka-ip-address-middleware

PSR-7 Middleware that determines the client IP address and stores it as an ServerRequest attribute

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

middleware psr7 ip