Skip to content

Commit

Permalink
account for potentially false return value of dns_get_record
Browse files Browse the repository at this point in the history
  • Loading branch information
transistive committed May 4, 2023
1 parent 01411f8 commit 8a83f4f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Common/DNSAddressResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ public function getAddresses(string $host): iterable
yield $host;

try {
/** @var list<array{ip?: string|null}> $records */
/** @var list<array{ip?: string|null}>|false $records */
$records = dns_get_record($host, DNS_A | DNS_AAAA);
} catch (Throwable) {
$records = []; // Failed DNS queries should not halt execution
}

if ($records === false) {
$records = [];
}

if (count($records) === 0) {
yield from $this->tryReverseLookup($host);
} else {
Expand All @@ -59,12 +63,16 @@ public function getAddresses(string $host): iterable
private function tryReverseLookup(string $host): iterable
{
try {
/** @var list<array{target?: string|null}> $records */
/** @var list<array{target?: string|null}>|false $records */
$records = dns_get_record($host.'.in-addr.arpa');
} catch (Throwable) {
$records = []; // Failed DNS queries should not halt execution
}

if ($records === false) {
$records = [];
}

if (count($records) !== 0) {
$records = array_map(static fn (array $x) => $x['target'] ?? '', $records);
$records = array_filter($records, static fn (string $x) => $x !== '');
Expand Down

0 comments on commit 8a83f4f

Please sign in to comment.