Fix Android OpenPGP message ID handling
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -404,7 +404,7 @@ class EpMailPlugin : Plugin() {
|
||||
val account = AndroidAccountVault.account()
|
||||
?: throw IllegalStateException("Add a mail account before opening messages.")
|
||||
val folder = call.getString("folder") ?: "Inbox"
|
||||
val uid = call.getLong("uid") ?: 0L
|
||||
val uid = PgpMessageUid.requireValid(call.data.opt("uid"))
|
||||
call.resolve(AndroidMailService.pgpMessageSource(account, folder, uid).toJSObject())
|
||||
} catch (error: Exception) {
|
||||
call.reject(error.message ?: "Could not load the encrypted message.")
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package studio.daybreak.mail
|
||||
|
||||
internal object PgpMessageUid {
|
||||
fun requireValid(value: Any?): Long {
|
||||
require(value is Number) { "That message could not be identified." }
|
||||
val number = value.toDouble()
|
||||
require(number.isFinite() && number >= 1 && number <= 4294967295.0 && number == kotlin.math.floor(number)) {
|
||||
"That message could not be identified."
|
||||
}
|
||||
return number.toLong()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package studio.daybreak.mail
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertThrows
|
||||
import org.junit.Test
|
||||
|
||||
class PgpMessageUidTest {
|
||||
@Test
|
||||
fun acceptsJsonIntegerAndLongUids() {
|
||||
assertEquals(42L, PgpMessageUid.requireValid(42))
|
||||
assertEquals(42L, PgpMessageUid.requireValid(42L))
|
||||
assertEquals(4294967295L, PgpMessageUid.requireValid(4294967295L))
|
||||
assertEquals(42L, PgpMessageUid.requireValid(42.0))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun rejectsMissingAndInvalidUids() {
|
||||
for (value in listOf(null, "42", 0, -1, 1.5, Double.NaN, Double.POSITIVE_INFINITY, 4294967296L)) {
|
||||
assertThrows(IllegalArgumentException::class.java) { PgpMessageUid.requireValid(value) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,6 +188,7 @@ test('Android OpenPGP uses the encrypted vault and queues only built MIME data',
|
||||
assert.match(androidPgp, /readSignature/);
|
||||
assert.match(pgpMailUi, /message\.signed/);
|
||||
assert.match(androidBridge, /buildOutgoing\(message\)/);
|
||||
assert.match(plugin, /PgpMessageUid\.requireValid\(call\.data\.opt\("uid"\)\)/);
|
||||
assert.match(androidBridge, /rawBase64, envelope: \{ from, to \}, text: '', attachments: \[\]/);
|
||||
assert.match(androidMailService, /validateOpenPgpMime\(message\)/);
|
||||
assert.match(androidMailService, /internal fun validateOpenPgpMime/);
|
||||
|
||||
Reference in New Issue
Block a user