Repository navigation
fix(auth): namespace subjects of several issuers - #512
Conversation
With several trusted issuers, subjects are now namespaced by issuer as <namespace>|<subject>, so that equal subjects of different issuers are different users in policies, resource roles and owners. Before, they got each other's rights. Issuers may be listed with settings of their own (IssuerConfig): the namespace defaults to the issuer with several issuers and to none with one, so single-issuer apps are unchanged; an empty namespace keeps the subjects of an issuer bare, e.g., the first one when a second is trusted. Issuers with the same namespace, or several bare ones, are refused at config load. get_on_behalf_of_user() namespaces named users with the service client's issuer, and namespace_subject() lets apps name users the same way. Bare subjects with '|' next to namespaced ones are not identified. The docs describe the migration to two issuers. Closes #509 Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Route namespaced users, which contain the issuer and thus slashes, in the documented sharing API (userId with format: path, rules with users/*), with a test of two issuers and of bare subjects that look namespaced (401). - Compute the namespaces of all issuers in one pass (get_namespaces()) instead of per issuer and request. - Clearer error for named users that cannot be identified, docstring fixes, a migration note on the issuers type, and a check for bare subjects with '|' before keeping an issuer bare. - Fix a duplicated line in the deployment docs. Refs #509 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Split the migration notes into one bullet per change, note proxies that merge slashes, rewrap edited bullets and fix docstrings (unvalidated changes of issuers, typo, wording). Refs #509 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR isolates equal subjects across multiple trusted issuers by deriving validated per-issuer namespaces and applying them consistently throughout authentication, access control, ownership, resource roles, and service-client impersonation, while retaining bare subjects for single-issuer deployments. It also updates routing, tests, configuration APIs, warnings, and migration documentation for the new behavior. Sequence diagram for namespaced subject enforcementsequenceDiagram
participant Token as ValidatedToken
participant Auth as foca.security.auth
participant Enforcement as AccessControl
participant Policy as PolicyEnforcer
Token->>Auth: get_namespaces()
Auth-->>Enforcement: namespace_subject(subject, issuer)
Enforcement-->>Policy: get_current_subject()
Policy->>Policy: enforce namespaced user subject
Policy-->>Enforcement: allow or deny access
alt single trusted issuer
Auth-->>Enforcement: bare subject
else multiple trusted issuers
Auth-->>Enforcement: namespace|subject
end
Sequence diagram for service-client user impersonationsequenceDiagram
participant Client as ServiceClient
participant Access as AccessControl
participant Auth as foca.security.auth
participant Policy as PolicyEnforcer
Client->>Access: get_on_behalf_of_user(user)
Access->>Auth: get_client_issuer(client, auth)
Auth-->>Access: trusted client issuer
Access->>Auth: namespace_subject(user, issuer, auth)
alt user can be identified
Auth-->>Access: namespace|user or bare user
Access-->>Policy: attributed user
else user cannot be identified
Auth-->>Access: None
Access-->>Client: 400 Bad Request
end
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 #512 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 55
Lines 6494 6558 +64
=========================================
+ Hits 6494 6558 +64
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.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="foca/security/auth.py" line_range="273" />
<code_context>
+ namespace = namespaces[issuer]
+ if namespace is None:
+ return None if NAMESPACE_SEPARATOR in subject else subject
+ return f"{namespace}{NAMESPACE_SEPARATOR}{subject}"
+
+
</code_context>
<issue_to_address>
**Namespaced users are denied access**
When a trusted issuer’s namespace and a valid subject together exceed `MAX_RULE_VALUE_LENGTH`, `namespace_subject()` returns an over-limit value, which `is_permitted()` rejects and `add_resource_role()` cannot accept; the issuer configuration is still accepted, but users cannot pass access control or receive resource roles or ownership.
Validate that configured namespaces leave room for allowed subjects, and reject any namespaced subject that exceeds `MAX_RULE_VALUE_LENGTH` before authorization or role assignment.
Also at `foca/models/config.py:1377`.
</issue_to_address>
### Comment 2
<location path="tests/security/access_control/test_register_access_control.py" line_range="619" />
<code_context>
self, caplog: pytest.LogCaptureFixture, issuers: list[str], warned: bool
) -> None:
- """Subjects of several issuers may collide, which is warned about."""
+ """Group claim values of several issuers may collide (warned about)."""
config = Config(
security=SecurityConfig(
</code_context>
<issue_to_address>
**Issuer warning test accepts stale warning**
When the multiple-issuer warning regresses to the old subject-only message, when the warning still says subjects can collide but omits group claim values, `test_several_issuers` passes: its assertion at line 631 checks only for the generic phrase `trusted token issuers`, which the old warning also contains.
Assert that the warning identifies group claim values as unscoped, and does not claim that subjects are unscoped.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and access-control checks now use issuer-namespaced subjects, so an incorrect mapping could attribute a request to the wrong user and grant that user another user's rights. Reverting restores the old subject interpretation, but role grants or owner values written with namespaced subjects can remain stored and may need cleanup.
Blocking findings: foca/security/auth.py:273, tests/security/access_control/test_register_access_control.py:619
Namespaces have at most 256 characters, also issuers used as default namespaces, so that namespaced subjects fit into policy rules (1024 characters per value); longer namespaced subjects are denied, as long bare ones are (Sourcery). The test of the startup warning about several issuers checks its new wording. Refs #509 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
With several trusted issuers, equal subjects of different issuers were the same user for access control, so they got each other's rights. Subjects are now namespaced by issuer.
<namespace>|<subject>(in rulesuser:<namespace>|<subject>). This applies toget_current_subject(), policies, resource roles, owners (also document owner fields) and plugin principals. By default the namespace is the issuer URL.security.auth.issuerstakes URLs or objects (IssuerConfig:issuer,namespace).namespaceto choose a shorter one.namespace: ""to keep an issuer's subjects bare, e.g., for the first issuer when a second one is added, so that stored rules stay valid.|, commas or line breaks. A bare subject containing|next to namespaced issuers is not identified: enforced operations answer401.get_on_behalf_of_user()namespaces named users with the service client's issuer, and answers400if it can't.namespace_subject()andget_client_issuer()infoca.security.auth;AuthConfig.issuer_urlsandAuthConfig.get_namespaces().userIdpath parameter getsformat: pathand the rules useusers/*, as for groups.issuerstoSequence[str | IssuerConfig]).Per-issuer audience, group claims and user info claims follow in #510.
Closes #509
Verification
Run locally on c2d510e:
ruff checkandruff format --check: pass.mypy: pass.pytest -m "not mongodb": 2764 passed, 1 skipped, with 100% coverage offoca.mkdocs build --strict: pass.pre-commit: pass.The MongoDB tests and semgrep did not run locally (CI runs them). CI on c2d510e: 27 checks pass, and 3 are skipped as intended.
Review: two rounds of adversarial and quality review.
issuerstype was missing;Sourcery: 2 findings, both fixed in c2d510e.
Declined review findings
get_namespaces()walks a few entries per call, which is negligible next to token validation. The config models don't validate assignment, so a cache could go stale when apps or tests changeissuers. The docstring says that such changes are not validated.test_namespaced: declined, as cosmetic. The section is one paragraph per topic plus a tip, and the test covers one behaviour (named users) in two cases.issuer: objectinstead ofAnyinnamespace_subject(): declined. Claims are untyped (dict[str, Any]), and the docstring says that any value is accepted.🤖 Generated with Claude Code