From d0a34b2782ba849cada1fa3485c5f61ee5fd81ab Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 15 Dec 2017 13:44:56 -0800 Subject: [PATCH] Fix #414 --- pom.xml | 2 +- release-notes/VERSION | 5 +++++ .../fasterxml/jackson/core/Base64Variant.java | 12 ++++++---- .../core/base64/Base64BinaryParsingTest.java | 22 +++++++++++++------ .../jackson/core/base64/Base64CodecTest.java | 10 +++++++++ 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 018634e45e..b6d366b088 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.fasterxml.jackson jackson-base - 2.9.3 + 2.9.4-SNAPSHOT com.fasterxml.jackson.core diff --git a/release-notes/VERSION b/release-notes/VERSION index 10a916bdf1..c7566600f0 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -14,6 +14,11 @@ JSON library. === Releases === ------------------------------------------------------------------------ +2.9.4 (not yet released) + +#414: Base64 MIME variant does not ignore white space chars as per RFC2045 + (reporte by tmoschou@github) + 2.9.3 (09-Dec-2017) #419: `ArrayIndexOutOfBoundsException` from `UTF32Reader.read()` on invalid input diff --git a/src/main/java/com/fasterxml/jackson/core/Base64Variant.java b/src/main/java/com/fasterxml/jackson/core/Base64Variant.java index 5cddea5108..68c5911f90 100644 --- a/src/main/java/com/fasterxml/jackson/core/Base64Variant.java +++ b/src/main/java/com/fasterxml/jackson/core/Base64Variant.java @@ -9,7 +9,7 @@ import com.fasterxml.jackson.core.util.ByteArrayBuilder; /** - * Abstract base class used to define specific details of which + * Class used to define specific details of which * variant of Base64 encoding/decoding is to be used. Although there is * somewhat standard basic version (so-called "MIME Base64"), other variants * exists, see Base64 Wikipedia entry for details. @@ -457,13 +457,17 @@ public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentE { int ptr = 0; int len = str.length(); - - while (ptr < len) { + + main_loop: + while (true) { // first, we'll skip preceding white space, if any char ch; do { + if (ptr >= len) { + break main_loop; + } ch = str.charAt(ptr++); - } while ((ptr < len) && (ch <= INT_SPACE)); + } while (ch <= INT_SPACE); int bits = decodeBase64Char(ch); if (bits < 0) { _reportInvalidBase64(ch, 0, null); diff --git a/src/test/java/com/fasterxml/jackson/core/base64/Base64BinaryParsingTest.java b/src/test/java/com/fasterxml/jackson/core/base64/Base64BinaryParsingTest.java index 24b53ea45b..3513eb55fe 100644 --- a/src/test/java/com/fasterxml/jackson/core/base64/Base64BinaryParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/base64/Base64BinaryParsingTest.java @@ -32,7 +32,11 @@ public void testStreaming() throws IOException public void testSimple() throws IOException { for (int mode : ALL_MODES) { - _testSimple(mode); + // [core#414]: Allow leading/trailign white-space, ensure it is accepted + _testSimple(mode, false, false); + _testSimple(mode, true, false); + _testSimple(mode, false, true); + _testSimple(mode, true, true); } } @@ -233,23 +237,27 @@ private void _testStreaming(int mode) throws IOException } } - private void _testSimple(int mode) - throws IOException + private void _testSimple(int mode, boolean leadingWS, boolean trailingWS) throws IOException { - /* The usual sample input string, from Thomas Hobbes's "Leviathan" - * (via Wikipedia) - */ + // The usual sample input string, from Thomas Hobbes's "Leviathan" + // (via Wikipedia) final String RESULT = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; final byte[] RESULT_BYTES = RESULT.getBytes("US-ASCII"); // And here's what should produce it... - final String INPUT_STR = + String INPUT_STR = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" +"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" +"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" +"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" +"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ; + if (leadingWS) { + INPUT_STR = " "+INPUT_STR; + } + if (leadingWS) { + INPUT_STR = INPUT_STR+" "; + } final String DOC = "\""+INPUT_STR+"\""; JsonParser p = createParser(mode, DOC); diff --git a/src/test/java/com/fasterxml/jackson/core/base64/Base64CodecTest.java b/src/test/java/com/fasterxml/jackson/core/base64/Base64CodecTest.java index 6b895ed128..a1f8174eb0 100644 --- a/src/test/java/com/fasterxml/jackson/core/base64/Base64CodecTest.java +++ b/src/test/java/com/fasterxml/jackson/core/base64/Base64CodecTest.java @@ -84,6 +84,16 @@ public void testConvenienceMethods() throws Exception Assert.assertArrayEquals(input, decoded); assertEquals(quote(encoded), std.encode(input, true)); + + // [core#414]: check white-space allow too + decoded = std.decode("\n"+encoded); + Assert.assertArrayEquals(input, decoded); + decoded = std.decode(" "+encoded); + Assert.assertArrayEquals(input, decoded); + decoded = std.decode(encoded + " "); + Assert.assertArrayEquals(input, decoded); + decoded = std.decode(encoded + "\n"); + Assert.assertArrayEquals(input, decoded); } @SuppressWarnings("unused")