From f73cb51c23a7c419ade463e8da9cfdc56c9fcb77 Mon Sep 17 00:00:00 2001 From: martgil <46025304+martgil@users.noreply.github.com> Date: Fri, 27 Nov 2026 17:56:50 +0800 Subject: [PATCH] fix: disallow inline styles on em in email sanitizer --- .../flowcrypt/email/security/pgp/PgpMsg.kt | 1 - .../security/pgp/PgpMsgHtmlSanitizerTest.kt | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 FlowCrypt/src/test/java/com/flowcrypt/email/security/pgp/PgpMsgHtmlSanitizerTest.kt diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/security/pgp/PgpMsg.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/security/pgp/PgpMsg.kt index d6e77add5..ef974180c 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/security/pgp/PgpMsg.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/security/pgp/PgpMsg.kt @@ -176,7 +176,6 @@ object PgpMsg { "span" to arrayOf("color"), "div" to arrayOf("color"), "p" to arrayOf("color"), - "em" to arrayOf("style"), // Typescript: tests rely on this, could potentially remove "td" to arrayOf("width", "height"), "hr" to arrayOf("color", "height"), ) diff --git a/FlowCrypt/src/test/java/com/flowcrypt/email/security/pgp/PgpMsgHtmlSanitizerTest.kt b/FlowCrypt/src/test/java/com/flowcrypt/email/security/pgp/PgpMsgHtmlSanitizerTest.kt new file mode 100644 index 000000000..a2085e751 --- /dev/null +++ b/FlowCrypt/src/test/java/com/flowcrypt/email/security/pgp/PgpMsgHtmlSanitizerTest.kt @@ -0,0 +1,61 @@ +/* + * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com + */ + +package com.flowcrypt.email.security.pgp + +import org.jsoup.Jsoup +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class PgpMsgHtmlSanitizerTest { + @Test + fun testStripsArbitraryCssFromEmStyle() { + val result = sanitize( + "FAKE SECURITY ALERT" + ) + + assertTrue(result.select("[style]").isEmpty()) + assertEquals("FAKE SECURITY ALERT", result.select("em").text()) + } + + @Test + fun testStripsEmStyleWithObfuscatedCssAndAttributeCase() { + val variants = listOf( + "overlay", + "overlay", + "overlay", + "overlay" + ) + + for (variant in variants) { + val result = sanitize(variant) + assertTrue("variant: $variant", result.select("[style]").isEmpty()) + assertEquals("variant: $variant", "overlay", result.select("em").text()) + } + } + + @Test + fun testStripsEmStyleWhenRemoteContentIsBlocked() { + val result = sanitize( + "" + + "overlay" + ) + + assertTrue(result.select("[style]").isEmpty()) + assertEquals("overlay", result.select("em").text()) + } + + @Test + fun testKeepsPlainEmphasis() { + val result = sanitize("important") + + assertTrue(result.select("[style]").isEmpty()) + assertEquals("important", result.select("em").text()) + } + + private fun sanitize(dirtyHtml: String) = + Jsoup.parse(requireNotNull(PgpMsg.sanitizeHtmlKeepBasicTags(dirtyHtml))) +}