1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-24 15:22:07 +01:00

Delete XML whitespace before feeding the Base64 decoder

This commit is contained in:
Florian Schmaus 2019-08-05 22:20:36 +02:00
parent 1bc8a22b28
commit d7b7abc7eb
2 changed files with 12 additions and 2 deletions

View file

@ -543,9 +543,15 @@ public class StringUtils {
return cs.toString();
}
private static final Pattern XMLWHITESPACE = Pattern.compile("[\t\n\r ]");
/**
* Defined by XML 1.0 § 2.3 as:
* S ::= (#x20 | #x9 | #xD | #xA)+
*
* @see <a href="https://www.w3.org/TR/xml/#sec-white-space">XML 1.0 § 2.3</a>
*/
private static final Pattern XML_WHITESPACE = Pattern.compile("[\t\n\r ]");
public static String deleteXmlWhitespace(String string) {
return XMLWHITESPACE.matcher(string).replaceAll("");
return XML_WHITESPACE.matcher(string).replaceAll("");
}
}

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smack.util.stringencoder;
import java.nio.charset.StandardCharsets;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
public class Base64 {
@ -57,6 +58,9 @@ public class Base64 {
// TODO: We really should not mask the IllegalArgumentException. But some unit test depend on this behavior, like
// ibb.packet.DataPacketExtension.shouldReturnNullIfDataIsInvalid().
public static final byte[] decode(String string) {
// xs:base64Binary may include XML whitespace which we need to delete before feeding the string into the Base64
// decoder. See also XML Schema Part 2: Datatypes Second Edition § 3.2.16.
string = StringUtils.deleteXmlWhitespace(string);
try {
return base64encoder.decode(string);
} catch (IllegalArgumentException e) {