From 41d02a30bb69c573cbf3d9329f06dcbfa591f050 Mon Sep 17 00:00:00 2001 From: Austin Heap Date: Sat, 31 Mar 2018 08:56:32 -0700 Subject: [PATCH] Use ext-filter instead of inet_pton to avoid error suppression (#73) --- composer.json | 3 ++- lib/BasicResolver.php | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 77ced0c..400e094 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,8 @@ "amphp/parser": "^1", "amphp/uri": "^0.1", "amphp/windows-registry": "^0.3", - "daverandom/libdns": "^2.0.1" + "daverandom/libdns": "^2.0.1", + "ext-filter": "*" }, "require-dev": { "amphp/phpunit-util": "^1", diff --git a/lib/BasicResolver.php b/lib/BasicResolver.php index bd88ff8..86df5b2 100644 --- a/lib/BasicResolver.php +++ b/lib/BasicResolver.php @@ -89,23 +89,28 @@ final class BasicResolver implements Resolver { yield $this->reloadConfig(); } - $inAddr = @\inet_pton($name); - - if ($inAddr !== false) { - // It's already a valid IP, don't query, immediately return - if ($typeRestriction) { - if ($typeRestriction === Record::A && isset($inAddr[4])) { + switch ($typeRestriction) { + case Record::A: + if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + return [new Record($name, Record::A, null)]; + } elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { throw new ResolutionException("Got an IPv6 address, but type is restricted to IPv4"); } - - if ($typeRestriction === Record::AAAA && !isset($inAddr[4])) { + break; + case Record::AAAA: + if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + return [new Record($name, Record::AAAA, null)]; + } elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { throw new ResolutionException("Got an IPv4 address, but type is restricted to IPv6"); } - } - - return [ - new Record($name, isset($inAddr[4]) ? Record::AAAA : Record::A, null), - ]; + break; + default: + if (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { + return [new Record($name, Record::A, null)]; + } elseif (filter_var($name, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { + return [new Record($name, Record::AAAA, null)]; + } + break; } $name = normalizeDnsName($name);