Skip to content
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
42 changes: 40 additions & 2 deletions src/Cms/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public function login( array $Parameters ): string

// Redirect to intended URL or dashboard
$requestedRedirect = $_POST['redirect_url'] ?? '/admin/dashboard';
$RedirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
$RedirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
: '/admin/dashboard';
header( 'Location: ' . $RedirectUrl );
exit;
Expand All @@ -168,4 +168,42 @@ public function logout( array $Parameters ): string
header( 'Location: /login' );
exit;
}

/**
* Validate if a redirect URL is safe to use.
* Only allows relative URLs (starting with /) to prevent open redirect vulnerabilities.
*
* @param string $url The URL to validate
* @return bool True if the URL is safe, false otherwise
*/
private function isValidRedirectUrl( string $url ): bool
{
// Empty URLs are not valid
if( $url === '' )
{
return false;
}

// Only allow relative URLs that start with /
if( $url[0] !== '/' )
{
return false;
}

// Prevent protocol-relative URLs (//example.com)
if( strlen( $url ) > 1 && $url[1] === '/' )
{
return false;
}

// Check for malicious patterns
// Prevent URLs with @ symbol (could be used for phishing: /path@evil.com)
// Prevent URLs with backslashes (could bypass filters: /\evil.com)
if( strpos( $url, '@' ) !== false || strpos( $url, '\\' ) !== false )
{
return false;
}

return true;
}
}
Loading