คุณสามารถใช้กฎความปลอดภัยของ Firebase เพื่อเขียนข้อมูลใหม่ตามเงื่อนไขโดยอิงตามข้อมูลที่มีอยู่ในฐานข้อมูลหรือที่เก็บข้อมูลของคุณ คุณยังสามารถเขียนกฎที่บังคับใช้การตรวจสอบความถูกต้องของข้อมูลได้โดยการจำกัดการเขียนตามข้อมูลใหม่ที่กำลังเขียน อ่านต่อเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับกฎที่ใช้ข้อมูลที่มีอยู่เพื่อสร้างเงื่อนไขด้านความปลอดภัย
เลือกผลิตภัณฑ์ในแต่ละส่วนเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับกฎการตรวจสอบข้อมูล
ข้อจำกัดเกี่ยวกับข้อมูลใหม่
คลาวด์ไฟร์สโตร์
หากคุณต้องการตรวจสอบให้แน่ใจว่าไม่ได้สร้างเอกสารที่มีฟิลด์เฉพาะ คุณสามารถรวมฟิลด์นั้นไว้ในเงื่อนไข 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 สามารถอนุญาตหรือปฏิเสธการเข้าถึงแบบไดนามิกโดยอิงตามข้อมูลเอกสาร:
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
จะไม่เรียงซ้อน หากกฎการตรวจสอบล้มเหลวบนเส้นทางหรือเส้นทางย่อยในกฎ การดำเนินการเขียนทั้งหมดจะถูกปฏิเสธ นอกจากนี้ ข้อกำหนดในการตรวจสอบจะตรวจสอบเฉพาะค่าที่ไม่ใช่ค่าว่าง และต่อมาจะละเว้นคำขอใดๆ ที่กำลังลบข้อมูล
พิจารณากฎ .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
มีอยู่ใน เอกสารอ้างอิง