डेटा सत्यापन

आप अपने डेटाबेस या स्टोरेज बकेट में मौजूद डेटा के आधार पर, शर्तों के साथ नया डेटा लिखने के लिए Firebase Security Rules का इस्तेमाल कर सकते हैं. ऐसे नियम भी लिखे जा सकते हैं जिनसे डेटा की पुष्टि की जा सके. इसके लिए, लिखे जा रहे नए डेटा के आधार पर, डेटा लिखने पर पाबंदी लगाई जा सकती है. सुरक्षा की शर्तें बनाने के लिए, मौजूदा डेटा का इस्तेमाल करने वाले नियमों के बारे में ज़्यादा जानने के लिए, आगे पढ़ें.

डेटा की पुष्टि करने के नियमों के बारे में ज़्यादा जानने के लिए, हर सेक्शन में कोई प्रॉडक्ट चुनें.

नए डेटा पर पाबंदियां

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` वैरिएबल के बारे में ज़्यादा जानने के लिए, `रेफ़रंस दस्तावेज़ देखें`.

डेटा लिखते समय, हो सकता है कि आपको आने वाले डेटा की तुलना मौजूदा डेटा से करनी पड़े. इससे, यह पक्का किया जा सकता है कि कोई फ़ील्ड बदला न हो, किसी फ़ील्ड में सिर्फ़ एक की बढ़ोतरी हुई हो या नई वैल्यू कम से कम एक हफ़्ते बाद की हो. इस मामले में, अगर आपके नियमों का सेट, लंबित डेटा को लिखने की अनुमति देता है, तो 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 नियम, कैस्केड नहीं होते. अगर नियम में किसी पाथ या सबपाथ पर, पुष्टि करने का कोई नियम काम नहीं करता है, तो डेटा लिखने की पूरी कार्रवाई को अस्वीकार कर दिया जाएगा. इसके अलावा, पुष्टि करने की परिभाषाएं सिर्फ़ ऐसी वैल्यू की जांच करती हैं जो शून्य नहीं हैं. साथ ही, डेटा मिटाने के किसी भी अनुरोध को अनदेखा कर देती हैं.

.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
ध्यान दें: Firebase का यह प्रॉडक्ट, App Clip टारगेट पर उपलब्ध नहीं है.
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
ध्यान दें: Firebase का यह प्रॉडक्ट, App Clip टारगेट पर उपलब्ध नहीं है.
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.json

Cloud 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';
        }
      }
    }
  }

write अनुरोधों (जैसे, अपलोड, मेटाडेटा अपडेट, और मिटाने के अनुरोध) पर, request.resource ऑब्जेक्ट का भी इस्तेमाल किया जा सकता है. अगर write की अनुमति दी जाती है, तो request.resource ऑब्जेक्ट, उस फ़ाइल से मेटाडेटा लेता है जिसे लिखा जाएगा.

इन दो वैल्यू का इस्तेमाल, अनचाहे या गलत अपडेट को रोकने या ऐप्लिकेशन की कंस्ट्रेंट लागू करने के लिए किया जा सकता है. जैसे, फ़ाइल टाइप या साइज़.

  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 ऑब्जेक्ट में मौजूद प्रॉपर्टी की पूरी सूची, रेफ़रंस दस्तावेज़ में उपलब्ध है.