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
ここまで出来るよ 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
400
Raycast AI APIを使ってちょっと便利なAI拡張機能を作ってみた
kawamataryo
1
620
退屈なことはDevinにやらせよう〜〜Devin APIを使ったVisual Regression Testの自動追加〜
kawamataryo
5
2.4k
SaaS公式MCPサーバーをリリースして得た学び
kawamataryo
7
2.2k
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
1
930
ts-morphのパフォーマンス改善Tips
kawamataryo
0
150
webpack to Rspack
kawamataryo
0
150
GitHub Actions と Datadog でコードベースの定点観測
kawamataryo
7
2.2k
個人開発駆動学習 / personal development driven learning
kawamataryo
1
320
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
55
8.4k
Chasing Engaging Ingredients in Design
codingconduct
0
370
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Designing Powerful Visuals for Engaging Learning
tmiket
1
620
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
960
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
A designer walks into a library…
pauljervisheath
211
25k
HDC tutorial
michielstock
2
980
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
430
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 ライフを送ろう!!