Skip to content

Commit 63426fc

Browse files
committed
Prevent adding newline if Base64 buffer encoded ends directly on MAX_LINE_LENGTH
Motivation: We need to ensure we not add a newline if the Base64 encoded buffer ends directly on the MAX_LINE_LENGTH. If we miss to do so this produce invalid data. Because of this bug OpenSslServerContext and OpenSslClientContext may fail to load a cert. Modifications: - Only add NEW_LINE if we not are on the end of the dst buffer. - Add unit test Result: Correct result in all cases
1 parent ee56a4a commit 63426fc

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

codec/src/main/java/io/netty/handler/codec/base64/Base64.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ public static ByteBuf encode(
121121
int e = 0;
122122
int len2 = len - 2;
123123
int lineLength = 0;
124-
for (; d < len2; d += 3, e += 4) {
124+
for (; d < len2; e += 4) {
125125
encode3to4(src, d + off, 3, dest, e, dialect);
126126

127127
lineLength += 4;
128-
if (breakLines && lineLength == MAX_LINE_LENGTH) {
128+
d += 3;
129+
130+
if (breakLines && lineLength == MAX_LINE_LENGTH
131+
// Only add NEW_LINE if we not ended directly on the MAX_LINE_LENGTH
132+
&& d < len2) {
129133
dest.setByte(e + 4, NEW_LINE);
130134
e ++;
131135
lineLength = 0;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.codec.base64;
17+
18+
import io.netty.buffer.ByteBuf;
19+
import io.netty.util.CharsetUtil;
20+
import org.junit.Test;
21+
22+
23+
import static io.netty.buffer.Unpooled.copiedBuffer;
24+
import static org.junit.Assert.assertEquals;
25+
26+
public class Base64Test {
27+
28+
@Test
29+
public void testNotAddNewLineWhenEndOnLimit() {
30+
ByteBuf src = copiedBuffer("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde",
31+
CharsetUtil.US_ASCII);
32+
ByteBuf expectedEncoded =
33+
copiedBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5emFiY2Rl",
34+
CharsetUtil.US_ASCII);
35+
testEncode(src, expectedEncoded);
36+
}
37+
38+
@Test
39+
public void testAddNewLine() {
40+
ByteBuf src = copiedBuffer("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz12345678",
41+
CharsetUtil.US_ASCII);
42+
ByteBuf expectedEncoded =
43+
copiedBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejEyMzQ1\nNjc4",
44+
CharsetUtil.US_ASCII);
45+
testEncode(src, expectedEncoded);
46+
}
47+
48+
private static void testEncode(ByteBuf src, ByteBuf expectedEncoded) {
49+
ByteBuf encoded = Base64.encode(src, true, Base64Dialect.STANDARD);
50+
try {
51+
assertEquals(expectedEncoded, encoded);
52+
} finally {
53+
src.release();
54+
expectedEncoded.release();
55+
encoded.release();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)