Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Fixed

- Debug mode (`debugMode=true`) now validates the ticket with the query parameters login used to issue it,
so it works for a service given as `TARGET` and with `renew` set, and ignores parameters in the request body (Marko Ivančić)

## [7.0.6] - 2026-11-10

### Security
Expand Down
28 changes: 24 additions & 4 deletions src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function __construct(
* @param string|null $entityId
* @param string|null $debugMode
* @param string|null $method
* @param string|null $pgtUrl Only used by debug mode, which passes it on to the ticket validation
*
* @return \SimpleSAML\HTTP\RunnableResponse|\SimpleSAML\XHTML\Template
* @throws \SimpleSAML\Error\ConfigurationError
Expand All @@ -137,6 +138,7 @@ public function login(
#[MapQueryParameter] ?string $entityId = null,
#[MapQueryParameter] ?string $debugMode = null,
#[MapQueryParameter] ?string $method = null,
#[MapQueryParameter] ?string $pgtUrl = null,
): RunnableResponse|Template {
$forceAuthn = $renew;
$serviceUrl = $service ?? $TARGET ?? null;
Expand Down Expand Up @@ -254,10 +256,15 @@ public function login(

// Check if we are in debug mode.
if ($debugMode !== null && $this->casConfig->getOptionalBoolean('debugMode', false)) {
// Validate with the parameters the ticket was issued with, not with whatever the request holds
[$templateName, $statusCode, $DebugModeXmlString] = $this->handleDebugMode(
$request,
$debugMode,
$serviceTicket,
$renew,
$TARGET,
$service,
$pgtUrl,
);
$t = new Template($this->sspConfig, (string)$templateName);
$t->data['debugMode'] = $debugMode === 'true' ? 'Default' : $debugMode;
Expand Down Expand Up @@ -290,16 +297,29 @@ public function login(


/**
* Validate the ticket login() has just issued, for debug mode to show the response.
*
* $renew, $target, $service and $pgtUrl are login()'s query parameters as it mapped them, so the
* ticket is validated under the conditions it was issued with.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string|null $debugMode
* @param array $serviceTicket
* @param bool $renew
* @param string|null $target
* @param string|null $service
* @param string|null $pgtUrl
*
* @return array []
*/
public function handleDebugMode(
Request $request,
?string $debugMode,
array $serviceTicket,
bool $renew = false,
?string $target = null,
?string $service = null,
?string $pgtUrl = null,
): array {
// Check if the debugMode is supported
if (!in_array($debugMode, self::DEBUG_MODES, true)) {
Expand All @@ -311,11 +331,11 @@ public function handleDebugMode(
$xmlResponse = $this->validate(
request: $request,
method: 'serviceValidate',
renew: $request->get('renew', false),
target: $request->get('target'),
renew: $renew,
target: $target,
ticket: $serviceTicket['id'],
service: $request->get('service'),
pgtUrl: $request->get('pgtUrl'),
service: $service,
pgtUrl: $pgtUrl,
);
return ['casserver:validate.twig', $xmlResponse->getStatusCode(), $xmlResponse->getContent()];
}
Expand Down
120 changes: 120 additions & 0 deletions tests/src/Controller/LoginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use SimpleSAML\Module\casserver\Controller\LoginController;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -105,6 +106,7 @@ private function callLogin(LoginController $controller, Request $request, array
entityId: isset($params['entityId']) ? (string)$params['entityId'] : null,
debugMode: isset($params['debugMode']) ? (string)$params['debugMode'] : null,
method: isset($params['method']) ? (string)$params['method'] : null,
pgtUrl: isset($params['pgtUrl']) ? (string)$params['pgtUrl'] : null,
);
}

Expand Down Expand Up @@ -848,4 +850,122 @@ public function testAuthenticatedPostSubmitsViaPostWithTicket(): void
$this->assertArrayHasKey('ticket', $params);
$this->assertStringStartsWith('ST-', $params['ticket']);
}


/**
* @return array<string, array{array<string, string>}>
*/
public static function debugModeServiceValidateProvider(): array
{
$service = 'https://example.com/ssp/module.php/cas/linkback.php';
return [
'service' => [['service' => $service, 'debugMode' => 'true']],
'TARGET' => [['TARGET' => $service, 'debugMode' => 'true']],
'service with renew' => [['service' => $service, 'debugMode' => 'true', 'renew' => 'true']],
'TARGET with renew' => [['TARGET' => $service, 'debugMode' => 'true', 'renew' => 'true']],
];
}


/**
* Debug mode validates the ticket it has just issued against the service it was issued for, whichever query
* parameter named the service, and whether or not renew was requested.
*
* @param array<string, string> $requestParams
*
* @throws \Exception
*/
#[DataProvider('debugModeServiceValidateProvider')]
public function testDebugModeValidatesTheIssuedTicket(array $requestParams): void
{
$loginRequest = Request::create(
uri: Module::getModuleURL('casserver/login'),
parameters: $requestParams,
);
$response = $this->callLogin($this->createDebugModeController(), $loginRequest, $requestParams);

$this->assertDebugModeValidated($response);
}


/**
* @return array<string, array{array<string, string>, array<string, mixed>}>
*/
public static function debugModeRequestBodyProvider(): array
{
$service = 'https://example.com/ssp/module.php/cas/linkback.php';
return [
// Would ask for a ticket from a fresh login, which this one, issued from the SSO session, is not
'renew' => [['service' => $service, 'debugMode' => 'true'], ['renew' => 'true']],
// Would name a service the ticket was not issued for
'service' => [['TARGET' => $service, 'debugMode' => 'true'], ['service' => 'https://example.com/other']],
// Would not be a string
'TARGET array' => [['service' => $service, 'debugMode' => 'true'], ['TARGET' => ['x']]],
];
}


/**
* Debug mode validates the ticket with the query parameters login() issued it with, so parameters
* in the POST body cannot change the result.
*
* @param array<string, string> $queryParams
* @param array<string, mixed> $bodyParams
*
* @throws \Exception
*/
#[DataProvider('debugModeRequestBodyProvider')]
public function testDebugModeIgnoresTheRequestBody(array $queryParams, array $bodyParams): void
{
$loginRequest = Request::create(
uri: Module::getModuleURL('casserver/login') . '?' . http_build_query($queryParams),
method: 'POST',
parameters: $bodyParams,
);
$response = $this->callLogin($this->createDebugModeController(), $loginRequest, $queryParams);

$this->assertDebugModeValidated($response);
}


/**
* A controller with debug mode enabled, for a user with an SSO session.
*/
private function createDebugModeController(): LoginController
{
$moduleConfig = $this->moduleConfig;
$moduleConfig['debugMode'] = true;
$casconfig = Configuration::loadFromArray($moduleConfig);

$controllerMock = $this->getMockBuilder(LoginController::class)
->setConstructorArgs([$this->sspConfig, $casconfig, $this->authSimpleMock, $this->httpUtils])
->onlyMethods(['getSession'])
->getMock();
$controllerMock->expects($this->once())->method('getSession')->willReturn($this->sessionMock);
$this->sessionMock->expects($this->exactly(2))->method('getSessionId')->willReturn(session_create_id());
$this->authSimpleMock->expects($this->atLeastOnce())->method('isAuthenticated')->willReturn(true);
$this->authSimpleMock->expects($this->once())->method('getAuthData')->with('Expire')->willReturn(9999999999);
$this->authSimpleMock->expects($this->once())->method('getAuthDataArray')->willReturn([
'Attributes' => ['eduPersonPrincipalName' => ['testuser@example.com']],
'Expire' => 9999999999,
]);

return $controllerMock;
}


/**
* Debug mode rendered a successful serviceValidate response for the test user.
*/
private function assertDebugModeValidated(mixed $response): void
{
$this->assertInstanceOf(Template::class, $response);
$this->assertSame(Response::HTTP_OK, $response->data['statusCode']);
$xml = simplexml_load_string((string)$response->data['DebugModeXml']);
$xml->registerXPathNamespace('cas', 'serviceResponse');
$this->assertSame(
'testuser@example.com',
(string)$xml->xpath('//cas:authenticationSuccess/cas:user')[0],
);
}
}
Loading