Skip to content

Commit abefa7f

Browse files
committed
Merge branch 'master' into feat-fticks-before-login
Bring in the 7.0.6 release. The only conflict was in docs/ChangeLog.md, where this branch's [Unreleased] entry now sits above the 7.0.6 section. Assisted-by: Claude Code (claude-opus-5-5, conflict resolution)
2 parents 8cf955d + 618b649 commit abefa7f

14 files changed

Lines changed: 470 additions & 48 deletions

‎README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ search for the `module.enable` key and set `casserver` to true:
3131

3232
See the `config-templates` folder for examples of configuring this module
3333

34+
## Upgrading to 7.0.6
35+
36+
Version 7.0.6 fixes GHSA-9g9q-j468-7fp4. The service, logout url and proxy target service are now validated
37+
exactly as received, so values whose host part contains userinfo (`user@`), percent-encoding, backslashes or
38+
whitespace are refused. Make sure every `legal_service_urls` and `legal_target_service_urls` prefix ends with
39+
`/` after the host (for example `https://app.example.com/`) and every regular expression is anchored with `^`.
40+
See [docs/ChangeLog.md](docs/ChangeLog.md) for details.
41+
3442
## Debug
3543

3644
To aid in debugging you can print out the CAS ticket xml rather then returning
3745
a ticket id. Enable `debugMode` in `module_casserver.php` and then add a query
3846
parameter `debugMode=true` to the CAS login url.
3947

4048
Logging in to
41-
`https://cas.example.com/cas/login?debugMode=true&service=http://localhost/`
49+
`https://cas.example.com/cas/login?debugMode=true&service=https://app.example.com/cas/`
4250
would now print the xml for that service.
4351

4452
```xml

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"ext-SimpleXML": "*",
3838
"ext-session": "*",
3939

40-
"beste/clock": "~4.0",
40+
"beste/clock": "~3.0 || ~4.0",
4141
"simplesamlphp/assert": "~2.0",
4242
"simplesamlphp/composer-module-installer": "~1.7",
4343
"simplesamlphp/simplesamlphp": "~2.5",
@@ -49,7 +49,7 @@
4949
"simplesamlphp/saml11": "~2.3"
5050
},
5151
"require-dev": {
52-
"simplesamlphp/simplesamlphp-test-framework": "~2.0",
52+
"simplesamlphp/simplesamlphp-test-framework": "~1.11.7 || ~2.0",
5353
"php-parallel-lint/php-parallel-lint": "^1.4"
5454
},
5555
"support": {

‎config/module_casserver.php.dist‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ $config = [
2121
]
2222
],
2323
'legal_service_urls' => [
24-
//Any service url string matching any of the following prefixes is accepted
24+
// Any service url string matching any of the following prefixes is accepted. Service urls must be absolute
25+
// ('scheme://host[:port]...'), and must not carry userinfo or percent-encoding before the path.
26+
// End every prefix at least with the '/' after the host (or port): a bare 'https://host.example' prefix
27+
// also matches 'https://host.example.other.example/'.
2528
'http://host1.domain:1234/path1',
2629
'https://host2.domain:5678/path2/path3',
27-
// So is regex
28-
'|^https://.*\.domain.com/|',
30+
// So is regex. Anchor it with '^' and match the host exactly; '.*' before the domain also matches
31+
// other hosts, e.g. 'https://other.example/x.domain.com/'.
32+
'|^https://[a-z0-9.-]+\.domain\.com/|i',
2933
// ONLY the FOLLOWING configuration options can be overridden. See OverrideConfigPropertiesEnum.
30-
'https://override.example.com' => [
34+
'https://override.example.com/' => [
3135
'attrname' => 'uid',
3236
'attributes_to_transfer' => ['cn'],
3337
//'attributes' => false,
@@ -37,7 +41,8 @@ $config = [
3741
],
3842

3943
'legal_target_service_urls' => [
40-
//Any target service url string matching any of the following prefixes is accepted
44+
// Any target service url string matching any of the following prefixes is accepted.
45+
// The advice for legal_service_urls above applies here too.
4146
'http://host3.domain:4321/path4',
4247
'https://host4.domain:8765/path5/path6',
4348
],

‎docs/ChangeLog.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,29 @@
66

77
- Give the authentication source Source and Destination before login, so that it can report a failed login against them (Marko Ivančić)
88

9+
## [7.0.6] - 2026-11-10
10+
11+
### Security
12+
13+
- Validate the service (login), return url (logout) and target service (proxy) exactly as received.
14+
A service must start with scheme://host[:port], its scheme and authority must not change when sanitized,
15+
and the authority must not contain userinfo, percent-encoding, backslashes, whitespace or control
16+
characters (GHSA-9g9q-j468-7fp4) (Marko Ivančić)
17+
18+
### Changed
19+
20+
- Recommend prefixes that end after the host, and anchored regular expressions, in the example configuration (Marko Ivančić)
21+
- Allow beste/clock ~3.0 || ~4.0 (#84)
22+
- Allow simplesamlphp/simplesamlphp-test-framework ~2.0 (#85)
23+
24+
### Upgrade notes
25+
26+
- Services, logout urls and proxy target services whose host part contains `user@`, `%`-encoding,
27+
backslashes, whitespace or control characters, or that do not start with `scheme://`, are now refused.
28+
- Check `legal_service_urls` and `legal_target_service_urls`: each prefix should end with `/` after the host
29+
(e.g. `https://app.example.com/`) and each regular expression should be anchored with `^`
30+
(e.g. `|^https://app\.example\.com/|`). See `config/module_casserver.php.dist`.
31+
932
## [7.0.5] - 2026-08-11
1033

