असुरक्षित नियमों से बचें

Firebase Security Rules कॉन्फ़िगरेशन में आम तौर पर होने वाली कमजोरियों को समझने के लिए, इस गाइड का इस्तेमाल करें. साथ ही, अपने नियमों की समीक्षा करें और उन्हें ज़्यादा सुरक्षित बनाएं. इसके अलावा, बदलावों को लागू करने से पहले उनकी जांच करें.

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

अपना Firebase Security Rules ऐक्सेस करना

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

Firebase कंसोल से अपने नियमों को ऐक्सेस करने के लिए, अपना प्रोजेक्ट चुनें. इसके बाद, Realtime Database, Cloud Firestore या स्टोरेज पर जाएं. सही डेटाबेस या स्टोरेज बकेट में जाने के बाद, नियम पर क्लिक करें.

Firebase सीएलआई से अपने नियमों को ऐक्सेस करने के लिए, firebase.json फ़ाइल में बताई गई नियम फ़ाइल पर जाएं.

Firebase Security Rules के बारे में जानकारी

Firebase Security Rules अपने डेटा को नुकसान पहुंचाने वाले उपयोगकर्ताओं से सुरक्षित रखना. Firebase कंसोल में डेटाबेस इंस्टेंस या Cloud Storage बकेट बनाते समय, आपके पास सभी उपयोगकर्ताओं को ऐक्सेस देने (टेस्ट मोड) या ऐक्सेस न देने (लॉक मोड) का विकल्प होता है. हो सकता है कि डेवलपमेंट के दौरान आपको ज़्यादा ओपन कॉन्फ़िगरेशन की ज़रूरत पड़े. हालांकि, ऐप्लिकेशन को डिप्लॉय करने से पहले, अपने नियमों को सही तरीके से कॉन्फ़िगर करें और अपने डेटा को सुरक्षित रखें.

ऐप्लिकेशन डेवलप करने और नियमों के लिए अलग-अलग कॉन्फ़िगरेशन की जांच करते समय, अपने ऐप्लिकेशन को किसी स्थानीय Firebase एमुलेटर का इस्तेमाल करके, स्थानीय डेवलपमेंट एनवायरमेंट में चलाएं.

असुरक्षित नियमों से जुड़ी सामान्य स्थितियां

हो सकता है कि आपने Rules को डिफ़ॉल्ट रूप से सेट अप किया हो या ऐप्लिकेशन को डेवलप करते समय, आपने इसे शुरू में सेट अप किया हो. ऐप्लिकेशन को डिप्लॉय करने से पहले, इसकी समीक्षा करें और इसे अपडेट करें. साथ ही, यहां बताई गई सामान्य गड़बड़ियों से बचकर, अपने उपयोगकर्ताओं के डेटा को सुरक्षित रखें.

ओपन ऐक्सेस

Firebase प्रोजेक्ट सेट अप करते समय, हो सकता है कि आपने डेवलपमेंट के दौरान ऐप्लिकेशन को सभी के लिए ऐक्सेस करने की अनुमति देने के लिए नियम सेट किए हों. आपको शायद लगे कि आपके ऐप्लिकेशन का इस्तेमाल सिर्फ़ आप ही कर रहे हैं. हालांकि, अगर आपने इसे डिप्लॉय किया है, तो यह इंटरनेट पर उपलब्ध हो जाता है. अगर उपयोगकर्ताओं की पुष्टि नहीं की जा रही है और सुरक्षा नियमों को कॉन्फ़िगर नहीं किया जा रहा है, तो आपके प्रोजेक्ट आईडी का अनुमान लगाकर कोई भी व्यक्ति डेटा को चुरा सकता है, उसमें बदलाव कर सकता है या उसे मिटा सकता है.

इसका सुझाव नहीं दिया जाता: सभी उपयोगकर्ताओं के लिए, पढ़ने और लिखने का ऐक्सेस.
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this ruleset in production; it allows
// anyone to overwrite your entire database.

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}
{
  // Allow read/write access to all users under any conditions
  // Warning: **NEVER** use this ruleset in production; it allows
  // anyone to overwrite your entire database.

  "rules": {
    ".read": true,
    ".write": true
  }
}
    
// Anyone can read or write to the bucket, even non-users of your app.
// Because it is shared with App Engine, this will also make
// files uploaded using App Engine public.
// Warning: This rule makes every file in your Cloud Storage bucket accessible to any user.
// Apply caution before using it in production, since it means anyone
// can overwrite all your files.

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}
    
समाधान: ऐसे नियम जो पढ़ने और लिखने के ऐक्सेस पर पाबंदी लगाते हैं.

