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

CDPT-887 Use HTTP_X_REAL_IP instead of REMOTE_ADDR for IP allow list. #515

Merged
merged 3 commits into from
Apr 11, 2024
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
13 changes: 9 additions & 4 deletions public/app/mu-plugins/moj-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ public function ipMatch($ip, $cidrs, &$match = null): bool
/**
* Check if the IP address is allowed.
*
* Checks that we have the environment variables ALLOWED_IPS and REMOTE_ADDR set.
* Runs the ipMatch method to check if the REMOTE_ADDR is in the ALLOWED_IPS.
* Checks that we have the environment variable ALLOWED_IPS and server property HTTP_X_REAL_IP set.
* Runs the ipMatch method to check if the HTTP_X_REAL_IP is in the ALLOWED_IPS.
*
* @return bool Returns true if the IP address is allowed, otherwise false.
*/

public function ipAddressIsAllowed(): bool
{

if (empty($_ENV['ALLOWED_IPS']) || empty($_SERVER['REMOTE_ADDR'])) {
if (empty($_ENV['ALLOWED_IPS']) || empty($_SERVER['HTTP_X_REAL_IP'])) {
return false;
}

Expand All @@ -94,7 +94,7 @@ public function ipAddressIsAllowed(): bool
preg_split($newline_pattern, preg_replace($comments_pattern, '', $_ENV['ALLOWED_IPS']))
);

return $this->ipMatch($_SERVER['REMOTE_ADDR'], $allowedIps);
return $this->ipMatch($_SERVER['HTTP_X_REAL_IP'], $allowedIps);
}

/**
Expand Down Expand Up @@ -172,6 +172,11 @@ public function handlePageRequest(string $required_role = 'reader'): void
// Get the JWT token from the request.
$jwt = $this->getJwt();

// If headers are already sent or we're doing a cron job, return early.
if (\headers_sent() || defined('DOING_CRON')) {
return;
}

// Get the roles from the JWT and check that they're sufficient.
$jwt_correct_role = $jwt && $jwt->roles ? in_array($required_role, $jwt->roles) : false;

Expand Down
5 changes: 0 additions & 5 deletions public/info.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
## -------------------------------------------------------------------------
## -------------------------------------------------------------------------

# Output the IP address of the client. To make sure ingress is passing it correctly.
if(!empty($_SERVER['REMOTE_ADDR'])) {
echo 'Your IP address is: ' . $_SERVER['REMOTE_ADDR'];
}


# output all settings concerning the PHP installation
phpinfo();
Expand Down
Loading