Skip to content

Commit 3125450

Browse files
authored
Merge pull request #340 from mauromol/trim-subject-name-id
Trim values obtained with getTextContent() on any XML node
2 parents 6fe8eec + 1562784 commit 3125450

File tree

16 files changed

+755
-153
lines changed

16 files changed

+755
-153
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: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import javax.xml.xpath.XPathExpressionException;
1515

1616
import com.onelogin.saml2.model.hsm.HSM;
17+
18+
import org.apache.commons.lang3.StringUtils;
1719
import org.joda.time.DateTime;
1820
import org.joda.time.Instant;
1921
import org.slf4j.Logger;
@@ -469,7 +471,10 @@ public Map<String,String> getNameIdData() throws Exception {
469471

470472
if (nameIdElem != null) {
471473
String value = nameIdElem.getTextContent();
472-
if (settings.isStrict() && value.isEmpty()) {
474+
if(value != null && settings.isTrimNameIds()) {
475+
value = value.trim();
476+
}
477+
if (settings.isStrict() && StringUtils.isEmpty(value)) {
473478
throw new ValidationError("An empty NameID value found", ValidationError.EMPTY_NAMEID);
474479
}
475480

@@ -596,7 +601,11 @@ public HashMap<String, List<String>> getAttributes() throws XPathExpressionExcep
596601
}
597602
for (int j = 0; j < childrens.getLength(); j++) {
598603
if ("AttributeValue".equals(childrens.item(j).getLocalName())) {
599-
attrValues.add(childrens.item(j).getTextContent());
604+
String attrValue = childrens.item(j).getTextContent();
605+
if(attrValue != null && settings.isTrimAttributeValues()) {
606+
attrValue = attrValue.trim();
607+
}
608+
attrValues.add(attrValue);
600609
}
601610
}
602611

@@ -699,8 +708,11 @@ public List<String> getAudiences() throws XPathExpressionException {
699708
for (int i = 0; i < entries.getLength(); i++) {
700709
if (entries.item(i) != null) {
701710
String value = entries.item(i).getTextContent();
702-
if (value != null && !value.trim().isEmpty()) {
703-
audiences.add(value.trim());
711+
if(value != null) {
712+
value = value.trim();
713+
}
714+
if(!StringUtils.isEmpty(value)) {
715+
audiences.add(value);
704716
}
705717
}
706718
}
@@ -722,7 +734,11 @@ public String getResponseIssuer() throws XPathExpressionException, ValidationErr
722734
NodeList responseIssuer = Util.query(samlResponseDocument, "/samlp:Response/saml:Issuer");
723735
if (responseIssuer.getLength() > 0) {
724736
if (responseIssuer.getLength() == 1) {
725-
return responseIssuer.item(0).getTextContent();
737+
String value = responseIssuer.item(0).getTextContent();
738+
if(value != null && settings.isTrimNameIds()) {
739+
value = value.trim();
740+
}
741+
return value;
726742
} else {
727743
throw new ValidationError("Issuer of the Response is multiple.", ValidationError.ISSUER_MULTIPLE_IN_RESPONSE);
728744
}
@@ -745,7 +761,11 @@ public String getResponseIssuer() throws XPathExpressionException, ValidationErr
745761
public String getAssertionIssuer() throws XPathExpressionException, ValidationError {
746762
NodeList assertionIssuer = this.queryAssertion("/saml:Issuer");
747763
if (assertionIssuer.getLength() == 1) {
748-
return assertionIssuer.item(0).getTextContent();
764+
String value = assertionIssuer.item(0).getTextContent();
765+
if(value != null && settings.isTrimNameIds()) {
766+
value = value.trim();
767+
}
768+
return value;
749769
} else {
750770
throw new ValidationError("Issuer of the Assertion not found or multiple.", ValidationError.ISSUER_NOT_FOUND_IN_ASSERTION);
751771
}

0 commit comments

Comments
 (0)