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.9k
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
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
130
パスワード再設定メールのURLについて知っておこう
uyuki234
0
200
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
240
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
140
JIS keyboard support in Omarchy
lef237
0
180
Next.js: The final boss of Web Performance
guaca
0
370
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
250
Zero-Tap Sign-In対応、結局何をすればいいの?
mitohato14
0
1.2k
Inertia Rails: The One-Person Framework with React & Friends
skryukov
0
200
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
210
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
1.2k
AmebaブログはKMPから卒業します
kmars0605
0
310
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
540
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
240
Site-Speed That Sticks
csswizardry
13
1.6k
The Curious Case for Waylosing
cassininazir
1
600
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
3
3.9k
The Curse of the Amulet
leimatthew05
3
16k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.2k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
960
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Code Review Best Practice
trishagee
74
20k
Amusing Abliteration
ianozsvald
1
370
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