Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): path traversal vulnerability #698

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# react-native-document-picker

📣📣 A full rewrite of the library is in progress. 📣📣

Please subscribe to [this issue](https://github.com/rnmods/react-native-document-picker/issues/603) to receive updates.

🚧🚧 GH discussions available 🚧🚧

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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private void copyFileToLocalStorage(Context context, WritableMap map, Uri uri) {
if (fileName == null) {
fileName = String.valueOf(System.currentTimeMillis());
}
File destFile = new File(dir, fileName);
File destFile = safeGetDestination(new File(dir, fileName), dir.getCanonicalPath());
Uri copyPath = copyFile(context, uri, destFile);
map.putString(FIELD_FILE_COPY_URI, copyPath.toString());
} catch (Exception e) {
Expand All @@ -326,6 +326,14 @@ private void copyFileToLocalStorage(Context context, WritableMap map, Uri uri) {
}
}

public File safeGetDestination(File destFile, String expectedDir) throws IllegalArgumentException, IOException {
String canonicalPath = destFile.getCanonicalPath();
if (!canonicalPath.startsWith(expectedDir)) {
throw new IllegalArgumentException("The copied file is attempting to write outside of the target directory.");
}
return destFile;
}

public static Uri copyFile(Context context, Uri uri, File destFile) throws IOException {
try(InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(destFile)) {
Expand Down
Loading