Skip to content

Commit 67fc16f

Browse files
[#325] restore permissive base64 decoding
1 parent 31505d0 commit 67fc16f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

api/src/main/java/jakarta/xml/bind/DatatypeConverterImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2007, 2024 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -711,8 +712,8 @@ private static int guessLength(String text) {
711712
* because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast)
712713
*/
713714
public static byte[] _parseBase64Binary(String text) {
714-
if (null == text || text.length() % 4 != 0) {
715-
throw new IllegalArgumentException("base64 text invalid.");
715+
if (null == text) {
716+
throw new IllegalArgumentException("base64 \"null\" text invalid.");
716717
}
717718
final int buflen = guessLength(text);
718719
final byte[] out = new byte[buflen];
@@ -738,9 +739,15 @@ public static byte[] _parseBase64Binary(String text) {
738739
// quadruplet is now filled.
739740
out[o++] = (byte) ((quadruplet[0] << 2) | (quadruplet[1] >> 4));
740741
if (quadruplet[2] != PADDING) {
742+
if (buflen == o) {
743+
throw new IllegalArgumentException("base64 text invalid.");
744+
}
741745
out[o++] = (byte) ((quadruplet[1] << 4) | (quadruplet[2] >> 2));
742746
}
743747
if (quadruplet[3] != PADDING) {
748+
if (buflen == o) {
749+
throw new IllegalArgumentException("base64 text invalid.");
750+
}
744751
out[o++] = (byte) ((quadruplet[2] << 6) | (quadruplet[3]));
745752
}
746753
q = 0;

api/src/test/java/org/eclipse/jaxb/api/DatatypeConverterTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -84,6 +85,8 @@ public void testPrint() {
8485
@Test
8586
public void testBase64() {
8687
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("Qxx=="));
88+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQQxx=="));
89+
Assert.assertThrows(IllegalArgumentException.class, () -> DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\nhbiBleGFtcGxl=="));
8790

8891
Assert.assertEquals("", new String(DatatypeConverter.parseBase64Binary("")));
8992
Assert.assertEquals("f", new String(DatatypeConverter.parseBase64Binary("Zg==")));
@@ -92,6 +95,9 @@ public void testBase64() {
9295
Assert.assertEquals("foob", new String(DatatypeConverter.parseBase64Binary("Zm9vYg==")));
9396
Assert.assertEquals("fooba", new String(DatatypeConverter.parseBase64Binary("Zm9vYmE=")));
9497
Assert.assertEquals("foobar", new String(DatatypeConverter.parseBase64Binary("Zm9vYmFy")));
98+
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB hbiBleGFtcGxl")));
99+
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\nhbiBleGFtcGxl")));
100+
Assert.assertEquals("this is an example", new String(DatatypeConverter.parseBase64Binary("dGhpcyBpcyB\thbiBleGFtcGxl")));
95101

96102
Assert.assertNotEquals("Hello, world!", new String(DatatypeConverter.parseBase64Binary("SGVsbG8sIJdvcmxkIQ==")));
97103

0 commit comments

Comments
 (0)