डेटा सत्यापन

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 के नियमों का इस्तेमाल करके डेटा स्ट्रक्चर लागू करें. साथ ही, डेटा के फ़ॉर्मैट और कॉन्टेंट की पुष्टि करें. 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 प्रॉडक्ट, ऐप्लिकेशन क्लिप टारगेट पर उपलब्ध नहीं है.
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 प्रॉडक्ट, ऐप्लिकेशन क्लिप टारगेट पर उपलब्ध नहीं है.
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';
        }
      }
    }
  }

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

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

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