Skip to content

fix: enforce valid_scopes for CIMD clients - #5829

Merged
zzstoatzz merged 3 commits into
PrefectHQ:mainfrom
BlueX888:fix/cimd-valid-scopes-bypass
Nov 22, 2026
Merged

zzstoatzz merged 3 commits into
PrefectHQ:mainfrom
BlueX888:fix/cimd-valid-scopes-bypass

Conversation

@BlueX888

@BlueX888 BlueX888 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #5828

Description

CIMD clients could bypass the OAuth proxy's scope policy. CIMDClientManager built the synthetic ProxyDCRClient with scope=cimd_doc.scope or self.default_scope, taking the scope verbatim from the client-hosted metadata document. OAuthProxy never passed its valid_scopes allowlist to the manager, so a document declaring scope="admin" passed the SDK's client.validate_scope() gate, was recorded in the OAuth transaction, and was forwarded to the upstream IdP — while the equivalent DCR registration/authorization for admin is rejected with invalid_scope when valid_scopes=["read"].

This change:

  • CIMDClientManager now accepts an optional keyword-only valid_scopes allowlist (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_scopes or, when absent, token_verifier.required_scopes) into the manager.
  • update_default_scopes() refreshes the manager's allowlist alongside default_scope and the registration options.
  • The legacy stored-CIMD-client fallback in OAuthProxy.get_client() constrains the persisted client's scope with the same allowlist before returning it when the refresh fetch fails.
  • When the document omits scope, the existing default_scope fallback is unchanged.
  • When the server configures neither valid_scopes nor required_scopes, document-declared CIMD scopes are stripped (empty allowlist), matching the DCR path's empty-allowlist behavior.

Contribution type

  • Bug fix (simple, well-scoped fix for a clearly broken behavior)
  • Documentation improvement
  • Enhancement

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:

E   TypeError: CIMDClientManager.__init__() got an unexpected keyword argument 'valid_scopes'
E   AttributeError: 'CIMDClientManager' object has no attribute 'valid_scopes'
E   KeyError: 'error'   # the /authorize request was forwarded upstream with scope=admin instead of invalid_scope
9 failed, 16 passed, 1 warning, 4 errors

The ninth failure is the legacy stored-client fallback: with a persisted scope=admin and a failing refresh fetch, /authorize likewise forwarded admin.

A positional-compatibility regression was also covered: an intermediate revision inserted valid_scopes as the second positional parameter, silently rebinding CIMDClientManager(True, "read", patterns) to valid_scopes and leaving allowed_redirect_uri_patterns=None (AssertionError: assert None == ['http://localhost:*', ...]). valid_scopes is now keyword-only, with a regression test pinning the positional constructor.

After the fix: 25 passed for the focused tests. An independent reproduction with a document declaring admin now returns error=invalid_scope, forwards no scope upstream, records no transaction scope, and validate_scope("admin") raises InvalidScopeError; a document declaring read still authorizes read and records ["read"]. The fallback regression returns invalid_scope without 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 passed
  • uv run --frozen ruff check and ruff format --check on the changed files -> clean

AI assistance

Tool(s) used: Claude Code
How you used it: investigated from a verified reproduction, drafted the fix and regression tests.

Checklist

  • This PR addresses an existing issue (or fixes a self-evident bug)
  • I have read CONTRIBUTING.md
  • I have added tests that cover my changes
  • I have run uv run prek run --all-files and all checks pass
  • I have self-reviewed my changes
  • If I used an LLM, it followed the repo's contributing conventions (not generic output)

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.
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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:

  1. A maintainer will review the issue and decide whether to assign you. Please don't comment just to request assignment.

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 missing-issue-link label to bypass this check.

@marvin-context-protocol marvin-context-protocol Bot added auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. high-priority security Security fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses. labels Oct 19, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T03:27:03.854891Z 80b4685 PR opened
ℹ️ 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" or "@codex security review".

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.
@github-actions github-actions Bot closed this Oct 19, 2026
@github-actions github-actions Bot reopened this Oct 19, 2026
@zzstoatzz
zzstoatzz merged commit c53e9a8 into PrefectHQ:main Nov 22, 2026
22 of 24 checks passed
@zzstoatzz zzstoatzz mentioned this pull request Nov 22, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Related to authentication (Bearer, JWT, OAuth, WorkOS) for client or server. bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. high-priority missing-issue-link security Security fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] CIMD clients bypass OAuthProxy valid_scopes

2 participants