Fix duration arithmetic mismatch in clean_old_tokens#1678
Open
naanlizard wants to merge 2 commits intolynndylanhurley:masterfrom
Open
Fix duration arithmetic mismatch in clean_old_tokens#1678naanlizard wants to merge 2 commits intolynndylanhurley:masterfrom
naanlizard wants to merge 2 commits intolynndylanhurley:masterfrom
Conversation
added 2 commits
March 2, 2026 01:20
This test demonstrates the bug where newly created tokens are immediately filtered out when token_lifespan is set to variable-length durations like 6.months. The test uses time travel to March 1st, 2026 to maximize the mismatch between calendar arithmetic and fixed-second arithmetic. This test will FAIL with the current code and PASS after the fix. Related to lynndylanhurley#1677
This fixes the bug where newly created tokens were immediately filtered out when token_lifespan was set to variable-length durations like 6.months. Changed line 265 to use calendar arithmetic matching TokenFactory.expiry: - Before: Time.now.to_i + DeviseTokenAuth.token_lifespan.to_i - After: (Time.zone.now + DeviseTokenAuth.token_lifespan).to_i This ensures consistency between token creation and cleanup. Fixes lynndylanhurley#1677
|
@naanlizard I just encountered this bug as well. Thank you for the fix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #1677
This PR fixes a bug introduced in v1.2.6 (PR #1657, commit 9719d24) where newly created tokens are immediately filtered out when
token_lifespanis set to variable-length durations like6.months.Commits
6.monthsduration with time travel to March 1st, 2026Problem
There was an arithmetic mismatch between two methods:
TokenFactory.expiryuses:(Time.zone.now + lifespan).to_i(calendar arithmetic)clean_old_tokensused:Time.now.to_i + lifespan.to_i(fixed-second arithmetic)For
6.monthsstarting March 1st:6.months.to_i= 15,778,476 seconds = 182.62 daysThis caused the filter at line 268 to reject newly created tokens because their expiry (calculated with calendar arithmetic) exceeded the
max_lifespan_expiry(calculated with fixed-second arithmetic).Solution
Changed line 265 in
app/models/devise_token_auth/concerns/user.rbto use the same calendar arithmetic asTokenFactory.expiry:Impact
6.months,1.year)2.weeks,30.days)max_number_of_deviceslimit is reachedReproduction
A complete standalone reproduction case is available at: https://github.com/naanlizard/dta-bug-repro
The reproduction uses a fixed date (March 1st, 2026) to consistently demonstrate the bug.
Testing
The first commit adds a test that will FAIL, demonstrating the bug. The second commit (coming shortly) will apply the fix and make the test PASS.