flawopen.com/xss/Kotlin
Learn how to fix Cross-Site Scripting (XSS) (CWE-79) in Kotlin. Side-by-side vulnerable vs secure code examples for Ktor HTML DSL and Thymeleaf template escaping.
想象一下,有人在社区公告栏上贴了一张便签:'凡阅读此便签者,请立刻将钱包交给身旁的人'。如果路过的居民盲目执行公告栏上的每一条纸条指令,任何张贴便签的人都能瞬间偷走所有人的财物。
Web Application SecurityCWE-79.CWE-79CWE-79):Standard Common Weakness Enumeration classification for xss-kotlin.Defense-in-Depth攻击者在表单输入框、URL 查询参数中输入恶意 HTML 标签或 JavaScript 代码(如 <script> 或 <img onerror=...>)。
应用程序将原始输入未经上下文 HTML 实体编码即直接拼接入页面或写入数据库。
当受害者访问该页面时,浏览器将未转义的字符解析为合法的可执行脚本而非普通文本。
恶意脚本在受害者会话上下文中执行,窃取 document.cookie、读取 LocalStorage 凭据或冒名发起操作。
// untrusted HTML loaded with JS + native bridge
webView.settings.javaScriptEnabled = true
webView.addJavascriptInterface(
NativeBridge(), "Android"
)
webView.loadData(untrustedHtml,
"text/html", "UTF-8")
// sanitize first; avoid exposing a bridge
val safeHtml = sanitize(untrustedHtml)
webView.settings.javaScriptEnabled = false
webView.loadData(safeHtml,
"text/html", "UTF-8")
addJavascriptInterface() on a WebView that can load untrusted content。