Skip to content

Commit ab20c86

Browse files
authored
Merge pull request SAML-Toolkits#621 from SAML-Toolkits/improve_validate_binary_sign_master
Improve validate binary sign [master]
2 parents 8e722b1 + d423211 commit ab20c86

File tree

3 files changed

+461
-0
lines changed

3 files changed

+461
-0
lines changed

lib/Saml2/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class OneLogin_Saml2_Error extends Exception
2525
const SAML_SINGLE_LOGOUT_NOT_SUPPORTED = 12;
2626
const PRIVATE_KEY_NOT_FOUND = 13;
2727
const UNSUPPORTED_SETTINGS_OBJECT = 14;
28+
const INVALID_PARAMETER = 15;
2829

2930
/**
3031
* Constructor

lib/Saml2/Utils.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ protected static function buildWithBaseURLPath($info)
730730
*/
731731
public static function extractOriginalQueryParam($name)
732732
{
733+
if (!isset($_SERVER['QUERY_STRING']) || empty($_SERVER['QUERY_STRING'])) {
734+
return '';
735+
}
736+
733737
$index = strpos($_SERVER['QUERY_STRING'], $name.'=');
734738
$substring = substr($_SERVER['QUERY_STRING'], $index + strlen($name) + 1);
735739
$end = strpos($substring, '&');
@@ -1511,12 +1515,41 @@ public static function validateBinarySign($messageType, $getData, $idpData, $ret
15111515
}
15121516

15131517
if ($retrieveParametersFromServer) {
1518+
if (!isset($_SERVER['QUERY_STRING']) || empty($_SERVER['QUERY_STRING'])) {
1519+
throw new OneLogin_Saml2_Error(
1520+
"No query string provided",
1521+
OneLogin_Saml2_Error::INVALID_PARAMETER
1522+
);
1523+
}
1524+
$keys = array("SAMLRequest", "SAMLResponse", "RelayState", "SigAlg", "Signature");
1525+
foreach ($keys as $key) {
1526+
if (substr_count($_SERVER['QUERY_STRING'], $key) > 1) {
1527+
throw new OneLogin_Saml2_Error(
1528+
"Duplicate parameter in query string",
1529+
OneLogin_Saml2_Error::INVALID_PARAMETER
1530+
);
1531+
}
1532+
}
1533+
if (substr_count($_SERVER['QUERY_STRING'], "SAMLRequest") > 0 && substr_count($_SERVER['QUERY_STRING'], "SAMLResponse") > 0) {
1534+
throw new OneLogin_Saml2_Error(
1535+
"Both SAMLRequest and SAMLResponse provided",
1536+
OneLogin_Saml2_Error::INVALID_PARAMETER
1537+
);
1538+
}
1539+
15141540
$signedQuery = $messageType.'='.OneLogin_Saml2_Utils::extractOriginalQueryParam($messageType);
15151541
if (isset($getData['RelayState'])) {
15161542
$signedQuery .= '&RelayState='.OneLogin_Saml2_Utils::extractOriginalQueryParam('RelayState');
15171543
}
15181544
$signedQuery .= '&SigAlg='.OneLogin_Saml2_Utils::extractOriginalQueryParam('SigAlg');
15191545
} else {
1546+
if (isset($getData['SAMLRequest']) && isset($getData['SAMLResponse'])) {
1547+
throw new OneLogin_Saml2_Error(
1548+
"Both SAMLRequest and SAMLResponse provided",
1549+
OneLogin_Saml2_Error::INVALID_PARAMETER
1550+
);
1551+
}
1552+
15201553
$signedQuery = $messageType.'='.urlencode($getData[$messageType]);
15211554
if (isset($getData['RelayState'])) {
15221555
$signedQuery .= '&RelayState='.urlencode($getData['RelayState']);

0 commit comments

Comments
 (0)