1134
### Changed

‎src/Cas/ServiceValidator.php‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,42 @@ public function __construct(
2525
}
2626

2727

28+
/**
29+
* Check that a service url, exactly as it was received in the request, is allowed, and if so return the
30+
* configuration to use.
31+
*
32+
* The legal service urls are matched against the sanitized form of the service (url decoded, without
33+
* ';jsessionid'), while tickets are issued for, and users are sent to, the service as received. The service is
34+
* therefore only accepted when it starts with "scheme://host[:port]" and sanitizing it leaves that part
35+
* untouched, so that the host that was checked is the host that is used.
36+
*
37+
* @param string $service The service url as received in the request, neither url decoded nor sanitized
38+
*
39+
* @return \SimpleSAML\Configuration|null Return the configuration to use for this service,
40+
* or null if service is not allowed
41+
* @throws \ErrorException
42+
*/
43+
public function checkRequestedServiceURL(string $service): ?Configuration
44+
{
45+
$sanitizedService = TicketValidator::sanitize($service);
46+
if (!self::hasStableAuthority($service, $sanitizedService)) {
47+
Logger::warning(
48+
'casserver: Rejecting service url with a missing or ambiguous authority: '
49+
. json_encode($service, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE),
50+
);
51+
return null;
52+
}
53+
54+
return $this->checkServiceURL($sanitizedService);
55+
}
56+
57+
2858
/**
2959
* Check that the $service is allowed, and if so return the configuration to use.
3060
*
61+
* Only the given string is checked. For a service url received in a request, which is later used as received,
62+
* use checkRequestedServiceURL() instead.
63+
*
3164
* @param string $service The service url. Assume to already be url decoded
3265
*
3366
* @return \SimpleSAML\Configuration|null Return the configuration to use for this service,
@@ -114,4 +147,48 @@ protected function validateServiceIsLegal(string $legalUrl, string $service): bo
114147

115148
return $isValid;
116149
}
150+
151+
152+
/**
153+
* Whether a service starts with a scheme and authority (RFC 3986, section 3) that its sanitized form shares, and
154+
* that authority is a plain host with an optional port.
155+
*
156+
* A service without a leading "scheme://" is refused: browsers and SimpleSAMLphp still find a host in forms such
157+
* as "//host", "https:\\host" or " https://host" (WHATWG parsers strip whitespace and accept backslashes).
158+
* Userinfo, percent-encoding, backslashes, whitespace and control characters are refused in the authority: URL
159+
* parsers disagree about where such an authority ends, and decoding can move that end, so the host that
160+
* receives the ticket could differ from the host that was checked.
161+
*
162+
* @param string $service The service url as received
163+
* @param string $sanitizedService The same service url after sanitizing
164+
*
165+
* @return bool
166+
*/
167+
private static function hasStableAuthority(string $service, string $sanitizedService): bool
168+
{
169+
$schemeAndAuthority = self::getSchemeAndAuthority($service);
170+
if ($schemeAndAuthority === null) {
171+
return false;
172+
}
173+
174+
$authority = substr($schemeAndAuthority, strpos($schemeAndAuthority, '://') + 3);
175+
if ($authority === '' || preg_match('/[@%\\\\\x00-\x20\x7F]/', $authority) === 1) {
176+
return false;
177+
}
178+
179+
return self::getSchemeAndAuthority($sanitizedService) === $schemeAndAuthority;
180+
}
181+
182+
183+
/**
184+
* The leading "scheme://authority" of a url, where the authority ends at the first "/", "?" or "#".
185+
*
186+
* @param string $url
187+
*
188+
* @return string|null The scheme and authority, or null if the url does not start with "scheme://"
189+
*/
190+
private static function getSchemeAndAuthority(string $url): ?string
191+
{
192+
return preg_match('~^[A-Za-z][A-Za-z0-9+.\-]*://[^/?#]*~', $url, $matches) === 1 ? $matches[0] : null;
193+
}
117194
}

‎src/Controller/Cas20Controller.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ public function proxy(
138138
$targetService === null => 'Missing target service parameter [targetService]',
139139
// pgt parameter is not defined
140140
$pgt === null => 'Missing proxy granting ticket parameter: [pgt]',
141-
!$this->checkServiceURL($this->sanitize($targetService), $legal_target_service_urls) =>
141+
// The proxy ticket is issued for $targetService as received, so check it as received.
142+
!$this->checkRequestedServiceURL($targetService, $legal_target_service_urls) =>
142143
"Target service parameter not listed as a legal service: [targetService] = {$targetService}",
143144
default => null,
144145
};

