सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
एक परीक्षण उपकरण जो Firebase सुरक्षा नियमों के व्यवहार का अनुकरण करता है, जिससे डेवलपर्स को वास्तविक Firebase बैकएंड से जुड़े बिना Firestore और Firebase स्टोरेज इंटरैक्शन का परीक्षण करने में सक्षम करता है। इसका उद्देश्य fake_cloud_firestore और mock_firebase_storage के साथ काम करने के लिए डिज़ाइन किया गया है, जो यूनिट और इंटीग्रेशन टेस्ट के लिए एक स्थिर वातावरण प्रदान करता है। इस पैकेज का उपयोग विकास के दौरान सुरक्षा नियम तर्क को सत्यापित करने में मदद करता है, जो नियंत्रित, स्थानीय सेटअप में वास्तविक नियम लागू करने के अनुकरण के माध्यम से किया जाता है।
उपयोग
Firestore पढ़ने/लिखने की अनुमति का परीक्षण
परीक्षण में सुरक्षित भंडारण प्राप्ति का नकल करना
लाइव फायरबेस के बिना नियम तर्क की जांच करना
CI/CD पाइपलाइन्स के साथ एकीकरण
ऑफलाइन परीक्षण सूट विकसित करना
मुख्य विशेषताएँ
वास्तविक Firebase सुरक्षा नियमों के व्यवहार के मेल खाता है
fake_cloud_firestore और mock_firebase_storage के साथ काम करता है
हल्का और एकीकरण में आसान
सभी प्रमुख प्लेटफॉर्म का समर्थन करता है
सटीक नियम मूल्यांकन प्रदान करता है
इनके लिए उपयुक्त
फ्लटर विकासकर्मी परीक्षण लिखते हैं
उत्पादन में Firebase का उपयोग करने वाली टीमें
टेस्ट-ड्रिवन डेवलपमेंट वर्कफ्लो
आइसोलेटेड टेस्टिंग की आवश्यकता वाले CI परिवेश
विचार योग्य बातें
उत्पादन उपयोग के लिए नहीं बनाया गया है
पूर्ण कार्यक्षमता के लिए सहयोगी पैकेज की आवश्यकता होती है
नियम वाक्य रचना वास्तविक Firebase नियमों के साथ सुसंगत होनी चाहिए
सिमुलेशन तक सीमित — वास्तविक सुरक्षा को बाध्य नहीं करता
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}
Concrete path, access method, eg an update on /databases/some-db/users/abcd,
Variables, eg a Map such as {'request': { 'auth': { 'uid': 'efgh' } } },
the library computes whether the request should be allowed or not.
Usage
import'package:fake_firebase_security_rules/fake_firebase_security_rules.dart';
// https://firebase.google.com/docs/rules/rules-and-auth#leverage_user_information_in_rulesfinal authUidDescription = '''
service cloud.firestore {
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user
// document. The wildcard expression {userId} makes the userId variable
// available in rules.
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
}''';
voidmain(List<String> args) async {
final securityRules = FakeFirebaseSecurityRules(authUidDescription);
final uid = 'a57293b';
final variables = {
'request': {
'auth': {'uid': uid}
}
};
// Prints out `true`.print(securityRules.isAllowed(
'databases/some-database/documents/users/$uid', Method.read,
variables: variables));
// Prints out `false`.print(securityRules.isAllowed(
'databases/some-database/documents/users/someone-elses-id', Method.read,
variables: variables));
// Prints out `false`.print(securityRules.isAllowed(
'databases/some-database/documents/somewhere-else/someone-doc',
Method.read,
variables: variables));
}
See the Unit tests for more advanced examples of usage.
Features
Supports:
rules declarations used in Firestore and Firebase Storage.
recursive match definitions.
exhaustive path matching with path variables and wildcards, eg /users/{userId}/{documents=**}.
request.auth object populated with uid, custom claims...
standard CEL types and methods, eg bool, int, double, String, Map, List, String.match(regexp), a in list... See cel-dart's supported features for an exhaustive list.
Missing:
timestamps.
durations.
custom functions.
resource object.
request.resource object.
exists(), get() functions.
Implementation details
Differences between Firebase Rules CEL and standard CEL
Timestamps. Although not implemented in this project yet, Firebase Rules uses its own Timestamp implementation while CEL uses google.protobuf.Timestamp (spec).
How the project works
FirestoreRules.g4 describes a grammar that parses security rules into Matches. A Match contains a path, made up of segments. Some segments might be variables or wildcards. The expression at the right of allow statements is in Common Expression Language (CEL). CEL is a language used by many security projects. See the CEL specs.
Upon initialization, FakeFirebaseSecurityRules parses security rules into a tree of Matches.
When a request comes in, FakeFirebaseSecurityRules finds the first Match and allow statement that corresponds to the given path and whose CEL expression evaluates to true.
Working on the grammar
The fastest way to get your environment up and running is to create a Codespace on the repository, then pip install antlr4-tools. Once this is done, you can run antlr4-parse to try out the rules against some inputs and antlr4 to regenerate the Parser in lib/src/parser/gen.
cd grammar
# Test the grammar on one file.
antlr4-parse FirestoreRules.g4 rulesDefinition -tree -tokens 9_custom_claims.txt
# Test the grammar on all sample files.
./checkAllDescriptions.sh
Generating the parser
If you modify FirestoreRules.g4, you may want to regenerate the parser: