NoPrivateNetworkHttpClient.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 string|array|null $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, string|array $subnets = null)
  49. {
  50. if (!class_exists(IpUtils::class)) {
  51. throw new \LogicException(sprintf('You cannot use "%s" if the HttpFoundation component is not installed. Try running "composer require symfony/http-foundation".', __CLASS__));
  52. }
  53. $this->client = $client;
  54. $this->subnets = $subnets;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function request(string $method, string $url, array $options = []): ResponseInterface
  60. {
  61. $onProgress = $options['on_progress'] ?? null;
  62. if (null !== $onProgress && !\is_callable($onProgress)) {
  63. throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, "%s" given.', get_debug_type($onProgress)));
  64. }
  65. $subnets = $this->subnets;
  66. $lastPrimaryIp = '';
  67. $options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, &$lastPrimaryIp): void {
  68. if ($info['primary_ip'] !== $lastPrimaryIp) {
  69. if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? self::PRIVATE_SUBNETS)) {
  70. throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url']));
  71. }
  72. $lastPrimaryIp = $info['primary_ip'];
  73. }
  74. null !== $onProgress && $onProgress($dlNow, $dlSize, $info);
  75. };
  76. return $this->client->request($method, $url, $options);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function stream(ResponseInterface|iterable $responses, float $timeout = null): ResponseStreamInterface
  82. {
  83. return $this->client->stream($responses, $timeout);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setLogger(LoggerInterface $logger): void
  89. {
  90. if ($this->client instanceof LoggerAwareInterface) {
  91. $this->client->setLogger($logger);
  92. }
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function withOptions(array $options): static
  98. {
  99. $clone = clone $this;
  100. $clone->client = $this->client->withOptions($options);
  101. return $clone;
  102. }
  103. public function reset()
  104. {
  105. if ($this->client instanceof ResetInterface) {
  106. $this->client->reset();
  107. }
  108. }
  109. }