Skip to content

Commit c6d5c60

Browse files
committed
base64: strip carriage returns and newlines
The guava [1] doc mentions that carriage returns and newlines are not permitted, and recommend using .withSeparator. We do not know whether the input string will contain a carriage return, or a newline, or both, so instead we'll just strip them each individually. This fixes an internal google bug that was introduced in as part of the move from Apache Commons Codec to Guava in ef9f914. 1: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/io/BaseEncoding.html#withSeparator(java.lang.String,%20int)
1 parent b62e506 commit c6d5c60

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

google-http-client/src/main/java/com/google/api/client/util/Base64.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public static byte[] decodeBase64(String base64String) {
105105
return null;
106106
}
107107

108+
base64String = base64String.replace("\r", "");
109+
base64String = base64String.replace("\n", "");
110+
108111
try {
109112
return BaseEncoding.base64().decode(base64String);
110113
} catch (IllegalArgumentException e) {

google-http-client/src/test/java/com/google/api/client/util/Base64Test.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package com.google.api.client.util;
1515

16+
import java.util.Arrays;
1617
import junit.framework.TestCase;
1718

1819
/**
@@ -54,4 +55,10 @@ public void testDecode() {
5455
String value = new String(Base64.decodeBase64(Base64.encodeBase64("foobar".getBytes())));
5556
assertEquals("foobar", value);
5657
}
58+
59+
public void testDecodeStripsCarriageReturnsAndNewlines() {
60+
byte[] actual = Base64.decodeBase64("aGVsbG8gd29ybGQ=\r\n");
61+
byte[] expected = "hello world".getBytes();
62+
assertTrue(Arrays.equals(actual, expected));
63+
}
5764
}

0 commit comments

Comments
 (0)