Skip to content

[WORK-11511] feat(token): reject tokens past a fixed expires_at - #8

Closed
murilooon wants to merge 2 commits into
mainfrom
session-fixed-expiry
Closed

murilooon wants to merge 2 commits into
mainfrom
session-fixed-expiry

Conversation

@murilooon

@murilooon murilooon commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What

Devise::Strategies::TokenAuthenticatable#unexpired? now rejects a token whose model has an expires_at value once Time.current > expires_at.

  • The sliding expiry (last_used_at + expires_in) still applies. Whichever expiry comes first ends the session.
  • The fixed check runs before the expiration_disabled? early return. So a token with no expires_in is still rejected after its expires_at.
  • A nil expires_at, or a token model without the column, behaves exactly as before.
  • A rejected token is not touched, so it cannot extend itself.
  • Released as 1.9.0: CHANGELOG (the Unreleased notes move under 1.9.0), version.rb and a README section on expires_at.

Why

Asana: https://app.asana.com/1/303134204339906/project/1220308862188378/task/1220303649568339

This is part of Works session expiration, Phase 1: every session ends at a fixed time decided at sign-in. Works will set expires_at through Tiddle's metadata: when it issues a token. The identity_auth_tokens column ships separately, in WORK-11508. Until both ship, Works behaves as it does today.

Note for the Works bump (WORK-11944): Works pins 947151d, which is not an ancestor of the current main. main was rewritten in July: #5, #6 and #7 were squashed into 98e986d. So the bump also brings in 98e986d: hash-style authentication_keys support, and required_ruby_version >= 3.3 / activerecord >= 7.2, which Works already meets.

Verification

Check Result
BUNDLE_GEMFILE=gemfiles/rails8.1.gemfile bundle exec rake (rubocop + specs, ActiveRecord) no offenses; 38 examples, 0 failures
Same suite with the strategy.rb change reverted the 3 specs that need the fix fail (past expires_at, no touch, past expires_at while expires_in has not passed)
rake locally on gemfiles/rails7.2.gemfile and rails8.0.gemfile (after the json pin below) no offenses; 38 examples, 0 failures each
CI matrix (18 jobs) at 5de5a97 all pass
Mongoid backend not run locally (no MongoDB); CI runs it

New specs cover:

  • expires_at in the future and in the past
  • past expires_at with expires_in not yet reached
  • future expires_at with the inactivity period already passed
  • nil expires_at, with the sliding expiry still enforced
  • a token model without the column

CI fix: json below 3 for Rails 7.2 and 8.0

CI had drifted since July, and this change did not cause it. json 3.x dropped the quirks_mode: keyword, which ActiveSupport 7.2.3 and 8.0 still pass to JSON.generate. So every Devise 401 JSON body raised ArgumentError, the request returned a 500, and every spec that expects a 401 failed. Plain origin/main fails the same way. Rails 8.1 no longer passes the keyword, so its jobs stayed green.

The fix pins gem "json", "< 3" in gemfiles/rails7.2.gemfile and gemfiles/rails8.0.gemfile only. It is test-only and does not touch the gemspec. Works runs Rails 8.1 with json 2.21.2, so it is not affected.

Security review

The security review (Works security-review skill on this diff) found no vulnerabilities.

  • The change only narrows when a token is accepted. Token minting and sign-out are unchanged.
  • A missing or nil expires_at falls back to the existing check. A past expires_at is checked first and denies.
  • expires_at is written only by the server through metadata. No request input reaches it.
  • No PII or credentials; the only migration change is in the spec dummy app; CI workflows unchanged.
  • Brakeman: not applicable to a gem with no app.

Reviews

Review Verdict Findings fixed
security-review No findings —
correctness-review Pass; 1 Low — (Low: purge_old_tokens keeps tokens by newest last_used_at, so a token past expires_at can outrank a live idle one for users with more than 20 tokens; pre-existing code, left as a follow-up)

🤖 Generated with Claude Code

A token whose model has an expires_at value is rejected once that time
has passed. The inactivity expiry from expires_in still applies, and the
earlier of the two ends the session. A nil value, or a model without the
column, behaves as before. Release 1.9.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@murilooon murilooon self-assigned this Nov 24, 2026
json 3 dropped the quirks_mode keyword that ActiveSupport 7.2 and 8.0
still pass to JSON.generate, so every Devise 401 JSON body raised and
became a 500. Rails 8.1 no longer passes it and keeps json 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@murilooon
murilooon marked this pull request as ready for review November 24, 2026 21:14
@murilooon

Copy link
Copy Markdown
Member Author

Closing: we're dropping the trusted/tiddle fork. Upstream tiddle 1.9.0 has the same code, and the fixed-expiry check moves into the identity engine (trusted/rails-identity-service#98).

@murilooon murilooon closed this Nov 25, 2026
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