| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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\HttpClient; |
| 13 |
|
| 14 |
use Psr\Log\LoggerAwareInterface; |
| 15 |
use Psr\Log\LoggerInterface; |
| 16 |
use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 17 |
use Symfony\Component\HttpClient\Exception\TransportException; |
| 18 |
use Symfony\Component\HttpFoundation\IpUtils; |
| 19 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 20 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 21 |
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 22 |
use Symfony\Contracts\Service\ResetInterface; |
| 23 |
|
| 24 |
/** |
| 25 |
* Decorator that blocks requests to private networks by default. |
| 26 |
* |
| 27 |
* @author Hallison Boaventura <[email protected]> |
| 28 |
*/ |
| 29 |
final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface |
| 30 |
{ |
| 31 |
use HttpClientTrait; |
| 32 |
|
| 33 |
private const PRIVATE_SUBNETS = [ |
| 34 |
'127.0.0.0/8', |
| 35 |
'10.0.0.0/8', |
| 36 |
'192.168.0.0/16', |
| 37 |
'172.16.0.0/12', |
| 38 |
'169.254.0.0/16', |
| 39 |
'0.0.0.0/8', |
| 40 |
'240.0.0.0/4', |
| 41 |
'::1/128', |
| 42 |
'fc00::/7', |
| 43 |
'fe80::/10', |
| 44 |
'::ffff:0:0/96', |
| 45 |
'::/128', |
| 46 |
]; |
| 47 |
|
| 48 |
private $client; |
| 49 |
private $subnets; |
| 50 |
|
| 51 |
/** |
| 52 |
* @param string|array|null $subnets String or array of subnets using CIDR notation that will be used by IpUtils. |
| 53 |
* If null is passed, the standard private subnets will be used. |
| 54 |
*/ |
| 55 |
public function __construct(HttpClientInterface $client, $subnets = null) |
| 56 |
{ |
| 57 |
if (!(\is_array($subnets) || \is_string($subnets) || null === $subnets)) { |
| 58 |
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be of the type array, string or null. "%s" given.', __METHOD__, get_debug_type($subnets))); |
| 59 |
} |
| 60 |
|
| 61 |
if (!class_exists(IpUtils::class)) { |
| 62 |
throw new \LogicException(sprintf('You cannot use "%s" if the HttpFoundation component is not installed. Try running "composer require symfony/http-foundation".', __CLASS__)); |
| 63 |
} |
| 64 |
|
| 65 |
$this->client = $client; |
| 66 |
$this->subnets = $subnets; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritdoc} |
| 71 |
*/ |
| 72 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 73 |
{ |
| 74 |
$onProgress = $options['on_progress'] ?? null; |
| 75 |
if (null !== $onProgress && !\is_callable($onProgress)) { |
| 76 |
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress))); |
| 77 |
} |
| 78 |
|
| 79 |
$subnets = $this->subnets; |
| 80 |
$lastPrimaryIp = ''; |
| 81 |
|
| 82 |
$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, &$lastPrimaryIp): void { |
| 83 |
if ($info['primary_ip'] !== $lastPrimaryIp) { |
| 84 |
if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? self::PRIVATE_SUBNETS)) { |
| 85 |
throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url'])); |
| 86 |
} |
| 87 |
|
| 88 |
$lastPrimaryIp = $info['primary_ip']; |
| 89 |
} |
| 90 |
|
| 91 |
null !== $onProgress && $onProgress($dlNow, $dlSize, $info); |
| 92 |
}; |
| 93 |
|
| 94 |
return $this->client->request($method, $url, $options); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* {@inheritdoc} |
| 99 |
*/ |
| 100 |
public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 101 |
{ |
| 102 |
return $this->client->stream($responses, $timeout); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* {@inheritdoc} |
| 107 |
*/ |
| 108 |
public function setLogger(LoggerInterface $logger): void |
| 109 |
{ |
| 110 |
if ($this->client instanceof LoggerAwareInterface) { |
| 111 |
$this->client->setLogger($logger); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* {@inheritdoc} |
| 117 |
*/ |
| 118 |
public function withOptions(array $options): self |
| 119 |
{ |
| 120 |
$clone = clone $this; |
| 121 |
$clone->client = $this->client->withOptions($options); |
| 122 |
|
| 123 |
return $clone; |
| 124 |
} |
| 125 |
|
| 126 |
public function reset() |
| 127 |
{ |
| 128 |
if ($this->client instanceof ResetInterface) { |
| 129 |
$this->client->reset(); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|