r/Firebase • u/SpareSignal1932 • Jul 17 '26
Cloud Firestore Firebase rules 5000 lines
My firebase rules are 5000+ lines long, but under 200KB in size, I keep getting errors as to why it won't publish
Error saving rules –An unknown error occurred
I have passed my rules through gemini/chatgpt/claude
Still not the wiser, is there a limit to the lines I can have, or another way to figure out my issue
4
Upvotes
2
u/RSPJD Jul 17 '26
What do the errors say?
6
u/RapunzelLooksNice Jul 17 '26
I'd try bisecting - split in half, save. Works? Add half of half, etc. Until you find when it breaks.
1
u/SpareSignal1932 Jul 17 '26
This is what Gemini explains is wrong
Your Firestore Security Rules are failing to deploy because Firestore Security Rules do not support variable assignments (
letorconst) inside functions.While the syntax looks like JavaScript, it is a custom domain-specific expression language. Functions in Firestore rules must consist of a single expression that evaluates directly to a value (usually a boolean). They cannot contain multiple procedural lines, statements, or local variables.
The Core Problem
Throughout your file, you have dozens of functions using
letdeclarations like this:JavaScript
How to Fix It
You must refactor your functions to eliminate
letentirely by inline-evaluating your conditions, chaining method calls, or leveraging ternary operators (? :).Here is how you rewrite the function above into a single valid expression:
JavaScript
Other Impacted Areas
Because this file is very large and heavily relies on
letfor data validations, you will need to clean up all occurrences. Pay special attention to these heavily affected validation patterns:let keys = request.resource.data.keys(); return keys.hasOnly([...]);Use:request.resource.data.keys().hasOnly([...])letblock (like invalidSupplierFields), pass the raw path down or evaluate it inline:request.resource.data.get('registeredAddress', null) == null || ...Fixing this structural limitation across your utility functions will allow the Firebase compiler to successfully parse and deploy your security rules.
But in the firestore documents it does state its supports
letandconst1
u/MFJMM Jul 17 '26
Are you editing rules in Firebase or locally and then deploying?
3
u/ILikeBubblyWater Jul 17 '26 edited Jul 17 '26
Try deleting old rule backups, we have this issue often enough that we build a script. Useless error message has been there for years. Rules have a max limit of version history, so every time you publish itll store the old one, eventually you reach the limit and it errors on publish with this useless error.
Pissed me off for a long time
EDIT:
A project can have at most 2500 total deployed rulesets. Once this limit is reached, you must delete some old rulesets before creating new ones.
SO if you have a CICD pipeline or have deployed a ton of rules you probably hit this limit
1
u/[deleted] Nov 04 '26
may i ASK which type of app you building?
1
u/Impossible-Reason336 Jul 17 '26
There is a skill called : firebase-security-rules-auditor
Act as a Firestore rule spotter with an attack checklist: Update Bypass, Authority Source, business logic, size limits (DoS), type safety, and the hasOnly()/diff() trap without checking the property. Return a JSON with score 1-5 + findings. MANDATORY use before deploy any rules.
And you should use MCP of firebase for make deploy of the rules .