NoPrivateNetworkHttpClient.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpClient;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  14. use Symfony\Component\HttpClient\Exception\TransportException;
  15. use Symfony\Component\HttpFoundation\IpUtils;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  19. /**
  20. * Decorator that blocks requests to private networks by default.
  21. *
  22. * @author Hallison Boaventura <hallisonboaventura@gmail.com>
  23. */
  24. final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwareInterface
  25. {
  26. use HttpClientTrait;
  27. private const PRIVATE_SUBNETS = [
  28. '127.0.0.0/8',
  29. '10.0.0.0/8',
  30. '192.168.0.0/16',
  31. '172.16.0.0/12',
  32. '169.254.0.0/16',
  33. '0.0.0.0/8',
  34. '240.0.0.0/4',
  35. '::1/128',
  36. 'fc00::/7',
  37. 'fe80::/10',
  38. '::ffff:0:0/96',
  39. '::/128',
  40. ];
  41. private $client;
  42. private $subnets;
  43. /**
  44. * @param string|array|null $subnets String or array of subnets using CIDR notation that will be used by IpUtils.
  45. * If null is passed, the standard private subnets will be used.
  46. */
  47. public function __construct(HttpClientInterface $client, $subnets = null)
  48. {
  49. if (!(\is_array($subnets) || \is_string($subnets) || null === $subnets)) {
  50. 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)));
  51. }
  52. if (!class_exists(IpUtils::class)) {
  53. throw new \LogicException(sprintf('You can not use "%s" if the HttpFoundation component is not installed. Try running "composer require symfony/http-foundation".', __CLASS__));
  54. }
  55. $this->client = $client;
  56. $this->subnets = $subnets;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function request(string $method, string $url, array $options = []): ResponseInterface
  62. {
  63. $onProgress = $options['on_progress'] ?? null;
  64. if (null !== $onProgress && !\is_callable($onProgress)) {
  65. throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress)));
  66. }
  67. $subnets = $this->subnets;
  68. $lastPrimaryIp = '';
  69. $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, &$lastPrimaryIp): void {
  70. if ($info['primary_ip'] !== $lastPrimaryIp) {
  71. if (IpUtils::checkIp($info['primary_ip'], $subnets ?? self::PRIVATE_SUBNETS)) {
  72. throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url']));
  73. }
  74. $lastPrimaryIp = $info['primary_ip'];
  75. }
  76. null !== $onProgress && $onProgress($dlNow, $dlSize, $info);
  77. };
  78. return $this->client->request($method, $url, $options);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function stream($responses, float $timeout = null): ResponseStreamInterface
  84. {
  85. return $this->client->stream($responses, $timeout);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function setLogger(LoggerInterface $logger): void
  91. {
  92. if ($this->client instanceof LoggerAwareInterface) {
  93. $this->client->setLogger($logger);
  94. }
  95. }
  96. }