diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 8cb504b..b998f4c 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -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 diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 161fa8b..0ebd646 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -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 @@ -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; @@ -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; @@ -290,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 [] */ @@ -300,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)) { @@ -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()]; } diff --git a/tests/src/Controller/LoginControllerTest.php b/tests/src/Controller/LoginControllerTest.php index 07b3592..9ed9e08 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; @@ -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, ); } @@ -848,4 +850,122 @@ 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 + { + $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; + $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], + ); + } }