คุณสามารถใช้กฎความปลอดภัยของ Firebase เพื่อเขียนข้อมูลใหม่อย่างมีเงื่อนไขโดยอิงจากข้อมูลที่มีอยู่ในฐานข้อมูลหรือบัคเก็ตพื้นที่เก็บข้อมูลของคุณ คุณยังสามารถเขียนกฎที่บังคับใช้การตรวจสอบข้อมูลโดยจำกัดการเขียนตามข้อมูลใหม่ที่กำลังเขียน อ่านเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับกฎที่ใช้ข้อมูลที่มีอยู่เพื่อสร้างเงื่อนไขความปลอดภัย
เลือกผลิตภัณฑ์ในแต่ละส่วนเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับกฎการตรวจสอบข้อมูล
ข้อจำกัดเกี่ยวกับข้อมูลใหม่
Cloud Firestore
ถ้าคุณต้องการให้แน่ใจว่าไม่มีการสร้างเอกสารที่มีฟิลด์ใดฟิลด์หนึ่ง คุณสามารถรวมฟิลด์นั้นไว้ในเงื่อนไข allow
ตัวอย่างเช่น หากคุณต้องการปฏิเสธการสร้างเอกสารใดๆ ที่มีฟิลด์ ranking
คุณจะต้องไม่อนุญาตในเงื่อนไข create
service cloud.firestore {
match /databases/{database}/documents {
// Disallow
match /cities/{city} {
allow create: if !("ranking" in request.resource.data)
}
}
}
ฐานข้อมูลเรียลไทม์
หากคุณต้องการให้แน่ใจว่าข้อมูลที่มีค่าบางอย่างไม่ถูกเพิ่มลงในฐานข้อมูลของคุณ คุณต้องรวมค่านั้นไว้ในกฎและไม่อนุญาตให้เขียน ตัวอย่างเช่น หากคุณต้องการปฏิเสธการเขียนใดๆ ที่มีค่า 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')
}
}
การจัดเก็บเมฆ
หากคุณต้องการให้แน่ใจว่าไม่มีการสร้างไฟล์ที่มีข้อมูลเมตาเฉพาะ คุณสามารถรวมข้อมูลเมตาไว้ในเงื่อนไข allow
ตัวอย่างเช่น หากคุณต้องการปฏิเสธการสร้างไฟล์ใดๆ ที่มีเมทาดา ranking
คุณจะต้องไม่อนุญาตในเงื่อนไข create
service firebase.storage {
match /b/{bucket}/o {
match /files/{allFiles=**} {
// Disallow
allow create: if !("ranking" in request.resource.metadata)
}
}
}
ใช้ข้อมูลที่มีอยู่ในกฎความปลอดภัยของ Firebase
Cloud Firestore
แอพจำนวนมากเก็บข้อมูลการควบคุมการเข้าถึงเป็นฟิลด์ในเอกสารในฐานข้อมูล กฎความปลอดภัยของ Cloud Firestore สามารถอนุญาตหรือปฏิเสธการเข้าถึงแบบไดนามิกตามข้อมูลเอกสาร:
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
โปรดดู เอกสารอ้างอิง
เมื่อเขียนข้อมูล คุณอาจต้องการเปรียบเทียบข้อมูลขาเข้ากับข้อมูลที่มีอยู่ การดำเนินการนี้ช่วยให้คุณทำสิ่งต่างๆ เช่น ตรวจสอบให้แน่ใจว่าฟิลด์ไม่มีการเปลี่ยนแปลง ฟิลด์นั้นเพิ่มขึ้นเพียงหนึ่งค่า หรือค่าใหม่ในอนาคตอย่างน้อยหนึ่งสัปดาห์ ในกรณีนี้ หากชุดกฎของคุณอนุญาตให้มีการเขียนที่รอดำเนินการ ตัวแปร 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 ให้ใช้กฎ .validate
เพื่อบังคับใช้โครงสร้างข้อมูลและตรวจสอบความถูกต้องของรูปแบบและเนื้อหาของข้อมูล กฎเรียกใช้ .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()"
}
}
}
}
การเขียนคำขอไปยังฐานข้อมูลด้วยกฎข้างต้นจะมีผลดังต่อไปนี้:
จาวาสคริปต์
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);
วัตถุประสงค์-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];
สวิฟต์
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);
ชวา
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);
พักผ่อน
# 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.json
การจัดเก็บเมฆ
เมื่อประเมินกฎ คุณอาจต้องการประเมินข้อมูลเมตาของไฟล์ที่กำลังอัปโหลด ดาวน์โหลด แก้ไข หรือลบ ซึ่งช่วยให้คุณสร้างกฎที่ซับซ้อนและมีประสิทธิภาพซึ่งทำสิ่งต่างๆ เช่น อนุญาตให้อัปโหลดไฟล์ที่มีเนื้อหาบางประเภทเท่านั้น หรือเฉพาะไฟล์ที่มีขนาดใหญ่กว่าขนาดที่กำหนดเท่านั้นที่จะถูกลบ
ออบเจ็ก resource
มีคู่คีย์/ค่าพร้อมข้อมูลเมตาของไฟล์ที่แสดงในออบเจ็กต์ Cloud Storage คุณสมบัติเหล่านี้สามารถตรวจสอบได้ในคำขอ read
หรือ write
เพื่อให้แน่ใจว่าข้อมูลมีความสมบูรณ์ ออบเจ็กต์ resource
ตรวจสอบข้อมูลเมตาของไฟล์ที่มีอยู่ในที่เก็บข้อมูล Cloud Storage
service firebase.storage {
match /b/{bucket}/o {
match /images {
match /{allImages=**} {
// Allow reads if a custom 'visibility' field is set to 'public'
allow read: if resource.metadata.visibility == 'public';
}
}
}
}
คุณยังสามารถใช้ออบเจ็กต์ request.resource
ในคำขอ write
(เช่น อัปโหลด อัปเดตข้อมูลเมตา และลบ ออบเจ็กต์ request.resource
รับข้อมูลเมตาจากไฟล์ที่จะเขียนหากอนุญาตให้ write
ได้
คุณสามารถใช้ค่าทั้งสองนี้เพื่อป้องกันการอัปเดตที่ไม่ต้องการหรือไม่สอดคล้องกัน หรือเพื่อบังคับใช้ข้อจำกัดของแอปพลิเคชัน เช่น ประเภทไฟล์หรือขนาด
service firebase.storage {
match /b/{bucket}/o {
match /images {
// Cascade read to any image type at any path
match /{allImages=**} {
allow read;
}
// 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) File name (stored in imageId wildcard variable) is less than 32 characters
match /{imageId} {
allow write: if request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('image/.*')
&& request.resource.contentType == resource.contentType
&& imageId.size() < 32
}
}
}
}
รายการคุณสมบัติทั้งหมดในวัตถุ resource
มีอยู่ใน เอกสารอ้างอิง