Skip to content

Commit 312437b

Browse files
mahibibackportbot[bot]
authored andcommitted
fix(notifications): abort NotificationWorker when user init fails
initDecryptedData() could silently leave `user` uninitialized (invalid signature, or an exception swallowed by the catch block), after which doWork() unconditionally called initNcApiAndCredentials(), crashing with UninitializedPropertyAccessException and dropping the notification. Now initDecryptedData() reports success/failure and doWork() bails out early with a log when it fails. Assisted-by: Claude Code:claude-sonnet-5 Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent d5ab95b commit 312437b

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
158158

159159
logger.d(TAG, "NotificationWorker::doWork")
160160

161-
initDecryptedData(inputData)
161+
if (!initDecryptedData(inputData)) {
162+
logger.e(TAG, "Aborting NotificationWorker::doWork because user/pushMessage could not be initialized")
163+
return Result.failure()
164+
}
162165
initNcApiAndCredentials()
163166

164167
notificationManager = NotificationManagerCompat.from(context!!)
@@ -423,14 +426,14 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
423426
}
424427

425428
@Suppress("TooGenericExceptionCaught", "NestedBlockDepth", "ComplexMethod", "LongMethod")
426-
private fun initDecryptedData(inputData: Data) {
429+
private fun initDecryptedData(inputData: Data): Boolean {
427430
try {
428431
if (inputData.hasKeyWithValueOfType(BundleKeys.KEY_NOTIFICATION_CLEARTEXT_SUBJECT, String::class.java)) {
429432
val subject = inputData.getString(BundleKeys.KEY_NOTIFICATION_CLEARTEXT_SUBJECT)
430433
val id = inputData.getLong(BundleKeys.KEY_NOTIFICATION_USER_ID, -1)
431434
user = userManager.getUserWithId(id).blockingGet()
432435
pushMessage = LoganSquare.parse(subject, DecryptedPushMessage::class.java)
433-
return
436+
return true
434437
}
435438

436439
val subject = inputData.getString(BundleKeys.KEY_NOTIFICATION_SUBJECT)
@@ -453,17 +456,21 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
453456
DecryptedPushMessage::class.java
454457
)
455458
user = signatureVerification.user!!
459+
return true
460+
} else {
461+
logger.e(TAG, "Signature verification failed, discarding push message")
456462
}
457463
} catch (e: NoSuchAlgorithmException) {
458-
Log.e(TAG, "No proper algorithm to decrypt the message ", e)
464+
logger.e(TAG, "No proper algorithm to decrypt the message ", e)
459465
} catch (e: NoSuchPaddingException) {
460-
Log.e(TAG, "No proper padding to decrypt the message ", e)
466+
logger.e(TAG, "No proper padding to decrypt the message ", e)
461467
} catch (e: InvalidKeyException) {
462-
Log.e(TAG, "Invalid private key ", e)
468+
logger.e(TAG, "Invalid private key ", e)
463469
}
464470
} catch (e: Exception) {
465-
Log.e(TAG, "Error occurred while initializing decoded data ", e)
471+
logger.e(TAG, "Error occurred while initializing decoded data ", e)
466472
}
473+
return false
467474
}
468475

469476
private fun decryptSubject(privateKey: PrivateKey, base64DecodedSubject: ByteArray): ByteArray =

0 commit comments

Comments
 (0)