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

Prevent redirecting to an absolute URL after login #961

Merged
merged 2 commits into from
Oct 10, 2024
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
19 changes: 17 additions & 2 deletions lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ private function buildProtocolErrorResponse(?bool $throttle = null): TemplateRes
return $this->buildFailureTemplateResponse('', 'error', $params, Http::STATUS_NOT_FOUND, $throttleMetadata, $throttle);
}

/**
* @param string|null $redirectUrl
* @return RedirectResponse
*/
private function getRedirectResponse(?string $redirectUrl = null): RedirectResponse {
// this could also be done with
// preg_replace('/^https?:\/\//', '', $redirectUrl)
// or even: if (preg_match('/https?:\/\//', $redirectUrl) === 1) return new RedirectResponse('/');
return new RedirectResponse(
$redirectUrl === null
? null
: parse_url($redirectUrl, PHP_URL_PATH)
);
}

/**
* @PublicPage
* @NoCSRFRequired
Expand All @@ -210,7 +225,7 @@ private function buildProtocolErrorResponse(?bool $throttle = null): TemplateRes
*/
public function login(int $providerId, ?string $redirectUrl = null) {
if ($this->userSession->isLoggedIn()) {
return new RedirectResponse($redirectUrl);
return $this->getRedirectResponse($redirectUrl);
}
if (!$this->isSecure()) {
return $this->buildProtocolErrorResponse();
Expand Down Expand Up @@ -602,7 +617,7 @@ public function code(string $state = '', string $code = '', string $scope = '',

$redirectUrl = $this->session->get(self::REDIRECT_AFTER_LOGIN);
if ($redirectUrl) {
return new RedirectResponse($redirectUrl);
return $this->getRedirectResponse($redirectUrl);
}

return new RedirectResponse(\OC_Util::getDefaultPageUrl());
Expand Down
Loading