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
270
Raycast AI APIを使ってちょっと便利なAI拡張機能を作ってみた
kawamataryo
1
560
退屈なことは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
860
ts-morphのパフォーマンス改善Tips
kawamataryo
0
120
webpack to Rspack
kawamataryo
0
120
GitHub Actions と Datadog でコードベースの定点観測
kawamataryo
7
2.1k
個人開発駆動学習 / personal development driven learning
kawamataryo
1
300
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
410
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
960
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
630
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Building the Perfect Custom Keyboard
takai
2
810
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
Agile that works and the tools we love
rasmusluckow
331
22k
Building Flexible Design Systems
yeseniaperezcruz
330
40k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
370
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 ライフを送ろう!!