搜尋套件、儲存庫與 APP 分類

套件詳情v0.6.0

fake_firebase_security_rules

Firebase 安全規則的模擬。供 fake_cloud_firestore 和 mock_firebase_storage 使用。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
36萬
喜歡數
1
Pub 分數
140

安裝套件

flutter pub add fake_firebase_security_rules

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個測試工具,可模擬 Firebase 安全規則行為,讓開發人員能在不連接真實 Firebase 後端的情況下測試 Firestore 和 Firebase 儲存互動。它設計用於與 fake_cloud_firestore 和 mock_firebase_storage 搭配使用,為單元測試和整合測試提供一致的環境。此套件透過在受控的本機設定中模擬真實規則執行,協助開發過程中驗證安全規則邏輯。

適用情境

  • 測試 Firestore 讀寫權限
  • 在測試中模擬安全儲存存取
  • 在不使用即時 Firebase 的情況下驗證規則邏輯
  • 與 CI/CD 流水線整合
  • 開發離線測試套件

主要特色

  • 符合真實的 Firebase 安全規則行為
  • 與 fake_cloud_firestore 和 mock_firebase_storage 相容
  • 輕量且易於整合
  • 支援所有主要平台
  • 提供精確的規則評估

適合對象

  • Flutter 開發人員撰寫測試
  • 在生產環境中使用 Firebase 的團隊
  • 測試驅動開發工作流程
  • 需要隔離測試的 CI 環境

使用建議

  • 不適用於生產環境
  • 需要搭配套件以獲得完整功能
  • 規則語法必須與真實的 Firebase 規則相容
  • 僅限模擬——不強制執行實際安全性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

fake_firebase_security_rules

pub package Tests

This project simulates Firebase Security Rules. It is meant to be used by Fake Cloud Firestore and Firebase Storage Mocks. Given these request inputs:

  • Firebase security rules such as:
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_rules
final 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;
    }
  }
}''';

void main(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.

  1. Upon initialization, FakeFirebaseSecurityRules parses security rules into a tree of Matches.
  2. 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:

cd grammar
./regenerateParser.sh

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案