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

Fix BackURL redirect with strict or lax session cookie security. #55

Merged
merged 1 commit into from
Jan 24, 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
10 changes: 10 additions & 0 deletions src/Control/SAMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ protected function getRedirect()
return $this->redirect($this->getRequest()->getSession()->get('BackURL'));
}

// In SAMLHelper, we use RelayState to convey BackURL because in a HTTP POST flow
// with lax or strict cookie security the session will not be there for us. RelayState
// will be reflected back in the acs POST request.
// Note if only assertion is signed, RelayState cannot be trusted. Prevent open relay
// as in https://github.com/SAML-Toolkits/php-saml#avoiding-open-redirect-attacks
$relayState = $this->owner->getRequest()->postVar('RelayState');
if ($relayState && Director::is_site_url($relayState)) {
return $this->redirect($relayState);
}

// Spoofing attack, redirect to homepage instead of spoofing url
if ($back && !Director::is_site_url($back)) {
return $this->redirect(Director::absoluteBaseURL());
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers/SAMLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function redirect(RequestHandler $requestHandler = null, HTTPRequest $req
$additionalGetQueryParams = $this->getAdditionalGETQueryParameters();

try {
$auth->login(Director::absoluteBaseURL() . 'saml/', $additionalGetQueryParams);
Copy link
Contributor Author

@mateusz mateusz Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, this usage of login's first parameter is wrong, it results in a URL like https://mysite.comsaml/, i.e. it's missing the slash. The bug didn't manifest as an issue, because RelayState isn't used anyway.

// Use RelayState to convey BackURL (will be handled in SAMLController).
$auth->login($backURL, $additionalGetQueryParams);
} catch (Exception $e) {
/** @var LoggerInterface $logger */
$logger = Injector::inst()->get(LoggerInterface::class);
Expand Down
Loading