fix: backdate JWT iat to avoid clock-skew 401s#64
Merged
Conversation
The server auth token stamped `iat = Time.now.to_i`. Because `iat` is a whole-second value (RFC 7519 NumericDate) and the server applies minimal forward leeway, a small fraction of requests were rejected with "token used before issue at (iat)" (HTTP 401) whenever the caller's clock was marginally ahead of the server and the second-truncation landed on a boundary. Observed at ~0.03% of requests, spread uniformly across all caller hosts. Backdate `iat` by Client::AUTH_IAT_LEEWAY_SECONDS (5s) so the token is always safely behind the server clock. The legacy stream-chat-ruby client never sent `iat`, which is why upgrades from it newly exposed this. Co-authored-by: Cursor <cursoragent@cursor.com>
Fixes Lint/UselessConstantScoping: `private` does not affect constants, so the constant is declared at the top of the class instead. Co-authored-by: Cursor <cursoragent@cursor.com>
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
Backdate the JWT
iatclaim on server auth tokens by a few seconds so tokens are never stamped ahead of the server clock.iatis a whole-second value (RFC 7519 NumericDate), soTime.now.to_itruncates to the second. With no safety margin, a caller whose clock is marginally ahead of the server can intermittently gettoken used before issue at (iat)(HTTP 401) when the truncation lands on a second boundary. BackdatingiatbyClient::AUTH_IAT_LEEWAY_SECONDS(5s) keeps it safely in the past.Changes
Client#generate_auth_header: stampiat: Time.now.to_i - AUTH_IAT_LEEWAY_SECONDS.spec/auth_token_spec.rbassertingiatis backdated and never ahead of "now".[Unreleased] / Fixed.Test plan
bundle exec rspec spec/auth_token_spec.rb