@@ -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}
0 commit comments