Skip to content

Commit

Permalink
allowRepeatAttributeName settings added in order to support Attribute…
Browse files Browse the repository at this point in the history
…Statements with Attribute elements with the same name
  • Loading branch information
pitbulk committed Nov 25, 2020
1 parent c12a61a commit 370a5d9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ $advancedSettings = array (
// attribute will not be rejected for this fact.
'relaxDestinationValidation' => false,

// If true, the toolkit will not raised an error when the Statement Element
// contain atribute elements with name duplicated
'allowRepeatAttributeName' => false,

// If true, Destination URL should strictly match to the address to
// which the response has been sent.
// Notice that if 'relaxDestinationValidation' is true an empty Destintation
Expand Down
4 changes: 4 additions & 0 deletions advanced_settings_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
// attribute will not be rejected for this fact.
'relaxDestinationValidation' => false,

// If true, the toolkit will not raised an error when the Statement Element
// contain atribute elements with name duplicated
'allowRepeatAttributeName' => false,

// If true, Destination URL should strictly match to the address to
// which the response has been sent.
// Notice that if 'relaxDestinationValidation' is true an empty Destintation
Expand Down
19 changes: 14 additions & 5 deletions lib/Saml2/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,9 @@ private function _getAttributesByKeyName($keyName = "Name")

$entries = $this->_queryAssertion('/saml:AttributeStatement/saml:Attribute');

$security = $this->_settings->getSecurityData();
$allowRepeatAttributeName = $security['allowRepeatAttributeName'];

/** @var $entry DOMNode */
foreach ($entries as $entry) {
$attributeKeyNode = $entry->attributes->getNamedItem($keyName);
Expand All @@ -790,10 +793,12 @@ private function _getAttributesByKeyName($keyName = "Name")
$attributeKeyName = $attributeKeyNode->nodeValue;

if (in_array($attributeKeyName, array_keys($attributes))) {
throw new OneLogin_Saml2_ValidationError(
"Found an Attribute element with duplicated ".$keyName,
OneLogin_Saml2_ValidationError::DUPLICATED_ATTRIBUTE_NAME_FOUND
);
if (!$allowRepeatAttributeName) {
throw new OneLogin_Saml2_ValidationError(
"Found an Attribute element with duplicated ".$keyName,
OneLogin_Saml2_ValidationError::DUPLICATED_ATTRIBUTE_NAME_FOUND
);
}
}

$attributeValues = array();
Expand All @@ -804,7 +809,11 @@ private function _getAttributesByKeyName($keyName = "Name")
}
}

$attributes[$attributeKeyName] = $attributeValues;
if (in_array($attributeKeyName, array_keys($attributes))) {
$attributes[$attributeKeyName] = array_merge($attributes[$attributeKeyName], $attributeValues);
} else {
$attributes[$attributeKeyName] = $attributeValues;
}
}
return $attributes;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Saml2/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ private function _addDefaultValues()
$this->_security['relaxDestinationValidation'] = false;
}

// Allow duplicated Attribute Names
if (!isset($this->_security['allowRepeatAttributeName'])) {
$this->_security['allowRepeatAttributeName'] = false;
}

// Strict Destination match validation
if (!isset($this->_security['destinationStrictlyMatches'])) {
Expand Down
8 changes: 8 additions & 0 deletions tests/src/OneLogin/Saml2/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,14 @@ public function testGetAttributes()
} catch (OneLogin_Saml2_ValidationError $e) {
$this->assertContains('Found an Attribute element with duplicated Name', $e->getMessage());
}

$settingsDir = TEST_ROOT .'/settings/';
include $settingsDir.'settings1.php';
$settingsInfo['security']['allowRepeatAttributeName'] = true;
$settings2 = new OneLogin_Saml2_Settings($settingsInfo);
$response5 = new OneLogin_Saml2_Response($settings2, $xml4);
$attrs = $response5->getAttributes();
$this->assertEquals([0 => "test", 1 => "test2"], $attrs['uid']);
}

/**
Expand Down

0 comments on commit 370a5d9

Please sign in to comment.