Skip to content

fix(backend): prevent forwarding request user-agent during machine token verification - #11035

Open
pranav514 wants to merge 1 commit into
clerk:mainfrom
pranav514:fix/backend-oauth-verify-user-agent
Open

pranav514 wants to merge 1 commit into
clerk:mainfrom
pranav514:fix/backend-oauth-verify-user-agent

Conversation

@pranav514

@pranav514 pranav514 commented Nov 29, 2026 •

Copy link
Copy Markdown

Description

AuthenticateContext previously read the incoming request's User-Agent header onto this.userAgent, while createAuthenticateRequest() did not forward the SDK's build-time userAgent option. Because verifyMachineAuthToken() passes authenticateContext to createBackendApiClient(), server-to-server verification requests (such as POST /oauth_applications/access_tokens/verify for opaque oat_ tokens) sent the incoming client's User-Agent instead of the SDK's configured User-Agent, which could result in upstream 403 rejections even when clerkClient().idPOAuthAccessToken.verify() succeeded.

This change:

  • Stops populating AuthenticateContext.userAgent from the incoming User-Agent request header and forwards the SDK's build-time userAgent through createAuthenticateRequest() (ignoring runtime overrides).
  • Consolidates opaque machine token verification (m2m_token, oauth_token, api_key) in verifyMachineAuthToken() into a shared helper.
  • Preserves Backend API error messages for non-401/404 responses in handleClerkAPIError() and exposes the HTTP status on unexpected-error-<status> auth reasons in handleMachineError().

Fixes #1103

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@vercel

vercel Bot commented Nov 29, 2026

Copy link
Copy Markdown

@pranav514 is attempting to deploy a commit to the Clerk Production Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Nov 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e1e78c2

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

This PR includes changesets to release 11 packages
Name Type
@clerk/backend Patch
@clerk/astro Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/hono Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/swingset 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

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Authentication now uses the build-time userAgent for machine-token verification instead of reading the incoming request header. Opaque M2M, OAuth, and API-key verification share an error-handling flow. API error messages are preserved, and unexpected-error authentication reasons include the HTTP status when available.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

Possibly related PRs

  • clerk/javascript#10358 — This PR addresses the same User-Agent forwarding issue for API-key verification, which is included in this PR’s shared opaque-token path.

Suggested reviewers: thiskevinwang, wobsoriano

Merge Risk: Minimal · up to e1e78

This change stops machine-token verification from forwarding the client's User-Agent and instead uses the SDK's configured value. It also keeps API error messages and adds the HTTP status to unexpected-error reasons. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the SDK stops forwarding the incoming request's User-Agent during machine token verification. It is a fix in the backend package. The title uses the conventional type(…
Linked Issues check Passed The only linked issue, #1103, is closed and completed. It supplies historical context only. No active directly linked target remains, so no linked-issue coding requirements apply to this pull request.…
Out of Scope Changes check Passed The change summary maps each modified file to the PR description. factory.ts and types.ts forward the build-time userAgent and ignore runtime overrides. authenticateContext.ts removes the incoming Use…
Description check Passed The description states the PR stops populating AuthenticateContext.userAgent from the incoming User-Agent header and forwards the SDK's build-time userAgent through createAuthenticateRequest(). It als…
Full details: Title check

Explanation

The title names the main change: the SDK stops forwarding the incoming request's User-Agent during machine token verification. It is a fix in the backend package. The title uses the conventional type(scope) prefix and fits the changeset, which adds a user-agent fix for authenticateRequest and the machine-token error changes.

Full details: Linked Issues check

Explanation

The only linked issue, #1103, is closed and completed. It supplies historical context only. No active directly linked target remains, so no linked-issue coding requirements apply to this pull request. The PR description states the current intent: stop forwarding the incoming User-Agent, forward the SDK build-time userAgent, consolidate opaque machine-token verification, and preserve Backend API error messages and HTTP status. Those changes are not assessed against #1103.

Full details: Out of Scope Changes check

Explanation

The change summary maps each modified file to the PR description. factory.ts and types.ts forward the build-time userAgent and ignore runtime overrides. authenticateContext.ts removes the incoming User-Agent read. verify.ts consolidates opaque m2m_token, oauth_token, and api_key verification into a shared helper and preserves Backend API error messages. request.ts appends the status to unexpected-error reasons. The test files cover these same behaviors. The changeset documents the backend change. No file was identified that lacks a connection to the stated objectives. This assessment relies on the summary and the PR description. No source hunks were inspected in this review.

Full details: Description check

Explanation

The description states the PR stops populating AuthenticateContext.userAgent from the incoming User-Agent header and forwards the SDK's build-time userAgent through createAuthenticateRequest(). It also covers the shared opaque-token verification helper and the handling of Backend API error messages and HTTP status in unexpected-error reasons. It relates directly to the changeset, so the check passes.

Finishing Touches

Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

LanguageTool

.changeset/fix-oauth-verify-user-agent.md

LanguageTool could not check every text chunk; analysis is incomplete

Linked repositories: This review skipped 6 repositories. Cross-repository analysis may be incomplete. Check linked repository access and settings.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: clerk/javascript
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: c7d761a
  • Layers: Repository YAML (base), Organization UI (inherited)
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 60fa35ae-9444-4952-9681-534448524262
Commits

Reviewing files that changed from the base of the PR and between c7d761a and e1e78c2.

Files selected for processing: 9

  • .changeset/fix-oauth-verify-user-agent.md
  • packages/backend/src/tokens/__tests__/factory.test.ts
  • packages/backend/src/tokens/__tests__/request.test.ts
  • packages/backend/src/tokens/__tests__/verify.test.ts
  • packages/backend/src/tokens/authenticateContext.ts
  • packages/backend/src/tokens/factory.ts
  • packages/backend/src/tokens/request.ts
  • packages/backend/src/tokens/types.ts
  • packages/backend/src/tokens/verify.ts
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Files with no reviewable changes: 1

  • packages/backend/src/tokens/authenticateContext.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@pranav514

Copy link
Copy Markdown
Author

@wobsoriano @thiskevinwang I have created the PR that resolves #1103, please do review it. Thank you

This branch has not been deployed

No deployments
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.

1 participant