‎src/Controller/LoginController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ public function handleServiceConfiguration(?string $serviceUrl): void
386386
if ($serviceUrl === null) {
387387
return;
388388
}
389-
$serviceCasConfig = $this->serviceValidator->checkServiceURL($this->sanitize($serviceUrl));
389+
// Tickets are issued for, and the user is sent to, $serviceUrl as received, so check it as received.
390+
$serviceCasConfig = $this->serviceValidator->checkRequestedServiceURL($serviceUrl);
390391
if (!isset($serviceCasConfig)) {
391392
$message = 'Service parameter provided to CAS server is not listed as a legal service: [service] = ' .
392393
var_export($serviceUrl, true);

‎src/Controller/LogoutController.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public function logout(
108108

109109
// Validate the return $url is valid
110110
if (!is_null($url)) {
111-
$isValidReturnUrl = !is_null($this->serviceValidator->checkServiceURL($this->sanitize($url)));
111+
// The user is sent to $url as received, so check it as received.
112+
$isValidReturnUrl = !is_null($this->serviceValidator->checkRequestedServiceURL($url));
112113
if (!$isValidReturnUrl) {
113114
try {
114115
$url = $this->httpUtils->checkURLAllowed($url);

‎src/Controller/Traits/UrlTrait.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
trait UrlTrait
1616
{
1717
/**
18+
* Check a service url, exactly as it was received in the request, against the given legal service urls.
19+
*
20+
* @param string $service The service url as received, neither url decoded nor sanitized
21+
* @param string[] $legal_service_urls
22+
*
23+
* @return bool
24+
* @throws \ErrorException
25+
* @see \SimpleSAML\Module\casserver\Cas\ServiceValidator::checkRequestedServiceURL()
26+
*/
27+
public function checkRequestedServiceURL(string $service, array $legal_service_urls): bool
28+
{
29+
$config = Configuration::loadFromArray(['legal_service_urls' => $legal_service_urls]);
30+
$serviceValidator = new ServiceValidator($config);
31+
return $serviceValidator->checkRequestedServiceURL($service) !== null;
32+
}
33+
34+
35+
/**
36+
* Only the given string is checked. For a service url received in a request, which is later used as received,
37+
* use checkRequestedServiceURL() instead.
38+
*
1839
* @param string $service
1940
* @param string[] $legal_service_urls
2041
*

‎tests/src/Controller/Cas20ControllerTest.php‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,53 @@ public function testProxyReturnsProxyTicket(): void
338338
}
339339

340340

341+
public function testProxyRejectsTargetServiceWithEncodedSlashesBeforeUserinfo(): void
342+
{
343+
$this->moduleConfig['legal_target_service_urls'] = ['https://myservice.com/abcd'];
344+
$casconfig = Configuration::loadFromArray($this->moduleConfig);
345+
// A valid proxy-granting ticket, so that only the target service can make the request fail.
346+
$this->ticketStore->addTicket([
347+
'id' => 'PGT-' . $this->sessionId,
348+
'validBefore' => 9999999999,
349+
'service' => 'https://myservice.com/abcd',
350+
'sessionId' => $this->sessionId,
351+
'forceAuthn' => false,
352+
'attributes' => ['eduPersonPrincipalName' => ['eduPersonPrincipalName@google.com']],
353+
'proxies' => [],
354+
]);
355+
$this->ticketStore->addTicket([
356+
'id' => $this->sessionId,
357+
'validBefore' => 9999999999,
358+
'service' => 'https://myservice.com/abcd',
359+
'sessionId' => $this->sessionId,
360+
]);
361+
362+
// Decodes to a legal target service, while its own host is evil.example.
363+
$targetService = 'https://myservice.com%2Fabcd@evil.example/';
364+
$params = ['pgt' => 'PGT-' . $this->sessionId, 'targetService' => $targetService];
365+
$request = Request::create(
366+
uri: Module::getModuleURL('casserver/proxy'),
367+
parameters: $params,
368+
);
369+
370+
$cas20Controller = new Cas20Controller($this->sspConfig, $casconfig);
371+
$response = $cas20Controller->proxy($request, ...$params);
372+
373+
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
374+
$this->assertStringContainsString(
375+
"Target service parameter not listed as a legal service: [targetService] = {$targetService}",
376+
$response->getContent(),
377+
);
378+
$xml = simplexml_load_string($response->getContent());
379+
$xml->registerXPathNamespace('cas', 'serviceResponse');
380+
$this->assertEmpty($xml->xpath('//cas:proxySuccess'));
381+
$this->assertEquals(
382+
C::ERR_INVALID_REQUEST,
383+
$xml->xpath('//cas:authenticationFailure')[0]->attributes()['code'],
384+
);
385+
}
386+
387+
341388
public static function validateFailsForEmptyServiceTicket(): array
342389
{
343390
return [

0 commit comments

Comments
 (0)