Skip to content

Commit 9823fec

Browse files
committed
Option to redirect to exact service url
Some clients will encode query parameters in their service url incorrectly. If SSP/php's query builder utils are used then the service url is parsed, and reconstructed differently then what is stored in the ticket. Sometimes badly encoded parameters are lost. Example: should space in a query param be encoded as '+' or '%20'
1 parent f27ac9f commit 9823fec

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

config-templates/module_casserver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
'attributes_to_transfer' => ['cn'],
3333
],
3434
],
35+
// Don't use php or SSP's built in methods for constructing queries. Default is false
36+
// Enabling this redirects the user back to the service with the exact service url provided.
37+
'noReencode' => false,
3538

3639
'legal_target_service_urls' => [
3740
//Any target service url string matching any of the following prefixes is accepted

tests/config/module_casserver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
],
3131
'http://changeTicketParam' => [
3232
'ticketName' => 'myTicket',
33+
],
34+
'https://buggy.edu' => [
35+
// Don't use php or SSP's built in methods for constructing queries.
36+
'noReencode' => true
3337
]
3438
],
3539

tests/www/LoginIntegrationTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,43 @@ public function testValidTicketNameOverride()
227227
);
228228
}
229229

230+
/**
231+
* Some clients don't correctly encode query parameters that are part their service
232+
* urls or encode a space in a different way then SSP will in a redirect. This workaround
233+
* is to allow those clients to work
234+
* @dataProvider buggyClientProvider
235+
* @return void
236+
*/
237+
public function testBuggyClientBadUrlEncodingWorkAround($service_url)
238+
{
239+
$this->authenticate();
240+
241+
/** @var array $resp */
242+
$resp = $this->server->get(
243+
self::$LINK_URL,
244+
['service' => $service_url],
245+
[
246+
CURLOPT_COOKIEJAR => $this->cookies_file,
247+
CURLOPT_COOKIEFILE => $this->cookies_file
248+
]
249+
);
250+
$this->assertEquals(302, $resp['code']);
251+
252+
$this->assertStringStartsWith(
253+
$service_url . '?ticket=ST-',
254+
$resp['headers']['Location'],
255+
'Ticket should be part of the redirect.'
256+
);
257+
}
258+
259+
public function buggyClientProvider(): array
260+
{
261+
return [
262+
['https://buggy.edu/kc/portal.do?solo&ct=Search%20Prot&curl=https://kc.edu/kc/IRB.do?se=1875*&runSearch=1'],
263+
['https://buggy.edu/kc'],
264+
];
265+
}
266+
230267

231268
/**
232269
* Test outputting user info instead of redirecting

www/login.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
* language
2929
*/
3030

31+
use SimpleSAML\Configuration;
32+
use SimpleSAML\Locale\Language;
33+
use SimpleSAML\Logger;
34+
use SimpleSAML\Module;
3135
use SimpleSAML\Module\casserver\Cas\AttributeExtractor;
3236
use SimpleSAML\Module\casserver\Cas\Protocol\SamlValidateResponder;
3337
use SimpleSAML\Module\casserver\Cas\ServiceValidator;
3438
use SimpleSAML\Module\casserver\Cas\Ticket\TicketFactory;
3539
use SimpleSAML\Module\casserver\Cas\Ticket\TicketStore;
36-
use SimpleSAML\Configuration;
37-
use SimpleSAML\Locale\Language;
38-
use SimpleSAML\Logger;
39-
use SimpleSAML\Module;
4040
use SimpleSAML\Session;
4141
use SimpleSAML\Utils\HTTP;
4242

@@ -223,7 +223,15 @@
223223
echo '<pre>' . htmlspecialchars($casResponse) . '</pre>';
224224
}
225225
} elseif ($redirect) {
226-
HTTP::redirectTrustedURL(HTTP::addURLParameters($serviceUrl, $parameters));
226+
if ($casconfig->getBoolean('noReencode', false)) {
227+
// Some client encode query params wrong, and calling HTTP::addURLParameters
228+
// will reencode them resulting in service mismatches
229+
$extraParams = http_build_query($parameters);
230+
$redirectUrl = $_GET['service'] . (strpos('?', $_GET['service']) === false ? '?' : '&') . $extraParams;
231+
HTTP::redirectTrustedURL($redirectUrl);
232+
} else {
233+
HTTP::redirectTrustedURL(HTTP::addURLParameters($_GET['service'], $parameters));
234+
}
227235
} else {
228236
HTTP::submitPOSTData($serviceUrl, $parameters);
229237
}

0 commit comments

Comments
 (0)