Skip to content

Commit 087f89e

Browse files
committed
Prevent arbitrary file writes with malicious resource names. (#3484)
* refactor: rename sanitize function * fix: expose getDir * fix: safe handling of untrusted resource names - fixes: GHSA-2hqv-2xv4-5h5w * test: sample file for GHSA-2hqv-2xv4-5h5w * refactor: avoid detection of absolute files for resource check * chore: enable info mode on gradle * test: skip test on windows * chore: debug windows handling * fix: normalize entry with file separators * fix: normalize filepath after cleansing * chore: Android paths are not OS specific * refactor: use java.nio for path traversal checking * chore: align path separator on Windows for Zip files * chore: rework towards basic directory traversal * chore: remove '--info' on build.yml
1 parent fedae0b commit 087f89e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import brut.directory.DirUtil;
2626
import brut.directory.Directory;
2727
import brut.directory.DirectoryException;
28+
import brut.util.BrutIO;
2829

2930
import java.io.*;
3031
import java.util.Map;
@@ -47,6 +48,13 @@ public void decode(ResResource res, Directory inDir, Directory outDir, Map<Strin
4748
String outResName = res.getFilePath();
4849
String typeName = res.getResSpec().getType().getName();
4950

51+
if (BrutIO.detectPossibleDirectoryTraversal(outResName)) {
52+
outResName = inFileName;
53+
LOGGER.warning(String.format(
54+
"Potentially malicious file path: %s, using instead %s", res.getFilePath(), outResName
55+
));
56+
}
57+
5058
String ext = null;
5159
String outFileName;
5260
int extPos = inFileName.lastIndexOf(".");

brut.j.util/src/main/java/brut/util/BrutIO.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public static String sanitizeUnknownFile(final File directory, final String entr
9494
return canonicalEntryPath.substring(canonicalDirPath.length());
9595
}
9696

97+
public static boolean detectPossibleDirectoryTraversal(String entry) {
98+
if (OSDetection.isWindows()) {
99+
return entry.contains("..\\") || entry.contains("\\..");
100+
}
101+
return entry.contains("../") || entry.contains("/..");
102+
}
103+
97104
public static String normalizePath(String path) {
98105
char separator = File.separatorChar;
99106

0 commit comments

Comments
 (0)