Skip to content

fix: refuse users without namespace in role APIs - #827

Draft
uniqueg wants to merge 1 commit into
devfrom
fix/refuse-bare-users
Draft

uniqueg wants to merge 1 commit into
devfrom
fix/refuse-bare-users

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Found in the pre-release review of 0.23.0. Since #773, users are named in rules as user:<namespace>|<subject> also with a single trusted issuer. FOCA refused configured rules naming other users (with the models it ships) but the APIs still stored them, so clients written for 0.22 granted roles that apply to nobody (e.g., PUT /pets/1/permissions/users/bob returned 200).

With the models that FOCA ships and at least one issuer that namespaces its subjects (the same conditions as the startup check of configured rules; namespace: "" keeps bare users valid):

  • Permissions API (POST/PUT /permissions): values naming such users are invalid fields with the new code unknown-user, at the value's pointer (e.g., #/rule/v0), without the value.
  • grant_role() / add_owner() (and so the endpoints for managing roles, 400): ValueError, as for groups and clients to which roles don't apply (_check_applicable()). Stored roles of such users therefore don't count as owners (so they don't block LastOwner checks) and can still be revoked; stored rules can still be deleted via the permissions API.
  • Custom models, which may match users in other ways, are not checked.
  • The packaged-model check (_is_packaged_model, _model_tokens) moves from register_access_control to the new module foca.security.access_control.users, with unknown_user_matcher(); the packaged models' tokens are read once (lru_cache).

Docs: docs/access-control.md (grant_role(), rule values), docs/problems/invalid-request.md (code unknown-user), docs/migration-0.23.md (section "Subjects are always namespaced by issuer"), docs/reference.md (unknown_user_matcher).

Closes #826

Verification

  • Tests first: tests/security/access_control/test_ownership.py::TestNamespacedBySingleIssuer::test_users_without_namespace* and tests/security/access_control/test_access_control_server.py::TestUsersWithoutNamespace failed before the fix (DID NOT RAISE), pass after.
  • Full suite with a private MongoDB 7.0.34 on port 12345: coverage run -m pytest: 5642 passed, 2 skipped; coverage report: 100%.
  • ruff check ., ruff format --check ., mypy (192 files): pass.
  • ast-grep scan --error=flask-import, semgrep scan --config .semgrep --error: no findings.
  • mkdocs build --strict: pass.
  • Not run locally: tests/integration_gateway_tests.py (Docker compose stack); CI runs it.

Review

The adversarial-reviewer and quality-reviewer subagents were not run before this push (the agent that wrote this PR couldn't start subagents); they are to be run on this branch before the PR is marked ready.

Declined review findings

None.

Open questions

None.

🤖 Generated with Claude Code

https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5

Summary by Sourcery

Prevent permissions and role APIs from accepting users that no trusted issuer can identify.

Bug Fixes:

  • Reject permissions rules and role grants for users that cannot match any trusted issuer, preventing clients from creating roles that apply to nobody.
  • Ensure stale roles for unrecognized users do not count toward ownership checks while remaining removable.

Enhancements:

  • Centralize detection of users without valid issuer namespaces and limit the check to packaged access-control models.
  • Document the new unknown-user validation and migration behavior for namespaced subjects.

Documentation:

  • Document validation errors, role-management behavior, and migration guidance for users without issuer namespaces.

Tests:

  • Add coverage for rejecting unnamespaced users, accepting valid namespaced users, preserving custom-model behavior, and deleting or revoking stale entries.

Since subjects are always namespaced by issuer (#773), rules naming
users without the namespace of a trusted issuer apply to nobody. FOCA
refused them only in configured rules; the permissions API and the
endpoints for managing roles still stored them, so clients written for
0.22 granted roles that never applied.

With the models that FOCA ships and an issuer that namespaces its
subjects, the permissions API now refuses such values (code
unknown-user, at the value's pointer, without the value), and
grant_role() and add_owner() raise ValueError (400 at the endpoints),
as for groups and clients to which roles don't apply. Stored roles of
such users don't count as owners and can still be revoked or deleted.

The checks of packaged models move to the new module
foca.security.access_control.users, with unknown_user_matcher(); the
tokens of the packaged models are read once.

Closes #826

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_011FSoqN5w1v5t1QRTKU7ad5
@sourcery-ai

sourcery-ai Bot commented Nov 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Introduce unknown_user_matcher() and move the packaged-model detection into users.py, caching packaged model tokens for reuse.

  • foca/security/access_control/register_access_control.py L55-278 (4 small edits)
  • foca/security/access_control/users.py L1-109

Reject role grants to users that no trusted issuer has, so stale roles cannot be granted or count as applicable owners while remaining revocable.

  • foca/security/access_control/ownership.py L70, L464-483

Reject permission-rule values naming unknown users with an unknown-user field error while leaving stored rules deletable.

  • foca/security/access_control/access_control_server.py L237-240, L293-325 (+2 small edits)

Documentation

Document which role subjects and policy-rule users are rejected, and explain the migration behavior and new unknown-user error.

  • docs/access-control.md L1179-1185, L1763-1768
  • docs/migration-0.23.md L64-68
  • docs/problems/invalid-request.md L129

Expose unknown_user_matcher in the API reference.

  • docs/reference.md L73-77

Tests

Test rejection and acceptance of permission rules, custom-model behavior, and deletion of previously stored unknown-user rules.

  • tests/security/access_control/test_access_control_server.py L27, L41, L760-843

Test role rejection for unknown users, their owner-check behavior and revocation, and the custom-model exception.

  • tests/security/access_control/test_ownership.py L56, L1057-1092

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 Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5f6c105) to head (29720f7).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #827   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           76        77    +1     
  Lines        10708     10739   +31     
=========================================
+ Hits         10708     10739   +31     
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 commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Deferred out of 0.23.0 after the pre-release review (#826 moved to v0.24.0). Two medium findings need a design decision first:

  1. A bare owner that is the only owner of a resource can't be revoked: bare owners no longer count, so revoke_roles() deletes the rule, _restore_owner_if_none() puts it back and raises LastOwner (409). Reproduced; the docs say such roles can be revoked.
  2. Resources owned only by bare users count as ownerless, so the ownership endpoints answer 404 (unless the app passes an existence check), which blocks granting a namespaced owner through them.

Option for 0.24.0: refuse only new grants of bare users and keep stored bare owners counting as before. 0.23.0 documents instead that these APIs still accept bare users, which apply to nobody (#823).

This branch has not been deployed

No deployments
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.

fix: refuse users without namespace in role APIs

1 participant