Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
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.6k
Other Decks in Programming
See All in Programming
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
590
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.4k
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
260
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
270
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
110
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
550
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
840
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
220
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
260
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.8k
170k Jobs a Day on GKE: Scaling Mercari's CI Platform - and What's Next for AI-Native Development
junyaokabe
0
110
Featured
See All Featured
Unsuck your backbone
ammeep
672
58k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
220
Building Applications with DynamoDB
mza
96
7.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
460
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
410
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
940
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
460
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
400
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