Skip to content

Commit

Permalink
Merge pull request #43552 from nextcloud/bugfix/noid/rfc7239-compatib…
Browse files Browse the repository at this point in the history
…le-proxy-handling

fix(request): Handle reverse proxy setting a port in Forwarded-For
  • Loading branch information
nickvergessen authored Feb 15, 2024
2 parents 9c00d12 + 696ed4a commit 9f38aab
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 292 deletions.
13 changes: 9 additions & 4 deletions lib/private/AppFramework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,10 +607,15 @@ public function getRemoteAddress(): string {
if (isset($this->server[$header])) {
foreach (array_reverse(explode(',', $this->server[$header])) as $IP) {
$IP = trim($IP);

// remove brackets from IPv6 addresses
if (str_starts_with($IP, '[') && str_ends_with($IP, ']')) {
$IP = substr($IP, 1, -1);
$colons = substr_count($IP, ':');
if ($colons > 1) {
// Extract IP from string with brackets and optional port
if (preg_match('/^\[(.+?)\](?::\d+)?$/', $IP, $matches) && isset($matches[1])) {
$IP = $matches[1];
}
} elseif ($colons === 1) {
// IPv4 with port
$IP = substr($IP, 0, strpos($IP, ':'));
}

if ($this->isTrustedProxy($trustedProxies, $IP)) {
Expand Down
Loading

0 comments on commit 9f38aab

Please sign in to comment.