Prevent restoring newer backups into older versions of Signal.

Relates to #8184
This commit is contained in:
Greyson Parrelli 2018-09-07 15:54:38 -07:00
parent d2a8abe769
commit 15b4517e35
3 changed files with 33 additions and 10 deletions

View File

@ -1335,6 +1335,7 @@
<string name="preferences_chats__create_backup">Create backup</string>
<string name="RegistrationActivity_enter_backup_passphrase">Enter backup passphrase</string>
<string name="RegistrationActivity_restore">Restore</string>
<string name="RegistrationActivity_backup_failure_downgrade">Cannot import backups from newer versions of Signal</string>
<string name="RegistrationActivity_incorrect_backup_passphrase">Incorrect backup passphrase</string>
<string name="RegistrationActivity_checking">Checking...</string>
<string name="RegistrationActivity_d_messages_so_far">%d messages so far...</string>

View File

@ -366,9 +366,9 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif
restoreButton.setIndeterminateProgressMode(true);
restoreButton.setProgress(50);
new AsyncTask<Void, Void, Boolean>() {
new AsyncTask<Void, Void, BackupImportResult>() {
@Override
protected Boolean doInBackground(Void... voids) {
protected BackupImportResult doInBackground(Void... voids) {
try {
Context context = RegistrationActivity.this;
String passphrase = prompt.getText().toString();
@ -383,23 +383,32 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif
TextSecurePreferences.setBackupEnabled(context, true);
TextSecurePreferences.setBackupPassphrase(context, passphrase);
return true;
return BackupImportResult.SUCCESS;
} catch (FullBackupImporter.DatabaseDowngradeException e) {
Log.w(TAG, "Failed due to the backup being from a newer version of Signal.", e);
return BackupImportResult.FAILURE_VERSION_DOWNGRADE;
} catch (IOException e) {
Log.w(TAG, e);
return false;
return BackupImportResult.FAILURE_UNKNOWN;
}
}
@Override
protected void onPostExecute(@NonNull Boolean result) {
protected void onPostExecute(@NonNull BackupImportResult result) {
restoreButton.setIndeterminateProgressMode(false);
restoreButton.setProgress(0);
restoreBackupProgress.setText("");
if (result) {
displayInitialView(true);
} else {
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show();
switch (result) {
case SUCCESS:
displayInitialView(true);
break;
case FAILURE_VERSION_DOWNGRADE:
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_backup_failure_downgrade, Toast.LENGTH_LONG).show();
break;
case FAILURE_UNKNOWN:
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show();
break;
}
}
}.execute();
@ -1115,4 +1124,8 @@ public class RegistrationActivity extends BaseActionBarActivity implements Verif
this.gcmToken = previous.gcmToken;
}
}
private enum BackupImportResult {
SUCCESS, FAILURE_VERSION_DOWNGRADE, FAILURE_UNKNOWN
}
}

View File

@ -98,7 +98,11 @@ public class FullBackupImporter extends FullBackupBase {
EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.FINISHED, count));
}
private static void processVersion(@NonNull SQLiteDatabase db, DatabaseVersion version) {
private static void processVersion(@NonNull SQLiteDatabase db, DatabaseVersion version) throws IOException {
if (version.getVersion() > db.getVersion()) {
throw new DatabaseDowngradeException(db.getVersion(), version.getVersion());
}
db.setVersion(version.getVersion());
}
@ -328,4 +332,9 @@ public class FullBackupImporter extends FullBackupBase {
}
}
public static class DatabaseDowngradeException extends IOException {
DatabaseDowngradeException(int currentVersion, int backupVersion) {
super("Tried to import a backup with version " + backupVersion + " into a database with version " + currentVersion);
}
}
}