• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    PHP
  • License
    Other
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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'].

Build status

Configuration

The constructor takes 4 parameters which can be used to configure this middleware.

Check proxy headers

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.

Trusted Proxies

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.

Attribute name

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

Headers to inspect

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.

If you use the nginx, set_real_ip_from directive, then you should probably set this to:

$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:

$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).

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

Installation

composer require akrabat/ip-address-middleware

Usage

In Slim 3:

$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, add to your pipeline.php config at the correct stage, usually just before the DispatchMiddleware:

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

Testing

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

More Repositories

1

slim3-skeleton

Simple Slim Framework 3 skeleton with Twig & Monolog
PHP
349
star
2

zf2-tutorial

PHP
145
star
3

slim3-bookshelf

Exploratory Slim Framework 3 application.
PHP
124
star
4

AkrabatZF

Akrabat_Db_Schema_Manager and a DatabaseSchemaProvider for Zend_Tool
PHP
95
star
5

SublimeFunctionNameDisplay

Display current class and function name on the status bar
Python
90
star
6

lambda-php

The basics of using Serverless Framework for AWS Lambda PHP applications.
PHP
84
star
7

slim-bookshelf-api

A simple API written in Slim Framework
PHP
83
star
8

avalanche

A theme for landslide
CSS
68
star
9

ZF2TestApp

PHP
54
star
10

rka-slim-controller

Dynamically instantiated controller classes for Slim Framework 2
PHP
48
star
11

rka-slim-session-middleware

Simple session middleware for Slim Framework
PHP
42
star
12

rka-content-type-renderer

Render an array(or HAL object) to a JSON/XML/HTML PSR-7 Response based on a PSR-7 Request's Accept header.
PHP
41
star
13

proxy-detection-middleware

PSR-7 / PSR-15 middleware to detect scheme and host from proxy headers
PHP
31
star
14

slim4-starter

Starter project for Slim 4 that includes PHP-DI and Monolog
PHP
27
star
15

slim4-empty

Minimal Slim 4 starter project
PHP
18
star
16

TweetGT

Tweets: Geotagged
PHP
16
star
17

slim-2fa

An example Slim Framework project that 2FA authentication Google Authenticator
PHP
15
star
18

zf2-tutorial-to-go

Ready to run version of the ZF2 tutorial
CSS
14
star
19

AkrabatSession

A ZF2 module for configuring a session
PHP
13
star
20

TodoIt

ZF example app
PHP
13
star
21

apigility-music-api

A simple Apigility API to view music albums
Puppet
11
star
22

sublime-akrabat

Useful SublimeText2 stuff
C
10
star
23

ow-php-ftime

PHP
10
star
24

rodeo

A command line tool to work with Flickr and images
Go
10
star
25

project365-photos-website

Photo-a-day static CloudFront/S3 website created by Lambda/PHP
HTML
10
star
26

slim4-rps-api

An API that implements the Rock-Paper-Scissors game written in Slim 4.
PHP
9
star
27

lumen-bookshelf-api

A simple Lumen API
PHP
9
star
28

rka-slim-zfsm-container

Integration of Zend\ServiceManager into Slim 3
PHP
9
star
29

zf2-tutorial-apigility

Apigility-enabled version of the ZF2 Tutorial
CSS
7
star
30

ow-php-todo-backend

A Todo-Backend implementation in OpenWhisk PHP
PHP
7
star
31

slim-api-skeleton

Simple Slim Framework API skeleton
PHP
6
star
32

rst2pdf_example_presentation

Presentations with rst2pdf
Makefile
6
star
33

collection-creator-lrplugin

Lightroom Classic plug-in to create collections within collection sets from a hierarchy of keywords
Lua
6
star
34

zf2-bookshelf

This is a simple ZF2 application.
PHP
6
star
35

SideBarHider

Sublime Text 3 plugin to automatically hide the side bar when it loses focus
Python
6
star
36

slim-zendform

An example Slim Framework project that integrates the Zend Framework Form component.
PHP
5
star
37

ow-php-hello-zip

An example PHP OpenWhisk action that uses multiple PHP files.
Makefile
5
star
38

slim3-abstract-action-factory

Slim 3 / Zend\ServiceManager Abstract Action Factory example
PHP
5
star
39

ow-swift-drinkchooser

OpenWhisk actions to choose a drink
Swift
5
star
40

slim-di

Dynamically instantiated controller actions in Slim Framework
PHP
5
star
41

zf2-learning

A place for me to learn about some ZF2 components.
PHP
4
star
42

kitura_bookshelfapi

Swift
4
star
43

slim3-api-output-format

One way to control output format based on Accept header.
PHP
4
star
44

new-collection-set-lrplugin

Lightroom Classic Plug-in to create a collection set containing a collection and a smart collection from the name given
Lua
4
star
45

alexa-binday

Alexa skill to tell me which bin to put out when I ask
Swift
4
star
46

bref-image-resizer

Serverless PHP (Bref/Lambda) function to resize an image on upload to an S3 bucket
Makefile
4
star
47

slim4-pimple

Simple Slim4 with Pimple DI example application
PHP
4
star
48

slim-api-starter

Slim API Starter
PHP
3
star
49

ow-php-form-to-google-sheets

OpenWhisk PHP action used to store data from an HTML form into Google Sheets
PHP
3
star
50

expressive-bookshelf

An example Zend Expressive application
PHP
3
star
51

rka-doctrine-migrations-runner

Standalone runner for Doctrine Migrations
PHP
3
star
52

apigility-xml

Apigility module providing XML content-negotiation features
PHP
2
star
53

bibliotheque

A set of simple domain objects for use in example PHP applications
PHP
2
star
54

zf2-api-example

A simple Zend Framework 2 API example using the AbstractRestfulController
PHP
2
star
55

ow-swift-flashcards

OpenWhisk Swift API called from JS frontend website
Swift
2
star
56

mezzio-session-example

Simple example website showing the use of Mezzio-Session
PHP
2
star
57

zf2-module-concepts

PHP
2
star
58

zf2-api-response-code

Testing setting a response code in a ZF2 AbstractRestfulController application
PHP
2
star
59

FormExample

PHP
2
star
60

zf2-action-skeleton

Looking at creating one class per action and dispatching to __invoke()
PHP
2
star
61

openwhisk-client-php

A PHP client library to access Openwhisk API
PHP
2
star
62

zf-tutorial

Zend Framework 1 Tutorial
PHP
2
star
63

booklist-apigility

Playing with Apigility
PHP
2
star
64

slimbench

Shell
2
star
65

Nennius

Nennius was an 9th century historian
PHP
2
star
66

apigility1-bookshelf

Apigility v1.x Bookshelf Application
PHP
2
star
67

slim3-profile-test

Simple Slim 3 Hello World with a vagrant VM that has a profiler in it
PHP
2
star
68

phpnw11

PHP
2
star
69

rka-slim-zendform

PHP
1
star
70

ow-php-docker

Run PHP as an OpenWhisk Docker action
Shell
1
star
71

old-sample-ow-php-action

A simple PHP OpenWhisk action
PHP
1
star
72

empty-slim-skeleton

An Slim 3 empty project
PHP
1
star
73

slim-bot

CoffeeScript
1
star
74

slim4-csrf-example

Example showing use of Slim-Csrf on a form, using Twig-View
PHP
1
star