Skip to content

fix: prevent and recover from JWT token corruption in keyring storage#1358

Merged
ramya18101 merged 5 commits into
mainfrom
DXCDT-1272-Fix-400-Bad-HTTP-authentication-header-format
Oct 16, 2025
Merged

fix: prevent and recover from JWT token corruption in keyring storage#1358
ramya18101 merged 5 commits into
mainfrom
DXCDT-1272-Fix-400-Bad-HTTP-authentication-header-format

Conversation

@ramya18101

@ramya18101 ramya18101 commented Oct 13, 2025

Copy link
Copy Markdown
Contributor

🔧 Changes

Problem: JWT access tokens stored in keyring chunks were getting corrupted, causing cryptic authentication failures like "invalid character 'e' looking for beginning of object value". This happened because old token chunks weren't cleared before storing new ones, leading to contaminated token reconstruction.

Solution:

  • Prevention: Clear all existing token chunks before storing new tokens to eliminate contamination
  • Recovery: Added JWT validation using proper parsing libraries and enhanced error messages with actionable recovery steps
  • User Experience: Replaced cryptic errors with clear guidance like "Your authentication token appears corrupted. Please run: auth0 login"

Methods Changed:

  • StoreAccessToken() - Now clears old chunks before storing new tokens
  • ValidateAccessToken() - New method for JWT validation using proper parsing
  • initializeManagementClientWithTokenValidation() - Enhanced error handling with styled messages

📚 References

🔬 Testing

Manual Testing:

  • Tested authentication flow with corrupted tokens to verify error message clarity
  • Verified successful token storage and retrieval after chunk clearing implementation
  • Confirmed ANSI styling works correctly in terminal output

📝 Checklist

  • [] All new/changed/fixed functionality is covered by tests (or N/A)
  • [] I have added documentation for all new/changed functionality (or N/A)

@ramya18101 ramya18101 marked this pull request as ready for review October 13, 2025 05:08
@ramya18101 ramya18101 requested a review from a team as a code owner October 13, 2025 05:08
Comment thread internal/keyring/keyring.go Outdated
@kushalshit27 kushalshit27 requested a review from Copilot October 15, 2025 07:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes JWT token corruption issues in keyring storage by implementing prevention and recovery mechanisms. The corruption was caused by old token chunks not being cleared before storing new ones, leading to contaminated token reconstruction and cryptic authentication errors.

Key changes:

  • Added chunk clearing logic in StoreAccessToken() to prevent token contamination
  • Implemented JWT validation with proper error handling and user-friendly messages
  • Enhanced authentication flow with token validation before API client initialization

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/keyring/keyring.go Added token validation, chunk clearing logic, and JWT parsing functionality
internal/cli/management.go Enhanced management client initialization with token validation and styled error messages
internal/cli/cli.go Updated to use new token validation during authentication setup

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread internal/keyring/keyring.go Outdated
Comment thread internal/keyring/keyring.go Outdated
Comment thread internal/cli/management.go Outdated
@kushalshit27 kushalshit27 self-requested a review October 15, 2025 07:44
kushalshit27
kushalshit27 previously approved these changes Oct 15, 2025

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

Please check the comment by the review bot before merging the PR

duedares-rvj
duedares-rvj previously approved these changes Oct 16, 2025
@ramya18101 ramya18101 merged commit 809fe14 into main Oct 16, 2025
6 checks passed
@ramya18101 ramya18101 deleted the DXCDT-1272-Fix-400-Bad-HTTP-authentication-header-format branch October 16, 2025 15:51
bkiran6398 pushed a commit to sparkling/auth0-cli that referenced this pull request May 21, 2026
… client secret

StoreAccessToken's "clear existing chunks" loop deleted the
secretClientSecret key on every iteration instead of the indexed
secretAccessToken chunks. Two consequences:

1. Stale access-token chunks were never cleared, so re-storing a shorter
   token left trailing chunks that concatenated into a malformed JWT,
   surfacing as "authentication token is corrupted".
2. The client secret was deleted immediately after a machine login
   stored it, so RegenerateAccessToken could no longer refresh the
   token and client-credentials sessions broke at the first expiry.

Delete the indexed access-token chunks (matching DeleteSecretsForTenant
and the store/get loops) and leave the client secret untouched. Adds
regression tests covering both failure modes.

Introduced in auth0#1358.
ramya18101 added a commit that referenced this pull request May 21, 2026
… client secret (#1527)

StoreAccessToken's "clear existing chunks" loop deleted the
secretClientSecret key on every iteration instead of the indexed
secretAccessToken chunks. Two consequences:

1. Stale access-token chunks were never cleared, so re-storing a shorter
   token left trailing chunks that concatenated into a malformed JWT,
   surfacing as "authentication token is corrupted".
2. The client secret was deleted immediately after a machine login
   stored it, so RegenerateAccessToken could no longer refresh the
   token and client-credentials sessions broke at the first expiry.

Delete the indexed access-token chunks (matching DeleteSecretsForTenant
and the store/get loops) and leave the client secret untouched. Adds
regression tests covering both failure modes.

Introduced in #1358.

Co-authored-by: Ramya Anusri <62586490+ramya18101@users.noreply.github.com>
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.

[QUESTION] Missing scope?

4 participants