Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonNode.binaryValue() ignores illegal character if it's the last one #1425

Closed
binoternary opened this issue Oct 20, 2016 · 1 comment
Closed
Milestone

Comments

@binoternary
Copy link

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.

@cowtowncoder
Copy link
Member

Interesting. If I use direct data-binding with property of type byte[], tests work as expected.
So it seems to be due to slightly different code path for tree access that is at work here...

@cowtowncoder cowtowncoder added this to the 2.8.6 milestone Nov 26, 2016
@cowtowncoder cowtowncoder removed the 2.8 label Nov 26, 2016
@cowtowncoder cowtowncoder changed the title JsonNode::binaryValue ignores illegal character JsonNode.binaryValue() ignores illegal character if it's the last one Nov 26, 2016
cowtowncoder added a commit that referenced this issue Nov 26, 2016
cowtowncoder added a commit that referenced this issue Nov 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants