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

Exception when decoding Jackson-encoded Base64 binary value in YAML #90

Closed
tlrx opened this issue Jun 13, 2018 · 3 comments
Closed

Exception when decoding Jackson-encoded Base64 binary value in YAML #90

tlrx opened this issue Jun 13, 2018 · 3 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@tlrx
Copy link

tlrx commented Jun 13, 2018

Using jackson-dataformats-text-2.9.6 binary values that exceed a given length cannot be decoded back and the following exception is thrown:

com.fasterxml.jackson.core.JsonParseException: Illegal character '\' (code 0x5c) in base64 content

This is related to #62 which forces the Base64Variant to be MIME. When the encoded value exceeds a given size, line seperators are inserted into the encoded value but it then fails to be decoded.

I created the following small test and added it to the SimpleGenerationTest in yaml:

public void testCase() throws Exception {
        final int length = 50;
        final byte[] data = new byte[length];
        Arrays.fill(data, (byte) 1);

        final YAMLFactory yamlFactory = new YAMLFactory();

        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        try (JsonGenerator generator = yamlFactory.createGenerator(os, JsonEncoding.UTF8)) {
            generator.writeStartObject();
            generator.writeBinaryField("data", data);
            generator.writeEndObject();
        }

        try (JsonParser parser = yamlFactory.createParser(os.toByteArray())) {
            assertEquals(JsonToken.START_OBJECT, parser.nextToken());
            assertEquals(JsonToken.FIELD_NAME, parser.nextToken());
            assertEquals("data", parser.currentName());
            assertEquals(JsonToken.VALUE_EMBEDDED_OBJECT, parser.nextToken());
            assertArrayEquals(data, parser.getBinaryValue());
            assertEquals(JsonToken.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
    }

This test passed correctly when length is small (50) but with larger size (like 100) it fails.

@cowtowncoder cowtowncoder added yaml Issue related to YAML format backend 2.9 labels Jun 13, 2018
@cowtowncoder
Copy link
Member

Ok. Thank you for reporting this. Sounds like a bug indeed, related to splitting of base64 encoded lines to conform to 76 character limitation (for some variants).

Not sure what would be the best way to fix, and which part (jackson yaml codec or snakeyaml) is responsible for either unnecessary escaping, or missing decoding.

@vboulaye
Copy link
Contributor

I have the same problem encoding/decoding binary arrays longer than 76 characters after upgrading from 2.9.2 to 2.9.6.

I submitted a PR to try to fix it by changing the way the encoding is done in Base64Variant

vboulaye added a commit to vboulaye/jackson-dataformats-text that referenced this issue Oct 20, 2018
vboulaye added a commit to vboulaye/jackson-dataformats-text that referenced this issue Oct 20, 2018
mederly added a commit to Evolveum/midpoint that referenced this issue Nov 5, 2018
Because of FasterXML/jackson-dataformats-text#90
binary (Base64) data in YAML are not serialized correctly. This commit
introduces quite ugly hack that deals with it.
@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Feb 21, 2019
@cowtowncoder cowtowncoder added this to the 2.10.0 milestone Feb 27, 2019
@cowtowncoder cowtowncoder changed the title [YAML] Exception when decoding Base64 binary value Exception when decoding Jackson-encoded Base64 binary value in YAML Feb 27, 2019
cowtowncoder added a commit that referenced this issue Feb 27, 2019
@cowtowncoder
Copy link
Member

@tlrx Thank you for reporting this; fixed for 2.9.9/2.10.0 now. And special thanks to @vboulaye for providing the fix here. Sorry it took this long to get simple thing fixed, but better late than never.

pjankovsky pushed a commit to fivetran/jackson-dataformats-text that referenced this issue Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

3 participants