आप अपने डेटाबेस या स्टोरेज बकेट में मौजूदा डेटा के आधार पर सशर्त रूप से नया डेटा लिखने के लिए फायरबेस सुरक्षा नियमों का उपयोग कर सकते हैं। आप ऐसे नियम भी लिख सकते हैं जो लिखे जा रहे नए डेटा के आधार पर लिखने को प्रतिबंधित करके डेटा सत्यापन लागू करते हैं। सुरक्षा स्थितियाँ बनाने के लिए मौजूदा डेटा का उपयोग करने वाले नियमों के बारे में अधिक जानने के लिए आगे पढ़ें।
डेटा सत्यापन नियमों के बारे में अधिक जानने के लिए प्रत्येक अनुभाग में एक उत्पाद का चयन करें।
नए डेटा पर प्रतिबंध
क्लाउड फायरस्टोर
यदि आप यह सुनिश्चित करना चाहते हैं कि कोई दस्तावेज़ जिसमें कोई विशिष्ट फ़ील्ड शामिल है, नहीं बनाया गया है, तो आप फ़ील्ड को 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)
}
}
}
फायरबेस सुरक्षा नियमों में मौजूदा डेटा का उपयोग करें
क्लाउड फायरस्टोर
कई ऐप्स एक्सेस कंट्रोल जानकारी को डेटाबेस में दस्तावेज़ों पर फ़ील्ड के रूप में संग्रहीत करते हैं। क्लाउड फायरस्टोर सुरक्षा नियम दस्तावेज़ डेटा के आधार पर गतिशील रूप से पहुंच की अनुमति या अस्वीकार कर सकते हैं:
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;
}
}
}
रीयलटाइम डेटाबेस
रीयलटाइम डेटाबेस में, डेटा संरचनाओं को लागू करने और डेटा के प्रारूप और सामग्री को मान्य करने के लिए .validate
नियमों का उपयोग करें। यह सत्यापित करने के बाद कि .write
नियम पहुँच प्रदान करता है, नियम .validate
नियमों को चलाते हैं।
.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);
उद्देश्य सी
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
ऑब्जेक्ट में क्लाउड स्टोरेज ऑब्जेक्ट में सामने आए फ़ाइल मेटाडेटा के साथ कुंजी/मूल्य जोड़े होते हैं। डेटा अखंडता सुनिश्चित करने के लिए read
या write
अनुरोध पर इन गुणों का निरीक्षण किया जा सकता है। resource
ऑब्जेक्ट आपके क्लाउड स्टोरेज बकेट में मौजूदा फ़ाइलों पर मेटाडेटा की जाँच करता है।
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';
}
}
}
}
आप write
अनुरोधों (जैसे अपलोड, मेटाडेटा अपडेट और डिलीट) पर request.resource
ऑब्जेक्ट का भी उपयोग कर सकते हैं। 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
ऑब्जेक्ट में गुणों की पूरी सूची संदर्भ दस्तावेज़ में उपलब्ध है।