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

Crash after launch fixed #3508

Merged
merged 6 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -55,7 +55,7 @@ class FhirEngineModule {
FhirEngineProvider.init(
FhirEngineConfiguration(
enableEncryptionIfSupported = !BuildConfig.DEBUG,
databaseErrorStrategy = DatabaseErrorStrategy.UNSPECIFIED,
databaseErrorStrategy = DatabaseErrorStrategy.RECREATE_AT_OPEN,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the implication for using this DatabaseErrorStrategy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the implication for using this DatabaseErrorStrategy?

The DatabaseErrorStrategy.RECREATE_AT_OPEN was implemented to solve a critical issue where the app crashes on startup after restoring a backup. The application is unable to read from the restored database due to this error:

FATAL EXCEPTION: main Process: org.smartregister.opensrp.eusm, PID: 18215 java.lang.RuntimeException: Unable to create application org.smartregister.fhircore.quest.QuestApplication: android.database.sqlite.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master; at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7003) at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2236) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294) at android.app.ActivityThread.main(ActivityThread.java:8177) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) Caused by: android.database.sqlite.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master; at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method) at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:89) at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62) at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:91) at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:50) at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60) at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:2019) at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1905) at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2672) at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2602) at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1250) at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1325) at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:167) at com.google.android.fhir.db.impl.SQLCipherSupportHelper.getWritableDatabase(SQLCipherSupportHelper.kt:91) at com.google.android.fhir.db.impl.SQLCipherSupportHelper.<init>(SQLCipherSupportHelper.kt:122) at com.google.android.fhir.db.impl.SQLCipherSupportHelper.<init>(SQLCipherSupportHelper.kt:36) at com.google.android.fhir.db.impl.DatabaseImpl.lambda$2$lambda$1(DatabaseImpl.kt:97) at com.google.android.fhir.db.impl.DatabaseImpl$$ExternalSyntheticLambda0.create(D8$$SyntheticClass:0) at com.google.android.fhir.db.impl.ResourceDatabase_Impl.createOpenHelper(ResourceDatabase_Impl.java:378) at androidx.room.RoomDatabase.init(RoomDatabase.kt:186) at androidx.room.RoomDatabase$Builder.build(RoomDatabase.kt:1358) at com.google.android.fhir.db.impl.DatabaseImpl.<init>(DatabaseImpl.kt:116) at com.google.android.fhir.FhirServices$Builder.build(FhirServices.kt:83) at com.google.android.fhir.FhirEngineProvider.getOrCreateFhirService(FhirEngineProvider.kt:99) at com.google.android.fhir.FhirEngineProvider.getInstance(FhirEngineProvider.kt:63) at org.smartregister.fhircore.engine.di.FhirEngineModule.provideFhirEngine(FhirEngineModule.kt:82) at org.smartregister.fhircore.engine.di.FhirEngineModule_ProvideFhirEngineFactory.provideFhirEngine(FhirEngineModule_ProvideFhirEngineFactory.java:60) at org.smartregister.fhircore.quest.DaggerQuestApplication_HiltComponents_SingletonC$SingletonCImpl$SwitchingProvider.get(DaggerQuestApplication_HiltComponents_SingletonC.java:1384) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at org.smartregister.fhircore.quest.DaggerQuestApplication_HiltComponents_SingletonC$SingletonCImpl$SwitchingProvider.get(DaggerQuestApplication_HiltComponents_SingletonC.java:1517) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) 2024-09-18 14:47:49.475 18215-18215 AndroidRuntime pid-18215 E at org.smartregister.fhircore.quest.DaggerQuestApplication_HiltComponents_SingletonC$SingletonCImpl.injectQuestApplication2(DaggerQuestApplication_HiltComponents_SingletonC.java:1347) at org.smartregister.fhircore.quest.DaggerQuestApplication_HiltComponents_SingletonC$SingletonCImpl.injectQuestApplication(DaggerQuestApplication_HiltComponents_SingletonC.java:1336) at org.smartregister.fhircore.quest.Hilt_QuestApplication.hiltInternalInject(Hilt_QuestApplication.java:52) at org.smartregister.fhircore.quest.Hilt_QuestApplication.onCreate(Hilt_QuestApplication.java:43) at org.smartregister.fhircore.quest.QuestApplication.onCreate(QuestApplication.kt:59) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6998) ... 9 more

This strategy ensures the app remains functional by resetting the database if it can't be read.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This strategy is NOT respected when opening a previously unencrypted database with an encrypted configuration or vice versa. An IllegalStateException is thrown instead.

Out of curiosity does the new version of the APK use the same encryption for the DB?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR fixes the problem at hanf. Good job on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity does the new version of the APK use the same encryption for the DB?

No changes were made to the encryption strategy in this PR.

ServerConfiguration(
baseUrl = configService.provideAuthConfiguration().fhirServerBaseUrl,
authenticator = tokenAuthenticator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.smartregister.fhircore.engine.util

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
Expand All @@ -34,8 +35,18 @@

@Singleton
class SecureSharedPreference @Inject constructor(@ApplicationContext val context: Context) {
private val secureSharedPreferences: SharedPreferences by lazy {
initEncryptedSharedPreferences()
}

private fun initEncryptedSharedPreferences() =
runCatching { createEncryptedSharedPreferences() }
.getOrElse {
resetSharedPrefs()
createEncryptedSharedPreferences()

Check warning on line 46 in android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt

View check run for this annotation

Codecov / codecov/patch

android/engine/src/main/java/org/smartregister/fhircore/engine/util/SecureSharedPreference.kt#L45-L46

Added lines #L45 - L46 were not covered by tests
}

private val secureSharedPreferences =
private fun createEncryptedSharedPreferences() =
EncryptedSharedPreferences.create(
context,
SECURE_STORAGE_FILE_NAME,
Expand Down
5 changes: 4 additions & 1 deletion android/quest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="false"
android:windowSoftInputMode="adjustResize"
android:allowBackup="false"
android:fullBackupContent="false"
android:dataExtractionRules="@xml/data_extraction_rules"
tools:ignore="UnusedAttribute,LockedOrientationActivity"
tools:replace="android:theme">
tools:replace="android:allowBackup,android:theme">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We disallowed backup @Aleem92 for privacy & security reasons. This backup is stored in the Android logged-in user drive, compromising the confidentiality of the data collected via the app.

Copy link
Contributor Author

@Aleem92 Aleem92 Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We disallowed backup @Aleem92 for privacy & security reasons. This backup is stored in the Android logged-in user drive, compromising the confidentiality of the data collected via the app.

1- allowBackup="false" and fullBackupContent="false" are disabling the backup system entirely.

2- dataExtractionRules="@xml/data_extraction_rules" is pointing to the XML file that would prevent any data from being backed up or transferred. This is now the recommended way to control backups for newer Android versions.

3- tools:replace="android:allowBackup" does the following:

  • Overrides the android:allowBackup value from any libraries.
  • Ensures the currently mentioned android:allowBackup setting in the manifest takes priority.

<profileable
android:shell="true"
tools:targetApi="29" />
Expand Down
17 changes: 17 additions & 0 deletions android/quest/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<data-extraction-rules>
<cloud-backup>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
</cloud-backup>
<device-transfer>
<exclude domain="root" path="." />
<exclude domain="file" path="." />
<exclude domain="database" path="." />
<exclude domain="sharedpref" path="." />
<exclude domain="external" path="." />
</device-transfer>
</data-extraction-rules>
Loading