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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
410
Raycast AI APIを使ってちょっと便利なAI拡張機能を作ってみた
kawamataryo
1
630
退屈なことは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
940
ts-morphのパフォーマンス改善Tips
kawamataryo
0
160
webpack to Rspack
kawamataryo
0
150
GitHub Actions と Datadog でコードベースの定点観測
kawamataryo
7
2.2k
個人開発駆動学習 / personal development driven learning
kawamataryo
1
330
Featured
See All Featured
Practical Orchestrator
shlominoach
192
12k
Automating Front-end Workflow
addyosmani
1370
210k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
600
Evolving SEO for Evolving Search Engines
ryanjones
0
360
Building Adaptive Systems
keathley
44
3.3k
Designing Powerful Visuals for Engaging Learning
tmiket
1
650
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Code Review Best Practice
trishagee
74
20k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
590
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Accessibility Awareness
sabderemane
1
290
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
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 ライフを送ろう!!