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
ここまで出来るよ Firestore セキュリティルール
Search
ryo
October 10, 2019
3.1k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ここまで出来るよ Firestore セキュリティルール
もくテク #3
ryo
October 10, 2019
More Decks by ryo
See All by ryo
Bridging Social Graphs
kawamataryo
0
340
Raycast AI APIを使ってちょっと便利なAI拡張機能を作ってみた
kawamataryo
1
590
退屈なことはDevinにやらせよう〜〜Devin APIを使ったVisual Regression Testの自動追加〜
kawamataryo
5
2.3k
SaaS公式MCPサーバーをリリースして得た学び
kawamataryo
7
2.2k
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
1
900
ts-morphのパフォーマンス改善Tips
kawamataryo
0
140
webpack to Rspack
kawamataryo
0
140
GitHub Actions と Datadog でコードベースの定点観測
kawamataryo
7
2.1k
個人開発駆動学習 / personal development driven learning
kawamataryo
1
310
Featured
See All Featured
KATA
mclloyd
PRO
35
15k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
220
The Mindset for Success: Future Career Progression
greggifford
PRO
0
480
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
180
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
630
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
550
HDC tutorial
michielstock
2
820
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
630
The Spectacular Lies of Maps
axbom
PRO
1
950
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
450
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.5k
Transcript
ここまで出来るよ Firestore セキュリティルール @Ryo Kawamata
0. 自己紹介
@RyoKawamata 消防士歴 6年半 エンジニア歴 1年半 Ruby / Typescript Firebase /
Vue.js
1. Firestoreって何?
Firebaseが提供する スケーラブルなNoSQL のデータベース
Firestoreの特徴
ドキュメント指向のNoSQL
Data Document Collection
Collection Data Document
リアルタイム同期
websocket不要でデータの更新が リアルタイム同期される
NoSQLなのに アトミックオペレーション が使える
トランザクション 一括書き込み 一つ以上のドキュメントに対して対 して書き込みを行う一連のオペ レーション。読み込みでのロックを 行わないため、実行速度が早い。 一つ以上のドキュメントに対して読 み書きを行う一連のオペレーショ ン。読み込みのロックのため、整合 性は高いが、実行速度は遅い。
フロントエンドの 開発スピードアップ
通常の Webアプリ Front end Back end DB ORM REST API
Firestore利用 Front end Fire Store Client SDK
使いやすいSDK
ドキュメントの追 加 ドキュメントの取 得 firestore().collection("users").add({ name: "太郎", sales: 10000 });
firestore().collection("users") .where("sales", ">", 5000) .orderBy("sales", "desc");
高いSLOとスケーラビリティ
Resional 99.99 % Multi-Resion 99.999 % SLO Designed to scale
We've designed Cloud Firestore to handle the toughest database workloads from the world's biggest apps. 世界最大のアプリからの最も厳しいデータベース ワークロードを処理するように設計.
良心的な料金体系
Sparkプラン (無料) Flameプラン ($25/月) Blazeプラン (従量課金) 保存データ 合計 1GiB 合計
2.5GiB $0.18 / GiB 帯域幅 10GiB / 月 20GiB / 月 Google Cloud pricing ドキュメントの 書き込み 2万 / 日 10万 / 日 $0.18 / 10万 ドキュメントの 読み取り 5万 / 日 25万 / 日 $0.06 / 10万 ドキュメントの 削除 2万 / 日 10万 / 日 $0.02 / 10万 2019/10/8 時点
もはや最高..!!!!!
でも..
clientから処理されるって、 セキュリティ大丈夫... スキーマレスって変なデータ入って バグになりやすそう.. Validationがフロントだけだと、 怖いような..
大丈夫!!!
セキュリティルール がある!!
3. セキュリティルールとは?
Firestoreが提供する サーバーサイドでアクセス制御、 データ検証が出来る機能
service cloud.firestore { match /databases/{database}/documents { match /users/{userId} { allow
read: if true; allow write: if false; } } } ex) usersコレクションを読み取り専用にするルール 対象のコレクション 条件 許可する操作
4. セキュリティルール で出来ること
ユーザー認証
Firebase Authentication 認証済みのユーザーで、かつ 自分のドキュメントしか閲覧できない RULE
function isAuthUser(auth, userId) { return auth != null && auth.uid
== userId } match /databases/{database}/documents { match /users/{userId} { allow read: if isAuthUser(request.auth, userId); } } request.auth で認証情報を取得
スキーマ検証
ドキュメントは以下スキーマを持つ RULE カラム名 型 name String sales Number
function isValidSchema(data) { return data.size() == 2 && 'name' in
data && data.name is string && 'sales' in data && data.sales is number } match /databases/{database}/documents { match /users/{userId} { allow create: if isValidSchema(request.resources.data); } } request.resources.data で送信データを取得
バリデーション
ドキュメントは以下条件を持つ RULE カラム名 条件 name 30文字以内 gender male, female, genderDivers
のいずれかの値を持つ
function isValidData(data) { return 'name' in data && 1 <=
data.name.size() && data.name.size() <= 30 && 'gender' in data && data.gender.matches('male|female|genderDiverse') } match /databases/{database}/documents { match /users/{userId} { allow create: if isValidData(request.resources.data); } } 関数 で送信データを判定
関連でのバリデーション
messagesはusersドキュメントの ageが18以上の場合に追加できる RULE
function isValidData(data) { return get(/databases/$(database)/documents/users/$(data.userRef)) .data.age >= 18 } match
/databases/{database}/documents { match /messages/{message} { allow create: if isValidData(request.resources.data); } } get() で別ドキュメントのデータを参照
Firebase Authとの連携
admin 権限を持つユーザーのみ 投稿を許可する RULE
function isAdminUser(auth) { return auth.token.admin == true; } match /databases/{database}/documents
{ match /users/{userId} { allow create: if isAdminUser(request.auth); } } custom claimsでadminを判定
パラメータの変化で判定
statusは`order`から`shipment` の順で変化する RULE
function isValidUpdate(oldData, newData) { return oldData.status.matches('order') && newData.status.matches('shipment') } match
/databases/{database}/documents { match /users/{userId} { allow update: if isValidUpdate(resource.data, request.resources.data); } } resource.data で 変更前のデータを取得して判定
4. 注意すべきところ
get関数は金額計算対象
Admin SDKでは セキュリティルールは効かない
5. まとめ
Firestoreは最高のデータベース。 ただそれを実現するためには、 セキュリティルールを しっかり書くことが大切 !! セキュリティルールを書いて より良いFirestore ライフを送ろう!!