From 0359885baf842f2443424036efb2913e684cc0cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ivan=C4=8Di=C4=87?= Date: Wed, 11 Nov 2026 16:07:41 +0100 Subject: [PATCH 1/2] Read TARGET and a boolean renew in debug mode handleDebugMode() validated the ticket it had just issued against the service from a lowercase 'target' parameter, which no client sends: login() and the validation endpoints read 'TARGET'. With only TARGET given, debug mode therefore answered "Missing service parameter". It also passed the 'renew' query string straight to validate(), which takes a bool, so renew=true failed with a TypeError under strict types. Read 'TARGET', convert 'renew' with FILTER_VALIDATE_BOOL, and test debug mode with service and with TARGET, each with and without renew. Assisted-by: Claude Code (claude-opus-5-5, implementation & unit tests) Assisted-by: Google Antigravity (gemini-3.8-flash-high, review) --- docs/ChangeLog.md | 6 ++ src/Controller/LoginController.php | 7 ++- tests/src/Controller/LoginControllerTest.php | 61 ++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 8cb504b..2a892f0 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Fixed + +- Debug mode (`debugMode=true`) now validates the ticket for a service given as `TARGET`, and no longer fails with a type error when `renew` is set (Marko Ivančić) + ## [7.0.6] - 2026-11-10 ### Security diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 161fa8b..ad118fb 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -28,6 +28,7 @@ use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; +use function filter_var; use function http_build_query; use function in_array; use function var_export; @@ -311,8 +312,10 @@ public function handleDebugMode( $xmlResponse = $this->validate( request: $request, method: 'serviceValidate', - renew: $request->get('renew', false), - target: $request->get('target'), + // The query parameter is a string, validate() takes a bool + renew: filter_var($request->get('renew', false), FILTER_VALIDATE_BOOL), + // The same parameter login() reads the service from when there is no 'service' + target: $request->get('TARGET'), ticket: $serviceTicket['id'], service: $request->get('service'), pgtUrl: $request->get('pgtUrl'), diff --git a/tests/src/Controller/LoginControllerTest.php b/tests/src/Controller/LoginControllerTest.php index 07b3592..1a44093 100644 --- a/tests/src/Controller/LoginControllerTest.php +++ b/tests/src/Controller/LoginControllerTest.php @@ -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; @@ -848,4 +849,64 @@ public function testAuthenticatedPostSubmitsViaPostWithTicket(): void $this->assertArrayHasKey('ticket', $params); $this->assertStringStartsWith('ST-', $params['ticket']); } + + + /** + * @return array}> + */ + 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 $requestParams + * + * @throws \Exception + */ + #[DataProvider('debugModeServiceValidateProvider')] + public function testDebugModeValidatesTheIssuedTicket(array $requestParams): void + { + $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, + ]); + + $loginRequest = Request::create( + uri: Module::getModuleURL('casserver/login'), + parameters: $requestParams, + ); + $response = $this->callLogin($controllerMock, $loginRequest, $requestParams); + + $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], + ); + } } From 6ba4d1829905d9d4dc125cc46ba5a397ee8954f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Ivan=C4=8Di=C4=87?= Date: Mon, 16 Nov 2026 13:30:01 +0100 Subject: [PATCH 2/2] Validate debug-mode tickets with login()'s mapped parameters handleDebugMode() read renew, TARGET, service and pgtUrl with Request::get(), a second time after login() had already mapped them with #[MapQueryParameter]. Request::get() also falls back to route attributes and the POST body, skips the mapper's validation, and is deprecated in Symfony 7.4. A renew or service in the POST body therefore changed what debug mode validated, and an array TARGET failed with a TypeError. login() now also maps pgtUrl and passes its own values to handleDebugMode(), so the ticket is validated with the parameters it was issued with. The new parameters are trailing and optional, so the public signature stays compatible, and filter_var() is no longer needed. As suggested in the PR 87 review. Test that renew, service or an array TARGET in the POST body does not change the result, and share the debug-mode tests' setup. Assisted-by: Claude Code (claude-opus-5-5, implementation & unit tests) --- docs/ChangeLog.md | 3 +- src/Controller/LoginController.php | 31 +++++++-- tests/src/Controller/LoginControllerTest.php | 69 ++++++++++++++++++-- 3 files changed, 90 insertions(+), 13 deletions(-) diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 2a892f0..b998f4c 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -4,7 +4,8 @@ ### Fixed -- Debug mode (`debugMode=true`) now validates the ticket for a service given as `TARGET`, and no longer fails with a type error when `renew` is set (Marko Ivančić) +- 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 diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index ad118fb..0ebd646 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -28,7 +28,6 @@ use Symfony\Component\HttpKernel\Attribute\AsController; use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; -use function filter_var; use function http_build_query; use function in_array; use function var_export; @@ -122,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 @@ -138,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; @@ -255,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; @@ -291,9 +297,18 @@ 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 [] */ @@ -301,6 +316,10 @@ 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)) { @@ -312,13 +331,11 @@ public function handleDebugMode( $xmlResponse = $this->validate( request: $request, method: 'serviceValidate', - // The query parameter is a string, validate() takes a bool - renew: filter_var($request->get('renew', false), FILTER_VALIDATE_BOOL), - // The same parameter login() reads the service from when there is no 'service' - 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()]; } diff --git a/tests/src/Controller/LoginControllerTest.php b/tests/src/Controller/LoginControllerTest.php index 1a44093..9ed9e08 100644 --- a/tests/src/Controller/LoginControllerTest.php +++ b/tests/src/Controller/LoginControllerTest.php @@ -106,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, ); } @@ -876,6 +877,61 @@ public static function debugModeServiceValidateProvider(): array */ #[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, array}> + */ + 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 $queryParams + * @param array $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; @@ -894,12 +950,15 @@ public function testDebugModeValidatesTheIssuedTicket(array $requestParams): voi 'Expire' => 9999999999, ]); - $loginRequest = Request::create( - uri: Module::getModuleURL('casserver/login'), - parameters: $requestParams, - ); - $response = $this->callLogin($controllerMock, $loginRequest, $requestParams); + 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']);