Check SQLite3 magic bytes before import

This commit is contained in:
Joe Stein 2019-05-18 14:22:37 +02:00
parent b89271329b
commit 09e138b51f
2 changed files with 30 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.util.Arrays;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences;
@ -109,9 +110,14 @@ public class ImportExportActivity extends AppCompatActivity {
} }
private void restoreFrom(Uri inputUri) { private void restoreFrom(Uri inputUri) {
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
InputStream inputStream = null; InputStream inputStream = null;
try { try {
if (!validateDB(inputUri)) {
displayBadFileDialog();
return;
}
File currentDB = getDatabasePath(PodDBAdapter.DATABASE_NAME);
inputStream = getContentResolver().openInputStream(inputUri); inputStream = getContentResolver().openInputStream(inputUri);
FileUtils.copyInputStreamToFile(inputStream, currentDB); FileUtils.copyInputStreamToFile(inputStream, currentDB);
displayImportSuccessDialog(); displayImportSuccessDialog();
@ -123,6 +129,28 @@ public class ImportExportActivity extends AppCompatActivity {
} }
} }
private static final byte[] SQLITE3_MAGIC = "SQLite format 3\0".getBytes();
private boolean validateDB(Uri inputUri) throws IOException {
try (InputStream inputStream = getContentResolver().openInputStream(inputUri)) {
byte[] magicBuf = new byte[SQLITE3_MAGIC.length];
if (inputStream.read(magicBuf) == magicBuf.length) {
return Arrays.equals(SQLITE3_MAGIC, magicBuf);
}
}
return false;
}
private void displayBadFileDialog() {
AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this);
d.setMessage(R.string.import_bad_file)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, ((dialogInterface, i) -> {
// do nothing
}))
.show();
}
private void displayImportSuccessDialog() { private void displayImportSuccessDialog() {
AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this); AlertDialog.Builder d = new AlertDialog.Builder(ImportExportActivity.this);
d.setMessage(R.string.import_ok); d.setMessage(R.string.import_ok);

View File

@ -796,4 +796,5 @@
<string name="notification_channel_playing_description">Allows to control playback. This is the main notification you see while playing a podcast.</string> <string name="notification_channel_playing_description">Allows to control playback. This is the main notification you see while playing a podcast.</string>
<string name="notification_channel_error">Errors</string> <string name="notification_channel_error">Errors</string>
<string name="notification_channel_error_description">Shown if something went wrong, for example if download or gpodder sync fails.</string> <string name="notification_channel_error_description">Shown if something went wrong, for example if download or gpodder sync fails.</string>
<string name="import_bad_file">Invalid/corrupt file</string>
</resources> </resources>