Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
"<em style=\"position:fixed;top:0;left:0;width:100%;height:100%;" +
"background:red;z-index:99999;\">FAKE SECURITY ALERT</em>"
)

assertTrue(result.select("[style]").isEmpty())
assertEquals("FAKE SECURITY ALERT", result.select("em").text())
}

@Test
fun testStripsEmStyleWithObfuscatedCssAndAttributeCase() {
val variants = listOf(
"<EM STYLE=\"POSITION:FIXED;Z-INDEX:99999\">overlay</EM>",
"<em style=\" position : fixed ; opacity : 0.01 \">overlay</em>",
"<em style=\"pos\\69tion:fixed;background:url(https://example.com/x)\">overlay</em>",
"<em style=\"font-style:italic\">overlay</em>"
)

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(
"<img src=\"https://example.com/pixel.png\">" +
"<em style=\"position:fixed;top:0;width:100%;height:100%\">overlay</em>"
)

assertTrue(result.select("[style]").isEmpty())
assertEquals("overlay", result.select("em").text())
}

@Test
fun testKeepsPlainEmphasis() {
val result = sanitize("<em>important</em>")

assertTrue(result.select("[style]").isEmpty())
assertEquals("important", result.select("em").text())
}

private fun sanitize(dirtyHtml: String) =
Jsoup.parse(requireNotNull(PgpMsg.sanitizeHtmlKeepBasicTags(dirtyHtml)))
}
Loading