ऐसे नियम बनाएं जो आपकी डेटा हैरारकी के हिसाब से सही हों. इस समस्या को हल करने का एक आम तरीका, Firebase Authentication की मदद से उपयोगकर्ता के हिसाब से सुरक्षा सेट करना है. नियमों की मदद से उपयोगकर्ताओं की पुष्टि करने के बारे में ज़्यादा जानें.

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow only authenticated content owners access
    match /some_collection/{document} {
      // Allow reads and deletion if the current user owns the existing document
      allow read, delete: if request.auth.uid == resource.data.author_uid;
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;

    }
  }
}
  
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      // Allow public reads
      allow read: if true
      // Allow creation if the current user owns the new document
      allow create: if request.auth.uid == request.resource.data.author_uid;
      // Allow updates by the owner, and prevent change of ownership
      allow update: if request.auth.uid == request.resource.data.author_uid
                    && request.auth.uid == resource.data.author_uid;
      // Allow deletion if the current user owns the existing document
      allow delete: if request.auth.uid == resource.data.author_uid;
    }
  }
}
  
{
  "rules": {
    "some_path": {
      "$uid": {
        // Allow only authenticated content owners access to their data
        ".read": "auth !== null && auth.uid === $uid",
        ".write": "auth !== null && auth.uid === $uid"
      }
    }
  }
}
    
{
  // Allow anyone to read data, but only authenticated content owners can
  // make changes to their data

  "rules": {
    "some_path/$uid": {
      ".read": true,
      // or ".read": "auth.uid !== null" for only authenticated users
      ".write": "auth.uid === $uid"
    }
  }
}
    
// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/file.txt"
    match /user/{userId}/{fileName} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/file.txt"
    match /user/{userId}/{fileName} {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}
  

पुष्टि किए गए किसी भी उपयोगकर्ता के लिए ऐक्सेस

कभी-कभी, Rules यह जांच करता है कि कोई उपयोगकर्ता लॉग इन है या नहीं, लेकिन पुष्टि करने के आधार पर ऐक्सेस पर पाबंदी नहीं लगाता. अगर आपके किसी नियम में auth != null शामिल है, तो पुष्टि करें कि आपको लॉग इन किए हुए किसी भी उपयोगकर्ता को डेटा का ऐक्सेस देना है.

इसका सुझाव नहीं दिया जाता: लॉग इन करने वाले किसी भी उपयोगकर्ता के पास आपके पूरे डेटाबेस को पढ़ने और उसमें बदलाव करने का ऐक्सेस होता है.
service cloud.firestore {
  match /databases/{database}/documents {
    match /some_collection/{document} {
      allow read, write: if request.auth.uid != null;
    }
  }
}
{
  "rules": {
    ".read": "auth.uid !== null",
    ".write": "auth.uid !== null"
  }
}
// Only authenticated users can read or write to the bucket
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}
समाधान: सुरक्षा से जुड़ी शर्तों का इस्तेमाल करके, ऐक्सेस को सीमित करें.

पुष्टि करने के दौरान, आपके पास पुष्टि करने वाली किसी प्रॉपर्टी का इस्तेमाल करने का विकल्प भी होता है. इससे, किसी खास डेटा सेट के लिए, कुछ उपयोगकर्ताओं के ऐक्सेस पर और पाबंदी लगाई जा सकती है. पुष्टि करने की अलग-अलग प्रॉपर्टी के बारे में ज़्यादा जानें.

service cloud.firestore {
  match /databases/{database}/documents {
    // Assign roles to all users and refine access based on user roles
    match /some_collection/{document} {
     allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Reader"
     allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == "Writer"

     // Note: Checking for roles in your database using `get` (as in the code
     // above) or `exists` carry standard charges for read operations.
    }
  }
}
// Give each user in your database a particular attribute
// and set it to true/false
// Then, use that attribute to grant access to subsets of data
// For example, an "administrator" attribute set
// to "true" grants write access to data

service cloud.firestore {
  match /databases/{database}/documents {
    match /some_collection/{document} {
      allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
      allow read: true;
    }
  }
}
  
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow public read access, but only content owners can write
    match /some_collection/{document} {
      allow read: if true
      allow write: if request.auth.uid == request.resource.data.author_uid
    }
  }
}
  
{
  "rules": {
    "some_path": {
      "$uid": {
        // Allow only authenticated content owners access to their data
        ".read": "auth.uid === $uid",
        ".write": "auth.uid === $uid"
      }
    }
  }
}
    
{
  "rules": {
    "some_path/$uid": {
      ".write": "auth.uid === $uid",
      // Create a "public" subpath in your dataset
      "public": {
        ".read": true
        // or ".read": "auth.uid !== null"
      },
      // Create a "private" subpath in your dataset
      "private": {
        ".read": "auth.uid === $uid"
      }
    }
  }
}
    
