MM-33495 Sanitize filename in Android ShareExtension (#5210)

* MM-33495 Sanitize filename in Android ShareExtension

* Apply sanitization after getting uri last path segment

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Elias Nahum 2021-03-08 14:38:06 -03:00 committed by GitHub
parent 5f6552a649
commit 86a096d1ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -96,20 +96,25 @@ public class RealPathUtil {
File tmpFile;
String fileName = null;
if (uri == null || uri.isRelative()) {
return null;
}
// Try and get the filename from the Uri
try {
Cursor returnCursor =
context.getContentResolver().query(uri, null, null, null, null);
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
returnCursor.moveToFirst();
fileName = returnCursor.getString(nameIndex);
fileName = sanitizeFilename(returnCursor.getString(nameIndex));
} catch (Exception e) {
// just continue to get the filename with the last segment of the path
}
try {
if (fileName == null) {
fileName = uri.getLastPathSegment().toString().trim();
if (TextUtils.isEmpty(fileName)) {
fileName = sanitizeFilename(uri.getLastPathSegment().toString().trim());
}
@ -230,4 +235,13 @@ public class RealPathUtil {
fileOrDirectory.delete();
}
private static String sanitizeFilename(String filename) {
if (filename == null) {
return null;
}
File f = new File(filename);
return f.getName();
}
}