Firebase Security Rules ช่วยให้คุณควบคุมสิทธิ์เข้าถึงข้อมูลที่จัดเก็บไว้ได้ ไวยากรณ์ของกฎที่ยืดหยุ่นช่วยให้คุณสร้างกฎที่ตรงกับทุกอย่างได้ ตั้งแต่การเขียนทั้งหมดไปยังฐานข้อมูลทั้งหมดไปจนถึงการดำเนินการกับเอกสารที่เฉพาะเจาะจง
คู่มือนี้อธิบายกรณีการใช้งานพื้นฐานบางอย่างที่คุณอาจต้องการนำไปใช้เมื่อตั้งค่าแอปและปกป้องข้อมูล อย่างไรก็ตาม ก่อนที่จะเริ่มเขียนกฎ คุณอาจต้องดูข้อมูลเพิ่มเติมเกี่ยวกับภาษาที่ใช้เขียนและลักษณะการทำงานของกฎ
หากต้องการเข้าถึงและอัปเดตกฎ ให้ทำตามขั้นตอนที่ระบุไว้ในหัวข้อจัดการและติดตั้งใช้งาน Firebase Security Rules
พรอมต์ AI รายการสำหรับความช่วยเหลือจาก AI
หากต้องการความช่วยเหลือจาก AI ให้คัดลอกพรอมต์ด้านล่างลงในตัวแทนการเขียนโค้ด AI (เช่น Gemini ใน Android Studio, Antigravity, Claude Code, Codex และ Cursor) คุณสามารถปรับแต่งพรอมต์และทำซ้ำกับ Agent ได้ ดูข้อมูลการติดตั้งโดยละเอียดได้ที่ทักษะเอเจนต์ 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 data.
กฎเริ่มต้น: โหมดล็อกขณะคุมสอบ
เมื่อสร้างฐานข้อมูลหรืออินสแตนซ์พื้นที่เก็บข้อมูลในคอนโซล Firebase คุณจะเลือกได้ว่าจะให้ Firebase Security Rules จำกัดการเข้าถึงข้อมูล (โหมดล็อก) หรืออนุญาตให้ทุกคนเข้าถึง (โหมดทดสอบ) ใน Cloud Firestore และ Realtime Database กฎเริ่มต้นสำหรับโหมดล็อกจะปฏิเสธการเข้าถึงสำหรับผู้ใช้ทุกคน ใน Cloud Storage เฉพาะผู้ใช้ที่ตรวจสอบสิทธิ์แล้วเท่านั้นที่จะเข้าถึงที่เก็บข้อมูลได้
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}
Realtime Database
{
"rules": {
".read": false,
".write": false
}
}
Cloud Storage
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
}
}
}
กฎสภาพแวดล้อมในการพัฒนาซอฟต์แวร์
ขณะที่กำลังพัฒนาแอป คุณอาจต้องการสิทธิ์เข้าถึงข้อมูลที่ค่อนข้างเปิดกว้างหรือไม่มีข้อจำกัด อย่าลืมอัปเดต Security Rules ก่อนที่จะทำให้แอปใช้งานได้จริง นอกจากนี้ โปรดทราบว่าหากคุณทำให้แอปใช้งานได้ แอปจะเข้าถึงได้แบบสาธารณะ แม้ว่าคุณจะยังไม่ได้เปิดตัวก็ตาม
โปรดทราบว่า Firebase อนุญาตให้ไคลเอ็นต์เข้าถึงข้อมูลของคุณได้โดยตรง และ Firebase Security Rules เป็นมาตรการป้องกันเพียงอย่างเดียวที่บล็อกการเข้าถึงของผู้ใช้ที่เป็นอันตราย การกำหนดกฎแยกจากตรรกะของผลิตภัณฑ์มีข้อดีหลายประการ ได้แก่ ไคลเอ็นต์ไม่ต้องรับผิดชอบในการบังคับใช้การรักษาความปลอดภัย การติดตั้งใช้งานที่มีข้อบกพร่องจะไม่ทำให้ข้อมูลของคุณเสียหาย และที่สำคัญที่สุดคือ คุณไม่ต้องพึ่งพาเซิร์ฟเวอร์ตัวกลางเพื่อปกป้องข้อมูลจากโลกภายนอก
ผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์ทั้งหมด
แม้ว่าเราจะไม่แนะนำให้ปล่อยให้ผู้ใช้ที่ลงชื่อเข้าใช้เข้าถึงข้อมูลของคุณได้ แต่การตั้งค่าการเข้าถึงสำหรับผู้ใช้ที่ตรวจสอบสิทธิ์แล้วอาจเป็นประโยชน์ในขณะที่คุณกำลังพัฒนาแอป
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
match /some_collection/{document} {
allow read, write: if request.auth != null;
}
}
}
Realtime Database
{
"rules": {
"some_path": {
".read": "auth.uid !== null",
".write": "auth.uid !== null"
}
}
}
Cloud Storage
service firebase.storage {
match /b/{bucket}/o {
match /some_folder/{fileName} {
allow read, write: if request.auth != null;
}
}
}
กฎที่พร้อมใช้งานจริง
ขณะเตรียมติดตั้งใช้งานแอป โปรดตรวจสอบว่าข้อมูลของคุณได้รับการปกป้องและผู้ใช้ได้รับสิทธิ์เข้าถึงอย่างเหมาะสม ใช้ Authentication เพื่อตั้งค่าการเข้าถึงตามผู้ใช้และอ่านโดยตรงจากฐานข้อมูลเพื่อตั้งค่าการเข้าถึงตามข้อมูล
พิจารณาเขียนกฎขณะจัดโครงสร้างข้อมูล เนื่องจากวิธีตั้งค่ากฎจะส่งผลต่อวิธีจำกัดการเข้าถึงข้อมูลในเส้นทางต่างๆ
สิทธิ์เข้าถึงเฉพาะเจ้าของเนื้อหาเท่านั้น
กฎเหล่านี้จำกัดการเข้าถึงเฉพาะเจ้าของเนื้อหาที่ผ่านการตรวจสอบสิทธิ์แล้วเท่านั้น ข้อมูลจะอ่านและเขียนได้โดยผู้ใช้เพียงรายเดียว และเส้นทางข้อมูลจะมีรหัสของผู้ใช้
กรณีที่กฎนี้ทำงาน: กฎนี้จะทำงานได้ดีหากข้อมูลถูกแยกตามผู้ใช้ กล่าวคือ ผู้ใช้เพียงคนเดียวที่จำเป็นต้องเข้าถึงข้อมูลคือผู้ใช้คนเดียวกับที่สร้างข้อมูล
กรณีที่กฎนี้ใช้ไม่ได้: ชุดกฎนี้จะใช้ไม่ได้เมื่อผู้ใช้หลายคนต้องการเขียนหรืออ่านข้อมูลเดียวกัน โดยผู้ใช้จะเขียนทับข้อมูลหรือเข้าถึงข้อมูลที่ตนสร้างขึ้นไม่ได้
วิธีตั้งค่ากฎนี้: สร้างกฎที่ยืนยันว่าผู้ใช้ที่ขอสิทธิ์เข้าถึงเพื่ออ่านหรือเขียนข้อมูลเป็นผู้ใช้ที่เป็นเจ้าของข้อมูลนั้น
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated content owners access
match /some_collection/{userId}/{document} {
allow read, write: if request.auth != null && request.auth.uid == userId
}
}
}
Realtime Database
{
"rules": {
"some_path": {
"$uid": {
// Allow only authenticated content owners access to their data
".read": "auth !== null && auth.uid === $uid",
".write": "auth !== null && auth.uid === $uid"
}
}
}
}
Cloud Storage
// Grants a user access to a node matching their user ID
service firebase.storage {
match /b/{bucket}/o {
// Files look like: "user/<UID>/file.txt"
match /user/{userId}/{fileName} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
การเข้าถึงแบบผสมทั้งสาธารณะและส่วนตัว
กฎนี้อนุญาตให้ทุกคนอ่านชุดข้อมูลได้ แต่จำกัดความสามารถในการสร้างหรือแก้ไขข้อมูลในเส้นทางที่กำหนดไว้ให้เฉพาะเจ้าของเนื้อหาที่ผ่านการตรวจสอบสิทธิ์เท่านั้น
กรณีที่กฎนี้ทำงาน: กฎนี้ทำงานได้ดีกับแอปที่ต้องใช้องค์ประกอบที่อ่านได้แบบสาธารณะ แต่ต้องจำกัดสิทธิ์แก้ไขให้เฉพาะเจ้าขององค์ประกอบเหล่านั้น เช่น แอปแชทหรือบล็อก
กรณีที่กฎนี้ใช้ไม่ได้: เช่นเดียวกับกฎเฉพาะเจ้าของเนื้อหา ชุดกฎนี้จะใช้ไม่ได้เมื่อผู้ใช้หลายคนต้องแก้ไขข้อมูลเดียวกัน ผู้ใช้จะเขียนทับข้อมูลของกันและกันในที่สุด
วิธีตั้งค่ากฎนี้: สร้างกฎที่เปิดใช้สิทธิ์การอ่านสำหรับผู้ใช้ทุกคน (หรือผู้ใช้ที่ตรวจสอบสิทธิ์แล้วทุกคน) และยืนยันว่าผู้ใช้ที่เขียนข้อมูลเป็นเจ้าของ
Cloud Firestore
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;
}
}
}
Realtime Database
{
// Allow anyone to read data, but only authenticated content owners can
// make changes to their data
"rules": {
"some_path": {
"$uid": {
".read": true,
// or ".read": "auth.uid !== null" for only authenticated users
".write": "auth.uid === $uid"
}
}
}
}
Cloud Storage
service firebase.storage {
match /b/{bucket}/o {
// Files look like: "user/<UID>/file.txt"
match /user/{userId}/{fileName} {
allow read;
allow write: if request.auth.uid == userId;
}
}
}
การเข้าถึงตามแอตทริบิวต์และตามบทบาท
เพื่อให้กฎเหล่านี้ทำงานได้ คุณต้องกำหนดและมอบหมายแอตทริบิวต์ให้กับผู้ใช้ในข้อมูลของคุณ Firebase Security Rules จะตรวจสอบคำขอกับข้อมูลจากฐานข้อมูลหรือข้อมูลเมตาของไฟล์เพื่อยืนยันหรือปฏิเสธการเข้าถึง
กรณีที่กฎนี้ทำงาน: หากคุณมอบหมายบทบาทให้กับผู้ใช้ กฎนี้จะช่วยให้คุณจำกัดการเข้าถึงตามบทบาทหรือกลุ่มผู้ใช้ที่เฉพาะเจาะจงได้ ตัวอย่างเช่น หากคุณจัดเก็บคะแนน คุณสามารถกำหนดระดับการเข้าถึงที่แตกต่างกันให้กับกลุ่ม "นักเรียน" (อ่านเนื้อหาของตนเองเท่านั้น) กลุ่ม "ครู" (อ่านและเขียนในวิชาของตนเอง) และกลุ่ม "ผู้บริหาร" (อ่านเนื้อหาทั้งหมด)
กรณีที่กฎนี้ใช้ไม่ได้: ใน Realtime Database และ Cloud Storage กฎของคุณจะใช้วิธี get() ที่กฎ Cloud Firestore สามารถรวมไว้ด้วยไม่ได้
ดังนั้น คุณจึงต้องจัดโครงสร้างฐานข้อมูลหรือข้อมูลเมตาของไฟล์ให้สอดคล้องกับแอตทริบิวต์ที่ใช้ในกฎ
วิธีตั้งค่ากฎนี้: ใน Cloud Firestore ให้ใส่ช่องในเอกสารของผู้ใช้ที่คุณอ่านได้ จากนั้นสร้างโครงสร้างกฎเพื่ออ่านช่องนั้นและให้สิทธิ์เข้าถึงแบบมีเงื่อนไข ใน Realtime Database ให้สร้างเส้นทางข้อมูลที่กำหนดผู้ใช้แอปของคุณและให้บทบาทแก่ผู้ใช้เหล่านั้นในโหนดระดับย่อย
นอกจากนี้ คุณยังตั้งค่าการอ้างสิทธิ์ที่กำหนดเองใน Authentication ได้ด้วย จากนั้นจึงดึงข้อมูลดังกล่าวจากตัวแปร auth.token ใน Firebase Security Rules ใดก็ได้
แอตทริบิวต์และบทบาทที่กำหนดโดยข้อมูล
กฎเหล่านี้ใช้ได้เฉพาะใน Cloud Firestore และ Realtime Database เท่านั้น
Cloud Firestore
โปรดทราบว่าทุกครั้งที่กฎของคุณมีการอ่าน เช่น กฎด้านล่าง ระบบจะเรียกเก็บเงินคุณสำหรับการดำเนินการอ่านใน Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
// For attribute-based access control, Check a boolean `admin` attribute
allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
allow read: true;
// Alterntatively, for role-based access, assign specific roles to users
match /some_collection/{document} {
allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"
}
}
}
Realtime Database
{
"rules": {
"some_path": {
"${subpath}": {
//
".write": "root.child('users').child(auth.uid).child('role').val() === 'admin'",
".read": true
}
}
}
}
แอตทริบิวต์และบทบาทของการอ้างสิทธิ์ที่กำหนดเอง
หากต้องการใช้กฎเหล่านี้ ให้ตั้งค่าการอ้างสิทธิ์ที่กำหนดเองใน Firebase Authentication แล้วใช้การอ้างสิทธิ์ในกฎ
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
// For attribute-based access control, check for an administrator claim
allow write: if request.auth.token.admin == true;
allow read: true;
// Alterntatively, for role-based access, assign specific roles to users
match /some_collection/{document} {
allow read: if request.auth.token.reader == "true";
allow write: if request.auth.token.writer == "true";
}
}
}
Realtime Database
{
"rules": {
"some_path": {
"$uid": {
// Create a custom claim for each role or group
// you want to use
".write": "auth.uid !== null && auth.token.writer === true",
".read": "auth.uid !== null && auth.token.reader === true"
}
}
}
}
Cloud Storage
service firebase.storage {
// Allow reads if the group ID in your token matches the file metadata's `owner` property
// Allow writes if the group ID is in the user's custom token
match /files/{groupId}/{fileName} {
allow read: if resource.metadata.owner == request.auth.token.groupId;
allow write: if request.auth.token.groupId == groupId;
}
}
แอตทริบิวต์การเช่า
หากต้องการใช้กฎเหล่านี้ ให้ตั้งค่าแบ่งการใช้ทรัพยากรให้ผู้ใช้จำนวนมาก (Multi-Tenancy)ใน Google Cloud Identity Platform (GCIP) แล้วใช้กลุ่มผู้ใช้ในกฎ ตัวอย่างต่อไปนี้อนุญาตให้เขียนจากผู้ใช้ในกลุ่มผู้ใช้ที่เฉพาะเจาะจง เช่น tenant2-m6tyz
Cloud Firestore
service cloud.firestore {
match /databases/{database}/documents {
// For tenant-based access control, check for a tenantID
allow write: if request.auth.token.firebase.tenant == 'tenant2-m6tyz';
allow read: true;
}
}
Realtime Database
{
"rules": {
"some_path": {
"$uid": {
// Only allow reads and writes if user belongs to a specific tenant
".write": "auth.uid !== null && auth.token.firebase.tenant === 'tenant2-m6tyz'",
".read": "auth.uid !== null
}
}
}
}
Cloud Storage
service firebase.storage {
// Only allow reads and writes if user belongs to a specific tenant
match /files/{tenantId}/{fileName} {
allow read: if request.auth != null;
allow write: if request.auth.token.firebase.tenant == tenantId;
}
}