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: sdk 400 ensure empty key is not accepted as encryption key for #293

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
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static RudderClient getInstance(@NonNull Context context, @Nullable Strin

private static void initiateRudderReporter(Context context, @Nullable String writeKey) {
String writeKeyOrBlank = writeKey == null ? "" : writeKey;
if (rudderReporter == null) {
if (rudderReporter == null && context.getResources() != null) {
rudderReporter = new DefaultRudderReporter(context, METRICS_URL_PROD,
new Configuration(new LibraryMetadata(
BuildConfig.LIBRARY_PACKAGE_NAME, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE, writeKeyOrBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ static class ProviderParams {

@Override
public Persistence get(Persistence.DbCreateListener dbCreateListener) {
if (!params.isEncrypted) {
if (!params.isEncrypted
|| params.encryptionKey == null || params.encryptedDbName == null) {
return getDefaultPersistence(dbCreateListener);
} else {
return getEncryptedPersistence(dbCreateListener);
Expand All @@ -61,7 +62,7 @@ private EncryptedPersistence getEncryptedPersistence(@Nullable Persistence.DbCre
if (!checkDatabaseExists(params.encryptedDbName)
&& checkDatabaseExists(params.dbName)) {
migrateToEncryptedDatabase(encryptedDbPath);
}else {
} else {
if (!checkIfEncryptionIsValid(encryptedDbPath))
deleteEncryptedDb(); //drop database
}
Expand All @@ -72,7 +73,7 @@ && checkDatabaseExists(params.dbName)) {
private EncryptedPersistence createEncryptedObject(@Nullable Persistence.DbCreateListener dbCreateListener) {
return new EncryptedPersistence(application,
new EncryptedPersistence.DbParams(params.encryptedDbName,
params.dbVersion, params.encryptionKey), dbCreateListener);
params.dbVersion, params.encryptionKey), dbCreateListener);
}


Expand All @@ -94,7 +95,7 @@ private DefaultPersistence getDefaultPersistence(@Nullable Persistence.DbCreateL
checkDatabaseExists(params.encryptedDbName)) {
initCipheredDatabase();
createDefaultDatabase();
try{
try {
migrateToDefaultDatabase(application.getDatabasePath(params.dbName));
} catch (Exception e) {
RudderLogger.logError("Encryption key is invalid: Dumping the database and constructing a new unencrypted one");
Expand All @@ -114,25 +115,32 @@ private void initCipheredDatabase() {
SQLiteDatabase.loadLibs(application);
}

private boolean deleteEncryptedDb() {
private void deleteEncryptedDb() {
File encryptedDb = application.getDatabasePath(params.encryptedDbName);
if (encryptedDb.exists())
return encryptedDb.delete();
return false;
if (encryptedDb.exists()) {
deleteFile(encryptedDb);
}
}

private void migrateToDefaultDatabase(File databasePath) {

File encryptedDb = application.getDatabasePath(params.encryptedDbName);
String encryptedPath = encryptedDb.getAbsolutePath();
SQLiteDatabase database = SQLiteDatabase.openDatabase(encryptedPath, params.encryptionKey, null, SQLiteDatabase.OPEN_READWRITE);
//will throw exception if encryption key is invalid
database.isDatabaseIntegrityOk();
database.rawExecSQL(String.format("ATTACH DATABASE '%s' AS rl_persistence KEY ''",
databasePath.getAbsolutePath()));
database.rawExecSQL("select sqlcipher_export('rl_persistence')");
database.rawExecSQL("DETACH DATABASE rl_persistence");
database.close();
encryptedDb.delete();
deleteFile(encryptedDb);
}

private void deleteFile(File encryptedDb) {
if (!encryptedDb.delete()) {
RudderLogger.logError("Unable to delete database " + encryptedDb.getAbsolutePath());
}
}


Expand All @@ -147,13 +155,13 @@ private void migrateToEncryptedDatabase(File encryptedDbPath) {
database.rawExecSQL("select sqlcipher_export('rl_persistence_encrypted')");
database.rawExecSQL("DETACH DATABASE rl_persistence_encrypted");
database.close();
decryptedDb.delete();
deleteFile(decryptedDb);

}


private boolean checkDatabaseExists(String dbName) {
return application.getDatabasePath(dbName).exists();
private boolean checkDatabaseExists(@Nullable String dbName) {
return dbName != null && application.getDatabasePath(dbName).exists();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rudderstack.android.sdk.core.persistence;

import android.app.Application;
import android.text.TextUtils;

import com.rudderstack.android.sdk.core.RudderLogger;

Expand Down Expand Up @@ -47,10 +48,11 @@ public PersistenceProvider create(Application application) {
dbVersion = 1;
}
if (isEncrypted) {
if (encryptionKey == null) {
RudderLogger.logWarn("DBPersistentManager: isEncrypted is true but encryptionKey is null. Proceeding with null key");
if (TextUtils.isEmpty(encryptionKey)) {
RudderLogger.logWarn("DBPersistentManager: isEncrypted is true but encryptionKey is null or empty. Proceeding with unencrypted database");
isEncrypted = false;
}
if (encryptedDbName == null) {
else if (encryptedDbName == null) {
RudderLogger.logError("DBPersistentManager: isEncrypted is true but encryptedDbName is null. Aborting Db creation");
return null;
}
Expand Down