คุณใช้ Firebase Security Rules เพื่อเขียนข้อมูลใหม่แบบมีเงื่อนไขตามข้อมูลที่มีอยู่ ในฐานข้อมูลหรือ Bucket ที่เก็บข้อมูลได้ นอกจากนี้ คุณยังเขียนกฎที่บังคับใช้การตรวจสอบข้อมูลได้โดยจำกัดการเขียนตามข้อมูลใหม่ที่กำลังเขียน อ่านต่อ เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับกฎที่ใช้ข้อมูลที่มีอยู่เพื่อสร้างเงื่อนไขด้านความปลอดภัย
เลือกผลิตภัณฑ์ในแต่ละส่วนเพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับกฎการตรวจสอบข้อมูล
พรอมต์ 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 data.
ข้อจำกัดเกี่ยวกับข้อมูลใหม่
Cloud Firestore
หากต้องการตรวจสอบว่าไม่มีการสร้างเอกสารที่มีฟิลด์ที่เฉพาะเจาะจง คุณสามารถรวมฟิลด์นั้นไว้ในallowเงื่อนไข เช่น หากคุณต้องการปฏิเสธการสร้างเอกสารที่มีฟิลด์ ranking คุณจะต้องไม่อนุญาตในเงื่อนไข create
service cloud.firestore {
match /databases/{database}/documents {
// Disallow
match /cities/{city} {
allow create: if !("ranking" in request.resource.data)
}
}
}
Realtime Database
หากต้องการตรวจสอบว่าระบบจะไม่เพิ่มข้อมูลที่มีค่าบางอย่าง
ลงในฐานข้อมูล คุณจะต้องรวมค่านั้นไว้ในกฎและไม่อนุญาตให้
เขียน เช่น หากต้องการปฏิเสธการเขียนที่มีค่า ranking
คุณจะต้องไม่อนุญาตการเขียนสำหรับเอกสารที่มีค่า ranking
{
"rules": {
// Write is allowed for all paths
".write": true,
// Allows writes only if new data doesn't include a `ranking` child value
".validate": "!newData.hasChild('ranking')
}
}
Cloud Storage
หากต้องการตรวจสอบว่าระบบจะไม่สร้างไฟล์ที่มีข้อมูลเมตาที่เฉพาะเจาะจง คุณสามารถรวมข้อมูลเมตาไว้ในallowเงื่อนไข เช่น หากคุณต้องการปฏิเสธการสร้างไฟล์ที่มีข้อมูลเมตา ranking
คุณจะต้องไม่อนุญาตในเงื่อนไข create
service firebase.storage {
match /b/{bucket}/o {
match /files/{fileName} {
// Disallow
allow create: if !("ranking" in request.resource.metadata)
}
}
}
ใช้ข้อมูลที่มีอยู่ใน Firebase Security Rules
Cloud Firestore
แอปจำนวนมากจัดเก็บข้อมูลการควบคุมการเข้าถึงเป็นฟิลด์ในเอกสารในฐานข้อมูล Cloud Firestore Security Rules สามารถอนุญาตหรือปฏิเสธการเข้าถึงแบบไดนามิกตามข้อมูลเอกสาร ได้ดังนี้
service cloud.firestore {
match /databases/{database}/documents {
// Allow the user to read data if the document has the 'visibility'
// field set to 'public'
match /cities/{city} {
allow read: if resource.data.visibility == 'public';
}
}
}
ตัวแปร resource หมายถึงเอกสารที่ขอ และ resource.data คือ
แผนที่ของฟิลด์และค่าทั้งหมดที่จัดเก็บไว้ในเอกสาร ดูข้อมูลเพิ่มเติมเกี่ยวกับตัวแปร resource ได้ที่เอกสารประกอบอ้างอิง
เมื่อเขียนข้อมูล คุณอาจต้องการเปรียบเทียบข้อมูลที่เข้ามากับข้อมูลที่มีอยู่ ซึ่งช่วยให้คุณทำสิ่งต่างๆ ได้ เช่น ตรวจสอบว่าฟิลด์ไม่ได้เปลี่ยนแปลง ฟิลด์เพิ่มขึ้นเพียง 1 หรือค่าใหม่เป็นเวลาอย่างน้อย 1 สัปดาห์ในอนาคต
ในกรณีนี้ หากชุดกฎอนุญาตการเขียนที่รอดำเนินการ request.resource
ตัวแปรจะมีสถานะในอนาคตของเอกสาร สำหรับupdateการดำเนินการที่
แก้ไขเฉพาะฟิลด์เอกสารบางส่วน ตัวแปร request.resource จะ
มีสถานะเอกสารที่รอดำเนินการหลังจากการดำเนินการ คุณสามารถตรวจสอบค่าฟิลด์
ใน request.resource เพื่อป้องกันการอัปเดตข้อมูลที่ไม่ต้องการหรือไม่สอดคล้องกันได้โดยทำดังนี้
service cloud.firestore {
match /databases/{database}/documents {
// Make sure all cities have a positive population and
// the name is not changed
match /cities/{city} {
allow update: if request.resource.data.population > 0
&& request.resource.data.name == resource.data.name;
}
}
}
Realtime Database
ใน Realtime Database ให้ใช้กฎ .validate เพื่อบังคับใช้โครงสร้างข้อมูลและตรวจสอบรูปแบบและเนื้อหาของข้อมูล Security Rulesเรียกใช้กฎ .validate หลังจาก
ยืนยันว่ากฎ .write ให้สิทธิ์เข้าถึง
กฎ .validate จะไม่เรียงซ้อนกัน หากกฎการตรวจสอบใดๆ ล้มเหลวในเส้นทางหรือเส้นทางย่อยใดๆ ในกฎ ระบบจะปฏิเสธการดำเนินการเขียนทั้งหมด
นอกจากนี้ นิยามการตรวจสอบจะตรวจสอบเฉพาะค่าที่ไม่ใช่ Null และ
จากนั้นจะละเว้นคำขอที่ลบข้อมูล
โปรดพิจารณากฎต่อไปนี้.validate
{
"rules": {
// write is allowed for all paths
".write": true,
"widget": {
// a valid widget must have attributes "color" and "size"
// allows deleting widgets (since .validate is not applied to delete rules)
".validate": "newData.hasChildren(['color', 'size'])",
"size": {
// the value of "size" must be a number between 0 and 99
".validate": "newData.isNumber() &&
newData.val() >= 0 &&
newData.val() <= 99"
},
"color": {
// the value of "color" must exist as a key in our mythical
// /valid_colors/ index
".validate": "root.child('valid_colors/' + newData.val()).exists()"
}
}
}
}
การเขียนคำขอไปยังฐานข้อมูลด้วยกฎข้างต้นจะมีผลลัพธ์ดังนี้
JavaScript
var ref = db.ref("/widget"); // PERMISSION_DENIED: does not have children color and size ref.set('foo'); // PERMISSION DENIED: does not have child color ref.set({size: 22}); // PERMISSION_DENIED: size is not a number ref.set({ size: 'foo', color: 'red' }); // SUCCESS (assuming 'blue' appears in our colors list) ref.set({ size: 21, color: 'blue'}); // If the record already exists and has a color, this will // succeed, otherwise it will fail since newData.hasChildren(['color', 'size']) // will fail to validate ref.child('size').set(99);
Objective-C
FIRDatabaseReference *ref = [[[FIRDatabase database] reference] child: @"widget"]; // PERMISSION_DENIED: does not have children color and size [ref setValue: @"foo"]; // PERMISSION DENIED: does not have child color [ref setValue: @{ @"size": @"foo" }]; // PERMISSION_DENIED: size is not a number [ref setValue: @{ @"size": @"foo", @"color": @"red" }]; // SUCCESS (assuming 'blue' appears in our colors list) [ref setValue: @{ @"size": @21, @"color": @"blue" }]; // If the record already exists and has a color, this will // succeed, otherwise it will fail since newData.hasChildren(['color', 'size']) // will fail to validate [[ref child:@"size"] setValue: @99];
Swift
var ref = FIRDatabase.database().reference().child("widget") // PERMISSION_DENIED: does not have children color and size ref.setValue("foo") // PERMISSION DENIED: does not have child color ref.setValue(["size": "foo"]) // PERMISSION_DENIED: size is not a number ref.setValue(["size": "foo", "color": "red"]) // SUCCESS (assuming 'blue' appears in our colors list) ref.setValue(["size": 21, "color": "blue"]) // If the record already exists and has a color, this will // succeed, otherwise it will fail since newData.hasChildren(['color', 'size']) // will fail to validate ref.child("size").setValue(99);
Java
FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference ref = database.getReference("widget"); // PERMISSION_DENIED: does not have children color and size ref.setValue("foo"); // PERMISSION DENIED: does not have child color ref.child("size").setValue(22); // PERMISSION_DENIED: size is not a number Map<String,Object> map = new HashMap<String, Object>(); map.put("size","foo"); map.put("color","red"); ref.setValue(map); // SUCCESS (assuming 'blue' appears in our colors list) map = new HashMap<String, Object>(); map.put("size", 21); map.put("color","blue"); ref.setValue(map); // If the record already exists and has a color, this will // succeed, otherwise it will fail since newData.hasChildren(['color', 'size']) // will fail to validate ref.child("size").setValue(99);
REST
# PERMISSION_DENIED: does not have children color and size
curl -X PUT -d 'foo' \
https://docs-examples.firebaseio.com/rest/securing-data/example.json
# PERMISSION DENIED: does not have child color
curl -X PUT -d '{"size": 22}' \
https://docs-examples.firebaseio.com/rest/securing-data/example.json
# PERMISSION_DENIED: size is not a number
curl -X PUT -d '{"size": "foo", "color": "red"}' \
https://docs-examples.firebaseio.com/rest/securing-data/example.json
# SUCCESS (assuming 'blue' appears in our colors list)
curl -X PUT -d '{"size": 21, "color": "blue"}' \
https://docs-examples.firebaseio.com/rest/securing-data/example.json
# If the record already exists and has a color, this will
# succeed, otherwise it will fail since newData.hasChildren(['color', 'size'])
# will fail to validate
curl -X PUT -d '99' \
https://docs-examples.firebaseio.com/rest/securing-data/example/size.jsonCloud Storage
เมื่อประเมินกฎ คุณอาจต้องประเมินข้อมูลเมตาของไฟล์ที่กำลังอัปโหลด ดาวน์โหลด แก้ไข หรือลบด้วย ซึ่งจะช่วยให้คุณสร้างกฎที่ซับซ้อนและมีประสิทธิภาพซึ่งทำสิ่งต่างๆ ได้ เช่น อนุญาตให้อัปโหลดเฉพาะไฟล์ที่มีประเภทเนื้อหาบางอย่าง หรือลบเฉพาะไฟล์ที่มีขนาดใหญ่กว่าขนาดที่กำหนด
ออบเจ็กต์ resource มีคู่คีย์-ค่าที่มีข้อมูลเมตาของไฟล์ซึ่งแสดงในออบเจ็กต์
Cloud Storage คุณตรวจสอบพร็อพเพอร์ตี้เหล่านี้ได้ในคำขอ read หรือ write เพื่อให้มั่นใจว่าข้อมูลมีความสมบูรณ์ resourceออบเจ็กต์จะตรวจสอบข้อมูลเมตา
ในไฟล์ที่มีอยู่ในที่เก็บข้อมูล Cloud Storage
service firebase.storage {
match /b/{bucket}/o {
match /images {
match /{fileName} {
// Allow reads if a custom 'visibility' field is set to 'public'
allow read: if resource.metadata.visibility == 'public';
}
}
}
}
นอกจากนี้ คุณยังใช้ออบเจ็กต์ request.resource ในคำขอ write (เช่น การอัปโหลด การอัปเดตข้อมูลเมตา และการลบ) ได้ด้วย ออบเจ็กต์ request.resource จะรับ
ข้อมูลเมตาจากไฟล์ที่จะเขียนหากwriteได้รับอนุญาต
คุณใช้ค่าทั้ง 2 นี้เพื่อป้องกันการอัปเดตที่ไม่ต้องการหรือไม่สอดคล้องกัน หรือเพื่อบังคับใช้ข้อจำกัดของแอปพลิเคชัน เช่น ประเภทหรือขนาดไฟล์ ได้
service firebase.storage {
match /b/{bucket}/o {
match /images {
// Allow write files to the path "images/*", subject to the constraints:
// 1) File is less than 5MB
// 2) Content type is an image
// 3) Uploaded content type matches existing content type
// 4) Filename (stored in imageId wildcard variable) is less than 32 characters
match /{imageId} {
allow read;
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*')
&& request.resource.contentType == resource.contentType
&& imageId.size() < 32
}
}
}
}
ดูรายการพร็อพเพอร์ตี้ทั้งหมดในออบเจ็กต์ resource ได้ในเอกสารอ้างอิง