Skip to content

Commit b6a2218

Browse files
authored
(doc) [IO-484] Fix incorrect FilenameUtils documentation for null bytes (#310)
* (doc) [IO-484] Fix incorrect FilenameUtils documentation for null bytes * (doc) Consistently use term "null character" * (doc) Remove TODO comments for null character handling
1 parent 4810d9c commit b6a2218

2 files changed

Lines changed: 40 additions & 27 deletions

File tree

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ public class FilenameUtils {
194194
*
195195
* @param basePath the base path to attach to, always treated as a path
196196
* @param fullFileNameToAdd the fileName (or path) to attach to the base
197-
* @return the concatenated path, or null if invalid. Null bytes inside string will be removed
197+
* @return the concatenated path, or null if invalid
198+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
198199
*/
199200
public static String concat(final String basePath, final String fullFileNameToAdd) {
200201
final int prefix = getPrefixLength(fullFileNameToAdd);
@@ -260,6 +261,7 @@ public static boolean directoryContains(final String canonicalParent, final Stri
260261
* @param fileName the fileName
261262
* @param includeSeparator true to include the end separator
262263
* @return the path
264+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
263265
*/
264266
private static String doGetFullPath(final String fileName, final boolean includeSeparator) {
265267
if (fileName == null) {
@@ -291,7 +293,8 @@ private static String doGetFullPath(final String fileName, final boolean include
291293
*
292294
* @param fileName the fileName
293295
* @param separatorAdd 0 to omit the end separator, 1 to return it
294-
* @return the path. Null bytes inside string will be removed
296+
* @return the path
297+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
295298
*/
296299
private static String doGetPath(final String fileName, final int separatorAdd) {
297300
if (fileName == null) {
@@ -315,7 +318,8 @@ private static String doGetPath(final String fileName, final int separatorAdd) {
315318
* @param fileName the fileName
316319
* @param separator The separator character to use
317320
* @param keepSeparator true to keep the final separator
318-
* @return the normalized fileName. Null bytes inside string will be removed.
321+
* @return the normalized fileName
322+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
319323
*/
320324
private static String doNormalize(final String fileName, final char separator, final boolean keepSeparator) {
321325
if (fileName == null) {
@@ -565,8 +569,8 @@ private static int getAdsCriticalOffset(final String fileName) {
565569
* </p>
566570
*
567571
* @param fileName the fileName to query, null returns null
568-
* @return the name of the file without the path, or an empty string if none exists. Null bytes inside string
569-
* will be removed
572+
* @return the name of the file without the path, or an empty string if none exists
573+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
570574
*/
571575
public static String getBaseName(final String fileName) {
572576
return removeExtension(getName(fileName));
@@ -639,6 +643,7 @@ public static String getExtension(final String fileName) throws IllegalArgumentE
639643
*
640644
* @param fileName the fileName to query, null returns null
641645
* @return the path of the file, an empty string if none exists, null if invalid
646+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
642647
*/
643648
public static String getFullPath(final String fileName) {
644649
return doGetFullPath(fileName, true);
@@ -671,6 +676,7 @@ public static String getFullPath(final String fileName) {
671676
*
672677
* @param fileName the fileName to query, null returns null
673678
* @return the path of the file, an empty string if none exists, null if invalid
679+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
674680
*/
675681
public static String getFullPathNoEndSeparator(final String fileName) {
676682
return doGetFullPath(fileName, false);
@@ -693,8 +699,8 @@ public static String getFullPathNoEndSeparator(final String fileName) {
693699
* </p>
694700
*
695701
* @param fileName the fileName to query, null returns null
696-
* @return the name of the file without the path, or an empty string if none exists.
697-
* Null bytes inside string will be removed
702+
* @return the name of the file without the path, or an empty string if none exists
703+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
698704
*/
699705
public static String getName(final String fileName) {
700706
if (fileName == null) {
@@ -726,8 +732,8 @@ public static String getName(final String fileName) {
726732
* </p>
727733
*
728734
* @param fileName the fileName to query, null returns null
729-
* @return the path of the file, an empty string if none exists, null if invalid.
730-
* Null bytes inside string will be removed
735+
* @return the path of the file, an empty string if none exists, null if invalid
736+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
731737
*/
732738
public static String getPath(final String fileName) {
733739
return doGetPath(fileName, 1);
@@ -757,8 +763,8 @@ public static String getPath(final String fileName) {
757763
* </p>
758764
*
759765
* @param fileName the fileName to query, null returns null
760-
* @return the path of the file, an empty string if none exists, null if invalid.
761-
* Null bytes inside string will be removed
766+
* @return the path of the file, an empty string if none exists, null if invalid
767+
* @throws IllegalArgumentException if the result path contains the null character ({@code U+0000})
762768
*/
763769
public static String getPathNoEndSeparator(final String fileName) {
764770
return doGetPath(fileName, 0);
@@ -793,7 +799,8 @@ public static String getPathNoEndSeparator(final String fileName) {
793799
* </p>
794800
*
795801
* @param fileName the fileName to query, null returns null
796-
* @return the prefix of the file, null if invalid. Null bytes inside string will be removed
802+
* @return the prefix of the file, null if invalid
803+
* @throws IllegalArgumentException if the result contains the null character ({@code U+0000})
797804
*/
798805
public static String getPrefix(final String fileName) {
799806
if (fileName == null) {
@@ -838,7 +845,7 @@ public static String getPrefix(final String fileName) {
838845
* ~user --&gt; 6 --&gt; named user (slash added)
839846
* //server/a/b/c.txt --&gt; 9
840847
* ///a/b/c.txt --&gt; -1 --&gt; error
841-
* C: --&gt; 0 --&gt; valid filename as only null byte and / are reserved characters
848+
* C: --&gt; 0 --&gt; valid filename as only null character and / are reserved characters
842849
* </pre>
843850
* <p>
844851
* The output will be the same irrespective of the machine that the code is running on.
@@ -987,7 +994,7 @@ private static boolean isEmpty(final String string) {
987994
* @param fileName the fileName to query, null returns false
988995
* @param extensions the extensions to check for, null checks for no extension
989996
* @return true if the fileName is one of the extensions
990-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
997+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
991998
*/
992999
public static boolean isExtension(final String fileName, final Collection<String> extensions) {
9931000
if (fileName == null) {
@@ -1017,7 +1024,7 @@ public static boolean isExtension(final String fileName, final Collection<String
10171024
* @param fileName the fileName to query, null returns false
10181025
* @param extension the extension to check for, null or empty checks for no extension
10191026
* @return true if the fileName has the specified extension
1020-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
1027+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
10211028
*/
10221029
public static boolean isExtension(final String fileName, final String extension) {
10231030
if (fileName == null) {
@@ -1041,7 +1048,7 @@ public static boolean isExtension(final String fileName, final String extension)
10411048
* @param fileName the fileName to query, null returns false
10421049
* @param extensions the extensions to check for, null checks for no extension
10431050
* @return true if the fileName is one of the extensions
1044-
* @throws java.lang.IllegalArgumentException if the supplied fileName contains null bytes
1051+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
10451052
*/
10461053
public static boolean isExtension(final String fileName, final String... extensions) {
10471054
if (fileName == null) {
@@ -1253,7 +1260,8 @@ private static boolean isValidHostName(final String name) {
12531260
* (Note the file separator returned will be correct for Windows/Unix)
12541261
*
12551262
* @param fileName the fileName to normalize, null returns null
1256-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1263+
* @return the normalized fileName, or null if invalid
1264+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
12571265
*/
12581266
public static String normalize(final String fileName) {
12591267
return doNormalize(fileName, SYSTEM_NAME_SEPARATOR, true);
@@ -1300,7 +1308,8 @@ public static String normalize(final String fileName) {
13001308
* @param fileName the fileName to normalize, null returns null
13011309
* @param unixSeparator {@code true} if a unix separator should
13021310
* be used or {@code false} if a windows separator should be used.
1303-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1311+
* @return the normalized fileName, or null if invalid
1312+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
13041313
* @since 2.0
13051314
*/
13061315
public static String normalize(final String fileName, final boolean unixSeparator) {
@@ -1346,7 +1355,8 @@ public static String normalize(final String fileName, final boolean unixSeparato
13461355
* (Note the file separator returned will be correct for Windows/Unix)
13471356
*
13481357
* @param fileName the fileName to normalize, null returns null
1349-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1358+
* @return the normalized fileName, or null if invalid
1359+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
13501360
*/
13511361
public static String normalizeNoEndSeparator(final String fileName) {
13521362
return doNormalize(fileName, SYSTEM_NAME_SEPARATOR, false);
@@ -1392,7 +1402,8 @@ public static String normalizeNoEndSeparator(final String fileName) {
13921402
* @param fileName the fileName to normalize, null returns null
13931403
* @param unixSeparator {@code true} if a unix separator should
13941404
* be used or {@code false} if a windows separator should be used.
1395-
* @return the normalized fileName, or null if invalid. Null bytes inside string will be removed
1405+
* @return the normalized fileName, or null if invalid
1406+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
13961407
* @since 2.0
13971408
*/
13981409
public static String normalizeNoEndSeparator(final String fileName, final boolean unixSeparator) {
@@ -1415,6 +1426,7 @@ public static String normalizeNoEndSeparator(final String fileName, final boolea
14151426
*
14161427
* @param fileName the fileName to query, null returns null
14171428
* @return the fileName minus the extension
1429+
* @throws IllegalArgumentException if the fileName contains the null character ({@code U+0000})
14181430
*/
14191431
public static String removeExtension(final String fileName) {
14201432
if (fileName == null) {
@@ -1430,17 +1442,18 @@ public static String removeExtension(final String fileName) {
14301442
}
14311443

14321444
/**
1433-
* Checks the input for null bytes, a sign of unsanitized data being passed to to file level functions.
1445+
* Checks the input for null characters ({@code U+0000}), a sign of unsanitized data being passed to to file level functions.
14341446
*
14351447
* This may be used for poison byte attacks.
14361448
*
14371449
* @param path the path to check
14381450
* @return The input
1451+
* @throws IllegalArgumentException if path contains the null character ({@code U+0000})
14391452
*/
14401453
private static String requireNonNullChars(final String path) {
14411454
if (path.indexOf(0) >= 0) {
14421455
throw new IllegalArgumentException(
1443-
"Null byte present in file/path name. There are no known legitimate use cases for such data, but several injection attacks may use it");
1456+
"Null character present in file/path name. There are no known legitimate use cases for such data, but several injection attacks may use it");
14441457
}
14451458
return path;
14461459
}

src/test/java/org/apache/commons/io/FilenameUtilsTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public void testGetBaseName() {
229229
}
230230

231231
@Test
232-
public void testGetBaseName_with_nullByte() {
232+
public void testGetBaseName_with_null_character() {
233233
try {
234234
assertEquals("file.txt", FilenameUtils.getBaseName("fil\u0000e.txt.bak"));
235235
} catch (final IllegalArgumentException ignore) {
@@ -419,7 +419,7 @@ public void testGetPath() {
419419

420420

421421
@Test
422-
public void testGetPath_with_nullbyte() {
422+
public void testGetPath_with_null_character() {
423423
assertThrows(IllegalArgumentException.class, () -> FilenameUtils.getPath("~user/a/\u0000b/c.txt"));
424424
}
425425

@@ -461,7 +461,7 @@ public void testGetPathNoEndSeparator() {
461461
}
462462

463463
@Test
464-
public void testGetPathNoEndSeparator_with_null_byte() {
464+
public void testGetPathNoEndSeparator_with_null_character() {
465465
try {
466466
assertEquals("a/b", FilenameUtils.getPathNoEndSeparator("~user/a\u0000/b/c.txt"));
467467
} catch (final IllegalArgumentException ignore) {
@@ -515,7 +515,7 @@ public void testGetPrefix() {
515515
}
516516

517517
@Test
518-
public void testGetPrefix_with_nullbyte() {
518+
public void testGetPrefix_with_null_character() {
519519
try {
520520
assertEquals("~user\\", FilenameUtils.getPrefix("~u\u0000ser\\a\\b\\c.txt"));
521521
} catch (final IllegalArgumentException ignore) {
@@ -957,7 +957,7 @@ public void testNormalize() {
957957
/**
958958
*/
959959
@Test
960-
public void testNormalize_with_nullbytes() {
960+
public void testNormalize_with_null_character() {
961961
try {
962962
assertEquals("a" + SEP + "b" + SEP + "c.txt", FilenameUtils.normalize("a\\b/c\u0000.txt"));
963963
} catch (final IllegalArgumentException ignore) {

0 commit comments

Comments
 (0)