Repository navigation
fix: enforce valid_scopes for CIMD clients - #5829
Conversation
The CIMD client manager built the synthetic client with the scope declared in the client-hosted metadata document, without intersecting it with the proxy's valid_scopes allowlist. A document declaring an out-of-policy scope therefore passed the SDK scope gate and was recorded/forwarded upstream, while the equivalent DCR registration was rejected. Thread valid_scopes into CIMDClientManager and grant only the intersection of the document scope and the server allowlist. When the document omits scope, the existing default_scope fallback is unchanged. update_default_scopes() also refreshes the manager's allowlist.
When a CIMD refresh fetch fails, OAuthProxy.get_client() falls back to a client persisted by an older version. That record carries the verbatim document scope, so a persisted scope outside valid_scopes still passed the SDK scope gate and was forwarded upstream. Extract the allowlist intersection into CIMDClientManager.restrict_scope() and apply it to the fallback client before returning it. Add a regression test that stores a legacy client with scope=admin, fails the refresh fetch, and asserts /authorize returns invalid_scope without recording or forwarding the scope.
|
This pull request is awaiting maintainer assignment. Linked PRs stay open while maintainers triage the issue; this check remains failing until assignment. Per CONTRIBUTING.md, an external PR must reference an issue that's assigned to its author. To get there:
Once you're assigned and the link is present, this check is re-run automatically. Previously gate-closed PRs also reopen — no further action needed. Maintainers: reopen this PR or remove the |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 80b4685862
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| self, | ||
| enable_cimd: bool = True, | ||
| default_scope: str = "", | ||
| valid_scopes: list[str] | None = None, |
There was a problem hiding this comment.
Preserve the existing third positional constructor argument
For callers using the existing positional form CIMDClientManager(True, "read", patterns), inserting valid_scopes here silently rebinds patterns from allowed_redirect_uri_patterns to the new scope allowlist. Because both parameters are list[str], type checking will not expose this: the server-specific redirect policy becomes None, while the URI patterns are treated as scope names and can also make otherwise valid authorizations fail. Add the new option after the existing parameter (or otherwise preserve the old positional binding).
AGENTS.md reference: AGENTS.md:L156-L158
Useful? React with 👍 / 👎.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Adding valid_scopes before allowed_redirect_uri_patterns silently rebound the existing CIMDClientManager(True, "read", patterns) positional form: patterns became the scope allowlist and the redirect policy was dropped. Make valid_scopes keyword-only after the original parameters and add a regression test covering the positional constructor form.
Closes #5828
Description
CIMD clients could bypass the OAuth proxy's scope policy.
CIMDClientManagerbuilt the syntheticProxyDCRClientwithscope=cimd_doc.scope or self.default_scope, taking the scope verbatim from the client-hosted metadata document.OAuthProxynever passed itsvalid_scopesallowlist to the manager, so a document declaringscope="admin"passed the SDK'sclient.validate_scope()gate, was recorded in the OAuth transaction, and was forwarded to the upstream IdP — while the equivalent DCR registration/authorization foradminis rejected withinvalid_scopewhenvalid_scopes=["read"].This change:
CIMDClientManagernow accepts an optional keyword-onlyvalid_scopesallowlist (the existing positional signature is unchanged) and, when a document declares a scope, grants only the intersection of the document scope and that allowlist (document order preserved).OAuthProxy.__init__passes the same allowlist used for DCR (valid_scopesor, when absent,token_verifier.required_scopes) into the manager.update_default_scopes()refreshes the manager's allowlist alongsidedefault_scopeand the registration options.OAuthProxy.get_client()constrains the persisted client's scope with the same allowlist before returning it when the refresh fetch fails.default_scopefallback is unchanged.valid_scopesnorrequired_scopes, document-declared CIMD scopes are stripped (empty allowlist), matching the DCR path's empty-allowlist behavior.Contribution type
Bug fix verification
Test paths:
tests/server/auth/test_cimd_validators.py::TestCIMDClientManagerGetClientOptions,tests/server/auth/oauth_proxy/test_oauth_proxy.py::TestOAuthProxyInitialization,tests/server/auth/oauth_proxy/test_oauth_proxy.py::TestCIMDScopeEnforcement.Before the fix:
The ninth failure is the legacy stored-client fallback: with a persisted
scope=adminand a failing refresh fetch,/authorizelikewise forwardedadmin.A positional-compatibility regression was also covered: an intermediate revision inserted
valid_scopesas the second positional parameter, silently rebindingCIMDClientManager(True, "read", patterns)tovalid_scopesand leavingallowed_redirect_uri_patterns=None(AssertionError: assert None == ['http://localhost:*', ...]).valid_scopesis now keyword-only, with a regression test pinning the positional constructor.After the fix:
25 passedfor the focused tests. An independent reproduction with a document declaringadminnow returnserror=invalid_scope, forwards noscopeupstream, records no transaction scope, andvalidate_scope("admin")raisesInvalidScopeError; a document declaringreadstill authorizesreadand records["read"]. The fallback regression returnsinvalid_scopewithout writing a transaction.Validation
uv run --frozen pytest -q tests/server/auth/test_cimd.py tests/server/auth/test_cimd_validators.py tests/server/auth/oauth_proxy/-> 309 passeduv run --frozen ruff checkandruff format --checkon the changed files -> cleanAI assistance
Tool(s) used: Claude Code
How you used it: investigated from a verified reproduction, drafted the fix and regression tests.
Checklist
uv run prek run --all-filesand all checks pass