Skip to content

Commit 947645d

Browse files
committed
clean up a little
1 parent af0c032 commit 947645d

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

framework/core/src/Forum/Controller/LogOutController.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,27 @@ public function handle(Request $request): ResponseInterface
9292
$actor = RequestUtil::getActor($request);
9393
$base = $this->url->to('forum')->base();
9494

95-
$sanitizedUrl = $this->sanitizeReturnUrl((string) Arr::get($request->getQueryParams(), 'return', $base));
95+
$rurl = (string) Arr::get($request->getQueryParams(), 'return');
96+
$return = $this->sanitizeReturnUrl($rurl, $base);
9697

97-
// If there is no user logged in, return to the index.
98+
// If there is no user logged in, return to the index or the return url if it's set.
9899
if ($actor->isGuest()) {
99-
return new RedirectResponse(empty($sanitizedUrl) ? $base : $sanitizedUrl);
100+
return new RedirectResponse($return);
100101
}
101102

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

106107
if (Arr::get($request->getQueryParams(), 'token') !== $csrfToken) {
107-
$return = $this->sanitizeReturnUrl($request->getQueryParams()['return'] ?? $base);
108-
109108
$view = $this->view->make('flarum.forum::log-out')
110109
->with('url', $this->url->to('forum')->route('logout') . '?token=' . $csrfToken . ($return ? '&return=' . urlencode($return) : ''));
111110

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

115114
$accessToken = $session->get('access_token');
116-
$response = new RedirectResponse($sanitizedUrl);
115+
$response = new RedirectResponse($return);
117116

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

@@ -124,12 +123,16 @@ public function handle(Request $request): ResponseInterface
124123
return $this->rememberer->forget($response);
125124
}
126125

127-
protected function sanitizeReturnUrl(string $url): string
126+
protected function sanitizeReturnUrl(string $url, string $base): string
128127
{
128+
if (empty($url)) {
129+
return $base; // Return base URL for empty return URL
130+
}
131+
129132
$parsed = parse_url($url);
130133

131134
if (!$parsed || !isset($parsed['host'])) {
132-
return ''; // Return early for invalid URLs
135+
return $base; // Return early for invalid URLs
133136
}
134137

135138
$host = $parsed['host'];
@@ -138,13 +141,19 @@ protected function sanitizeReturnUrl(string $url): string
138141
return $url;
139142
}
140143

141-
return ''; // Return empty string for non-whitelisted domains
144+
return $base; // Return base url for non-whitelisted domains
142145
}
143146

144147
protected function getWhitelistedRedirectDomains(): array
145148
{
149+
$forumUrl = $this->config->url();
150+
$parsedForumUrl = parse_url($forumUrl);
151+
152+
// Extract the host from the parsed forum URL
153+
$forumHost = isset($parsedForumUrl['host']) ? $parsedForumUrl['host'] : '';
154+
146155
return array_merge(
147-
[$this->config->url()],
156+
[$forumHost],
148157
$this->config->offsetGet('redirectDomains') ?? []
149158
);
150159
}

0 commit comments

Comments
 (0)