Skip to content

fix(crud): apply read permissions on CRUD INDEX routes - #47

Merged
zedrdave merged 3 commits into
mainfrom
fix/index-read-perms
Nov 21, 2026
Merged

zedrdave merged 3 commits into
mainfrom
fix/index-read-perms

Conversation

@zedrdave

Copy link
Copy Markdown
Member

Problem

CRUDBlueprint enforces can_read() on GET /x/<id> (via BasePermsModel.get_by) but not on GET /x/. Every row matching the filters was listed for any authenticated user (or anonymous on a public INDEX). Reproduced downstream: a USER-role account lists an admin-owned Voyage, then gets 403 opening it.

Fix

  • BasePermsModel: new classmethods _user_bypasses_perms(user) (user-side half of _check_admin_bypass, behaviour of the latter unchanged) and _readable_filter(user) -> ColumnElement | None, a SQL criterion hook defaulting to None.
  • Built-in filters: UserOwnershipMixin (simple ownership only; delegated mode or an overridden _can_read falls back), AbstractUser (id == user.id), Domain (sa.true()).
  • index_get: ANDs the criterion onto the query before counting, so X-Pagination totals and page boundaries stay exact. Models without a criterion are filtered per row with can_read() before pagination (correct, loads the whole filtered set). Models with an is_admin column are checked per row for admins too, matching _check_admin_bypass.
  • One INFO log per model on the per-row fallback at route registration.

bypass_perms(), no-request-context and admin callers (except on UserRole) list unfiltered as before. Plain BaseModel blueprints are untouched.

Tests

tests/integration/test_index_perms.py: SQL path (owner A/B/admin/anonymous), per-row fallback with row-dependent readability and page_size=1 pagination, bypass_perms(), nested path-filtered list, is_admin targets for admins, _can_read override on an ownership model. Full suite, ruff and pyright pass.

Release

Intended as 0.17.0 (minor): lists shrink for apps that have not defined _readable_filter, so consumers on ^0.16 should upgrade deliberately. CHANGELOG entry under [Unreleased]; version bump via scripts/bump_version.sh minor on main.

GET /x/ listed every row matching the filters, while GET /x/<id> refused rows
whose can_read() is false. INDEX now applies the same rule.

- BasePermsModel: new classmethods _user_bypasses_perms (user-side half of
  _check_admin_bypass) and _readable_filter(user), a SQL criterion hook.
- UserOwnershipMixin, AbstractUser and Domain ship a _readable_filter.
- index_get ANDs the criterion onto the query; models without one are
  filtered per row with can_read() before pagination, so item_count and
  page boundaries stay exact. Models with an is_admin column are checked
  per row for admins too, matching _check_admin_bypass.
- One INFO log per model on the per-row fallback at registration.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The inherited filter bypasses custom _can_read overrides, potentially hiding readable profiles or exposing the caller’s profile despite an explicit denial.

1 open issue
What changed in this PR

This PR applies model read permissions to CRUD INDEX routes, aligning list visibility with individual-resource access while keeping pagination totals accurate.

Changes:

  • Adds SQL permission filters with a per-row fallback for custom read rules.
  • Integrates permission filtering before counting and pagination, preserving bypass behavior.
  • Adds integration coverage and a security changelog entry.
File Description
tests/​integration/​test_index_perms.py Tests filtering, pagination, bypasses, nested routes, and permission overrides.
flask_more_smorest/​perms/​models/​role.py Adds an unrestricted SQL read filter for Domain.
flask_more_smorest/​perms/​models/​abstract_user.py Adds a self-profile SQL read filter.
flask_more_smorest/​perms/​model_mixins.py Adds ownership filtering with fallback for delegated or overridden rules.
flask_more_smorest/​perms/​base_perms_model.py Introduces shared bypass logic and the SQL read-filter hook.
flask_more_smorest/​crud/​crud_blueprint.py Enforces INDEX permissions before pagination and logs fallback use.
CHANGELOG.md Documents the list-permission security change.

Review effort: Balanced

…dden

Custom user models that override _can_read inherited the self-only SQL
filter, which could hide readable profiles or list the caller's own row
against the override. AbstractUser now returns None (per-row fallback)
unless _can_read is the inherited one; the default User, whose _can_read
is the same self-only rule, keeps the SQL path with its own filter.
…ridden

Same guard as UserOwnershipMixin and AbstractUser: a Domain subclass that
tightens _can_read is checked per row instead of inheriting sa.true().
@zedrdave
zedrdave merged commit 223e4bd into main Nov 21, 2026
11 checks passed
@zedrdave
zedrdave deleted the fix/index-read-perms branch November 21, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants