mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
sanitize filename before creating it in the blob dir
This commit is contained in:
@@ -31,6 +31,7 @@ import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationCenter;
|
||||
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.FileUtils;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
import java.io.File;
|
||||
@@ -356,6 +357,8 @@ public class DcHelper {
|
||||
}
|
||||
|
||||
public static String getBlobdirFile(DcContext dcContext, String filename, String ext) {
|
||||
filename = FileUtils.sanitizeFilename(filename);
|
||||
ext = FileUtils.sanitizeFilename(ext);
|
||||
String outPath = null;
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
String test = dcContext.getBlobdir() + "/" + filename + (i == 0 ? "" : i < 100 ? "-" + i : "-" + (new Date().getTime() + i)) + ext;
|
||||
|
||||
@@ -1,9 +1,49 @@
|
||||
package org.thoughtcrime.securesms.util;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class FileUtils {
|
||||
|
||||
public static native int getFileDescriptorOwner(FileDescriptor fileDescriptor);
|
||||
|
||||
public static String sanitizeFilename(String name) {
|
||||
if (TextUtils.isEmpty(name) || ".".equals(name) || "..".equals(name)) {
|
||||
return "(invalid)";
|
||||
}
|
||||
final StringBuilder res = new StringBuilder(name.length());
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
final char c = name.charAt(i);
|
||||
if (isValidFilenameChar(c)) {
|
||||
res.append(c);
|
||||
} else {
|
||||
res.append('_');
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
private static boolean isValidFilenameChar(char c) {
|
||||
if ((0x00 <= c && c <= 0x1f)) {
|
||||
return false;
|
||||
}
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '*':
|
||||
case '/':
|
||||
case ':':
|
||||
case '<':
|
||||
case '>':
|
||||
case '?':
|
||||
case '\\':
|
||||
case '\0':
|
||||
case '|':
|
||||
case 0x7F:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user