-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
Some invalid base64 inputs do not throw an exception while others do.
Code to reproduce the problem (using Jackson version 2.8.4):
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
public class JacksonBase64Decode {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
String base64Hello = Base64.getEncoder().encodeToString("hello".getBytes(StandardCharsets.US_ASCII));
String json_1 = "{\"foo\": \""+ base64Hello +"\"}";
String json_2 = "{\"foo\": \""+ base64Hello + "!" +"\"}";
String json_3 = "{\"foo\": \""+ base64Hello + "!!" +"\"}";
for (String json : Arrays.asList(json_1, json_2, json_3)) {
try {
JsonNode tree = mapper.readTree(json);
JsonNode foo = tree.get("foo");
byte[] value = foo.binaryValue();
String reEncodedValue = Base64.getEncoder().encodeToString(value);
System.out.printf("original json: %s; original textValue: %s; re-encoded binary value: %s%n",
json, foo.textValue(), reEncodedValue);
} catch (Exception e) {
System.out.printf("original json: %s; exception: %s%n", json, e.getMessage());
}
}
}
}
Output:
original json: {"foo": "aGVsbG8="}; original textValue: aGVsbG8=; re-encoded binary value: aGVsbG8=
original json: {"foo": "aGVsbG8=!"}; original textValue: aGVsbG8=!; re-encoded binary value: aGVsbG8=
original json: {"foo": "aGVsbG8=!!"}; exception: Illegal character '!' (code 0x21) in base64 content
at [Source: N/A; line: -1, column: -1]
First case - everything OK
Second case - I would expect the same behavior as in the third case
Third case - intuitively it's not surprising that an exception is thrown here.
However there seems to be a mismatch between code behavior and JavaDoc of binaryValue()
method which says that JsonNode::binaryValue
returns "Binary data this node contains, iff it is a binary node; null otherwise". So based on the javadoc I would expect that in the second and third case binaryValue()
returns null.
Metadata
Metadata
Assignees
Labels
No labels