Skip to content

Fix OAuth state parameter security vulnerability - #521

Merged
ghostwriternr merged 3 commits into
mainfrom
fix-oauth-state
Sep 29, 2025
Merged

ghostwriternr merged 3 commits into
mainfrom
fix-oauth-state

Conversation

@ghostwriternr

Copy link
Copy Markdown
Member

Replace OAuth client_id values in state parameters with secure random tokens to prevent CSRF attacks and state prediction vulnerabilities.

The current implementation exposes the OAuth client_id directly in the state parameter, making it predictable and potentially enabling cross-site request forgery attacks.

Solution

Replace state parameter with secure random tokens while storing clientId separately:

// Before: Predictable state value
authUrl.searchParams.append("state", client_id);

// After: Secure random token
const stateToken = nanoid();
authUrl.searchParams.set("state", stateToken);
this._clientId_ = client_id; // Store separately

Update callback handler to read clientId from stored property instead of state parameter, with fallback to legacy behavior for backward compatibility.

References

  • OAuth 2.0 Security BCP: RFC 6819 - State parameter should be unguessable random values to prevent CSRF attacks
  • OWASP OAuth Guidelines: State parameters should be cryptographically random

Supports both static and dynamic client registration flows while maintaining backward compatibility.

Replace clientId in state parameter with secure random tokens to
prevent potential security issues. Update callback handler to use
stored clientId instead of reading from state parameter.

Supports both static and dynamic client registration flows while
maintaining backward compatibility.
@changeset-bot

changeset-bot Bot commented Sep 26, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dd8c7d1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 26, 2025 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@521

commit: dd8c7d1

@threepointone threepointone 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.

Changeset

Comment thread packages/agents/src/mcp/do-oauth-client-provider.ts Outdated
We already persist the clientId in sqlite and post hibernation, gets
picked up from the reconnect params where the clientId is stored.
@ghostwriternr
ghostwriternr merged commit 1bd0c75 into main Sep 29, 2025
3 checks passed
@ghostwriternr
ghostwriternr deleted the fix-oauth-state branch September 29, 2025 09:53
@threepointone threepointone mentioned this pull request Sep 29, 2025
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.

2 participants