Storage policies for access control in Supabase - Time & Space Complexity
When using storage policies for access control, it is important to understand how the number of checks grows as more files or users are involved.
We want to know how the system handles access decisions as the data size increases.
Analyze the time complexity of checking access with storage policies.
// Example: Check if a user can read a file
const { data, error } = await supabase
.storage
.from('documents')
.download('file-id');
// Storage policy enforces user_id match for access
This sequence tries to download a file only if the storage policy allows the current user to access it.
Look at what happens repeatedly when many files or users are involved.
- Primary operation: Access check against storage policy for each file request.
- How many times: Once per file access attempt.
Each file access requires one policy check. If you access more files, the checks increase linearly.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 access checks |
| 100 | 100 access checks |
| 1000 | 1000 access checks |
Pattern observation: The number of access checks grows directly with the number of file accesses.
Time Complexity: O(n)
This means the time to check access grows in a straight line as you try to access more files.
[X] Wrong: "Access checks happen once for all files together, so time stays the same no matter how many files."
[OK] Correct: Each file access triggers its own policy check, so more files mean more checks and more time.
Understanding how access control scales helps you design systems that stay fast and secure as they grow.
"What if storage policies cached user permissions? How would that affect the time complexity of access checks?"
Practice
Solution
Step 1: Understand storage policies
Storage policies define rules about who can access or change files.Step 2: Identify the main goal
The main goal is to control access like reading, uploading, or deleting files to keep data safe.Final Answer:
To control who can read, upload, or delete files -> Option CQuick Check:
Storage policies = Access control [OK]
- Thinking policies increase storage size
- Confusing policies with backup features
- Believing policies speed up uploads
Solution
Step 1: Recall Supabase policy syntax
Supabase uses SQL-like expressions with functions like auth.uid() to check user identity.Step 2: Identify correct condition for authenticated users
auth.uid() returns user ID if logged in, so checking auth.uid() != null allows only authenticated users.Final Answer:
using (auth.uid() != null) -> Option BQuick Check:
auth.uid() != null = authenticated user check [OK]
- Using incorrect property names like user.is_authenticated
- Writing non-SQL syntax like permit or allow keywords
- Checking auth.id instead of auth.uid()
using (bucket_id = 'public' OR auth.uid() = owner_id)
What does this policy allow?
Solution
Step 1: Analyze the condition
The condition allows access if bucket_id equals 'public' OR if the user's ID matches owner_id.Step 2: Interpret the meaning
This means anyone can access files in the 'public' bucket, and owners can access their own files in any bucket.Final Answer:
Anyone can access files in the 'public' bucket or owners can access their files -> Option AQuick Check:
bucket_id='public' OR auth.uid()=owner_id = public or owner access [OK]
- Thinking only owners have access
- Assuming bucket_id restricts all access
- Ignoring the OR condition
using (auth.uid() = owner_id AND bucket_id = 'private')
But users report they cannot read their private files. What is the likely problem?
Solution
Step 1: Understand the AND condition
AND means both auth.uid() = owner_id and bucket_id = 'private' must be true for access.Step 2: Identify why access fails
If bucket_id is not exactly 'private' for some files, even owners cannot access them, causing the problem.Final Answer:
The policy blocks access because AND requires both conditions, but bucket_id might differ -> Option AQuick Check:
AND requires both true, bucket_id mismatch blocks access [OK]
- Thinking auth.uid() is invalid
- Assuming owner_id is wrong field
- Believing AND is not allowed in policies
Solution
Step 1: Define the requirements
Users can delete files only if they own them and the file is not in the 'public' bucket.Step 2: Match the condition
auth.uid() = owner_id ensures ownership; bucket_id != 'public' excludes the public bucket.Final Answer:
using (auth.uid() = owner_id AND bucket_id != 'public') -> Option DQuick Check:
Owner AND not public bucket = correct delete policy [OK]
- Using OR instead of AND allowing wrong deletes
- Checking for equality with 'public' instead of exclusion
- Mixing up != and = operators
Ready to prove it?
Pass the test and this pattern moves unknown
