[DO NOT MERGE] # Fix: Prevent Infinite Transaction Cookie Accumulation + Configuration Options #2236
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
THIS IS A DRAFT
Summary
This PR fixes the infinite transaction cookie accumulation issue in nextjs-auth0 v4 where unauthenticated users navigating to protected routes create new transaction cookies (
__txn_*
) without cleaning up previous ones, leading to HTTP header size limits and eventual 413 errors.New in this version: Added comprehensive configuration options for transaction cookie management, allowing users to customize behavior for their specific needs.
Root Cause Analysis
The transaction cookie lifecycle had a fundamental flaw:
startInteractiveLogin()
creates a new transaction cookie with unique state (__txn_{state}
)handleCallback()
This led to linear growth (~400-700 bytes per navigation) until HTTP header limits (8KB) were exceeded after just 9 login attempts.
Solution
1. Configurable Transaction Cookie Management
Added three new constructor parameters to
AuthClient
for flexible transaction cookie management:enableParallelTransactions?: boolean
(default:true
)true
: Supports multi-tab login scenarios with threshold-based cleanupfalse
: Only maintains one transaction cookie at a time (deletes all existing cookies on new login)txnCookieExpiration?: number
(default:3600
)maxTxnCookieCount?: number
(default:2
)enableParallelTransactions
istrue
, sets the maximum number of transaction cookies to maintain2. Threshold-Based Cleanup in
startInteractiveLogin()
Added intelligent cleanup logic that respects configuration:
Strategy: Configurable approach supports both simple single-transaction mode and advanced multi-tab scenarios.
3. Enhanced Transaction Store with Custom Expiration
Updated
TransactionStore.save()
to accept custom expiration:4. Error Callback Cleanup
Enhanced error handling to clean up transaction cookies on callback failures:
Impact: Ensures failed login attempts clean up their cookies instead of leaving orphans.
Configuration Examples
Example 1: Single Transaction Mode (Recommended for Simple Use Cases)
Benefits: Simplest cookie management, prevents any accumulation, good for most applications.
Example 2: High-Concurrency Multi-Tab Support
Benefits: Supports power users with multiple tabs, prevents accumulation with higher limits.
Example 3: Default Configuration (Backward Compatible)
Benefits: Maintains existing behavior with cookie accumulation protection added.
Technical Implementation
Core Changes
File:
src/server/auth-client.ts
startInteractiveLogin()
to accept request cookiescleanupExcessTransactionCookies()
with count-based filteringhandleCallbackError()
for automatic cleanupAlgorithm Details
Key Decisions:
Testing
Unit Tests
Created comprehensive test suite in
tests/v4-infinitely-stacking-cookies.test.ts
:All tests pass with 100% coverage of the new functionality.
Manual Testing Strategy
The fix has been validated against the scenarios identified in the bug reports:
Backward Compatibility
✅ Fully backward compatible:
Performance Impact
Migration & Rollout
Related Issues
Risk Assessment
Risk Level: LOW
Verification Steps
For reviewers to verify the fix:
pnpm test tests/v4-infinitely-stacking-cookies.test.ts
pnpm build
(compiles without errors)Additional Notes
This fix implements the recommended solution from the GitHub issue discussions, providing a clean resolution to the transaction cookie accumulation problem while maintaining full compatibility with existing multi-tab authentication flows.
The solution is designed to be robust, performant, and maintainable, following the existing codebase patterns and architectural decisions.