Skip to content
Closed
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
51 changes: 47 additions & 4 deletions src/Cms/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class LoginController extends Content
private SessionManager $_SessionManager;
private CsrfTokenManager $_CsrfManager;

/**
* Default redirect URL after login
*/
private const DEFAULT_REDIRECT_URL = '/admin/dashboard';

public function __construct( ?Application $app = null )
{
parent::__construct( $app );
Expand All @@ -44,6 +49,36 @@ public function __construct( ?Application $app = null )
$this->_CsrfManager = new CsrfTokenManager( $this->_SessionManager );
}

/**
* Validate redirect URL to prevent open redirect vulnerabilities
*
* @param string $url The URL to validate
* @return bool True if the URL is valid, false otherwise
*/
private function isValidRedirectUrl( string $url ): bool
{
// Only allow internal relative paths
if( empty( $url ) )
{
return false;
}

// Reject URLs with schemes (http://, https://, javascript:, etc.)
if( preg_match( '/^[a-zA-Z][a-zA-Z0-9+.-]*:/', $url ) )
{
return false;
}

// Reject protocol-relative URLs (//example.com)
if( str_starts_with( $url, '//' ) )
{
return false;
}

// Must start with / for internal path
return str_starts_with( $url, '/' );
}

/**
* Show login form
*/
Expand All @@ -52,17 +87,22 @@ public function showLoginForm( array $Parameters ): string
// If already logged in, redirect to dashboard
if( $this->_AuthManager->check() )
{
header( 'Location: /admin/dashboard' );
header( 'Location: ' . self::DEFAULT_REDIRECT_URL );
exit;
}

$requestedRedirect = $_GET['redirect'] ?? self::DEFAULT_REDIRECT_URL;
$redirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
: self::DEFAULT_REDIRECT_URL;

$ViewData = [
'Title' => 'Login | ' . $this->getName(),
'Description' => 'Login to ' . $this->getName(),
'CsrfToken' => $this->_CsrfManager->getToken(),
'Error' => $this->_SessionManager->getFlash( 'error' ),
'Success' => $this->_SessionManager->getFlash( 'success' ),
'RedirectUrl' => $_GET['redirect'] ?? '/admin/dashboard'
'RedirectUrl' => $redirectUrl
];

return $this->renderHtml(
Expand Down Expand Up @@ -112,8 +152,11 @@ public function login( array $Parameters ): string
$this->_SessionManager->flash( 'success', 'Welcome back!' );

// Redirect to intended URL or dashboard
$RedirectUrl = $_POST['redirect_url'] ?? '/admin/dashboard';
header( 'Location: ' . $RedirectUrl );
$requestedRedirect = $_POST['redirect_url'] ?? self::DEFAULT_REDIRECT_URL;
$redirectUrl = $this->isValidRedirectUrl( $requestedRedirect )
? $requestedRedirect
: self::DEFAULT_REDIRECT_URL;
header( 'Location: ' . $redirectUrl );
exit;
}

Expand Down
Loading