Repository navigation
fix: harden access control enforcement - #263
Conversation
Address automated review findings on #259: - Decide whether to enforce policies per operation as resolved by Connexion, instead of pre-scanning the raw spec, so that operations of path items defined via `$ref` are enforced, too. - Do not enforce operations with optional authentication, i.e., with an empty security requirement alternative (e.g., `security: [{}]`). - Wrap callable resolvers, as accepted by Connexion, in a `Resolver`. - Refuse configurations enforcing access control without authentication, instead of silently skipping enforcement. - Reject an empty `subject_prefix` and prefixes overlapping with `group:`, which would let subjects collide with roles or groups. Refs #262 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Document prefixed subjects in rules (including for the permissions API), the relation to `check_permissions`, and the enforcement changes. Refs #262 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThe PR hardens access-control enforcement by basing policy wrapping on Connexion’s fully resolved operations, correctly handling references and optional authentication, supporting callable resolvers, and failing fast on configurations or subject namespaces that could bypass or undermine enforcement; documentation and regression coverage are updated accordingly. Sequence diagram for resolved access-control enforcementsequenceDiagram
participant App
participant Connexion
participant Resolver as AccessControlResolver
participant Handler
App->>Connexion: add_api(resolver)
Connexion->>Resolver: resolve(operation)
Resolver->>Resolver: resolver.resolve(operation)
alt security requires authentication
Resolver->>Handler: enforce_access_control(function)
else public, optional auth, or opted out
Resolver-->>Connexion: Resolution(function)
end
Connexion-->>App: registered resolved operation
Flow diagram for access-control configuration validationflowchart TD
A[Load SecurityConfig] --> B{access_control.enforce?}
B -- No --> C[Start normally]
B -- Yes --> D{auth.required?}
D -- No --> E[Reject configuration]
D -- Yes --> C
F[Validate subject_prefix] --> G{Empty or overlaps group:?}
G -- Yes --> H[Reject configuration]
G -- No --> C
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 #263 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 22 22
Lines 1099 1113 +14
=========================================
+ Hits 1099 1113 +14
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 changes address the stated enforcement gaps with regression coverage and no unresolved findings.
No open issues 🎉
What changed in this PR
Hardens FOCA’s access-control enforcement to address #262 and the unreleased gaps from #259.
Changes:
- Enforces resolved operations, handles optional authentication, and supports callable resolvers.
- Rejects unsafe authentication settings and subject prefixes.
- Adds regression tests and clarifies policy subjects and enforcement modes.
| File | Description |
|---|---|
| tests/test_foca.py | Checks startup rejection of invalid enforcement settings. |
| tests/security/access_control/test_enforcement.py | Tests references, optional authentication, and callable resolvers. |
| tests/models/test_config.py | Tests prefix and enforcement validation. |
| foca/security/access_control/register_access_control.py | Keeps permissions API enforcement unconditional. |
| foca/security/access_control/enforcement.py | Selects resolved operations and wraps callable resolvers. |
| foca/security/access_control/constants.py | Centralizes the group prefix. |
| foca/models/config.py | Rejects unsafe access-control configurations. |
| foca/foca.py | Removes the log-only enforcement fallback. |
| foca/api/register_openapi.py | Removes raw-spec enforcement selection. |
| docs/access_control/README.md | Clarifies enforcement, namespaces, and legacy permission checks. |
Review effort: Balanced
Test enforcement with a `Resolver` instance as well as a callable, as both are accepted by Connexion; restores 100% coverage. Refs #262 Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. If operation resolution or security-requirement handling skips enforcement, callers may reach protected endpoints without the policy check; any data or access exposed before the defect is fixed cannot be undone by reverting. The configuration validation and prefix checks also change authorization behavior, so verify those boundaries carefully.
Normalize resolvers the same way as Connexion's `App.add_api()`: wrap only callables in a `Resolver`, and use any other resolver object (with a `resolve()` method) as is. Previously, resolver objects not inheriting from `Resolver` were wrongly wrapped as function resolvers, breaking API registration with enforcement enabled. Refs #262 Co-Authored-By: Claude Opus 5.5 <[email protected]>

Closes #262
Summary
Addresses the automated review findings (Sourcery, Copilot) on #259, which was merged before they were handled. Nothing affected has been released yet (last tag: v0.13.0).
Fixes
$refpath items were not enforced (security):AccessControlResolvernow decides per operation as resolved by Connexion (references resolved, effectivesecurity), instead ofregister_openapipre-scanning the raw spec._get_enforced_operations()is removed.security: [{}, {bearerAuth: []}]) are treated as public, likesecurity: [].App.add_api()does: callables are wrapped inResolver(function_resolver=...), any other resolver object (with aresolve()method) is used as is.enforce: truewithauth.required: falsefailed open:SecurityConfignow rejects this combination, so FOCA refuses to start (previously only an error was logged and enforcement was skipped).subject_prefix: an empty prefix, or one overlapping withgroup:, is rejected at config validation.GROUP_PREFIXmoved toconstants.pyfor that.Docs
user:<id>).check_permissionsis a separate, header-based mechanism and should not be combined withenforce: true.Declined (rationale in #262 and in the #259 threads): duplicate rules from concurrent identical grants (harmless; needs a unique index in the shared adapter), and non-atomic resource and grant changes (app's transaction design).
Automated reviews of this PR
Resolveras functions. Agreed and fixed in 25b1100 (same rule as Connexion, plus a regression test); thread resolved.Verification
CI: all checks pass on 25b1100: 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 (coverage stays at 100%), 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: 206 passed; coverage of all changed modules 100%$refpath item enforced (non-member 403, admin 200); optional authentication not enforced (anonymous and authenticated 200); custom resolvers as a function, aResolverinstance and a resolver object not inheriting fromResolver(registration works, policies enforced); invalidsubject_prefixvalues ("",group:,group:x,g,group) rejected;enforcewithoutauth.requiredrejected, also when startingFoca.Risks
enforce: trueandauth.required: falsenow fail to start. That configuration never enforced anything, so this surfaces an existing misconfiguration.subject_prefix: ""(or overlappinggroup:) now fail config validation;""was documented as an option only as an unsafe one, in an unreleased version.x-foca-access-controlis read from Connexion's internal operation dict (_operation), as Connexion has no public accessor for vendor extensions (Connexion reads its own vendor extensions the same way). FOCA pinsconnexion~=2.11; a Connexion 3 migration would need to revisit this.Open questions
None.
🤖 Generated with Claude Code