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エンティティ化を行わずにページ内へ反映またはデータベースに保存します。
被害者がページを読み込んだ際、ブラウザが挿入されたテキストを実行可能なスクリプトとして解釈します。
被害者の権限下でスクリプトが動作し、認証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 しないでください。