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
4 changes: 4 additions & 0 deletions lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ public function getRemoteAddress(): string {
if (preg_match('/^\[(.+?)\](?::\d+)?$/', $IP, $matches) && isset($matches[1])) {
$IP = $matches[1];
}
// Strip the zone of link-local addresses (fe80::1%eth0)
$IP = explode('%', $IP, 2)[0];
} elseif ($colons === 1) {
// IPv4 with port
$IP = substr($IP, 0, strpos($IP, ':'));
Expand All @@ -610,6 +612,8 @@ public function getRemoteAddress(): string {
if (filter_var($IP, FILTER_VALIDATE_IP) !== false) {
return $IP;
}

return $remoteAddress;
}
}
}
Expand Down
64 changes: 64 additions & 0 deletions tests/lib/AppFramework/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,70 @@ public static function dataGetRemoteAddress(): array {
['HTTP_X_FORWARDED_FOR'],
'2001:db8:85a3:8d3:1319:8a2e:370:7348',
],
'IPv6 link-local with zone' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, fe80::a%eth0',
],
['10.0.0.2'],
['HTTP_X_FORWARDED_FOR'],
'fe80::a',
],
'IPv6 link-local with zone, brackets and port' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, [fe80::a%eth0]:8080',
],
['10.0.0.2'],
['HTTP_X_FORWARDED_FOR'],
'fe80::a',
],
'Trusted proxy configured without zone' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, fe80::1%eth0',
],
['10.0.0.2', 'fe80::1'],
['HTTP_X_FORWARDED_FOR'],
'203.0.113.1',
],
'Invalid entry "unknown" stops the walk' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, unknown',
],
['10.0.0.2'],
['HTTP_X_FORWARDED_FOR'],
'10.0.0.2',
],
'Invalid entry "unix:" stops the walk' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, unix:',
],
['10.0.0.2'],
['HTTP_X_FORWARDED_FOR'],
'10.0.0.2',
],
'Invalid entry between trusted proxies' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, unknown, 10.0.0.3',
],
['10.0.0.2', '10.0.0.3'],
['HTTP_X_FORWARDED_FOR'],
'10.0.0.2',
],
'Invalid entry does not fall back to another header' => [
[
'REMOTE_ADDR' => '10.0.0.2',
'HTTP_X_FORWARDED' => '203.0.113.2',
'HTTP_X_FORWARDED_FOR' => '203.0.113.1, unknown',
],
['10.0.0.2'],
['HTTP_X_FORWARDED', 'HTTP_X_FORWARDED_FOR'],
'10.0.0.2',
],
];
}

Expand Down
Loading