Skip to content

Commit ad0b5e5

Browse files
committed
fix(android): path traversal vulnerability (#698)
* fix: path traversal vulnerability android * docs: library update
1 parent 5dd2e40 commit ad0b5e5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# react-native-document-picker
22

3+
📣📣 A full rewrite of the library is in progress. 📣📣
4+
5+
Please subscribe to [this issue](https://github.com/rnmods/react-native-document-picker/issues/603) to receive updates.
6+
37
🚧🚧 GH discussions available 🚧🚧
48

59
If you want to ask questions, we opened [GH discussions](https://github.com/rnmods/react-native-document-picker/discussions) for that purpose! 🤗 Issue tracker is now reserved for bugs and feature requests only and issues not following the issue template can be closed. Thank you!

android/src/main/java/com/reactnativedocumentpicker/DocumentPickerModule.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private void copyFileToLocalStorage(Context context, WritableMap map, Uri uri) {
311311
if (fileName == null) {
312312
fileName = String.valueOf(System.currentTimeMillis());
313313
}
314-
File destFile = new File(dir, fileName);
314+
File destFile = safeGetDestination(new File(dir, fileName), dir.getCanonicalPath());
315315
Uri copyPath = copyFile(context, uri, destFile);
316316
map.putString(FIELD_FILE_COPY_URI, copyPath.toString());
317317
} catch (Exception e) {
@@ -321,6 +321,14 @@ private void copyFileToLocalStorage(Context context, WritableMap map, Uri uri) {
321321
}
322322
}
323323

324+
public File safeGetDestination(File destFile, String expectedDir) throws IllegalArgumentException, IOException {
325+
String canonicalPath = destFile.getCanonicalPath();
326+
if (!canonicalPath.startsWith(expectedDir)) {
327+
throw new IllegalArgumentException("The copied file is attempting to write outside of the target directory.");
328+
}
329+
return destFile;
330+
}
331+
324332
public static Uri copyFile(Context context, Uri uri, File destFile) throws IOException {
325333
try(InputStream inputStream = context.getContentResolver().openInputStream(uri);
326334
FileOutputStream outputStream = new FileOutputStream(destFile)) {

0 commit comments

Comments
 (0)