Skip to content
Merged
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
Fix IPv6 remote addresses from X_FORWARDED_FOR headers before validating
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Jul 2, 2020
commit 8cba764b599041fdccf19a91a8ee34de80dea8ba
6 changes: 6 additions & 0 deletions lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,12 @@ public function getRemoteAddress(): string {
if(isset($this->server[$header])) {
foreach(explode(',', $this->server[$header]) as $IP) {
$IP = trim($IP);

// remove brackets from IPv6 addresses
if (strpos($IP, '[') === 0 && substr($IP, -1) === ']') {
$IP = substr($IP, 1, -1);
}

if (filter_var($IP, FILTER_VALIDATE_IP) !== false) {
return $IP;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/AppFramework/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,34 @@ public function testGetRemoteAddressWithNotMatchingCidrTrustedRemote() {
$this->assertSame('192.168.3.99', $request->getRemoteAddress());
}

public function testGetRemoteAddressWithXForwardedForIPv6() {
$this->config
->expects($this->at(0))
->method('getSystemValue')
->with('trusted_proxies')
->willReturn(['192.168.2.0/24']);
$this->config
->expects($this->at(1))
->method('getSystemValue')
->with('forwarded_for_headers')
->willReturn(['HTTP_X_FORWARDED_FOR']);

$request = new Request(
[
'server' => [
'REMOTE_ADDR' => '192.168.2.99',
'HTTP_X_FORWARDED_FOR' => '[2001:db8:85a3:8d3:1319:8a2e:370:7348]',
],
],
$this->secureRandom,
$this->config,
$this->csrfTokenManager,
$this->stream
);

$this->assertSame('2001:db8:85a3:8d3:1319:8a2e:370:7348', $request->getRemoteAddress());
}

/**
* @return array
*/
Expand Down