Skip to content

fix: Base64 decoding to discard newline characters #1941

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

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
@Deprecated
public class Base64 {
// Special decoders that discards the new line character so that the behavior matches what
// we had with Apache Commmons Codec's decodeBase64.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be worth commenting on what the 64 means. (It doesn't mean very much, since it's only used for encoding and we don't do that.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explanation of the 2nd parameter of the withSeparator method.

private static final BaseEncoding BASE64 = BaseEncoding.base64().withSeparator("\n", 64);
private static final BaseEncoding BASE64URL = BaseEncoding.base64Url().withSeparator("\n", 64);

/**
* Encodes binary data using the base64 algorithm but does not chunk the output.
Expand Down Expand Up @@ -92,6 +96,9 @@ public static byte[] decodeBase64(byte[] base64Data) {
* Decodes a Base64 String into octets. Note that this method handles both URL-safe and
* non-URL-safe base 64 encoded strings.
*
* <p>For the compatibility with the old version that used Apache Commons Coded's decodeBase64,
* this method discards new line characters and trailing whitespaces.
*
* @param base64String String containing Base64 data or {@code null} for {@code null} result
* @return Array containing decoded data or {@code null} for {@code null} input
*/
Expand All @@ -100,10 +107,10 @@ public static byte[] decodeBase64(String base64String) {
return null;
}
try {
return BaseEncoding.base64().decode(base64String);
return BASE64.decode(base64String);
} catch (IllegalArgumentException e) {
if (e.getCause() instanceof DecodingException) {
return BaseEncoding.base64Url().decode(base64String.trim());
return BASE64URL.decode(base64String.trim());
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,45 @@ public void test_encodeBase64URLSafe_withNull_shouldReturnNull() {
public void test_encodeBase64_withNull_shouldReturnNull() {
assertNull(Base64.encodeBase64(null));
}

public void test_decodeBase64_newline_character_invalid_length() {
// The RFC 4648 (https://datatracker.ietf.org/doc/html/rfc4648#section-3.3) states that a
// specification referring to the Base64 encoding may state that it ignores characters outside
// the base alphabet.

// In Base64 encoding, 3 characters (24 bits) are converted to 4 of 6-bits, each of which is
// converted to a byte (a character).
// Base64encode("abc") => "YWJj" (4 characters)
// Base64encode("def") => "ZGVm" (4 characters)
// Adding a new line character between them. This should be discarded.
String encodedString = "YWJj\nZGVm";

// This is a reference implementation by Apache Commons Codec. It discards the new line
// characters.
assertEquals(
"abcdef",
new String(
org.apache.commons.codec.binary.Base64.decodeBase64(encodedString),
StandardCharsets.UTF_8));
// This is our implementation
assertEquals("abcdef", new String(Base64.decodeBase64(encodedString), StandardCharsets.UTF_8));
}

public void test_decodeBase64_newline_character_between() {
// In Base64 encoding, 2 characters (16 bits) are converted to 3 of 6-bits plus the padding
// character ('=").
// Base64encode("ab") => "YWI=" (3 characters + padding character)
// Adding a new line character that should be discarded between them
String encodedString = "YW\nI=";

// This is a reference implementation by Apache Commons Codec. It discards the new line
// characters.
assertEquals(
"ab",
new String(
org.apache.commons.codec.binary.Base64.decodeBase64(encodedString),
StandardCharsets.UTF_8));
// This is our implementation
assertEquals("ab", new String(Base64.decodeBase64(encodedString), StandardCharsets.UTF_8));
}
}
Loading