Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/private/Http/Client/DnsPinMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ private function soaRecord(string $target): ?array {
$top = count($labels) >= 2 ? array_pop($labels) : '';
$second = array_pop($labels);

$hostname = $second . '.' . $top;
// Before looking up any DNS record, we need to make sure the
// provided target is an FQDN by adding a dot to the end.
$hostname = $second . '.' . $top . '.';
$responses = $this->dnsGetRecord($hostname, DNS_SOA);

if ($responses === false || count($responses) === 0) {
Expand All @@ -59,6 +61,10 @@ private function dnsResolve(string $target, int $recursionCount) : array {
$dnsTypes = \defined('AF_INET6') || @inet_pton('::1')
? [DNS_A, DNS_AAAA, DNS_CNAME]
: [DNS_A, DNS_CNAME];

// Before looking up any DNS record, we need to make sure the
// provided target is an FQDN by adding a dot to the end.
$target = str_ends_with($target, '.') ? $target : "$target.";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$target = str_ends_with($target, '.') ? $target : "$target.";
if (!str_ends_with($target, '.')) {
$target .= '.';
}

Copy link
Contributor

@kesselb kesselb Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer rtrim($target, '.') . '.' ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe through a small helper function enforceFdqn or such.

foreach ($dnsTypes as $dnsType) {
if ($canHaveCnameRecord === false && $dnsType === DNS_CNAME) {
continue;
Expand Down
36 changes: 18 additions & 18 deletions tests/lib/Http/Client/DnsPinMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static function (RequestInterface $request, array $options) {
->method('dnsGetRecord')
->willReturnCallback(function (string $hostname, int $type) {
// example.com SOA
if ($hostname === 'example.com') {
if ($hostname === 'example.com.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -76,7 +76,7 @@ static function (RequestInterface $request, array $options) {
}

// example.com A, AAAA, CNAME
if ($hostname === 'www.example.com') {
if ($hostname === 'www.example.com.') {
return match ($type) {
DNS_A => [],
DNS_AAAA => [],
Expand All @@ -93,7 +93,7 @@ static function (RequestInterface $request, array $options) {
}

// example.net SOA
if ($hostname === 'example.net') {
if ($hostname === 'example.net.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -108,7 +108,7 @@ static function (RequestInterface $request, array $options) {
}

// example.net A, AAAA, CNAME
if ($hostname === 'www.example.net') {
if ($hostname === 'www.example.net.') {
return match ($type) {
DNS_A => [
[
Expand Down Expand Up @@ -154,7 +154,7 @@ static function (RequestInterface $request, array $options) {
->method('dnsGetRecord')
->willReturnCallback(function (string $hostname, int $type) {
// example.com SOA
if ($hostname === 'example.com') {
if ($hostname === 'example.com.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -169,7 +169,7 @@ static function (RequestInterface $request, array $options) {
}

// example.com A, AAAA, CNAME
if ($hostname === 'www.example.com') {
if ($hostname === 'www.example.com.') {
return match ($type) {
DNS_A => [],
DNS_AAAA => [],
Expand All @@ -186,7 +186,7 @@ static function (RequestInterface $request, array $options) {
}

// example.net SOA
if ($hostname === 'example.net') {
if ($hostname === 'example.net.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -201,7 +201,7 @@ static function (RequestInterface $request, array $options) {
}

// example.net A, AAAA, CNAME
if ($hostname === 'www.example.net') {
if ($hostname === 'www.example.net.') {
return match ($type) {
DNS_A => [
[
Expand Down Expand Up @@ -378,7 +378,7 @@ static function (RequestInterface $request, array $options): void {
->method('dnsGetRecord')
->willReturnCallback(function (string $hostname, int $type) {
// example.com SOA
if ($hostname === 'example.com') {
if ($hostname === 'example.com.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -393,7 +393,7 @@ static function (RequestInterface $request, array $options): void {
}

// example.com A, AAAA, CNAME
if ($hostname === 'www.example.com') {
if ($hostname === 'www.example.com.') {
return match ($type) {
DNS_A => [],
DNS_AAAA => [],
Expand All @@ -410,7 +410,7 @@ static function (RequestInterface $request, array $options): void {
}

// example.net SOA
if ($hostname === 'example.net') {
if ($hostname === 'example.net.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -425,7 +425,7 @@ static function (RequestInterface $request, array $options): void {
}

// example.net A, AAAA, CNAME
if ($hostname === 'www.example.net') {
if ($hostname === 'www.example.net.') {
return match ($type) {
DNS_A => [
[
Expand Down Expand Up @@ -496,7 +496,7 @@ static function (RequestInterface $request, array $options): void {
$dnsQueries[] = $hostname . $type;

// example.com SOA
if ($hostname === 'example.com') {
if ($hostname === 'example.com.') {
return match ($type) {
DNS_SOA => [
[
Expand All @@ -511,7 +511,7 @@ static function (RequestInterface $request, array $options): void {
}

// example.net A, AAAA, CNAME
if ($hostname === 'subsubdomain.subdomain.example.com') {
if ($hostname === 'subsubdomain.subdomain.example.com.') {
return match ($type) {
DNS_A => [
[
Expand Down Expand Up @@ -540,10 +540,10 @@ static function (RequestInterface $request, array $options): void {
);

$this->assertCount(3, $dnsQueries);
$this->assertContains('example.com' . DNS_SOA, $dnsQueries);
$this->assertContains('subsubdomain.subdomain.example.com' . DNS_A, $dnsQueries);
$this->assertContains('subsubdomain.subdomain.example.com' . DNS_AAAA, $dnsQueries);
$this->assertContains('example.com.' . DNS_SOA, $dnsQueries);
$this->assertContains('subsubdomain.subdomain.example.com.' . DNS_A, $dnsQueries);
$this->assertContains('subsubdomain.subdomain.example.com.' . DNS_AAAA, $dnsQueries);
// CNAME should not be queried if A or AAAA succeeded already
$this->assertNotContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries);
$this->assertNotContains('subsubdomain.subdomain.example.com.' . DNS_CNAME, $dnsQueries);
}
}
Loading