Registry / gcp / firebase-rules-parser

firebase-rules-parser

JSON →
library2.0.1jsnpmunverified

The `firebase-rules-parser` library provides a parser and emulator for Firebase security rules, allowing developers to programmatically load and test Firebase rule files. Its primary use case is facilitating unit testing for Firebase projects, particularly as a companion to `ts-mock-firebase`. The current stable version is 2.0.1, released in May 2019. While it aims to support nearly all Firebase rules language functionality, it explicitly notes limitations with duration, latlong, and timestamp functions. The project's release cadence appears to be slow or inactive since 2019, and it differentiates itself by offering an off-platform rule emulation capability without requiring a live Firebase instance, making it suitable for local development and CI/CD pipelines. It ships with TypeScript types, enhancing developer experience for TypeScript users.

npm install firebase-rules-parser
INSTALL
IMPORT
SIG · FIREBASE-RULES-PAR
F
firebase-rules-parser
gcpjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createFirebaseRulesIntepreter
import createFirebaseRulesIntepreter from 'firebase-rules-parser';
const createFirebaseRulesIntepreter = require('firebase-rules-parser').createFirebaseRulesIntepreter;
This is the default export for the interpreter factory. The CommonJS `require` pattern for default exports is often incorrect and might return `undefined` or the whole module object.
createFirebaseRulesContext
import { createFirebaseRulesContext } from 'firebase-rules-parser';
import createFirebaseRulesContext from 'firebase-rules-parser/createFirebaseRulesContext';
This is a named export. Incorrectly importing it as a default export or from a deep path is a common mistake.
FirebaseRulesIntepreter
import type { FirebaseRulesIntepreter } from 'firebase-rules-parser';
import { FirebaseRulesIntepreter } from 'firebase-rules-parser';
This is the type for the interpreter instance, not a runtime value. Use `import type` to avoid bundling issues and clearly indicate its type-only nature. Before v2.0.0, it was named `FirebaseRulesIntepreterFacade`.

Demonstrates initializing the Firebase rules parser with a rules string and then using it to check read and write access for different paths and authentication contexts.

import createFirebaseRulesIntepreter, { createFirebaseRulesContext } from 'firebase-rules-parser'; const rulesSource = ` service cloud.firestore { match /databases/{database}/documents { match /users/{userId} { allow read: if request.auth.uid == userId; allow write: if request.auth.uid == userId && resource.data.name is string; } match /public/{documentId} { allow read: if true; } } } `; // Create an instance of the interpreter const rules = createFirebaseRulesIntepreter(); // Load your rules source string rules.init(rulesSource); // Create a context object for the access check const context = createFirebaseRulesContext({ auth: { uid: 'testUserId123', email: 'test@example.com' }, resource: { id: 'testUserId123', data: { name: 'John Doe', value: 123 } }, // Define triggers for `exists()` and `get()` calls within rules onExistsCall: (path) => { return path === '/databases/DEFAULT/documents/users/testUserId123'; }, onGetCall: (path) => { if (path === '/databases/DEFAULT/documents/users/testUserId123') { return { uid: 'testUserId123', name: 'Test User' }; } return null; } }); // Check access for a specific path and operation const hasReadAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', context); console.log('Read access:', hasReadAccess.read); // Should be true const hasWriteAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', context); console.log('Write access:', hasWriteAccess.write); // Should be true const noAccessContext = createFirebaseRulesContext({ auth: { uid: 'anotherUser' }, resource: { id: 'testUserId123', data: { name: 'Another Name' } } }); const noReadAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', noAccessContext); console.log('No read access:', noReadAccess.read); // Should be false or undefined const publicReadAccess = rules.hasAccess('/databases/DEFAULT/documents/public/someDoc', noAccessContext); console.log('Public read access:', publicReadAccess.read); // Should be true
Debug
Known issues
breakingThe `FirebaseRulesIntepreterFacade` class was renamed to `FirebaseRulesIntepreter`.
fix
Update all references from `FirebaseRulesIntepreterFacade` to `FirebaseRulesIntepreter`.
affects: >=2.0.0
gotchaThe `antlr4` dependency was moved from a direct dependency to a peer dependency. Consumers of the library are now responsible for installing `antlr4` themselves.
fix
Ensure `antlr4` is installed in your project: `npm install antlr4` or `yarn add antlr4`.
affects: >=1.0.2
gotchaThis library is based on reverse-engineering Firebase rules behavior. There might be subtle differences between its emulation and the actual Firebase rules engine, especially with less common functions (e.g., duration, latlong, timestamp) or newer Firebase features.
fix
Always cross-reference critical rule logic with actual Firebase behavior or official documentation. Report discrepancies to the library maintainers if significant.
affects: *
gotchaThe project appears to be minimally maintained since its last release in 2019. It may not support newer Firebase rules features or address recent security updates to the Firebase platform itself.
fix
Evaluate against current Firebase security rules documentation. Consider contributing to the project or forking it if specific new features are required.
affects: >=2.0.1
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'init')
Attempting to call `init` on an undefined interpreter instance, likely due to incorrect default import syntax for `createFirebaseRulesIntepreter` with CommonJS.
fix
Use `const createFirebaseRulesIntepreter = require('firebase-rules-parser').default;` for CommonJS, or preferably use ES module imports `import createFirebaseRulesIntepreter from 'firebase-rules-parser';`.
Error: Cannot find module 'antlr4'
The `antlr4` library, a peer dependency, is not installed in the project.
fix
Install `antlr4` as a direct dependency: `npm install antlr4` or `yarn add antlr4`.
Property 'FirebaseRulesIntepreterFacade' does not exist on type 'typeof import("firebase-rules-parser")'.
Attempting to reference the old class name `FirebaseRulesIntepreterFacade` after upgrading to v2.0.0 or later.
fix
Rename `FirebaseRulesIntepreterFacade` to `FirebaseRulesIntepreter` in your code.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
antlr4requiredCore parsing engine, moved to peer dependency in v1.0.2 to allow consumers to manage its version.
Agent activity
35 hits · last 30 days
node
28
OpenAI (training)
1
Resources
firebase-rules-parser — npm install firebase-rules-parser · libregistry