Repository navigation
Description
Problem to Solve
There is currently no built-in mechanism in Serverpod to restrict access to specific database rows based on the authenticated user. This limits the framework’s ability to enforce fine-grained access control at the data layer, requiring developers to implement such logic manually in endpoints or business logic.
Proposal
Introduce support for row-based access control using an enhanced secure keyword in the model definition.
Rows can be protected based on a match between the AuthenticationInfo.userId and a field in the model. The secure keyword should support a list of conditions to allow more flexibility, such as future support for scopes.
The proposal focuses on matching the AuthenticationInfo.userId as a starting point, but long term, all fields of the AuthenticationInfo object should be able to be matched.
Example:
class: Channel
table: channel
secure: userId=author
fields:
name: String,
channel: String
author: intThis restricts access so users can only query rows where author == AuthenticationInfo.userId.
UserIdis mapped toAuthenticationInfo.userId.
Use Case
This feature enables data privacy and ownership enforcement within shared tables, such as:
- Limiting access to chat messages or channels based on authorship
- Restricting visibility of user-specific logs, preferences, or submissions
- Enforcing user-based access in shared multi-tenant tables
Alternatives
- Manually filtering results in endpoint logic (hard to maintain)
- Duplicating tables per user or tenant (inefficient and inflexible)
- Managing raw SQL with PostgreSQL row-level security externally (not integrated)
Additional context
This proposal aligns with common multi-tenant access patterns and would improve security guarantees by enforcing restrictions at the query layer.
Support for lists in the secure keyword opens the door to future expansion, such as evaluating access scopes (e.g. roles, permissions). For example:
secure: userId=author, scope=adminTo integrate with PostgreSQL’s native row-level security (RLS), session variables can be set per transaction using SET LOCAL:
SET LOCAL serverpod.user_id = '123';
SET LOCAL serverpod.scopes = 'admin,read_only';These can be referenced within PostgreSQL policies like:
CREATE POLICY user_owns_row ON channel
USING (author::text = current_setting('serverpod.user_id', true));Serverpod should set these automatically based on the authenticated user's ID and scopes to support seamless RLS integration with minimal configuration.
The policies should be generated as part of database migrations based on what is specified in the yaml file.
How experienced are you with this library?
None
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsPlanned 🔭
Activity
SandPod commentedon Apr 4, 2025
This needs to be further investigated before implementation to ensure a secure transactional model.
Further consideration should be taken around the DB admin view and the possibility of managing all entries in the database.
specified policy rules#496 as a duplicate of this issueIsakdl commentedon May 9, 2025
A point on the yaml syntax, I don't think it makes sense to make the
securefield to an array, the example essentially has an array with Maps that contains a single key/value pair. The array can be ditched and you would still have the same flexibility. We already have support for comma seperated key/value pairs too!E.g.
And