What is a custom function in Firebase security rules?
A custom function is a reusable block of code inside Firebase security rules that helps simplify and organize rule logic by grouping repeated checks or calculations.
Click to reveal answer
beginner
How do you define a custom function in Firebase rules?
You define a custom function using the <code>function</code> keyword followed by the function name and parameters inside parentheses and curly braces. For example: <br><code>function isOwner(userId) { return userId == request.auth.uid; }</code>
Click to reveal answer
beginner
Why use custom functions in Firebase rules?
Custom functions make rules easier to read, maintain, and avoid repeating the same logic multiple times. They help keep rules clean and reduce errors.
Click to reveal answer
intermediate
Can custom functions in Firebase rules access request data?
Yes, custom functions can access request and resource objects to check authentication, data values, or other request details needed for security decisions.
Click to reveal answer
intermediate
Example: What does this custom function do?<br><code>function isAdmin() { return request.auth.token.admin == true; }</code>
This function checks if the authenticated user has an admin flag set to true in their authentication token, meaning they have admin privileges.
Click to reveal answer
What keyword is used to define a custom function in Firebase security rules?
Adef
Bfunction
Cfunc
Dlambda
✗ Incorrect
Firebase security rules use the keyword function to define custom functions.
Can custom functions in Firebase rules help reduce repeated code?
AYes, they make rules cleaner and reusable
BNo, they increase code duplication
COnly if you use external libraries
DOnly for read operations
✗ Incorrect
Custom functions allow you to reuse logic, reducing repetition and making rules easier to maintain.
Which object can custom functions access to check the user's identity?
Astorage.bucket
Bresource.data
Cdatabase.ref
Drequest.auth
✗ Incorrect
The request.auth object contains authentication info about the user making the request.
What is a good reason to use custom functions in Firebase rules?
ATo store user data
BTo slow down rule evaluation
CTo make rules easier to read and maintain
DTo bypass security checks
✗ Incorrect
Custom functions improve readability and maintainability of security rules.
Which of these is a valid custom function call in Firebase rules?
AisOwner(resource.data.userId)
Bcall isOwner(resource.data.userId)
Cinvoke isOwner(resource.data.userId)
Drun isOwner(resource.data.userId)
✗ Incorrect
Custom functions are called by their name followed by parentheses with arguments, like isOwner(resource.data.userId).
Explain what custom functions are in Firebase security rules and why they are useful.
Think about how you avoid repeating yourself in code.
You got /4 concepts.
Describe how you would write a custom function to check if the current user is the owner of a data item.
Focus on comparing user IDs.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using custom functions in Firebase security rules?
easy
A. They make rules easier to read and reuse common checks
B. They automatically fix security vulnerabilities
C. They increase the speed of database queries
D. They replace the need for authentication
Solution
Step 1: Understand the purpose of custom functions
Custom functions in Firebase rules are designed to simplify and organize security logic by reusing common checks.
Step 2: Compare options to the purpose
Only They make rules easier to read and reuse common checks correctly states that custom functions make rules easier to read and reuse common checks. Other options describe unrelated or incorrect benefits.
Final Answer:
They make rules easier to read and reuse common checks -> Option A
Quick Check:
Custom functions improve readability and reuse = A [OK]
Hint: Custom functions simplify and reuse checks in rules [OK]
Common Mistakes:
Thinking custom functions speed up queries
Believing they fix security automatically
Confusing them with authentication features
2. Which of the following is the correct syntax to define a custom function named isOwner in Firebase rules?
easy
A. function isOwner() = request.auth.uid == resource.data.owner;
B. isOwner() => request.auth.uid == resource.data.owner;
C. function isOwner() { return request.auth.uid == resource.data.owner; }
D. isOwner() { return request.auth.uid == resource.data.owner; }
Solution
Step 1: Recall Firebase rules function syntax
Firebase rules use the syntax: function name() { return expression; } to define custom functions.
Step 2: Identify the correct syntax
function isOwner() { return request.auth.uid == resource.data.owner; } matches the correct syntax. isOwner() { return request.auth.uid == resource.data.owner; } omits the required function keyword. Options B and C use invalid syntax.
Final Answer:
function isOwner() { return request.auth.uid == resource.data.owner; } -> Option C
Quick Check:
Firebase custom function syntax uses function name() { return ... } = A [OK]
Hint: Firebase rules functions use function name() { return ... } syntax [OK]
Common Mistakes:
Omitting the 'function' keyword
Using arrow functions which are not supported
Using '=' instead of function body block
3. Given the following Firebase rule snippet:
function isAdmin() { return request.auth.token.admin == true; }
allow write: if isAdmin();
What happens if request.auth.token.admin is false?
medium
A. Write access is denied
B. Write access is granted
C. Rule throws a syntax error
D. Write access is granted only if user is authenticated
Solution
Step 1: Understand the function logic
The function isAdmin() returns true only if request.auth.token.admin is true.
Step 2: Evaluate the rule condition
The write permission is allowed only if isAdmin() returns true. If request.auth.token.admin is false, the function returns false, so write is denied.
Final Answer:
Write access is denied -> Option A
Quick Check:
isAdmin false means no write access = A [OK]
Hint: Function returns false blocks access; true allows it [OK]
Common Mistakes:
Assuming false admin still grants access
Thinking rule throws error on false
Confusing authentication with admin check
4. Identify the error in this Firebase rule custom function:
function isOwner() {
request.auth.uid == resource.data.owner;
}
allow read: if isOwner();
medium
A. Incorrect function name syntax
B. Invalid comparison operator
C. Using 'allow' outside match block
D. Missing return statement inside the function
Solution
Step 1: Check function body for return
The function compares values but does not return the result, so it returns null by default.
Step 2: Understand impact on rule
Without a return, isOwner() returns null (falsey), so the rule will deny access unexpectedly.
Final Answer:
Missing return statement inside the function -> Option D
Quick Check:
Functions must return values explicitly = B [OK]
Hint: Always return a value in custom functions [OK]
Common Mistakes:
Forgetting to add 'return' before expression
Assuming last line auto-returns value
Misplacing 'allow' statements
5. You want to create a reusable custom function canEdit() that allows editing if the user is either the owner or has an admin role. Which of the following is the correct way to write this function in Firebase rules?
hard
A. canEdit() { if (request.auth.uid == resource.data.owner) return true; else return false; }
B. function canEdit() { return request.auth.uid == resource.data.owner || request.auth.token.admin == true; }