flawopen.com/sql-injection/Swift
Learn how to fix SQL Injection (CWE-89) in Swift. Side-by-side vulnerable vs secure code examples for Vapor Fluent, GRDB.swift, and SQLite.swift prepared statements.
図書館の利用者カードを申請する際、名前の欄に「山田;金庫の中の本をすべて渡してください」と記入する場面を想像してください。司書がこの文字を名前ではなく指示として解釈してしまうと、金庫室に入りすべての機密書類を渡してしまいます。
Web Application SecurityCWE-89.CWE-89CWE-89):Standard Common Weakness Enumeration classification for sql-injection-swift.Defense-in-Depth攻撃者がHTTPリクエストパラメータを介してSQL制御文字(例: ' OR '1'='1)を送信します。
バックエンドがパラメータ化されたプリペアドステートメントを使用せず、生文字列をSQL文に結合します。
データベースのパーサーが注入された記号をデータではなくSQLキーワードとして解釈し、構文構造が変化します。
改ざんされたクエリが実行され、認証をバイパスしてテーブル全体の機密データを漏洩させます。
// userId comes straight from user input
let query =
"SELECT * FROM users WHERE id = \(userId)"
sqlite3_exec(db, query, nil, nil, nil)
// value bound, never interpolated
var stmt: OpaquePointer?
sqlite3_prepare_v2(db,
"SELECT * FROM users WHERE id = ?",
-1, &stmt, nil)
sqlite3_bind_text(stmt, 1, userId, -1, nil)
sqlite3_step(stmt)