Skip to content

Commit 080962f

Browse files
committed
Merge branch '3.0' into 3.1
* 3.0: [HttpKernel] Add listener that checks when request has both Forwarded and X-Forwarded-For [HttpKernel] Move conflicting origin IPs handling to catch block [travis] Fix deps=low/high patching
2 parents 4ac9f72 + 9c0ca19 commit 080962f

File tree

8 files changed

+119
-25
lines changed

8 files changed

+119
-25
lines changed

.travis.php renamed to .github/travis.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<?php
22

33
if (4 > $_SERVER['argc']) {
4-
echo "Usage: branch dir1 dir2 ... dirN\n";
4+
echo "Usage: branch version dir1 dir2 ... dirN\n";
55
exit(1);
66
}
77

88
$dirs = $_SERVER['argv'];
99
array_shift($dirs);
1010
$branch = array_shift($dirs);
11+
$version = array_shift($dirs);
1112

1213
$packages = array();
1314
$flags = PHP_VERSION_ID >= 50400 ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE : 0;
1415

1516
foreach ($dirs as $dir) {
16-
if (!`git diff --name-only $branch...HEAD -- $dir`) {
17+
if (!system("git diff --name-only $branch...HEAD -- $dir", $exitStatus)) {
18+
if ($exitStatus) {
19+
exit($exitStatus);
20+
}
1721
continue;
1822
}
1923
echo "$dir\n";
@@ -32,7 +36,7 @@
3236
file_put_contents($dir.'/composer.json', $json);
3337
passthru("cd $dir && tar -cf package.tar --exclude='package.tar' *");
3438

35-
$package->version = 'master' !== $branch ? $branch.'.x-dev' : 'dev-master';
39+
$package->version = 'master' !== $version ? $version.'.x-dev' : 'dev-master';
3640
$package->dist['type'] = 'tar';
3741
$package->dist['url'] = 'file://'.__DIR__."/$dir/package.tar";
3842

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ before_install:
7171
install:
7272
- if [[ ! $skip ]]; then COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n'); fi
7373
# Create local composer packages for each patched components and reference them in composer.json files when cross-testing components
74-
- if [[ ! $skip && $deps ]]; then php .travis.php $TRAVIS_BRANCH $COMPONENTS; fi
74+
- if [[ ! $skip && $deps ]]; then git fetch origin $TRAVIS_BRANCH && php .github/travis.php FETCH_HEAD $TRAVIS_BRANCH $COMPONENTS; fi
7575
# For the master branch when deps=high, the version before master is checked out and tested with the locally patched components
7676
- if [[ $deps = high && $TRAVIS_BRANCH = master ]]; then SYMFONY_VERSION=$(git ls-remote --heads | grep -o '/[1-9].*' | tail -n 1 | sed s/.//); else SYMFONY_VERSION=$(cat composer.json | grep '^ *"dev-master". *"[1-9]' | grep -o '[0-9.]*'); fi
7777
- if [[ $deps = high && $TRAVIS_BRANCH = master ]]; then git fetch origin $SYMFONY_VERSION; git checkout -m FETCH_HEAD; COMPONENTS=$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n'); ./phpunit install; fi

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,9 @@
6161
<argument type="service" id="request_stack" />
6262
<tag name="kernel.event_subscriber" />
6363
</service>
64+
65+
<service id="validate_request_listener" class="Symfony\Component\HttpKernel\EventListener\ValidateRequestListener">
66+
<tag name="kernel.event_subscriber" />
67+
</service>
6468
</services>
6569
</container>

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/config": "~2.8|~3.0",
2525
"symfony/event-dispatcher": "~2.8|~3.0",
2626
"symfony/http-foundation": "~3.1",
27-
"symfony/http-kernel": "~3.1",
27+
"symfony/http-kernel": "~3.1.2|~3.2",
2828
"symfony/polyfill-mbstring": "~1.0",
2929
"symfony/filesystem": "~2.8|~3.0",
3030
"symfony/finder": "~2.8|~3.0",
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+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16+
use Symfony\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 ValidateRequestListener implements EventSubscriberInterface
25+
{
26+
/**
27+
* Performs the validation.
28+
*
29+
* @param GetResponseEvent $event
30+
*/
31+
public function onKernelRequest(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+
public static function getSubscribedEvents()
48+
{
49+
return array(
50+
KernelEvents::REQUEST => array(
51+
array('onKernelRequest', 256),
52+
),
53+
);
54+
}
55+
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
6767
try {
6868
return $this->handleRaw($request, $type);
6969
} catch (\Exception $e) {
70+
if ($e instanceof ConflictingHeadersException) {
71+
$e = new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
72+
}
7073
if (false === $catch) {
7174
$this->finishRequest($request, $type);
7275

@@ -119,13 +122,6 @@ public function terminateWithException(\Exception $exception)
119122
*/
120123
private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
121124
{
122-
if (self::MASTER_REQUEST === $type && $request::getTrustedProxies()) {
123-
try {
124-
$request->getClientIps();
125-
} catch (ConflictingHeadersException $e) {
126-
throw new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
127-
}
128-
}
129125
$this->requestStack->push($request);
130126

131127
// request
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
17+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
use Symfony\Component\HttpKernel\KernelEvents;
20+
21+
class ValidateRequestListenerTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @expectedException Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException
25+
*/
26+
public function testListenerThrowsWhenMasterRequestHasInconsistentClientIps()
27+
{
28+
$dispatcher = new EventDispatcher();
29+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
30+
31+
$request = new Request();
32+
$request->setTrustedProxies(array('1.1.1.1'));
33+
$request->server->set('REMOTE_ADDR', '1.1.1.1');
34+
$request->headers->set('FORWARDED', '2.2.2.2');
35+
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
36+
37+
$dispatcher->addListener(KernelEvents::REQUEST, array(new ValidateRequestListener(), 'onKernelRequest'));
38+
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
39+
40+
$dispatcher->dispatch(KernelEvents::REQUEST, $event);
41+
}
42+
}

src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,26 +307,19 @@ public function testVerifyRequestStackPushPopDuringHandle()
307307
*/
308308
public function testInconsistentClientIpsOnMasterRequests()
309309
{
310-
$kernel = $this->getHttpKernel(new EventDispatcher());
311310
$request = new Request();
312311
$request->setTrustedProxies(array('1.1.1.1'));
313312
$request->server->set('REMOTE_ADDR', '1.1.1.1');
314313
$request->headers->set('FORWARDED', '2.2.2.2');
315314
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
316315

317-
$kernel->handle($request, $kernel::MASTER_REQUEST, false);
318-
}
319-
320-
public function testInconsistentClientIpsOnSubRequests()
321-
{
322-
$kernel = $this->getHttpKernel(new EventDispatcher());
323-
$request = new Request();
324-
$request->setTrustedProxies(array('1.1.1.1'));
325-
$request->server->set('REMOTE_ADDR', '1.1.1.1');
326-
$request->headers->set('FORWARDED', '2.2.2.2');
327-
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
316+
$dispatcher = new EventDispatcher();
317+
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
318+
$event->getRequest()->getClientIp();
319+
});
328320

329-
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $kernel->handle($request, $kernel::SUB_REQUEST, false));
321+
$kernel = $this->getHttpKernel($dispatcher);
322+
$kernel->handle($request, $kernel::MASTER_REQUEST, false);
330323
}
331324

332325
private function getHttpKernel(EventDispatcherInterface $eventDispatcher, $controller = null, RequestStack $requestStack = null, array $arguments = array())

0 commit comments

Comments
 (0)