Repository navigation
feat: apply resource roles to sub-paths - #261
Conversation
Roles of resource-scoped role definitions (three fields, e.g. `g2 = _, _, _`) granted on a resource path now also apply to its sub-paths, split on `/`: owning `/services/ABC` covers `/services/ABC/permissions`, but not `/services/ABCD`. What a role may do there is still decided by `p` rules. Trailing slashes in stored paths are ignored; roles on `/` only apply to `/`. Enabled by default via `access_control.resource_roles_cover_subpaths`; disable it to match exact resource paths only. Refs #260 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Refs #260 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideResource-scoped roles now apply to path-boundary-safe descendants by default, with a global configuration switch for exact matching; policy rules remain authoritative for permitted actions, and documentation and enforcement tests cover the new behavior. Sequence diagram for resource role sub-path enforcementsequenceDiagram
participant Request
participant Enforcer
participant Casbin
participant Policy
Request->>Enforcer: enforce_access_control(request)
Enforcer->>Casbin: enforce(subject, path, action)
Casbin->>Casbin: _is_within_path(request_path, resource_path)
Casbin->>Policy: evaluate p rule for role and path
Policy-->>Casbin: allow or deny action
Casbin-->>Enforcer: enforcement result
Enforcer-->>Request: permit or reject
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #261 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 1087 1099 +12
=========================================
+ Hits 1087 1099 +12
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
The focused implementation matches the requirements, includes regression coverage, and has no identified blocking issues.
No open issues 🎉
What changed in this PR
Extends FOCA’s resource-scoped roles to sub-paths while keeping policy rules responsible for allowed actions, addressing #260.
Changes:
- Adds path-boundary matching with trailing-slash and root-path handling.
- Adds an option to restore exact resource matching.
- Documents the behavior and adds regression coverage.
| File | Description |
|---|---|
| tests/security/access_control/test_enforcement.py | Tests sub-path access, boundaries, and exact-match mode. |
| foca/security/access_control/enforcement.py | Registers resource-domain path matching. |
| foca/security/access_control/api/resource_role_model.conf | Adds explanatory comments and a permissions-policy example. |
| foca/models/config.py | Defines and documents the new configuration option. |
| docs/access_control/README.md | Explains sub-path behavior and configuration. |
Review effort: Balanced
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. With the default enabled, a role granted on a resource also satisfies role checks for matching sub-paths; an existing policy rule can therefore authorize an action there that was previously denied. Reverting stops future access, but cannot undo data read or changes made while the broader authorization behavior was live.
Closes #260
Summary
Resource-scoped roles (from #259) now also apply to sub-paths of the resource they were granted on, so endpoints below a resource (e.g. owner-managed access at
/services/ABC/permissions) can be enforced by config instead of being opted out and checked in app code.get_enforcer()registers a Casbin domain matching function for every role definition with three fields (e.g.g2 = _, _, _). A role granted on/services/ABCapplies to/services/ABCand anything below/services/ABC/, split on/, so/services/ABCDis not covered.prules (e.g.p, owner, /services/:id/permissions, (GET)|(PUT)). Maintainers get no extra access unless a rule says so./only applies to/itself (rather than to every path).access_control.resource_roles_cover_subpaths(defaulttrue); set it tofalsefor exact matching only.docs/access_control/README.mdupdated with a sub-path example.Verification
CI: all checks pass: lint, mypy and unit tests on Python 3.9–3.12 × MongoDB 4.4–7.0 (16 jobs, including the MongoDB-backed tests), petstore integration tests, Docker builds (4 jobs), Codecov patch and project, Read the Docs, Sourcery.
Local venv (Python 3.12,
pip install -e .[dev]):flake8: cleanmypy foca: no issuespytest tests/security/access_control/test_enforcement.py tests/api tests/test_foca.py tests/models tests/security/test_auth.py tests/config: 180 passed; coverage ofenforcement.pyandconfig.py100%GET/PUT /items/1/permissions; stored path with trailing slash; no access to/items/10(shared prefix), to other resources' sub-paths, or for maintainers/non-members without aprule; admins unaffected; exact matching withresource_roles_cover_subpaths: false; unit tests for the path matching function, including the root path.check_permissions) not run locally (no MongoDB onlocalhost:12345); they pass in CI.Risks
/services/ABC/versions/1), whereverprules allow that role there. Apps needing strictly separate sub-resource ownership can disable sub-path matching./-segment prefix matching on those values; such apps can disable it.Open questions
🤖 Generated with Claude Code