8000 Request: throw dedicted exceptions. · symfony/symfony@9807f06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9807f06

Browse files
committed
Request: throw dedicted exceptions.
1 parent fedbc3f commit 9807f06

File tree

8 files changed

+169
-14
lines changed

8 files changed

+169
-14
lines changed
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+
namespace Symfony\Component\HttpFoundation\Exception;
13+
14+
/**
15+
* THe HTTP request contains incorrect host data.
16+
*
17+
* @author SpacePossum
18+
*/
19+
abstract class AbstractHostException extends \UnexpectedValueException implements ExceptionInterface
20+
{
21+
/**
22+
* @var string
23+
*/
24+
private $host;
25+
26+
/**
27+
* @param string $host
28+
* @param string $message
29+
*/
30+
public function __construct($host, $message)
31+
{
32+
parent::__construct($message);
33+
$this->host = $host;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getHost()
40+
{
41+
return $this->host;
42+
}
43+
}

src/Symfony/Component/HttpFoundation/Exception/ConflictingHeadersException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
*
1919
* @author Magnus Nordlander <magnus@fervo.se>
2020
*/
21-
class ConflictingHeadersException extends \RuntimeException
21+
class ConflictingHeadersException extends \UnexpectedValueException implements ExceptionInterface
2222
{
2323
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\HttpFoundation\Exception;
13+
14+
/**
15+
* Base ExceptionInterface for the Request component.
16+
*
17+
* @author SpacePossum
18+
*/
19+
interface ExceptionInterface
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\HttpFoundation\Exception;
13+
14+
/**
15+
* The HTTP request contains invalid host data.
16+
*
17+
* @author SpacePossum
18+
*/
19+
final class InvalidHostException extends AbstractHostException
20+
{
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\HttpFoundation\Exception;
13+
14+
final class InvalidTrustedHeaderException extends \InvalidArgumentException implements ExceptionInterface
15+
{
16+
/**
17+
* @var string
18+
*/
19+
private $header;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $value;
25+
26+
/**
27+
* @param string $header
28+
* @param string|null $value
29+
* @param string $message
30+
*/
31+
public function __construct($header, $value, $message)
32+
{
33+
parent::__construct($message);
34+
$this->header = $header;
35+
$this->value = $value;
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\HttpFoundation\Exception;
13+
14+
/**
15+
* The HTTP request contains host data which is not trusted.
16+
*
17+
* @author SpacePossum
18+
*/
19+
final class UntrustedHostException extends AbstractHostException
20+
{
21+
}

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Symfony\Component\HttpFoundation;
1313

1414
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
15+
use Symfony\Component\HttpFoundation\Exception\InvalidHostException;
16+
use Symfony\Component\HttpFoundation\Exception\InvalidTrustedHeaderException;
17+
use Symfony\Component\HttpFoundation\Exception\UntrustedHostException;
1518
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1619

1720
/**
@@ -602,12 +605,12 @@ public static function getTrustedHosts()
602605
* @param string $key The header key
603606
* @param string $value The header name
604607
*
605-
* @throws \InvalidArgumentException
608+
* @throws InvalidTrustedHeaderException
606609
*/
607610
public static function setTrustedHeaderName($key, $value)
608611
{
609612
if (!array_key_exists($key, self::$trustedHeaders)) {
610-
throw new \InvalidArgumentException(sprintf('Unab F987 le to set the trusted header name for key "%s".', $key));
613+
throw new InvalidTrustedHeaderException($key, $value, sprintf('Unable to set the trusted header name for key "%s".', $key));
611614
}
612615

613616
self::$trustedHeaders[$key] = $value;
@@ -620,12 +623,12 @@ public static function setTrustedHeaderName($key, $value)
620623
*
621624
* @return string The header name
622625
*
623-
* @throws \InvalidArgumentException
626+
* @throws InvalidTrustedHeaderException
624627
*/
625628
public static function getTrustedHeaderName($key)
626629
{
627630
if (!array_key_exists($key, self::$trustedHeaders)) {
628-
throw new \InvalidArgumentException(sprintf('Unable to get the trusted header name for key "%s".', $key));
631+
throw new InvalidTrustedHeaderException($key, null, sprintf('Unable to get the trusted header name for key "%s".', $key));
629632
}
630633

631634
return self::$trustedHeaders[$key];
@@ -788,6 +791,8 @@ public function setSession(SessionInterface $session)
788791
*
789792
* @return array The client IP addresses
790793
*
794+
* @throws ConflictingHeadersException
795+
*
791796
* @see getClientIp()
792797
*/
793798
public function getClientIps()
@@ -819,7 +824,7 @@ public function getClientIps()
819824
}
820825

821826
if ($hasTrustedForwardedHeader && $hasTrustedClientIpHeader && $forwardedClientIps !== $xForwardedForClientIps) {
822-
throw new ConflictingHeadersException('The request has both a trusted Forwarded header and a trusted Client IP header, conflicting with each other with regards to the originating IP addresses of the request. This is the result of a misconfiguration. You should either configure your proxy only to send one of these headers, or configure Symfony to distrust one of them.');
827+
throw new ConflictingHeadersException('The request has both a trusted Forwarded header and a trusted Client IP header, conflicting with each other with regards to the originating IP addresses of the request. This is the result of a misconfiguration. You should either configure your proxy only to send one of these headers, or configure your project to distrust one of them.');
823828
}
824829

825830
if (!$hasTrustedForwardedHeader && !$hasTrustedClientIpHeader) {
@@ -1198,7 +1203,8 @@ public function isSecure()
11981203
*
11991204
* @return string
12001205
*
1201-
* @throws \UnexpectedValueException when the host name is invalid
1206+
* @throws InvalidHostException when the host name is invalid
1207+
* @throws UntrustedHostException when the host is not trusted
12021208
*/
12031209
public function getHost()
12041210
{
@@ -1220,7 +1226,7 @@ public function getHost()
12201226
// check that it does not contain forbidden characters (see RFC 952 and RFC 2181)
12211227
// use preg_replace() instead of preg_match() to prevent DoS attacks with long host names
12221228
if ($host && '' !== preg_replace('/(?:^\[)?[a-zA-Z0-9-:\]_]+\.?/', '', $host)) {
1223-
throw new \UnexpectedValueException(sprintf('Invalid Host "%s"', $host));
1229+
throw new InvalidHostException($host, sprintf('Invalid host "%s".', $host));
12241230
}
12251231

12261232
if (count(self::$trustedHostPatterns) > 0) {
@@ -1238,7 +1244,7 @@ public function getHost()
12381244
}
12391245
}
12401246

1241-
throw new \UnexpectedValueException(sprintf('Untrusted Host "%s"', $host));
1247+
throw new UntrustedHostException($host, sprintf('Untrusted host "%s".', $host));
12421248
}
12431249

12441250
return $host;
@@ -1515,7 +1521,7 @@ public function getContent($asResource = false)
15151521
{
15161522
$currentContentIsResource = is_resource($this->content);
15171523
if (PHP_VERSION_ID < 50600 && false === $this->content) {
1518-
throw new \LogicException('getContent() can only be called once when using the resource return type and PHP below 5.6.');
1524+
throw new \LogicException(sprintf('Method %s can only be called once when using the resource return type and PHP below 5.6.', __METHOD__));
15191525
}
15201526

15211527
if (true === $asResource) {
@@ -1940,7 +1946,12 @@ private static function createRequestFromFactory(array $query = array(), array $
19401946
$request = call_user_func(self::$requestFactory, $query, $request, $attributes, $cookies, $files, $server, $content);
19411947

19421948
if (!$request instanceof self) {
1943-
throw new \LogicException('The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.');
1949+
throw new \UnexpectedValueException(
1950+
sprintf(
1951+
'The Request factory must return an instance of %s. Got %s.',
1952+
__CLASS__, is_object($request) ? get_class($request) : (null === $request ? 'null' : gettype($request))
1953+
)
1954+
);
19441955
}
19451956

19461957
return $request;

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

14+
use Symfony\Component\HttpFoundation\Exception\UntrustedHostException;
1415
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1516
use Symfony\Component\HttpFoundation\Session\Session;
1617
use Symfony\Component\HttpFoundation\Request;
@@ -1872,8 +1873,8 @@ public function testTrustedHosts()
18721873
try {
18731874
$request->getHost();
18741875
$this->fail('Request::getHost() should throw an exception when host is not trusted.');
1875-
} catch (\UnexpectedValueException $e) {
1876-
$this->assertEquals('Untrusted Host "evil.com"', $e->getMessage());
1876+
} catch (UntrustedHostException $e) {
1877+
$this->assertEquals('Untrusted host "evil.com".', $e->getMessage());
18771878
}
18781879

18791880
// trusted hosts
@@ -1936,7 +1937,7 @@ public function testHostValidity($host, $isValid, $expectedHost = null, $expecte
19361937
$this->assertSame($expectedPort, $request->getPort());
19371938
}
19381939
} else {
1939-
$this->setExpectedException('UnexpectedValueException', 'Invalid Host');
1940+
$this->setExpectedException('Symfony\Component\HttpFoundation\Exception\InvalidHostException', 'Invalid host');
19401941
$request->getHost();
19411942
}
19421943
}

0 commit comments

Comments
 (0)
0