Skip to content

Commit 9437a30

Browse files
committed
Use code recommendation
1 parent bbf4e5c commit 9437a30

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎config/autoload/ip-address.global.php‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@
1313
// ],
1414
// ];
1515

16+
// @see https://github.com/akrabat/ip-address-middleware/blob/main/src/IpAddress.php
1617
return [
1718
'rka' => [
1819
'ip_address' => [
20+
// Inspect proxy headers below instead of using REMOTE_ADDR only.
1921
'check_proxy_headers' => true,
20-
'trusted_proxies' => [],
21-
'attribute_name' => 'ip_address',
22-
'headers_to_inspect' => [
23-
'Forwarded',
22+
// Proxy IPs/CIDRs/wildcards trusted to set the headers below.
23+
'trusted_proxies' => [],
24+
// Request attribute the resolved IP is stored under.
25+
'attribute_name' => 'ip_address',
26+
// Headers checked, in order, for the client IP.
27+
'headers_to_inspect' => [
28+
// nginx specific
29+
'X-Real-IP',
2430
'X-Forwarded-For',
31+
// cloudflare specific
32+
'CF-Connecting-IP',
33+
'True-Client-IP',
34+
// generic
35+
'Forwarded',
2536
'X-Forwarded',
2637
'X-Cluster-Client-Ip',
2738
'Client-Ip',

‎config/pipeline.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
1414
use Mezzio\Router\Middleware\MethodNotAllowedMiddleware;
1515
use Mezzio\Router\Middleware\RouteMiddleware;
16-
use RKA\Middleware\IpAddress;
1716

1817
return function (Application $app): void {
1918
// The error handler should be the first (most outer) middleware to catch
@@ -64,8 +63,6 @@
6463
// - route-based validation
6564
// - etc.
6665

67-
$app->pipe(IpAddress::class);
68-
6966
// Register the dispatch middleware in the middleware pipeline
7067
$app->pipe(DispatchMiddleware::class);
7168

‎src/App/src/RoutesDelegator.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Light\App\Handler\PostCreateContactHandler;
1414
use Mezzio\Application;
1515
use Psr\Container\ContainerInterface;
16+
use RKA\Middleware\IpAddress;
1617

1718
use function assert;
1819

@@ -28,7 +29,7 @@ public function __invoke(ContainerInterface $container, string $serviceName, cal
2829
$app->get('/{categorySlug}/{slug}.md', [GetMarkdownArticleHandler::class], 'app::markdown-article');
2930

3031
$app->get('/contact/', [GetCreateContactFormHandler::class], 'app::create-contact-form');
31-
$app->post('/contact/', [PostCreateContactHandler::class], 'app::create-contact');
32+
$app->post('/contact/', [IpAddress::class, PostCreateContactHandler::class], 'app::create-contact');
3233

3334
// Route name kept as `page::…` because `@layout/default.html.twig` links it by name.
3435
// The matching entry must stay out of `routes.page` in local.php to avoid a duplicate.

‎test/Unit/App/RoutesDelegatorTest.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Psr\Container\ContainerExceptionInterface;
2020
use Psr\Container\ContainerInterface;
2121
use Psr\Container\NotFoundExceptionInterface;
22+
use RKA\Middleware\IpAddress;
2223

2324
class RoutesDelegatorTest extends UnitTest
2425
{
@@ -77,7 +78,10 @@ public function testWillRegisterAllRoutes(): void
7778
$this->assertSame([GetCreateContactFormHandler::class], $this->registeredRoutes['/contact/']['handler']);
7879
$this->assertSame('app::create-contact-form', $this->registeredRoutes['/contact/']['name']);
7980

80-
$this->assertSame([PostCreateContactHandler::class], $this->registeredRoutes['POST /contact/']['handler']);
81+
$this->assertSame(
82+
[IpAddress::class, PostCreateContactHandler::class],
83+
$this->registeredRoutes['POST /contact/']['handler']
84+
);
8185
$this->assertSame('app::create-contact', $this->registeredRoutes['POST /contact/']['name']);
8286
}
8387
}

0 commit comments

Comments
 (0)