Skip to content
Draft
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
2 changes: 1 addition & 1 deletion lib/private/Http/Client/DnsPinMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
$ports[] = (string)$port;
}

$targetIps = $this->dnsResolve(idn_to_utf8($hostName), 0);
$targetIps = $this->dnsResolve(idn_to_ascii($hostName, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, null), 0);

Check failure on line 116 in lib/private/Http/Client/DnsPinMiddleware.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidPassByReference

lib/private/Http/Client/DnsPinMiddleware.php:116:99: InvalidPassByReference: Parameter 4 of idn_to_ascii expects a variable (see https://psalm.dev/102)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
$targetIps = $this->dnsResolve(idn_to_ascii($hostName, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, null), 0);
$targetIps = $this->dnsResolve(idn_to_ascii($hostName, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46), 0);


if (empty($targetIps)) {
throw new LocalServerException('No DNS record found for ' . $hostName);
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/Http/Client/DnsPinMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,112 @@ static function (RequestInterface $request, array $options): void {
// CNAME should not be queried if A or AAAA succeeded already
$this->assertNotContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries);
}

public function testDnsGetRecordCalledWithPunycode() {
// Unicode hostname with umlaut (IDN)
$unicodeHost = 'bücher.com';
$punycodeHost = idn_to_ascii($unicodeHost, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);

// We expect that the middleware will call dnsGetRecord with the Punycode (not Unicode)
$this->dnsPinMiddleware
->expects($this->atLeastOnce())
->method('dnsGetRecord')
->with(
$this->callback(function ($hostname) use ($punycodeHost) {
// Should never be raw Unicode here!
$this->assertEquals($punycodeHost, $hostname, "dnsGetRecord should be called with Punycode ASCII host");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
$this->assertEquals($punycodeHost, $hostname, "dnsGetRecord should be called with Punycode ASCII host");
$this->assertEquals($punycodeHost, $hostname, 'dnsGetRecord should be called with Punycode ASCII host');

For a happy linter ;)

return true;
}),
$this->anything()
)
->willReturn([
[
'host' => $punycodeHost,
'class' => 'IN',
'ttl' => 1800,
'type' => 'A',
'ip' => '203.0.113.5'
]
]);

$stack = new HandlerStack(new MockHandler([
static fn () => new Response(200)
]));
$stack->push($this->dnsPinMiddleware->addDnsPinning());
$handler = $stack->resolve();

$handler(
new Request('GET', "https://$unicodeHost"),
['nextcloud' => ['allow_local_address' => false]]
);
}

public function testDnsGetRecordWithRawUnicodeFailsGracefully() {
// Simulate a middleware bug where Unicode is passed to dns_get_record
$unicodeHost = 'bücher.com';

$this->dnsPinMiddleware
->method('dnsGetRecord')
->willReturnCallback(function ($hostname, $type) use ($unicodeHost) {
if ($hostname === $unicodeHost) {
// Simulate real dns_get_record failure (returns false)
return false;
}
return [
[
'host' => $hostname,
'class' => 'IN',
'ttl' => 1800,
'type' => 'A',
'ip' => '203.0.113.5'
]
];
});

$stack = new HandlerStack(new MockHandler([
static fn () => new Response(200)
]));
$stack->push($this->dnsPinMiddleware->addDnsPinning());
$handler = $stack->resolve();

$this->expectException(LocalServerException::class);
$this->expectExceptionMessage('No DNS record found for ' . $unicodeHost);

$handler(
new Request('GET', "https://$unicodeHost"),
['nextcloud' => ['allow_local_address' => false]]
);
}

public function testDnsPinMiddlewareAcceptsPunycodeDirectly() {
$punycodeHost = 'xn--bcher-kva.com';

$this->dnsPinMiddleware
->expects($this->atLeastOnce())
->method('dnsGetRecord')
->with(
$punycodeHost,
$this->anything()
)
->willReturn([
[
'host' => $punycodeHost,
'class' => 'IN',
'ttl' => 1800,
'type' => 'A',
'ip' => '203.0.113.80'
]
]);

$stack = new HandlerStack(new MockHandler([
static fn () => new Response(200)
]));
$stack->push($this->dnsPinMiddleware->addDnsPinning());
$handler = $stack->resolve();

$handler(
new Request('GET', "https://$punycodeHost"),
['nextcloud' => ['allow_local_address' => false]]
);
}
}
Loading