ใช้คู่มือนี้เพื่อทำความเข้าใจช่องโหว่ที่พบบ่อยในCloud Firestore Security Rules การกำหนดค่า ตรวจสอบและรักษาความปลอดภัยของกฎของคุณให้ดียิ่งขึ้น และทดสอบการเปลี่ยนแปลงก่อนที่จะนำไปใช้
พรอมต์ AI สำหรับความช่วยเหลือจาก AI
หากต้องการความช่วยเหลือจาก AI ให้คัดลอกพรอมต์ด้านล่างลงใน เอเจนต์การเขียนโค้ด AI (เช่น Gemini ใน Android Studio, Antigravity, Claude Code, Codex และ Cursor) คุณปรับแต่งพรอมต์และทำซ้ำกับเอเจนต์ได้ ดูข้อมูลการติดตั้งโดยละเอียดได้ที่ ทักษะเอเจนต์ Firebase
Help me use Firebase in my app.
1. Make sure that **Firebase agent skills** are installed and updated. For Claude Code, Codex, or Cursor agents, help me install the Firebase plugin. For other agents, run `npx skills add firebase/agent-skills`. Always try to use **Firebase agent skills** for Firebase-related tasks.
2. Help me write, validate, and deploy Firebase Security Rules to protect my app's Firestore data.
หากได้รับการแจ้งเตือนว่าCloud Firestoreฐานข้อมูลไม่ปลอดภัยอย่างเหมาะสม คุณสามารถแก้ไขช่องโหว่ได้โดยการแก้ไขและทดสอบ Cloud Firestore Security Rules
หากต้องการดูกฎความปลอดภัยที่มีอยู่ ให้ไปที่แท็บกฎ ในคอนโซล Firebase
ทำความเข้าใจ Cloud Firestore Security Rules
Cloud Firestore Security Rules ปกป้องข้อมูลจากผู้ใช้ที่เป็นอันตราย กฎเริ่มต้น สำหรับอินสแตนซ์ Cloud Firestore ที่สร้างในคอนโซล Firebase จะปฏิเสธ การเข้าถึงของผู้ใช้ทั้งหมด หากต้องการพัฒนาแอปและเข้าถึงฐานข้อมูล คุณจะต้องแก้ไขกฎเหล่านั้นและอาจพิจารณาให้สิทธิ์เข้าถึงแบบครอบคลุมแก่ผู้ใช้ทั้งหมดในสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ อย่างไรก็ตาม ก่อนที่จะนําแอปไปใช้ในสภาพแวดล้อมฮาร์ดแวร์และซอฟต์แวร์ โปรดใช้เวลาในการกําหนดค่ากฎอย่างเหมาะสมและรักษาความปลอดภัยของข้อมูล
ขณะพัฒนาแอปและทดสอบการกำหนดค่าต่างๆ สำหรับกฎ ให้ใช้Cloud Firestoreโปรแกรมจำลองเพื่อเรียกใช้แอปในสภาพแวดล้อมในการพัฒนาซอฟต์แวร์ในเครื่อง
สถานการณ์ทั่วไปที่มีกฎที่ไม่ปลอดภัย
Cloud Firestore Security Rulesที่คุณอาจตั้งค่าไว้โดยค่าเริ่มต้นหรือเมื่อเริ่มพัฒนาแอปด้วย Cloud Firestore ควรได้รับการตรวจสอบและอัปเดตก่อนที่จะนําแอปไปใช้งาน โปรดตรวจสอบว่าคุณได้รักษาความปลอดภัยข้อมูลของผู้ใช้อย่างเหมาะสมโดยหลีกเลี่ยงข้อผิดพลาดทั่วไปต่อไปนี้
การเข้าถึงแบบเปิด
ขณะตั้งค่า Cloud Firestore คุณอาจตั้งกฎให้มีการเข้าถึงแบบเปิด ในระหว่างการพัฒนา คุณอาจคิดว่าคุณเป็นคนเดียวที่ใช้แอป แต่หากคุณได้ติดตั้งใช้งานแอปแล้ว แอปจะพร้อมใช้งานบนอินเทอร์เน็ต หากคุณไม่ได้ ตรวจสอบสิทธิ์ผู้ใช้และกำหนดค่ากฎความปลอดภัย ทุกคน ที่เดารหัสโปรเจ็กต์ของคุณได้จะขโมย แก้ไข หรือลบข้อมูลได้
| ไม่แนะนำ: สิทธิ์การอ่านและการเขียนสำหรับผู้ใช้ทั้งหมด |
// Allow read/write access to all users under any conditions // Warning: **NEVER** use this rule set in production; it allows // anyone to overwrite your entire database. service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } }
|
วิธีแก้ปัญหา: กฎที่จำกัดสิทธิ์การอ่านและ
การเขียน
สร้างกฎที่เหมาะสมกับลำดับชั้นของข้อมูล โซลูชันที่พบบ่อยอย่างหนึ่ง สำหรับความไม่ปลอดภัยนี้คือการรักษาความปลอดภัยตามผู้ใช้ด้วย Firebase Authentication ดูข้อมูลเพิ่มเติม เกี่ยวกับการตรวจสอบสิทธิ์ผู้ใช้ด้วยกฎ |
เจ้าของเนื้อหาเท่านั้น
service cloud.firestore { match /databases/{database}/documents { // Allow only authenticated content owners access match /some_collection/{document} { // Allow reads and deletion if the current user owns the existing document allow read, delete: if request.auth.uid == resource.data.author_uid; // Allow creation if the current user owns the new document allow create: if request.auth.uid == request.resource.data.author_uid; // Allow updates by the owner, and prevent change of ownership allow update: if request.auth.uid == request.resource.data.author_uid && request.auth.uid == resource.data.author_uid; } } }
การเข้าถึงแบบผสมทั้งแบบสาธารณะและส่วนตัว
service cloud.firestore { match /databases/{database}/documents { // Allow public read access, but only content owners can write match /some_collection/{document} { // Allow public reads allow read: if true // Allow creation if the current user owns the new document allow create: if request.auth.uid == request.resource.data.author_uid; // Allow updates by the owner, and prevent change of ownership allow update: if request.auth.uid == request.resource.data.author_uid && request.auth.uid == resource.data.author_uid; // Allow deletion if the current user owns the existing document allow delete: if request.auth.uid == resource.data.author_uid; } } }
สิทธิ์เข้าถึงสำหรับผู้ใช้ที่ผ่านการตรวจสอบสิทธิ์
ในบางครั้ง Cloud Firestore Security Rules จะตรวจสอบว่าผู้ใช้เข้าสู่ระบบแล้ว แต่ไม่ได้จำกัดการเข้าถึงเพิ่มเติมตามการตรวจสอบสิทธิ์นั้น หากกฎข้อใดข้อหนึ่งของคุณมี auth != null ให้ยืนยันว่าคุณต้องการให้ผู้ใช้ที่เข้าสู่ระบบมีสิทธิ์เข้าถึงข้อมูล
| ไม่แนะนำ: ผู้ใช้ที่เข้าสู่ระบบทุกคนมีสิทธิ์อ่าน และเขียนฐานข้อมูลทั้งหมด |
service cloud.firestore { match /databases/{database}/documents { match /some_collection/{document} { allow read, write: if request.auth != null; } } }
|
วิธีแก้ปัญหา: จำกัดสิทธิ์เข้าถึงโดยใช้เงื่อนไขด้านความปลอดภัย
เมื่อตรวจสอบการตรวจสอบสิทธิ์ คุณอาจต้องใช้พร็อพเพอร์ตี้การตรวจสอบสิทธิ์อย่างใดอย่างหนึ่งเพื่อจำกัดการเข้าถึงเพิ่มเติมสำหรับผู้ใช้ที่เฉพาะเจาะจงสำหรับชุดข้อมูลที่เฉพาะเจาะจง ดูข้อมูลเพิ่มเติมเกี่ยวกับ การเพิ่มเงื่อนไขด้านความปลอดภัยและ การเข้าถึงตามบทบาท |
สิทธิ์เข้าถึงตามบทบาท
service cloud.firestore { match /databases/{database}/documents { // Assign roles to all users and refine access based on user roles match /some_collection/{document} { allow read: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader" allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer" // Note: Checking for roles in your database using `get` (as in the code // above) or `exists` carry standard charges for read operations. } } }
การเข้าถึงตามแอตทริบิวต์
// Give each user in your database a particular attribute // and set it to true/false // Then, use that attribute to grant access to subsets of data // For example, an "admin" attribute set // to "true" grants write access to data service cloud.firestore { match /databases/{database}/documents { match /collection/{document} { allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true; allow read: true; } } }
การเข้าถึงแบบผสมทั้งแบบสาธารณะและส่วนตัว
service cloud.firestore {
match /databases/{database}/documents {
// Allow public read access, but only content owners can write
match /some_collection/{document} {
allow read: if true
allow write: if request.auth.uid == request.resource.data.author_uid
}
}
}
สิทธิ์เข้าถึงสำหรับอีเมลที่ไม่ได้รับการยืนยัน
บางครั้ง Cloud Firestore Security Rules ตรวจสอบว่าอีเมลของผู้ใช้เป็นของโดเมนใดโดเมนหนึ่ง แม้ว่าโดยทั่วไปแล้วแนวทางนี้จะเป็นแนวทางปฏิบัติที่ดี แต่อีเมลก็อาจไม่ได้รับการยืนยัน ในระหว่างการลงชื่อเข้าใช้จนกว่าผู้ใช้จะทำตามขั้นตอนเพิ่มเติมเมื่อได้รับ อีเมลยืนยัน ตรวจสอบว่าคุณกำลังยืนยันว่าอีเมลเป็นของผู้ใช้จริง
| ไม่แนะนำ: ผู้ใช้ทุกคนจะลงชื่อเข้าใช้ด้วยอีเมลใดก็ได้ |
service cloud.firestore { match /databases/{database}/documents { // Allow access based on email domain match /some_collection/{document} { allow read: if request.auth != null && request.auth.email.endsWith('@example.com') } } }
| วิธีแก้ปัญหา: จำกัดการเข้าถึงเฉพาะอีเมลที่ยืนยันแล้ว |
ยืนยันอีเมล
service cloud.firestore { match /databases/{database}/documents { // Allow access based on email domain match /some_collection/{document} { allow read: if request.auth != null && request.auth.email_verified && request.auth.email.endsWith('@example.com') } } }
การเข้าถึงแบบปิด
ในขณะที่คุณกำลังพัฒนาแอป อีกแนวทางที่พบบ่อยคือการล็อกข้อมูลของคุณ โดยปกติแล้ว การดำเนินการนี้หมายความว่าคุณได้ปิดการเข้าถึงแบบอ่านและเขียน สำหรับผู้ใช้ทั้งหมด ดังนี้
// Deny read/write access to all users under any conditions
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
Firebase Admin SDK และ Cloud Functions จะยังคงเข้าถึงฐานข้อมูลได้ ใช้กฎเหล่านี้เมื่อต้องการใช้ Cloud Firestore เป็นแบ็กเอนด์ฝั่งเซิร์ฟเวอร์เท่านั้น ร่วมกับ Firebase Admin SDK แม้ว่าการเชื่อมต่อนี้จะปลอดภัย แต่คุณควรทดสอบว่าไคลเอ็นต์ของแอปสามารถ ดึงข้อมูลได้อย่างถูกต้อง
ดูข้อมูลเพิ่มเติมเกี่ยวกับ Cloud Firestore Security Rules และวิธีการทำงานใน เริ่มต้นใช้งาน Cloud Firestore Security Rules
ตรวจสอบ Cloud Firestore Security Rules
หากต้องการตรวจสอบลักษณะการทำงานของแอปและยืนยันการกำหนดค่า Cloud Firestore Security Rules ให้ใช้โปรแกรมจำลอง Cloud Firestore ใช้Cloud Firestore โปรแกรมจำลองเพื่อเรียกใช้และทำให้การทดสอบหน่วยเป็นแบบอัตโนมัติในสภาพแวดล้อมภายในก่อนที่จะนำการเปลี่ยนแปลงไปใช้งาน
หากต้องการทดสอบ Cloud Firestore Security Rules ที่อัปเดตแล้วอย่างรวดเร็วในคอนโซล Firebase ให้ใช้ เครื่องมือ Rules Playground

- หากต้องการเปิด Rules Playground ให้คลิก Rules Playground จากแท็บกฎ
- ในการตั้งค่าสนามทดสอบกฎ ให้เลือกตัวเลือกสำหรับการทดสอบ ซึ่งรวมถึงตัวเลือกต่อไปนี้
- การทดสอบการอ่านหรือเขียน
- ตำแหน่งที่เฉพาะเจาะจงในฐานข้อมูลของคุณเป็นเส้นทาง
- ประเภทการตรวจสอบสิทธิ์ - ผู้ใช้ที่ไม่ได้รับการตรวจสอบสิทธิ์ ผู้ใช้ที่ไม่ระบุตัวตนที่ได้รับการตรวจสอบสิทธิ์ หรือ รหัสผู้ใช้ที่เฉพาะเจาะจง
- ข้อมูลเฉพาะเอกสารที่กฎอ้างอิงอย่างเจาะจง (เช่น หากกฎกำหนดให้ต้องมีฟิลด์ที่เฉพาะเจาะจงก่อนจึงจะอนุญาตให้เขียนได้)
- คลิกเรียกใช้ แล้วดูผลลัพธ์ในแบนเนอร์เหนือหน้าต่างกฎ