Skip to content

Commit cb24c1b

Browse files
author
merit\rembjo0
committed
Adds UTF-8 encoding when converting bytes to string
XML validation fails when the JVM does not use UTF-8 as the default encoding. Added UTF-8 encoding in byte to string conversion in: * SamlResponse - after base64decode SAMLResponse * Util.convertDocumentToString()
1 parent 26def30 commit cb24c1b

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public SamlResponse(Saml2Settings settings, HttpRequest request) throws XPathExp
120120
* @throws ValidationError
121121
*/
122122
public void loadXmlFromBase64(String responseStr) throws ParserConfigurationException, XPathExpressionException, SAXException, IOException, SettingsException, ValidationError {
123-
samlResponseString = new String(Util.base64decoder(responseStr));
123+
samlResponseString = new String(Util.base64decoder(responseStr), "UTF-8");
124124
samlResponseDocument = Util.loadXML(samlResponseString);
125125

126126
if (samlResponseDocument == null) {

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,16 @@ public static String convertDocumentToString(Document doc, Boolean c14n) {
329329
} else {
330330
XMLUtils.outputDOM(doc, baos);
331331
}
332-
return baos.toString();
332+
333+
return Util.toUtf8String(baos.toByteArray());
334+
}
335+
336+
private static String toUtf8String(byte[] bytes) {
337+
try {
338+
return new String(bytes, "UTF-8");
339+
} catch (UnsupportedEncodingException e) {
340+
throw new IllegalStateException(e);
341+
}
333342
}
334343

335344
/**

0 commit comments

Comments
 (0)