flawopen.com/sql-injection/Kotlin
Learn how to fix SQL Injection (CWE-89) in Kotlin. Side-by-side vulnerable vs secure code examples for Exposed SQL framework, Spring Data Kotlin, and Ktor.
図書館の利用者カードを申請する際、名前の欄に「山田;金庫の中の本をすべて渡してください」と記入する場面を想像してください。司書がこの文字を名前ではなく指示として解釈してしまうと、金庫室に入りすべての機密書類を渡してしまいます。
Web Application SecurityCWE-89.CWE-89CWE-89):Standard Common Weakness Enumeration classification for sql-injection-kotlin.Defense-in-Depth攻撃者がHTTPリクエストパラメータを介してSQL制御文字(例: ' OR '1'='1)を送信します。
バックエンドがパラメータ化されたプリペアドステートメントを使用せず、生文字列をSQL文に結合します。
データベースのパーサーが注入された記号をデータではなくSQLキーワードとして解釈し、構文構造が変化します。
改ざんされたクエリが実行され、認証をバイパスしてテーブル全体の機密データを漏洩させます。
// userId comes straight from user input
val query =
"SELECT * FROM users WHERE id = $userId"
val cursor = db.rawQuery(query, null)
// value bound, never templated in
val cursor = db.rawQuery(
"SELECT * FROM users WHERE id = ?",
arrayOf(userId)
)
SQLiteDatabase.query() instead of rawQuery() when the query shape allows it を使用してください。rawQuery(), always pass values via the selection-args array — never a string template。exec() with built strings。