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: Logout controller allows open redirects (#3948) #4091

Merged
merged 2 commits into from
Oct 24, 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
48 changes: 40 additions & 8 deletions framework/core/src/Forum/Controller/LogOutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Forum\Controller;

use Flarum\Foundation\Config;
use Flarum\Http\Rememberer;
use Flarum\Http\RequestUtil;
use Flarum\Http\SessionAuthenticator;
Expand All @@ -19,6 +20,7 @@
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Uri;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -30,37 +32,38 @@ public function __construct(
protected SessionAuthenticator $authenticator,
protected Rememberer $rememberer,
protected Factory $view,
protected UrlGenerator $url
protected UrlGenerator $url,
protected Config $config
) {
}

public function handle(Request $request): ResponseInterface
{
$session = $request->getAttribute('session');
$actor = RequestUtil::getActor($request);
$base = $this->url->to('forum')->base();

$url = Arr::get($request->getQueryParams(), 'return', $this->url->to('forum')->base());
$returnUrl = Arr::get($request->getQueryParams(), 'return');
$return = $this->sanitizeReturnUrl((string) $returnUrl, $base);

// If there is no user logged in, return to the index.
// If there is no user logged in, return to the index or the return url if it's set.
if ($actor->isGuest()) {
return new RedirectResponse($url);
return new RedirectResponse($return);
}

// If a valid CSRF token hasn't been provided, show a view which will
// allow the user to press a button to complete the log out process.
$csrfToken = $session->token();

if (Arr::get($request->getQueryParams(), 'token') !== $csrfToken) {
$return = Arr::get($request->getQueryParams(), 'return');

$view = $this->view->make('flarum.forum::log-out')
->with('url', $this->url->to('forum')->route('logout').'?token='.$csrfToken.($return ? '&return='.urlencode($return) : ''));
->with('url', $this->url->to('forum')->route('logout').'?token='.$csrfToken.($returnUrl ? '&return='.urlencode($return) : ''));

return new HtmlResponse($view->render());
}

$accessToken = $session->get('access_token');
$response = new RedirectResponse($url);
$response = new RedirectResponse($return);

$this->authenticator->logOut($session);

Expand All @@ -70,4 +73,33 @@ public function handle(Request $request): ResponseInterface

return $this->rememberer->forget($response);
}

protected function sanitizeReturnUrl(string $url, string $base): Uri
{
if (empty($url)) {
return new Uri($base);
}

try {
$parsedUrl = new Uri($url);
} catch (\InvalidArgumentException $e) {
return new Uri($base);
}

if (in_array($parsedUrl->getHost(), $this->getAllowedRedirectDomains())) {
return $parsedUrl;
}

return new Uri($base);
}

protected function getAllowedRedirectDomains(): array
{
$forumUri = $this->config->url();

return array_merge(
[$forumUri->getHost()],
$this->config->offsetGet('redirectDomains') ?? []
);
}
}
Loading