Skip to content

feat: apply resource roles to sub-paths - #261

Merged
uniqueg merged 2 commits into
devfrom
issue-260-subpath-roles
Oct 17, 2026
Merged

uniqueg merged 2 commits into
devfrom
issue-260-subpath-roles

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

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/ABC applies to /services/ABC and anything below /services/ABC/, split on /, so /services/ABCD is not covered.
  • Sub-path matching only makes a role apply; what it may do there is still decided by p rules (e.g. p, owner, /services/:id/permissions, (GET)|(PUT)). Maintainers get no extra access unless a rule says so.
  • Trailing slashes in stored paths are ignored. A role granted on / only applies to / itself (rather than to every path).
  • New config option access_control.resource_roles_cover_subpaths (default true); set it to false for exact matching only.
  • Reference model comments and docs/access_control/README.md updated 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: clean
  • mypy foca: no issues
  • pytest 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 of enforcement.py and config.py 100%
  • New tests, written first and failing before the change: owner reaches 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 a p rule; admins unaffected; exact matching with resource_roles_cover_subpaths: false; unit tests for the path matching function, including the root path.
  • MongoDB-backed tests (adapter, permission controllers, check_permissions) not run locally (no MongoDB on localhost:12345); they pass in CI.

Risks

  • Changes the behaviour of resource roles from exact to sub-path matching by default. Resource-scoped roles were merged after v0.13.0 and are unreleased, so no released version is affected.
  • Hierarchical: an owner of a resource gets their role on all sub-resources, including ones owned by others (e.g. /services/ABC/versions/1), wherever p rules allow that role there. Apps needing strictly separate sub-resource ownership can disable sub-path matching.
  • Applies to all three-field role definitions in the model. Models using three-field roles for non-path domains (e.g. tenant names) get the same /-segment prefix matching on those values; such apps can disable it.

Open questions

  • Should sub-path matching be configurable per role definition rather than globally? Not needed for current use cases, so kept global.

🤖 Generated with Claude Code

uniqueg and others added 2 commits October 17, 2026 02:54
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]>
@sourcery-ai

sourcery-ai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Resource-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 enforcement

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Extend resource-scoped Casbin roles to match resource sub-paths with boundary-safe path semantics.
  • Add a path matcher that supports exact paths and descendants while excluding shared-prefix siblings.
  • Ignore trailing slashes and treat the root resource as matching only /.
  • Register the matcher for all three-field role assertions when sub-path matching is enabled.
foca/security/access_control/enforcement.py
Add configurable control over resource-role sub-path inheritance.
  • Introduce resource_roles_cover_subpaths, enabled by default.
  • Preserve exact-match behavior when the option is disabled.
foca/models/config.py
foca/security/access_control/enforcement.py
Document sub-path role behavior and policy configuration.
  • Add the option to the access-control configuration example.
  • Document that policy rules still determine allowed actions on sub-paths.
  • Add an owner permissions-endpoint example and clarify trailing-slash, root-path, and exact-match behavior.
docs/access_control/README.md
foca/security/access_control/api/resource_role_model.conf
Add coverage for enforcement and path-matching edge cases.
  • Verify owner access to permitted sub-path operations and deny roles without matching policy rules.
  • Cover trailing slashes, prefix boundaries, unrelated resources, administrators, and root paths.
  • Verify exact matching when sub-path inheritance is disabled.
tests/security/access_control/test_enforcement.py

Assessment against linked issues

Issue Objective Addressed Explanation
#260 Make a resource-scoped role granted on a path apply to that resource’s sub-paths, while retaining path-segment boundaries and leaving permitted actions to the policy rules. ✅
#260 Provide a configuration option that disables sub-path matching and restores exact resource-path matching. ✅
#260 Update the reference model documentation and add tests covering sub-path access, boundaries, policy restrictions, and disabled matching. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (568a58d) to head (9e2cd24).

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     
Flag Coverage Δ
test_unit 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 17, 2026 01:00
Copilot AI balanced review requested due to automatic review settings October 17, 2026 01:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@uniqueg
uniqueg merged commit 4a9c55a into dev Oct 17, 2026
25 checks passed
@uniqueg
uniqueg deleted the issue-260-subpath-roles branch October 17, 2026 14:01
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.

Resource roles should cover sub-paths

2 participants