{
  // Allow anyone to read data, but only authenticated content owners can
  // make changes to their data

  "rules": {
    "some_path/$uid": {
      ".read": true,
      // or ".read": "auth.uid !== null" for only authenticated users
      ".write": "auth.uid === $uid"
    }
  }
}
    
// Allow reads if the group ID in your token matches the file metadata `owner` property
// Allow writes if the group ID is in the user's custom token
match /files/{groupId}/{fileName} {
  allow read: if resource.metadata.owner == request.auth.token.groupId;
  allow write: if request.auth.token.groupId == groupId;
}
// Grants a user access to a node matching their user ID
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/file.txt"
    match /user/{userId}/{fileName} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
service firebase.storage {
  match /b/{bucket}/o {
    // Files look like: "user/<UID>/file.txt"
    match /user/{userId}/{fileName} {
      allow read;
      allow write: if request.auth.uid == userId;
    }
  }
}
  

(Realtime Database) गलत तरीके से इनहेरिट किए गए नियम

Realtime Database Security Rules कैस्केड, जिसमें पैरंट पाथ के नियम, चाइल्ड नोड के नियमों को बदलते हैं. चाइल्ड नोड पर नियम लिखते समय, याद रखें कि यह सिर्फ़ अतिरिक्त विशेषाधिकार दे सकता है. अपने डेटाबेस के किसी गहरे पाथ में मौजूद डेटा के ऐक्सेस को बेहतर नहीं बनाया जा सकता या उसे रद्द नहीं किया जा सकता.

इसका सुझाव नहीं दिया जाता: चाइल्ड पाथ पर नियमों को बेहतर बनाना
{
  "rules": {
     "foo": {
        // allows read to /foo/*
        ".read": "data.child('baz').val() === true",
        "bar": {
          /* ignored, since read was allowed already */
          ".read": false
        }
     }
  }
}
समाधान: पैरंट पाथ पर ऐसे नियम लिखें जो ज़्यादा बड़े हों. साथ ही, चाइल्ड पाथ पर ज़्यादा खास अधिकार दें अगर आपको डेटा ऐक्सेस करने के लिए ज़्यादा जानकारी की ज़रूरत है, तो अपने नियमों को ज़्यादा जानकारी वाला रखें. Realtime Database Security Rules के कोर सिंटैक्स में, कैस्केडिंग Realtime Database Security Rules के बारे में ज़्यादा जानें.

क्लोज़्ड ऐक्सेस

ऐप्लिकेशन डेवलप करते समय, डेटा को लॉक करके रखने का एक और सामान्य तरीका है. आम तौर पर, इसका मतलब है कि आपने सभी उपयोगकर्ताओं के लिए, पढ़ने और लिखने का ऐक्सेस बंद कर दिया है. ऐसा करने के लिए, यह तरीका अपनाएं:

// Deny read/write access to all users under any conditions
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }
  }
}
{
  "rules": {
    ".read": false,
    ".write": false
  }
}
    
// Access to files through Cloud Storage is completely disallowed.
// Files may still be accessible through App Engine or Google Cloud Storage APIs.

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
  }
}

Firebase Admin SDK टूल और Cloud Functions अब भी आपके डेटाबेस को ऐक्सेस कर सकते हैं. Cloud Firestore या Realtime Database को Firebase Admin SDK के साथ, सिर्फ़ सर्वर के बैकएंड के तौर पर इस्तेमाल करने के लिए, इन नियमों का पालन करें. यह सुरक्षित है, लेकिन आपको यह जांच करनी चाहिए कि आपके ऐप्लिकेशन के क्लाइंट, डेटा को सही तरीके से वापस पा सकते हैं या नहीं.

Cloud Firestore Security Rules के बारे में ज़्यादा जानें और Cloud Firestore Security Rules का इस्तेमाल शुरू करें लेख में जानें कि यह कैसे काम करता है.

अपने Cloud Firestore Security Rules की जांच करना

अपने ऐप्लिकेशन के व्यवहार की जांच करने और अपने Cloud Firestore Security Rules कॉन्फ़िगरेशन की पुष्टि करने के लिए, Firebase एमुलेटर का इस्तेमाल करें. कोई भी बदलाव डिप्लॉय करने से पहले, Cloud Firestore एमुलेटर का इस्तेमाल करके, लोकल एनवायरमेंट में यूनिट टेस्ट चलाएं और उन्हें ऑटोमेट करें.

Firebase कंसोल में Firebase Security Rules की पुष्टि तुरंत करने के लिए, Firebase नियम सिम्युलेटर का इस्तेमाल करें.