-
-
Notifications
You must be signed in to change notification settings - Fork 83
Harden Oh Dear feed imports #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jbrooksuk
wants to merge
3
commits into
main
Choose a base branch
from
codex/harden-ohdear-import
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
|
|
||
| namespace Cachet\Actions\Integrations; | ||
|
|
||
| use Cachet\Data\ResolvedPublicUrl; | ||
| use InvalidArgumentException; | ||
|
|
||
| class ResolvePublicUrl | ||
| { | ||
| /** @var list<string> */ | ||
| private const BLOCKED_IPV4_RANGES = [ | ||
| '0.0.0.0/8', | ||
| '10.0.0.0/8', | ||
| '100.64.0.0/10', | ||
| '127.0.0.0/8', | ||
| '169.254.0.0/16', | ||
| '172.16.0.0/12', | ||
| '192.0.0.0/24', | ||
| '192.0.2.0/24', | ||
| '192.88.99.0/24', | ||
| '192.168.0.0/16', | ||
| '198.18.0.0/15', | ||
| '198.51.100.0/24', | ||
| '203.0.113.0/24', | ||
| '224.0.0.0/4', | ||
| '240.0.0.0/4', | ||
| ]; | ||
|
|
||
| /** @var list<string> */ | ||
| private const BLOCKED_IPV6_RANGES = [ | ||
| '::/8', | ||
| '2001::/23', | ||
| '2001:db8::/32', | ||
| '2002::/16', | ||
| '3fff::/20', | ||
| '5f00::/16', | ||
| 'fc00::/7', | ||
| 'fe80::/10', | ||
| 'fec0::/10', | ||
| 'ff00::/8', | ||
| ]; | ||
|
|
||
| /** | ||
| * Resolve and validate a URL before making an outbound request. | ||
| */ | ||
| public function handle(string $url): ResolvedPublicUrl | ||
| { | ||
| $parts = parse_url($url); | ||
|
|
||
| if (! is_array($parts) || ! isset($parts['scheme'], $parts['host'])) { | ||
| throw new InvalidArgumentException('The URL is invalid.'); | ||
| } | ||
|
|
||
| $scheme = strtolower($parts['scheme']); | ||
|
|
||
| if (! in_array($scheme, ['http', 'https'], true) || isset($parts['user'], $parts['pass'], $parts['query'], $parts['fragment'])) { | ||
| throw new InvalidArgumentException('The URL is invalid.'); | ||
| } | ||
|
|
||
| $host = trim($parts['host'], '[]'); | ||
| $addresses = $this->resolveAddresses($host); | ||
|
|
||
| if ($addresses === [] || collect($addresses)->contains(fn (string $address): bool => ! $this->isPublicAddress($address))) { | ||
| throw new InvalidArgumentException('The URL must resolve to a public address.'); | ||
| } | ||
|
|
||
| $port = $parts['port'] ?? ($scheme === 'https' ? 443 : 80); | ||
| $authority = str_contains($host, ':') ? "[{$host}]" : $host; | ||
|
|
||
| if (isset($parts['port'])) { | ||
| $authority .= ":{$port}"; | ||
| } | ||
|
|
||
| $path = rtrim($parts['path'] ?? '', '/'); | ||
|
|
||
| return new ResolvedPublicUrl( | ||
| url: "{$scheme}://{$authority}{$path}/json", | ||
| host: $host, | ||
| port: $port, | ||
| address: $addresses[0], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Resolve every address so mixed public/private DNS answers are rejected. | ||
| * | ||
| * @return list<string> | ||
| */ | ||
| private function resolveAddresses(string $host): array | ||
| { | ||
| if (filter_var($host, FILTER_VALIDATE_IP) !== false) { | ||
| return [$host]; | ||
| } | ||
|
|
||
| $records = dns_get_record($host, DNS_A | DNS_AAAA); | ||
|
|
||
| if ($records === false) { | ||
| return []; | ||
| } | ||
|
|
||
| $addresses = []; | ||
|
|
||
| foreach ($records as $record) { | ||
| $address = $record['ip'] ?? $record['ipv6'] ?? null; | ||
|
|
||
| if (is_string($address)) { | ||
| $addresses[] = $address; | ||
| } | ||
| } | ||
|
|
||
| return array_values(array_unique($addresses)); | ||
| } | ||
|
|
||
| private function isPublicAddress(string $address): bool | ||
| { | ||
| $ranges = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false | ||
| ? self::BLOCKED_IPV4_RANGES | ||
| : self::BLOCKED_IPV6_RANGES; | ||
|
|
||
| if (filter_var($address, FILTER_VALIDATE_IP) === false) { | ||
| return false; | ||
| } | ||
|
|
||
| return ! collect($ranges)->contains(fn (string $range): bool => $this->isInRange($address, $range)); | ||
| } | ||
|
|
||
| /** | ||
| * Compare packed addresses to avoid platform-dependent integer handling. | ||
| */ | ||
| private function isInRange(string $address, string $range): bool | ||
| { | ||
| [$network, $prefixLength] = explode('/', $range); | ||
| $addressBytes = inet_pton($address); | ||
| $networkBytes = inet_pton($network); | ||
|
|
||
| if ($addressBytes === false || $networkBytes === false || strlen($addressBytes) !== strlen($networkBytes)) { | ||
| return false; | ||
| } | ||
|
|
||
| $prefixLength = (int) $prefixLength; | ||
| $wholeBytes = intdiv($prefixLength, 8); | ||
| $remainingBits = $prefixLength % 8; | ||
|
|
||
| if (substr($addressBytes, 0, $wholeBytes) !== substr($networkBytes, 0, $wholeBytes)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ($remainingBits === 0) { | ||
| return true; | ||
| } | ||
|
|
||
| $mask = (0xFF << (8 - $remainingBits)) & 0xFF; | ||
|
|
||
| return (ord($addressBytes[$wholeBytes]) & $mask) === (ord($networkBytes[$wholeBytes]) & $mask); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace Cachet\Data; | ||
|
|
||
| final class ResolvedPublicUrl extends BaseData | ||
| { | ||
| public function __construct( | ||
| public readonly string $url, | ||
| public readonly string $host, | ||
| public readonly int $port, | ||
| public readonly string $address, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Format the validated DNS result for cURL address pinning. | ||
| */ | ||
| public function curlResolve(): string | ||
| { | ||
| $host = str_contains($this->host, ':') ? "[{$this->host}]" : $this->host; | ||
| $address = str_contains($this->address, ':') ? "[{$this->address}]" : $this->address; | ||
|
|
||
| return sprintf('%s:%d:%s', $host, $this->port, $address); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| use Cachet\Actions\Integrations\ResolvePublicUrl; | ||
|
|
||
| it('resolves a public IP URL for a pinned request', function () { | ||
| $resolvedUrl = app(ResolvePublicUrl::class)->handle('https://93.184.216.34/status/'); | ||
|
|
||
| expect($resolvedUrl->url)->toBe('https://93.184.216.34/status/json') | ||
| ->and($resolvedUrl->curlResolve())->toBe('93.184.216.34:443:93.184.216.34'); | ||
| }); | ||
|
|
||
| it('brackets public IPv6 addresses for a pinned request', function () { | ||
| $resolvedUrl = app(ResolvePublicUrl::class)->handle('https://[2606:4700:4700::1111]/'); | ||
|
|
||
| expect($resolvedUrl->url)->toBe('https://[2606:4700:4700::1111]/json') | ||
| ->and($resolvedUrl->curlResolve())->toBe('[2606:4700:4700::1111]:443:[2606:4700:4700::1111]'); | ||
| }); | ||
|
|
||
| it('rejects non-public destinations', function (string $url) { | ||
| app(ResolvePublicUrl::class)->handle($url); | ||
| })->throws(InvalidArgumentException::class)->with([ | ||
| 'loopback IPv4' => 'http://127.0.0.1', | ||
| 'private IPv4' => 'http://10.0.0.1', | ||
| 'link-local IPv4' => 'http://169.254.169.254', | ||
| 'multicast IPv4' => 'http://224.0.0.1', | ||
| 'loopback IPv6' => 'http://[::1]', | ||
| 'private IPv6' => 'http://[fc00::1]', | ||
| 'link-local IPv6' => 'http://[fe80::1]', | ||
| 'site-local IPv6' => 'http://[fec0::1]', | ||
| ]); | ||
|
|
||
| it('rejects unsupported URL schemes', function () { | ||
| app(ResolvePublicUrl::class)->handle('file:///etc/passwd'); | ||
| })->throws(InvalidArgumentException::class); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.