Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
LaravelでLIKE句のSQLインジェクション対策をする
Search
ゆい
September 25, 2022
Programming
3.8k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
LaravelでLIKE句のSQLインジェクション対策をする
ゆい
September 25, 2022
More Decks by ゆい
See All by ゆい
PHPの緩やかな比較の実態
fyui001
0
1.7k
Other Decks in Programming
See All in Programming
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
360
複雑なUIコンポーネントとの ⻑い付き合い⽅:波及するバグからテスト戦略を経て、安全な再設計へ
mixplace
0
290
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
320
phperだってパンチカードでコーディングしたい
o0h
PRO
0
250
dotNET_vulnclicker_intro.pdf
deka_morita
0
130
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
690
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
370
最近”作らせてる” ローカルAIアプリの話
akit37
0
430
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
590
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
450
Vue Fes Japan 2026 タイムテーブル徹底解説
448jp
1
650
CodeRabbitの効果検証と過ごしてみた3ヶ月
armondando
0
200
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
560
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
620
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
400
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
510
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
Odyssey Design
rkendrick25
PRO
2
870
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Technical Leadership for Architectural Decision Making
baasie
3
600
Transcript
Copyright© M&AΫϥυ LaravelͰLIKE۟ͷSQLΠϯδΣΫγϣϯରࡦΛ͢Δ PHP Conference Japan 2022
Copyright© M&AΫϥυ 2 Profile גࣜձࣾM&AΫϥυ Ώ͍ fyui001 @fyui_001
Copyright© M&AΫϥυ 3 खͳSQLΠϯδΣΫγϣϯҰൠతͳWebϑϨʔϜϫʔΫΛ༻͢Εجຊతʹൃੜ͠·ͤΜɻ ͔͠͠ɺLIKEݕࡧΛߦ͏߹DoS߈ཱܸ͕ͯ͠͠·͏͜ͱ͕͋Γ·͢ɻ LIKE "%a%b%c%d%e%e%f%g%@%.%" ্هͷΑ͏ͳΫΤϦSQLΤϯδϯʹେ͖ͳෛՙΛ͔͚·͢ɻ LIKE۟ͷϝλจࣈΤεέʔϓ͢Δඞཁ͕͋Γ·͕͢ɺ
$query->where('hoge', 'LIKE', '%' . $value . '%'); ͱʹॻ͍ͯ͠·͏έʔεଟ͍ͱࢥ͍·͢ɻ
Copyright© M&AΫϥυ 4 ରࡦ LaravelͰͷ͜ͷLIKE۟ͷΠϯδΣΫγϣϯରࡦ͓ͦΒ̏͘௨Γ΄Ͳ͋Δͱࢥ͏ͷͰ ͦΕͧΕͷιϦϡʔγϣϯΛ͝հ͍ͯ͜͠͏ͱࢥ͍·͢ɻ
Copyright© M&AΫϥυ 5 1. macroΛ༻ҙ͢Δ ·ͣBlueprintͷmacroΛఆٛ͢ΔͨΊʹ αʔϏεϓϩόΠμΛ৽͘͠࡞Γ·͢ɻ(AppServiceProvider.phpʹॻ͖ࠐΉํ๏͋Δɻ php artisan make:provider
BlueprintServiceProvider
Copyright© M&AΫϥυ 6 1. macroΛ༻ҙ͢Δ <?php namespace App\Providers; use Illuminate\Support\ServiceProvider;
class BlueprintServiceProvider extends ServiceProvider { /** * Register services. * * @return void */ public function register() { // } /** * Bootstrap services. * * @return void */ public function boot() { // } ͢ΔͱҎԼͷΑ͏ͳϑΝΠϧ͕ੜ͞Ε·͢ɻ
Copyright© M&AΫϥυ 7 1. macroΛ༻ҙ͢Δ ͜ͷbootϝιουʹmacroΛఆ͍͖ٛͯ͠·͢ɻ < /** * Bootstrap
services. * * @return void */ public function boot() { Builder::macro('whereLike', function (string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $this->where($attribute, 'LIKE', $condition); }); Builder::macro('orWhereLike', function (string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $this->orWhere($attribute, 'LIKE', $condition); }); }
Copyright© M&AΫϥυ 8 2.ΫΤϦείʔϓΛ͏ ModelͰҎԼͷΑ͏ʹఆٛ͠·͢ɻ <?php namespace App\Models; use Illuminate\Database\Eloquent\Model
as EloquentModel; /** * This class contains shared setup, properties and methods * of all application models * */ class Model extends EloquentModel { public function scopeWhereLike($query, string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $query->where($attribute, 'LIKE', $condition); } public function scopeOrWhereLike($query, string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $query->orWhere($attribute, 'LIKE', $condition); }
Copyright© M&AΫϥυ 9 3.TraitͰ͍ճͤΔύʔπͱͯ͠༻ҙ͢Δ macroɾΫΤϦείʔϓఆٛͰIDEࢧԉ͕ޮ͔ͳ͍͕͋Γ·͢ɻ ·ͨɺνʔϜͷنʹΑͬͯϑϨʔϜϫʔΫͷཧղϨϕϧʹόϥ͖͕ͭग़ͯ͘Δ͜ͱ͋Γ·͢ɻ ͳͷͰݴޠϨϕϧͰཧղͷ͍͢͠TraitͰػೳΛ༻ҙͯ͋͛͠Δͱ͍͏ղग़͖ͯ·͢ɻ TraitͳΒIDEͷࢧԉޮ͘ͷͰɺ৽ϝϯόʔͳͲ͕ίʔυΛݟͨͱ͖ʹίʔυδϟϯϓͰͨͲΓண͘ίετ͕͘ͳΔ͔͠Ε·ͤΜɻ ͕͔ͩ͠͠ɺTraitͰ͍͍ײ͡ʹ࣮͢Δํ๏͕Θ͔Βͳ͔ͬͨͷͰఘΊͨ Πϝʔδͱͯ͠ҎԼͷײ͡Ͱ࣮Ͱ͖ͨΒΑ͔ͬͨͷͰ͕͢ɺEloquent\Builder
Λ࣋ͯͳ͍ҝ $result = Model::whereLike('hoge', $value)->get() Έ͍ͨͳॻ͖ํग़དྷͯ $result = Model::where('hoge', $value)->orWhereLike('hoge', $value)->get(); Έ͍ͨͳEloquent\Builderͷ͔ؔΒݺͼग़ͦ͏ͱ͢Δͱવίέͯ͠·͍·͢ɻ ͳΜ͔͍͍ײ͡ʹTraitͰ࣮͢Δํ๏͕͋ͬͨΒڭ͑ͯԼ͍͞ɻ
Copyright© M&AΫϥυ 10 3.TraitͰ͍ճͤΔύʔπͱͯ͠༻ҙ͢Δ Ϙπ <?php declare(strict_types=1); namespace App\Libs; trait
EloquentQueryBuilder { protected function whereLike(string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $this->orWhere($attribute, 'LIKE', $condition); } protected function orWhereLike(string $attribute, string $keyword, int $position = 0) { $keyword = addcslashes($keyword, '\_%'); $condition = [ 1 => "{$keyword}%", -1 => "%{$keyword}", ][$position] ?? "%{$keyword}%"; return $this->orWhere($attribute, 'LIKE', $condition); } }
Copyright© M&AΫϥυ 11 ͍ํ $result = Model::whereLike('hoge', $keyword)->get(); // or
$query = Model::query(); $result = $query::whereLike('hoge', $keyword)->get(); whereLike $result = Model::where('hoge', $value)->orWhereLike('hoge', $keyword)->get(); // or $query = Model::query(); $result = $query::where('hoge', $value)->orWhereLike('hoge', $keyword)->get(); orWhereLike
Copyright© M&AΫϥυ 12 ·ͱΊ macro͔ΫΤϦείʔϓΛ࣮͢Δ͜ͱͰEloquentͷwhere۟Λॻ͘ͷͱಉ༷ͷه๏ͰҎԼͷ͕ؔ༻Ͱ͖·͢ɻ • whereLike • orWhereLike ͋ͱɺwhere۟ʹੜͷLIKE͕۟ࠞೖ͠ͳ͍Α͏ʹίʔυͷ࣭ΛΩʔϓ͢ΕղܾͰ͖·͢ɻ
લड़ͷ௨ΓmacroΫΤϦείʔϓͰIDEࢧԉ͕ޮ͔ͳ͍ͷͰɺ LaravelʹิɾܕใΛ༩ͯ͘͠ΕΔϥΠϒϥϦLaravel IDE Helper GeneratorͳͲΛ༻͢Δͱศརͩͱࢥ͍·͢ɻ
Copyright© M&AΫϥυ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ 13