Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use X-Real-IP header last #3230

Merged
merged 1 commit into from
Jan 25, 2023
Merged
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
17 changes: 9 additions & 8 deletions core/classes/Misc/HttpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ public static function getRemoteAddress(): ?string {
return $_SERVER['REMOTE_ADDR'];
}

// Try the simple headers first that only contain an IP address...

// Non-standard header that only contains the origin address
$x_real_ip = self::getHeader('X-Real-IP');
if ($x_real_ip !== null) {
return $x_real_ip;
}

// Non-standard header sent by Cloudflare that only contains the origin address
// We can trust this to be the real IP address, no real-world setup would
// have an additional proxy in front of CloudFlare.
$cf_connecting_ip = self::getHeader('CF-Connecting-IP');
if ($cf_connecting_ip !== null) {
return $cf_connecting_ip;
Expand Down Expand Up @@ -83,6 +77,13 @@ public static function getRemoteAddress(): ?string {
}
}

// Non-standard header that only contains the origin address. This header should be tried last, since it does
// not work in the case of multiple proxies where at least two of them set the X-Real-IP header.
$x_real_ip = self::getHeader('X-Real-IP');
if ($x_real_ip !== null) {
return $x_real_ip;
}

return $_SERVER['REMOTE_ADDR'];
}

Expand Down