NoPrivateNetworkHttpClient.php 4.1 KB

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