Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit23a588f

Browse files
magnusnordlandernicolas-grekas
authored andcommitted
[HttpKernel] Add listener that checks when request has both Forwarded and X-Forwarded-For
1 parente6e1919 commit23a588f

File tree

4 files changed

+104
-2
lines changed

4 files changed

+104
-2
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@
4646
<argumenttype="service"id="request_stack" />
4747
<tagname="kernel.event_subscriber" />
4848
</service>
49+
50+
<serviceid="validate_request_listener"class="Symfony\Component\HttpKernel\EventListener\ValidateRequestListener">
51+
<tagname="kernel.event_subscriber" />
52+
</service>
4953
</services>
5054
</container>

‎src/Symfony/Bundle/FrameworkBundle/composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"symfony/config":"~2.4",
2323
"symfony/event-dispatcher":"~2.5",
2424
"symfony/finder":"~2.0,>=2.0.5",
25-
"symfony/http-foundation":"~2.4.9|~2.5,>=2.5.4",
26-
"symfony/http-kernel":"~2.7",
25+
"symfony/http-foundation":"~2.7",
26+
"symfony/http-kernel":"~2.7.15|~2.8.8",
2727
"symfony/filesystem":"~2.3",
2828
"symfony/routing":"~2.6,>2.6.4",
2929
"symfony/security-core":"~2.6.13|~2.7.9|~2.8",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\HttpKernel\EventListener;
13+
14+
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
15+
useSymfony\Component\HttpKernel\Event\GetResponseEvent;
16+
useSymfony\Component\HttpKernel\KernelEvents;
17+
18+
/**
19+
* Validates that the headers and other information indicating the
20+
* client IP address of a request are consistent.
21+
*
22+
* @author Magnus Nordlander <magnus@fervo.se>
23+
*/
24+
class RequestListenerimplements EventSubscriberInterface
25+
{
26+
/**
27+
* Performs the validation.
28+
*
29+
* @param GetResponseEvent $event
30+
*/
31+
publicfunctiononKernelRequest(GetResponseEvent$event)
32+
{
33+
if (!$event->isMasterRequest()) {
34+
return;
35+
}
36+
$request =$event->getRequest();
37+
38+
if ($request::getTrustedProxies()) {
39+
// This will throw an exception if the headers are inconsistent.
40+
$request->getClientIps();
41+
}
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
publicstaticfunctiongetSubscribedEvents()
48+
{
49+
returnarray(
50+
KernelEvents::REQUEST =>array(
51+
array('onKernelRequest',256),
52+
),
53+
);
54+
}
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\HttpKernel\Tests\EventListener;
13+
14+
useSymfony\Component\EventDispatcher\EventDispatcher;
15+
useSymfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
16+
useSymfony\Component\HttpFoundation\Request;
17+
useSymfony\Component\HttpKernel\EventListener\RequestListener;
18+
useSymfony\Component\HttpKernel\Event\GetResponseEvent;
19+
useSymfony\Component\HttpKernel\HttpKernelInterface;
20+
useSymfony\Component\HttpKernel\KernelEvents;
21+
22+
class RequestListenerTestextends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* @expectedException Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException
26+
*/
27+
publicfunctiontestListenerThrowsWhenMasterRequestHasInconsistentClientIps()
28+
{
29+
$dispatcher =newEventDispatcher();
30+
$kernel =$this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
31+
32+
$request =newRequest();
33+
$request->setTrustedProxies(array('1.1.1.1'));
34+
$request->server->set('REMOTE_ADDR','1.1.1.1');
35+
$request->headers->set('FORWARDED','2.2.2.2');
36+
$request->headers->set('X_FORWARDED_FOR','3.3.3.3');
37+
38+
$dispatcher->addListener(KernelEvents::REQUEST,array(newRequestListener(),'onKernelRequest'));
39+
$event =newGetResponseEvent($kernel,$request, HttpKernelInterface::MASTER_REQUEST);
40+
41+
$dispatcher->dispatch(KernelEvents::REQUEST,$event);
42+
}
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp