Skip to content

Support row-based access control in queries using secure keyword #3388

Open

Description

@SandPod

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: int

This restricts access so users can only query rows where author == AuthenticationInfo.userId.

  • UserId is mapped to AuthenticationInfo.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=admin

To 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

Activity

  1. added
    area: authentication
    area: database
    area: models
    enhancement
    on Apr 4, 2025
  2. SandPod commented on Apr 4, 2025

    @SandPod
    ContributorAuthor

    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.

  3. Isakdl commented on May 9, 2025

    @Isakdl
    Collaborator

    A point on the yaml syntax, I don't think it makes sense to make the secure field 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.

    class: Channel
    table: channel
    secure: userId=author, scope=admin
    fields:
      name: String,
      channel: String
      author: int

    And

    class: Channel
    table: channel
    secure: 
      userId: author
      scope: admin
    fields:
      name: String,
      channel: String
      author: int
  4. moved this from Backlog 🗂️ to Next Up 🎯 in Serverpod Frameworkson Sep 4, 2026
  5. moved this from Next Up 🎯 to Planned 🔭 in Serverpod Frameworkson Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: authenticationarea: databasearea: modelsenhancement

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions