Skip to content

Commit 653615e

Browse files
authored
Merge pull request #1514 from nextcloud/carl/protect-redirect
fix: harden redirecting after login
2 parents ef24268 + d9474b7 commit 653615e

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

lib/Controller/LoginController.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,20 @@ private function buildProtocolErrorResponse(?bool $throttle = null): TemplateRes
142142
* @return RedirectResponse
143143
*/
144144
private function getRedirectResponse(?string $redirectUrl = null): RedirectResponse {
145+
$baseUrl = $this->urlGenerator->getBaseUrl();
146+
145147
if ($redirectUrl === null) {
146-
return new RedirectResponse($this->urlGenerator->getBaseUrl());
148+
return new RedirectResponse($baseUrl);
147149
}
148150

149151
// Remove protocol and domain name
150152
$filtered = preg_replace('/^https?:\/\/[^\/]+/', '', $redirectUrl) ?? '';
151153

152-
// Additional check: ensure the result starts with a single /
153-
if (!preg_match('/^\/[^\/]/', $filtered)) {
154-
return new RedirectResponse($this->urlGenerator->getBaseUrl());
154+
// Reject protocol-relative URLs and anything not starting with a single slash followed
155+
// by an alphanumeric or one of [_-.~?#]
156+
if (!preg_match('/^\/[A-Za-z0-9_\-.~?#]/', $filtered)) {
157+
$this->logger->error("Rejected invalid redirect url '{$filtered}'");
158+
return new RedirectResponse($baseUrl);
155159
}
156160

157161
return new RedirectResponse($filtered);

0 commit comments

Comments
 (0)