Skip to content

Commit 7143c55

Browse files
committed
Throw an IllegalArgumentException when a file name or comment in gzip
parameters encodes to a byte array with a 0 byte #618 - Sort members - Better local var names in tests - Use JUnit feature instead of custom code - Add missing assertions
1 parent 632434c commit 7143c55

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The <action> type attribute can be add,update,fix,remove.
5454
<action type="fix" dev="ggregory" due-to="Glavo">Remove unused local variable in ZipFile #615.</action>
5555
<action type="fix" dev="ggregory" due-to="Glavo, Gary Gregory">Optimize ZipEightByteInteger #614.</action>
5656
<action type="fix" dev="ggregory" due-to="Gary Gregory">ZipEightByteInteger.toString() now returns a number string without text prefix, like BigInteger.</action>
57+
<action type="fix" dev="ggregory" due-to="ddeschenes-1, Gary Gregory">Throw an IllegalArgumentException when a file name or comment in gzip parameters encodes to a byte array with a 0 byte #618.</action>
5758
<!-- ADD -->
5859
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.getModificationInstant().</action>
5960
<action type="add" dev="ggregory" due-to="Gary Gregory">Add GzipParameters.setModificationInstant(Instant).</action>

src/main/java/org/apache/commons/compress/compressors/gzip/GzipParameters.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,6 @@ public int type() {
299299
private int bufferSize = 512;
300300
private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
301301

302-
private String requireNonNulByte(final String text) {
303-
if (StringUtils.isNotEmpty(text) && ArrayUtils.contains(text.getBytes(fileNameCharset), (byte) 0)) {
304-
throw new IllegalArgumentException("String encoded in Charset '" + fileNameCharset + "' contains the nul byte 0 which is not supported in gzip.");
305-
}
306-
return text;
307-
}
308-
309302
/**
310303
* Gets size of the buffer used to retrieve compressed data.
311304
*
@@ -382,7 +375,6 @@ public String getFileName() {
382375
return fileName;
383376
}
384377

385-
386378
/**
387379
* Gets the Charset to use for writing file names and comments.
388380
* <p>
@@ -396,6 +388,7 @@ public Charset getFileNameCharset() {
396388
return fileNameCharset;
397389
}
398390

391+
399392
/**
400393
* Gets the most recent modification time (MTIME) of the original file being compressed.
401394
*
@@ -439,6 +432,13 @@ public OS getOS() {
439432
return operatingSystem;
440433
}
441434

435+
private String requireNonNulByte(final String text) {
436+
if (StringUtils.isNotEmpty(text) && ArrayUtils.contains(text.getBytes(fileNameCharset), (byte) 0)) {
437+
throw new IllegalArgumentException("String encoded in Charset '" + fileNameCharset + "' contains the nul byte 0 which is not supported in gzip.");
438+
}
439+
return text;
440+
}
441+
442442
/**
443443
* Sets size of the buffer used to retrieve compressed data from {@link Deflater} and write to underlying {@link OutputStream}.
444444
*

src/test/java/org/apache/commons/compress/compressors/gzip/GzipParametersTest.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.commons.compress.compressors.gzip;
2121

2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
2324
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
2526

@@ -43,12 +44,26 @@ public void testDeflaterStrategy() {
4344
assertEquals(Deflater.HUFFMAN_ONLY, gzipParameters.getDeflateStrategy());
4445
}
4546

46-
@Test
47-
public void testToString() {
47+
@ParameterizedTest
48+
//@formatter:off
49+
@CsvSource({
50+
" , hello\0world, false",
51+
"ISO-8859-1, hello\0world, false",
52+
"UTF-8 , hello\0world, false",
53+
"UTF-16BE , helloworld, false"
54+
})
55+
//@formatter:on
56+
public void testIllegalCommentOrFileName(final Charset charset, final String text) {
4857
final GzipParameters gzipParameters = new GzipParameters();
49-
assertTrue(gzipParameters.toString().contains("UNKNOWN"));
50-
gzipParameters.setOS(GzipParameters.OS.Z_SYSTEM);
51-
assertTrue(gzipParameters.toString().contains("Z_SYSTEM"));
58+
if (charset != null) {
59+
gzipParameters.setFileNameCharset(charset);
60+
}
61+
assertThrows(IllegalArgumentException.class, () -> gzipParameters.setComment(text));
62+
assertNull(gzipParameters.getComment());
63+
assertThrows(IllegalArgumentException.class, () -> gzipParameters.setFilename(text));
64+
assertNull(gzipParameters.getFileName());
65+
assertThrows(IllegalArgumentException.class, () -> gzipParameters.setFileName(text));
66+
assertNull(gzipParameters.getFileName());
5267
}
5368

5469
@ParameterizedTest
@@ -62,32 +77,24 @@ public void testToString() {
6277
"UTF-8 , helloéworld"
6378
})
6479
//@formatter:on
65-
public void testLegalCommentOrFileName(final String charset, final String text) {
66-
final GzipParameters p = new GzipParameters();
80+
public void testLegalCommentOrFileName(final Charset charset, final String text) {
81+
final GzipParameters gzipParameters = new GzipParameters();
6782
if (charset != null) {
68-
p.setFileNameCharset(Charset.forName(charset));
83+
gzipParameters.setFileNameCharset(charset);
6984
}
70-
p.setComment(text);
71-
p.setFilename(text);
72-
p.setFileName(text);
85+
gzipParameters.setComment(text);
86+
assertEquals(text, gzipParameters.getComment());
87+
gzipParameters.setFilename(text);
88+
assertEquals(text, gzipParameters.getFileName());
89+
gzipParameters.setFileName(text);
90+
assertEquals(text, gzipParameters.getFileName());
7391
}
7492

75-
@ParameterizedTest
76-
//@formatter:off
77-
@CsvSource({
78-
" , hello\0world, false",
79-
"ISO-8859-1, hello\0world, false",
80-
"UTF-8 , hello\0world, false",
81-
"UTF-16BE , helloworld, false"
82-
})
83-
//@formatter:on
84-
public void testIllegalCommentOrFileName(final String charset, final String text) {
85-
final GzipParameters p = new GzipParameters();
86-
if (charset != null) {
87-
p.setFileNameCharset(Charset.forName(charset));
88-
}
89-
assertThrows(IllegalArgumentException.class, () -> p.setComment(text));
90-
assertThrows(IllegalArgumentException.class, () -> p.setFilename(text));
91-
assertThrows(IllegalArgumentException.class, () -> p.setFileName(text));
93+
@Test
94+
public void testToString() {
95+
final GzipParameters gzipParameters = new GzipParameters();
96+
assertTrue(gzipParameters.toString().contains("UNKNOWN"));
97+
gzipParameters.setOS(GzipParameters.OS.Z_SYSTEM);
98+
assertTrue(gzipParameters.toString().contains("Z_SYSTEM"));
9299
}
93100
}

0 commit comments

Comments
 (0)