|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Imaging; |
| 4 | + |
| 5 | +use Statamic\Exceptions\InvalidRemoteUrlException; |
| 6 | +use Statamic\Support\Str; |
| 7 | + |
| 8 | +class RemoteUrlValidator |
| 9 | +{ |
| 10 | + protected $resolver; |
| 11 | + |
| 12 | + public function __construct(?callable $resolver = null) |
| 13 | + { |
| 14 | + $this->resolver = $resolver ?? fn ($host) => dns_get_record($host, DNS_A + DNS_AAAA) ?: []; |
| 15 | + } |
| 16 | + |
| 17 | + public function parse($url) |
| 18 | + { |
| 19 | + $parsed = parse_url($url); |
| 20 | + |
| 21 | + if (! is_array($parsed)) { |
| 22 | + throw new InvalidRemoteUrlException('Invalid URL.'); |
| 23 | + } |
| 24 | + |
| 25 | + $scheme = strtolower($parsed['scheme'] ?? ''); |
| 26 | + |
| 27 | + if (! in_array($scheme, ['http', 'https'])) { |
| 28 | + throw new InvalidRemoteUrlException('Only http and https URLs are allowed.'); |
| 29 | + } |
| 30 | + |
| 31 | + if (isset($parsed['user']) || isset($parsed['pass'])) { |
| 32 | + throw new InvalidRemoteUrlException('URLs with credentials are not allowed.'); |
| 33 | + } |
| 34 | + |
| 35 | + $host = $parsed['host'] ?? null; |
| 36 | + |
| 37 | + if (! is_string($host) || $host === '') { |
| 38 | + throw new InvalidRemoteUrlException('URL host is required.'); |
| 39 | + } |
| 40 | + |
| 41 | + $host = Str::lower(trim($host)); |
| 42 | + |
| 43 | + if ($host !== trim($host, '.')) { |
| 44 | + throw new InvalidRemoteUrlException('Invalid URL host.'); |
| 45 | + } |
| 46 | + |
| 47 | + if (! $this->isValidHost($host)) { |
| 48 | + throw new InvalidRemoteUrlException('Invalid URL host.'); |
| 49 | + } |
| 50 | + |
| 51 | + $this->ensureHostResolvesToPublicIps($host); |
| 52 | + |
| 53 | + $port = isset($parsed['port']) ? ':'.$parsed['port'] : ''; |
| 54 | + |
| 55 | + return [ |
| 56 | + 'path' => Str::after($parsed['path'] ?? '/', '/'), |
| 57 | + 'base' => $scheme.'://'.$host.$port, |
| 58 | + 'query' => $parsed['query'] ?? null, |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + public function validate($url) |
| 63 | + { |
| 64 | + $this->parse($url); |
| 65 | + } |
| 66 | + |
| 67 | + protected function isValidHost($host) |
| 68 | + { |
| 69 | + return filter_var($host, FILTER_VALIDATE_IP) |
| 70 | + || filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME); |
| 71 | + } |
| 72 | + |
| 73 | + protected function ensureHostResolvesToPublicIps($host) |
| 74 | + { |
| 75 | + if (filter_var($host, FILTER_VALIDATE_IP)) { |
| 76 | + $this->assertPublicIp($host); |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + $records = call_user_func($this->resolver, $host); |
| 82 | + $ips = collect($records)->flatMap(function ($record) { |
| 83 | + return [$record['ip'] ?? null, $record['ipv6'] ?? null]; |
| 84 | + })->filter()->values()->all(); |
| 85 | + |
| 86 | + if (empty($ips)) { |
| 87 | + throw new InvalidRemoteUrlException('Unable to resolve URL host.'); |
| 88 | + } |
| 89 | + |
| 90 | + foreach ($ips as $ip) { |
| 91 | + $this->assertPublicIp($ip); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + protected function assertPublicIp($ip) |
| 96 | + { |
| 97 | + $result = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE); |
| 98 | + |
| 99 | + if (! $result) { |
| 100 | + throw new InvalidRemoteUrlException('Destination IP is not publicly routable.'); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments