email: never run JavaScript in received mail (#900)
- Commit
f32d62b2f6fffee534fb2fe73a0fbc639d2b3b79- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-12 05:01 UTC (24 days ago)
- Parents
41bc5dc
Found during #900 triage. The email app rendered message bodies through HtmlText without passing `javaScriptEnabled = false`, so HtmlText's default (scripts on) applied, even though HtmlText's own docs say untrusted mail should pass false and SecureMail already does. Any <script> or inline event handler a sender put in an HTML email therefore ran as soon as the message was opened. No JS bridge is exposed and links open externally, but a script could still call the sender's server on open with fetch/XHR. The "remote images blocked" setting does not cover those requests, so a script defeats that privacy setting. It could also fingerprint the device, rewrite what the message shows (inject phishing content after the fact, hide the real link targets) and reach any WebView exploit that needs JS. None of the rendering needs scripts: quote hiding, dark mode, cid: images and the #900 fit-to-width layout are all CSS or done in Kotlin. Changes - Thread screen (MessageItem): HtmlText(javaScriptEnabled = false). - .eml viewer: the same. An .eml file is received mail too, and opening one from Files is an easy way to deliver hostile HTML. - HtmlText's default stays true for its other callers. securemail already passes false. The change is deliberately limited to the email app's call sites. User-visible effect - Nothing for ordinary mail. Mail that relied on scripts (interactive widgets, script-driven layout) now shows its static HTML, the same as in mainstream mail clients, which also do not run mail scripts. Verification (toolchain container, gbuild.sh, on this commit) - New ReceivedMailJavaScriptTest renders an HTML message containing a <script> on the thread screen and asserts every WebView in the hierarchy has javaScriptEnabled == false. A new EmlViewerSuccessTest case does the same for an .eml file with a <script>. Both would fail without this change, because HtmlText defaults to scripts on. - :email:compileDevKotlin, :library:ui:compileDebugKotlin: pass. - :email:testDevUnitTest: all 523 tests pass. :library:ui:testDebugUnitTest (HtmlText*): pass. - :email:lint: pass. :email:detekt: the same 11 findings as on main, none in touched files. :library:ui:detekt: the same single finding as on main (LargeClass in IconsSliceTest.kt). Needs on-device confirmation - Open an HTML newsletter and a mail with links, quotes and inline (cid:) images. It should render as before: links open in the browser, quotes toggle, images show.
Diff
email/src/main/java/com/vayunmathur/email/ui/EmlViewerScreen.kt
diff --git a/email/src/main/java/com/vayunmathur/email/ui/EmlViewerScreen.kt b/email/src/main/java/com/vayunmathur/email/ui/EmlViewerScreen.ktindex f2fede1522..9466e36de0 100644--- a/email/src/main/java/com/vayunmathur/email/ui/EmlViewerScreen.kt+++ b/email/src/main/java/com/vayunmathur/email/ui/EmlViewerScreen.kt@@ -159,6 +159,8 @@ fun EmlViewerScreen( html = msg.body, blockRemoteImages = !loadImages && cidMap.isEmpty(), cidMap = cidMap,+ // An .eml file is received mail too: never run its scripts.+ javaScriptEnabled = false, scrollInternally = true, fitContentToWidth = true, modifier = Modifier.weight(1f).fillMaxWidth().padding(horizontal = 8.dp),
email/src/main/java/com/vayunmathur/email/ui/MessageItem.kt
diff --git a/email/src/main/java/com/vayunmathur/email/ui/MessageItem.kt b/email/src/main/java/com/vayunmathur/email/ui/MessageItem.ktindex 3d12def5a9..3223819c01 100644--- a/email/src/main/java/com/vayunmathur/email/ui/MessageItem.kt+++ b/email/src/main/java/com/vayunmathur/email/ui/MessageItem.kt@@ -222,6 +222,8 @@ private fun ColumnScope.HtmlMessageBody( blockRemoteImages = remoteBlocked, hideQuotes = hasQuotes && !showQuotes, cidMap = cidMap,+ // Received mail is untrusted: a sender's script must never run on open.+ javaScriptEnabled = false, scrollInternally = true, fitContentToWidth = true, // 8dp here plus the document's 8px body padding lines text up with the
email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt b/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.ktindex 8f880611dc..06c0ae9fa4 100644--- a/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt+++ b/email/src/test/java/com/vayunmathur/email/ui/EmlViewerSuccessTest.kt@@ -2,6 +2,10 @@ package com.vayunmathur.email.ui import android.app.Application import android.net.Uri+import android.view.View+import android.view.ViewGroup+import android.webkit.WebView+import androidx.compose.ui.platform.LocalView import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.onNodeWithText@@ -10,6 +14,8 @@ import androidx.test.core.app.ApplicationProvider import java.io.File import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull import kotlin.test.assertTrue import org.junit.Rule import org.junit.runner.RunWith@@ -153,4 +159,41 @@ class EmlViewerSuccessTest { composeRule.waitForIdle() composeRule.onNodeWithText("Remote images blocked", substring = true).assertDoesNotExist() }++ @Test+ fun `html eml renders with javascript off`() {+ val file = writeEml(+ "script.eml",+ """+ From: [email protected]+ To: [email protected]+ Subject: Script mail+ Date: Wed, 17 Jul 2024 12:00:00 +0000+ MIME-Version: 1.0+ Content-Type: text/html; charset="UTF-8"++ <p>Hello</p><script>document.title='pwned'</script>+ """.trimIndent(),+ )+ var root: View? = null+ composeRule.setContent {+ root = LocalView.current+ EmlViewerScreen(uriString = Uri.fromFile(file).toString(), onBack = {})+ }+ composeRule.waitUntil(15_000) {+ composeRule.onAllNodesWithText("Remote images blocked", substring = true)+ .fetchSemanticsNodes().isNotEmpty()+ }+ composeRule.waitForIdle()+ val webViews = mutableListOf<WebView>()+ val q = ArrayDeque<View>().apply { add(assertNotNull(root).rootView) }+ while (q.isNotEmpty()) {+ val v = q.removeFirst()+ if (v is WebView) webViews.add(v)+ if (v is ViewGroup) for (i in 0 until v.childCount) q.add(v.getChildAt(i))+ }+ assertTrue(webViews.isNotEmpty(), "no WebView rendered")+ // An .eml file is received mail: a sender's script must not run on open.+ webViews.forEach { assertFalse(it.settings.javaScriptEnabled, "sender script would run on open") }+ } }
email/src/test/java/com/vayunmathur/email/ui/ReceivedMailJavaScriptTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/ui/ReceivedMailJavaScriptTest.kt b/email/src/test/java/com/vayunmathur/email/ui/ReceivedMailJavaScriptTest.ktnew file mode 100644index 0000000000..c35e112897--- /dev/null+++ b/email/src/test/java/com/vayunmathur/email/ui/ReceivedMailJavaScriptTest.kt@@ -0,0 +1,73 @@+package com.vayunmathur.email.ui++import android.content.Context+import android.net.Uri+import android.view.View+import android.view.ViewGroup+import android.webkit.WebView+import androidx.activity.ComponentActivity+import androidx.compose.ui.test.junit4.createAndroidComposeRule+import com.vayunmathur.email.data.Attachment+import com.vayunmathur.email.data.EmailMessage+import com.vayunmathur.email.platform.MessageThreadActions+import java.io.File+import kotlin.test.Test+import kotlin.test.assertFalse+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Received mail is untrusted, so the thread screen must render its HTML with+ * JavaScript off (HtmlText defaults to scripts on for its other callers). The+ * .eml viewer's counterpart lives in [EmlViewerSuccessTest], whose sandbox the+ * file-backed parse is reliable in.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ReceivedMailJavaScriptTest {++ @get:Rule+ val rule = createAndroidComposeRule<ComponentActivity>()++ private object NoopActions : MessageThreadActions {+ override fun markAsRead(accountEmail: String, folderName: String, uid: Long, isRead: Boolean) = Unit+ override fun snoozeMessage(accountEmail: String, folderName: String, uid: Long, until: Long) = Unit+ override fun blockSender(from: String) = Unit+ override fun fetchBodyIfNeeded(message: EmailMessage) = Unit+ override suspend fun getAttachments(accountEmail: String, messageId: Long): List<Attachment> = emptyList()+ override suspend fun loadCidMap(context: Context, message: EmailMessage): Map<String, File> = emptyMap()+ override fun downloadAttachment(attachment: Attachment, onSuccess: (String) -> Unit, onError: (String) -> Unit) = Unit+ override fun oneClickUnsubscribe(url: String, onResult: (Boolean) -> Unit) = Unit+ override fun exportEml(accountEmail: String, folderName: String, uid: Long, targetUri: Uri, onResult: (Boolean, String?) -> Unit) = Unit+ }++ private fun webViews(): List<WebView> {+ val found = mutableListOf<WebView>()+ rule.runOnUiThread {+ val q = ArrayDeque<View>()+ q.add(rule.activity.findViewById(android.R.id.content))+ while (q.isNotEmpty()) {+ val v = q.removeFirst()+ if (v is WebView) found.add(v)+ if (v is ViewGroup) for (i in 0 until v.childCount) q.add(v.getChildAt(i))+ }+ }+ return found+ }++ @Test+ fun `thread screen renders html mail with javascript off`() {+ val msg = EmailMessage(+ accountEmail = "[email protected]", folderName = "INBOX", id = 1L,+ subject = "Hi", from = "Eve <[email protected]>", date = "Wed Nov 27 10:00",+ body = "<p>Hello</p><script>document.title='pwned'</script>", isHtml = true, isRead = true,+ )+ rule.setContent { MessageThreadScreen(messages = listOf(msg), actions = NoopActions) }+ rule.waitForIdle()+ val views = webViews()+ assertFalse(views.isEmpty(), "no WebView rendered")+ views.forEach { assertFalse(it.settings.javaScriptEnabled, "sender script would run on open") }+ }+}