Skip to content

Commit ec625eb

Browse files
committed
Make Name ID and attribute value trimming an opt-in feature
Name IDs (including issuers) are by default left untouched, as well as attribute values, like it was before. However two new settings have been introduced (whose default value is false) which allow to enable trimming for such values, which is probably the desired behaviour in practice, although SAML specification says that no whitespace processing should be performed on strings. Another place where trimming may be desirable is in SessionIndex extraction from LogoutRequests: this is not performed at any point of the LogoutRequest processing, but an overloading has been provided so that the API consumer may still request trimming. AuthnResponseTest.testGetIssuersTrimming() is disabled by now because it fails due to a bug in SamlResponse.getIssuers() which is addressed by another PR.
1 parent 1a7b0da commit ec625eb

File tree

17 files changed

+659
-149
lines changed

17 files changed

+659
-149
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,15 @@ onelogin.saml2.security.digest_algorithm = http://www.w3.org/2001/04/xmlenc#sha2
362362
# Reject Signatures with deprecated algorithms (sha1)
363363
onelogin.saml2.security.reject_deprecated_alg = true
364364

365+
# Enable trimming of parsed Name IDs and attribute values
366+
# SAML specification states that no trimming for string elements should be performed, so no trimming will be
367+
# performed by default on extracted Name IDs and attribute values. However, some SAML implementations may add
368+
# undesirable surrounding whitespace when outputting XML (possibly due to formatting/pretty-printing).
369+
# These two options allow to optionally enable value trimming on extracted Name IDs (including issuers) and
370+
# attribute values.
371+
onelogin.saml2.parsing.trim_name_ids = false
372+
onelogin.saml2.parsing.trim_attribute_values = false
373+
365374
# Organization
366375
onelogin.saml2.organization.name = SP Java
367376
onelogin.saml2.organization.displayname = SP Java Example

core/src/main/java/com/onelogin/saml2/authn/SamlResponse.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public Map<String,String> getNameIdData() throws Exception {
471471

472472
if (nameIdElem != null) {
473473
String value = nameIdElem.getTextContent();
474-
if(value != null) {
474+
if(value != null && settings.isTrimNameIds()) {
475475
value = value.trim();
476476
}
477477
if (settings.isStrict() && StringUtils.isEmpty(value)) {
@@ -602,8 +602,8 @@ public HashMap<String, List<String>> getAttributes() throws XPathExpressionExcep
602602
for (int j = 0; j < childrens.getLength(); j++) {
603603
if ("AttributeValue".equals(childrens.item(j).getLocalName())) {
604604
String attrValue = childrens.item(j).getTextContent();
605-
if(attrValue != null) {
606-
attrValue = attrValue.toString();
605+
if(attrValue != null && settings.isTrimAttributeValues()) {
606+
attrValue = attrValue.trim();
607607
}
608608
attrValues.add(attrValue);
609609
}
@@ -735,7 +735,7 @@ public String getResponseIssuer() throws XPathExpressionException, ValidationErr
735735
if (responseIssuer.getLength() > 0) {
736736
if (responseIssuer.getLength() == 1) {
737737
String value = responseIssuer.item(0).getTextContent();
738-
if(value != null) {
738+
if(value != null && settings.isTrimNameIds()) {
739739
value = value.trim();
740740
}
741741
return value;
@@ -762,7 +762,7 @@ public String getAssertionIssuer() throws XPathExpressionException, ValidationEr
762762
NodeList assertionIssuer = this.queryAssertion("/saml:Issuer");
763763
if (assertionIssuer.getLength() == 1) {
764764
String value = assertionIssuer.item(0).getTextContent();
765-
if(value != null) {
765+
if(value != null && settings.isTrimNameIds()) {
766766
value = value.trim();
767767
}
768768
return value;

0 commit comments

Comments
 (0)