Unique Rule SQL Injection Warningについて調べてみた
元記事
https://gyazo.com/bea4763b324dea3a1c1ab496600919e3
Laravelで提供しているバリデーションにuniqueルールというのがあります。
これをバリデーションルールで指定すると、データベースのテーブルの指定したカラムで、値がユニークであるか(一意であるか)をチェックできます。例えば以下のように実装することで送られたた値がユニークであるかを簡単に判断する事ができます。
code:php
$request->validate([
// usersテーブルのlogin_nameカラムで一意チェックを行う
'login_name' => 'unique:users'
]);
今回報告された「Unique Rule SQL Injection Warning」に関しては、内容を読むと、uniqueチェックで指定したIDを除外したい場合で使うignoreメソッドに渡される値はアプリケーション側で生成されるauto-increment属性のIDとか、もしくはUUIDを受け入れるように実装されています。 Rule::unique('users')->ignore($request->input('id'))
The unique rule's "exclude ID" feature is intended to only accept system-generated IDs, such as auto-incrementing IDs or UUIDs generated by your application, which is the only way the documentation demonstrates using the rule
しかしながら、アプリケーション側で、除外しようとしているIDの検索を許可をしている場合に、ignoreメソッドの引数に悪意を持って作成されたSQL文を渡された場合に、SQLインジェクションの攻撃に繋がる可能性があります。
However, if users depart from the documented usage of the feature and allows user controlled data to specify the "exclude ID" value or column, a maliciously crafted request could lead to an SQL injection attack.
この脆弱性についてはLaravel 5.8.5で対応されています。
実際にPRでの変更箇所を見ると、ignoreメソッドに引数で渡されてきたidに対してstr_replaceを使って、エスケープ処理している事がわかります。 はてなブログに投稿済み