Skip to content

Commit 41a33b0

Browse files
authored
Merge pull request #21654 from nextcloud/backport/21653/stable18
[stable18] Fix IPv6 remote addresses from X_FORWARDED_FOR headers before validating
2 parents 2d2b413 + 8cba764 commit 41a33b0

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

lib/private/AppFramework/Http/Request.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,12 @@ public function getRemoteAddress(): string {
659659
if(isset($this->server[$header])) {
660660
foreach(explode(',', $this->server[$header]) as $IP) {
661661
$IP = trim($IP);
662+
663+
// remove brackets from IPv6 addresses
664+
if (strpos($IP, '[') === 0 && substr($IP, -1) === ']') {
665+
$IP = substr($IP, 1, -1);
666+
}
667+
662668
if (filter_var($IP, FILTER_VALIDATE_IP) !== false) {
663669
return $IP;
664670
}

tests/lib/AppFramework/Http/RequestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,34 @@ public function testGetRemoteAddressWithNotMatchingCidrTrustedRemote() {
634634
$this->assertSame('192.168.3.99', $request->getRemoteAddress());
635635
}
636636

637+
public function testGetRemoteAddressWithXForwardedForIPv6() {
638+
$this->config
639+
->expects($this->at(0))
640+
->method('getSystemValue')
641+
->with('trusted_proxies')
642+
->willReturn(['192.168.2.0/24']);
643+
$this->config
644+
->expects($this->at(1))
645+
->method('getSystemValue')
646+
->with('forwarded_for_headers')
647+
->willReturn(['HTTP_X_FORWARDED_FOR']);
648+
649+
$request = new Request(
650+
[
651+
'server' => [
652+
'REMOTE_ADDR' => '192.168.2.99',
653+
'HTTP_X_FORWARDED_FOR' => '[2001:db8:85a3:8d3:1319:8a2e:370:7348]',
654+
],
655+
],
656+
$this->secureRandom,
657+
$this->config,
658+
$this->csrfTokenManager,
659+
$this->stream
660+
);
661+
662+
$this->assertSame('2001:db8:85a3:8d3:1319:8a2e:370:7348', $request->getRemoteAddress());
663+
}
664+
637665
/**
638666
* @return array
639667
*/

0 commit comments

Comments
 (0)