Skip to content

Commit

Permalink
Block certain reports sent to ACRA
Browse files Browse the repository at this point in the history
  • Loading branch information
voczi committed Nov 10, 2024
1 parent 542649b commit a5e55a8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/usernames.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<w>quiro91</w>
<w>tarekkma</w>
<w>vianey</w>
<w>voczi</w>
<w>zanki</w>
</words>
</dictionary>
Expand Down
21 changes: 21 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/CrashReportService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.ichi2.anki.exception.UserSubmittedException
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.WebViewDebugging.setDataDirectorySuffix
import net.ankiweb.rsdroid.exceptions.BackendSyncException.BackendSyncServerMessageException
import org.acra.ACRA
import org.acra.ReportField
import org.acra.config.CoreConfigurationBuilder
Expand Down Expand Up @@ -281,12 +282,32 @@ object CrashReportService {
}
}
if (FEEDBACK_REPORT_NEVER != reportMode) {
if (!e.safeFromPII()) return

ACRA.errorReporter.putCustomData("origin", origin ?: "")
ACRA.errorReporter.putCustomData("additionalInfo", additionalInfo ?: "")
ACRA.errorReporter.handleException(e)
}
}

private fun Throwable.throwableRules() = listOf {
// BackendSyncServerMessage may contain PII and we do not want this leaked to ACRA.
// Related: https://github.com/ankidroid/Anki-Android/issues/17392
// and also https://github.com/ankitects/anki/commit/ba1f5f4
if (this is BackendSyncServerMessageException) false else true
}

// Check if the Throwable is safe from Personally Identifiable Information (PII)
// based on its class type. Returns false if it isn't, otherwise true.
fun Throwable.safeFromPII(): Boolean {
val current = throwableRules().all { rule -> rule.invoke() }
return if (current && this.cause != null && this.cause != this) {
this.cause!!.safeFromPII()
} else {
current
}
}

fun isProperServiceProcess(): Boolean {
return ACRA.isACRASenderServiceProcess()
}
Expand Down
46 changes: 46 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/CrashReportServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 voczi <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki

import androidx.test.ext.junit.runners.AndroidJUnit4
import anki.backend.BackendError
import com.ichi2.anki.CrashReportService.safeFromPII
import com.ichi2.testutils.JvmTest
import net.ankiweb.rsdroid.exceptions.BackendDeckIsFilteredException
import net.ankiweb.rsdroid.exceptions.BackendSyncException.BackendSyncServerMessageException
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue

@RunWith(AndroidJUnit4::class)
class CrashReportServiceTest : JvmTest() {
@Test
fun `Normal exceptions are flagged as PII-safe`() {
val exception = BackendDeckIsFilteredException(BackendError.newBuilder().build())
assertTrue(exception.safeFromPII(), "Exception reported as safe from PII")
}

@Test
fun `BackendSyncServerMessage exceptions are flagged as PII-unsafe`() {
val exception1 = BackendSyncServerMessageException(BackendError.newBuilder().build())
assertFalse(exception1.safeFromPII(), "Exception reported as not safe from PII")

val exception2 = Exception("", Exception("", exception1))
assertFalse(exception2.safeFromPII(), "Nested exception reported as not safe from PII")
}
}

0 comments on commit a5e55a8

Please sign in to comment.