Skip to content

Commit 3b41304

Browse files
author
akrcc
committed
Fix zip path traversal vulnerability
inspired by: * MobileChromeApps#92
1 parent 977b57d commit 3b41304

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/android/Zip.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,20 @@ private void unzipSync(CordovaArgs args, CallbackContext callbackContext) {
121121
anyEntries = true;
122122
String compressedName = ze.getName();
123123

124+
File file = new File(outputDirectory + compressedName);
125+
126+
String canonicalPath = file.getCanonicalPath();
127+
String canonicalOutputPath = (new File(outputDirectory)).getCanonicalPath();
128+
if (!canonicalPath.startsWith(canonicalOutputPath)) {
129+
String errorMessage = "Zip traversal security error";
130+
callbackContext.error(errorMessage);
131+
Log.e(LOG_TAG, errorMessage);
132+
return;
133+
}
134+
124135
if (ze.isDirectory()) {
125-
File dir = new File(outputDirectory + compressedName);
126-
dir.mkdirs();
136+
file.mkdirs();
127137
} else {
128-
File file = new File(outputDirectory + compressedName);
129138
file.getParentFile().mkdirs();
130139
if(file.exists() || file.createNewFile()){
131140
Log.w("Zip", "extracting: " + file.getPath());

0 commit comments

Comments
 (0)