Repository navigation
fix(crud): apply read permissions on CRUD INDEX routes - #47
Merged
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
CRUDBlueprintenforcescan_read()onGET /x/<id>(viaBasePermsModel.get_by) but not onGET /x/. Every row matching the filters was listed for any authenticated user (or anonymous on a public INDEX). Reproduced downstream: aUSER-role account lists an admin-ownedVoyage, 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 toNone.UserOwnershipMixin(simple ownership only; delegated mode or an overridden_can_readfalls back),AbstractUser(id == user.id),Domain(sa.true()).index_get: ANDs the criterion onto the query before counting, soX-Paginationtotals and page boundaries stay exact. Models without a criterion are filtered per row withcan_read()before pagination (correct, loads the whole filtered set). Models with anis_admincolumn are checked per row for admins too, matching_check_admin_bypass.INFOlog per model on the per-row fallback at route registration.bypass_perms(), no-request-context and admin callers (except onUserRole) list unfiltered as before. PlainBaseModelblueprints are untouched.Tests
tests/integration/test_index_perms.py: SQL path (owner A/B/admin/anonymous), per-row fallback with row-dependent readability andpage_size=1pagination,bypass_perms(), nested path-filtered list,is_admintargets for admins,_can_readoverride 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.16should upgrade deliberately. CHANGELOG entry under[Unreleased]; version bump viascripts/bump_version.sh minoronmain.