Skip to content

fix: resolve JWT authentication bypass vulnerability (#1854) - #1865

Open
atul-upadhyay-7 wants to merge 1 commit into
durdana3105:mainfrom
atul-upadhyay-7:fix/jwt-authentication-bypass-1854
Open

fix: resolve JWT authentication bypass vulnerability (#1854)#1865
atul-upadhyay-7 wants to merge 1 commit into
durdana3105:mainfrom
atul-upadhyay-7:fix/jwt-authentication-bypass-1854

Conversation

@atul-upadhyay-7

@atul-upadhyay-7 atul-upadhyay-7 commented Jul 27, 2026

Copy link
Copy Markdown

Summary

This PR fixes the P1 JWT authentication bypass vulnerability described in issue #1854.

Changes

backend/middlewares/requireAuth.js

  • Algorithm validation: Added strict ALLOWED_ALGORITHMS whitelist (HS256 only). Tokens with "none" algorithm or any non-HS256 algorithm are now rejected. This prevents attackers from crafting JWTs with the none algorithm to bypass signature verification.
  • Timing-safe comparison: Replaced native string comparison with crypto.timingSafeEqual() to prevent timing side-channel attacks on signature verification.
  • Claim validation: Added validation for exp (expiration), iat (issued-at), and iss (issuer) claims. Tokens with invalid or missing required claims are rejected.
  • Audience validation: Added type-checking for the aud claim.

backend/routers/cronRoutes.js

  • Applied timing-safe comparison for CRON_SECRET bearer token verification to prevent timing attacks.

backend/routers/notificationRoutes.js

  • Applied timing-safe comparison for WEBHOOK_SECRET bearer token verification to prevent timing attacks.

backend/config.js

  • Added SUPABASE_JWT_SECRET to the Zod env schema with a descriptive error message when missing.

.env.example

  • Added SUPABASE_JWT_SECRET to the environment variable example file with documentation.

Security Impact

Vulnerability Before After
Algorithm confusion attack (none algorithm) Vulnerable Protected
Timing attack on signature comparison Vulnerable Protected
Missing issuer/audience validation Vulnerable Protected
Cron/webhook secret timing attack Vulnerable Protected
Missing JWT secret configuration Silent fallback Warned + documented

Testing

  • npm run lint passes with no new warnings/errors
  • npm run build succeeds

Closes #1854

Summary by CodeRabbit

  • Security Improvements

    • Strengthened authentication by validating JWT algorithms, signatures, expiration, issuance time, issuer, and audience claims.
    • Improved protection for scheduled jobs and notification webhooks with timing-safe credential comparisons.
    • Added clearer handling when required authentication secrets are missing or invalid.
  • Configuration

    • Added support for configuring a backend JWT secret through the environment settings.

@vercel

vercel Bot commented Jul 27, 2026

Copy link
Copy Markdown

@atul-upadhyay-7 is attempting to deploy a commit to the durdana3105's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@atul-upadhyay-7, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 46 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 145dac23-a791-48e3-8c88-1307d83fa020

📥 Commits

Reviewing files that changed from the base of the PR and between ec879d8 and fde81ac.

📒 Files selected for processing (2)
  • backend/config.js
  • backend/middlewares/requireAuth.js
📝 Walkthrough

Walkthrough

Authentication configuration and verification are hardened across local JWT, cron secret, and webhook secret flows. JWT algorithm and claim validation are expanded, while secret comparisons use timing-safe checks.

Changes

Authentication hardening

Layer / File(s) Summary
JWT configuration and local verification
.env.example, backend/config.js, backend/middlewares/requireAuth.js
Adds optional SUPABASE_JWT_SECRET configuration and strengthens local JWT verification with algorithm, signature, and claim validation.
Cron request authentication
backend/routers/cronRoutes.js
Uses timing-safe cron secret comparison and returns distinct unauthorized responses for missing configuration and invalid requests.
Webhook authentication
backend/routers/notificationRoutes.js
Uses timing-safe webhook secret comparison while preserving fallback to standard authentication.

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

Possibly related PRs

Suggested labels: type:bug

Suggested reviewers: arshvermagit

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The cron and webhook secret timing-safe comparison changes address unrelated auth paths, not the JWT bypass issue in #1854. Remove or split the cron/webhook hardening into a separate PR unless it is added to the linked issue scope.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main security fix to JWT authentication bypass.
Linked Issues check ✅ Passed The PR enforces JWT algorithm and signature checks and validates exp/iat/iss/aud as required by #1854.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/middlewares/requireAuth.js`:
- Around line 56-69: The claim validation logic must enforce the complete
Supabase access-token contract when local HMAC verification is enabled. Update
the JWT validation block around the existing exp/iat/iss/aud checks to require
integer exp and iat, issuer `${SUPABASE_URL.replace(/\/$/, "")}/auth/v1`, string
sub, and an aud value containing the configured user audience (defaulting to
“authenticated”); reject missing or invalid claims while preserving valid array
and string audience handling.

In `@backend/routers/cronRoutes.js`:
- Around line 11-16: Update all three constantTimeCompare definitions in
backend/middlewares/requireAuth.js, backend/routers/cronRoutes.js (lines 11-16),
and backend/routers/notificationRoutes.js (lines 9-14) to check bufA.length and
bufB.length after creating the buffers and before calling
crypto.timingSafeEqual; return false when the byte lengths differ.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a6f86029-501f-47cb-88d7-631ab72a296d

📥 Commits

Reviewing files that changed from the base of the PR and between 3565841 and ec879d8.

📒 Files selected for processing (5)
  • .env.example
  • backend/config.js
  • backend/middlewares/requireAuth.js
  • backend/routers/cronRoutes.js
  • backend/routers/notificationRoutes.js

Comment thread backend/middlewares/requireAuth.js
Comment thread backend/routers/cronRoutes.js Outdated
- Enforce strict algorithm validation (reject 'none' algorithm, only accept HS256)
- Add timing-safe signature comparison to prevent timing attacks
- Validate JWT claims: exp, iat, iss, aud
- Add SUPABASE_JWT_SECRET to env config and .env.example
- Apply timing-safe comparison to cron and webhook secret verification
- Defense-in-depth against crafted JWTs with elevated roles
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.

P1: Authentication Bypass in the JSON Web Token (JWT) Verification Flow

1 participant