standard_id 0.38.0 → 0.39.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea8084bb866fc4da82d81ec18d2b24676e3209ac8d21975c20deb9aa334160fc
4
- data.tar.gz: 1cc922486ca4204c8308ff489dc5f425424c06e758a1a4b77276b6670a637f10
3
+ metadata.gz: aafcc86281b43717bf4afeae67e469f9f773f62bb674137adce6b7924eb24be2
4
+ data.tar.gz: 1236e399e761f545b7c2c749d44bd317f48355650b1f76accbf22704940f00d5
5
5
  SHA512:
6
- metadata.gz: 3e030d75f3f08d7961ac1cab6ede04da0940ee5471191bfc5c06ef832469f6da2a9af44539fa04e889801defded08c8bdf138c7a8bd1661d8c0628c5b707d08f
7
- data.tar.gz: b5301f0db610433bcb542699f95beddcd9e91e93dda098f940edafc9af5c70ab264ef6c70b427e84ac34a9edae861316cf3d608f4f5c2dad6996eb6b9780cbcf
6
+ metadata.gz: 0cbb9b1a8fd120918f0dbf986822b70990105c6e288ede4b8f2fc790b82cfa2f5ded7cbc774ae4e18afc6e03b29f40e93ffc736d8fd7021ce2ae81f10b03a59c
7
+ data.tar.gz: dcbdbea82b00733fb9de47a2ba1b4f44362f819ee27ccac2bbb3b1c10573a225afac8d98c50644648ca9756d5a15ecc4262448ff899632f109e8510f2edf5fcd
data/CHANGELOG.md CHANGED
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.39.0] - 2026-08-10
11
+
12
+ ### Added
13
+
14
+ - **A refresh whose RESPONSE never arrived no longer costs the user their session.** Rotation assumes the client receives the new token. When it does not — a timeout, a dead radio, the process dying between our `COMMIT` and the client's write — the server has rotated and the client still holds the previous token. Its next refresh presents an already-rotated token, which is indistinguishable from an attacker replaying a stolen one.
15
+
16
+ Treating both as an attack costs a healthy session, and the cost is total: `revoke_family!` also kills the **successor the client never received**, so the session cannot be recovered by retrying — every attempt re-presents the same dead token against an already-revoked family. `spec/lib/standard_id/refresh_rotation_lost_response_spec.rb` pins that behaviour before the fix, because it is the part that surprises.
17
+
18
+ `config.oauth.refresh_token_reuse_leeway` (default **`0` — off**) allows a replayed token to rotate from its successor instead, but ONLY while that successor is untouched: it must still be active and never itself have been rotated. A **used** successor proves the legitimate client received it, so anything presented afterwards is a genuine replay and the family dies exactly as before. The value is clamped to `RefreshTokenFlow::MAX_REUSE_LEEWAY_SECONDS` (120).
19
+
20
+ - **`OAUTH_REFRESH_TOKEN_REUSE_GRACED`** is published when the leeway fires. Graced replays are the quiet half of the feature: without an event, the only evidence the leeway is load-bearing is an absence of complaints.
21
+
22
+ ### Notes for hosts
23
+
24
+ - **Default-off means this release is inert on upgrade for every consumer.** Existing reuse-detection specs pass unchanged. A host opts in explicitly.
25
+
26
+ - **Enabling it is a deliberate security narrowing, and worth understanding before you do.** It does not claim to distinguish an attacker from an unlucky client — the server cannot. It bounds the damage. An attacker replaying inside the window gets a session, but the real client still holds the successor, and the moment it refreshes, that token is revoked-and-reused: the family dies and the user re-authenticates. Exposure is one refresh interval, against the current guarantee that the *honest* client loses its session.
27
+
28
+ - **Re-delivering the successor — the ideal, idempotent answer — is not possible here.** Only its digest is stored, by design, so the token string is unrecoverable. Rotating from it is the closest safe equivalent; the unused successor is retired rather than left live.
29
+
10
30
  ## [0.38.0] - 2026-08-06
11
31
 
12
32
  ### Added
@@ -285,6 +285,13 @@ StandardId::ConfigSchema.define do
285
285
  scope :oauth do
286
286
  field :default_token_lifetime, type: :integer, default: 3600 # 1 hour in seconds
287
287
  field :refresh_token_lifetime, type: :integer, default: 2592000 # 30 days in seconds
288
+ # Seconds for which a just-rotated refresh token is still honoured, so a
289
+ # client that never RECEIVED its successor (dropped response, killed process)
290
+ # can retry instead of losing the session to reuse detection. 0 = off, which
291
+ # is the default: this trades a bounded replay window for resilience and no
292
+ # host should get it by upgrading. Clamped to
293
+ # RefreshTokenFlow::MAX_REUSE_LEEWAY_SECONDS.
294
+ field :refresh_token_reuse_leeway, type: :integer, default: 0
288
295
  field :token_lifetimes, type: :hash, default: -> { {} }
289
296
  field :client_id, type: :string, default: nil
290
297
  field :client_secret, type: :string, default: nil
@@ -41,6 +41,7 @@ module StandardId
41
41
  OAUTH_CODE_CONSUMED = "oauth.code.consumed"
42
42
  OAUTH_TOKEN_REVOKED = "oauth.token.revoked"
43
43
  OAUTH_REFRESH_TOKEN_REUSE_DETECTED = "oauth.refresh_token.reuse_detected"
44
+ OAUTH_REFRESH_TOKEN_REUSE_GRACED = "oauth.refresh_token.reuse_graced"
44
45
  OAUTH_AUDIENCE_MISMATCH = "oauth.audience.mismatch"
45
46
 
46
47
  PASSWORDLESS_CODE_REQUESTED = "passwordless.code.requested"
@@ -116,6 +117,7 @@ module StandardId
116
117
  OAUTH_CODE_CONSUMED,
117
118
  OAUTH_TOKEN_REVOKED,
118
119
  OAUTH_REFRESH_TOKEN_REUSE_DETECTED,
120
+ OAUTH_REFRESH_TOKEN_REUSE_GRACED,
119
121
  OAUTH_AUDIENCE_MISMATCH
120
122
  ].freeze
121
123
 
@@ -1,6 +1,11 @@
1
1
  module StandardId
2
2
  module Oauth
3
3
  class RefreshTokenFlow < TokenGrantFlow
4
+ # Ceiling on `oauth.refresh_token_reuse_leeway`. A long leeway is a long
5
+ # window in which a stolen token still works, so the setting is clamped
6
+ # rather than trusted: this is a resilience allowance, not a lifetime.
7
+ MAX_REUSE_LEEWAY_SECONDS = 120
8
+
4
9
  expect_params :refresh_token, :client_id
5
10
  permit_params :client_secret, :scope, :audience
6
11
 
@@ -58,10 +63,30 @@ module StandardId
58
63
  end
59
64
 
60
65
  if @current_refresh_token_record.revoked?
61
- # Reuse detected: this token was already rotated. Revoke entire family.
62
- @current_refresh_token_record.revoke_family!
63
- emit_reuse_detected_event
64
- raise StandardId::InvalidGrantError, "Refresh token reuse detected"
66
+ # A rotated token can be presented for two reasons the server cannot tell
67
+ # apart: an attacker replaying a stolen one, or an honest client that
68
+ # never RECEIVED the successor — a timeout, a dead radio, the process
69
+ # dying between our COMMIT and the client's write. Rotation assumes
70
+ # delivery; nothing guarantees it.
71
+ #
72
+ # Treating both as an attack costs a healthy session: revoke_family!
73
+ # also kills the successor the client never saw, so a single dropped
74
+ # response becomes a forced re-login that no client-side retry or
75
+ # failure-budget can recover from (see
76
+ # spec/lib/standard_id/refresh_rotation_lost_response_spec.rb).
77
+ #
78
+ # So, within a short leeway, rotate from the successor instead — but
79
+ # ONLY while that successor is untouched (see #graced_successor_for).
80
+ # Disabled by default: a host must opt in.
81
+ if (successor = graced_successor_for(@current_refresh_token_record))
82
+ emit_reuse_graced_event(@current_refresh_token_record, successor)
83
+ @current_refresh_token_record = successor
84
+ else
85
+ # Reuse detected: this token was already rotated. Revoke entire family.
86
+ @current_refresh_token_record.revoke_family!
87
+ emit_reuse_detected_event
88
+ raise StandardId::InvalidGrantError, "Refresh token reuse detected"
89
+ end
65
90
  end
66
91
 
67
92
  unless @current_refresh_token_record.active?
@@ -151,6 +176,77 @@ module StandardId
151
176
  raise StandardId::InvalidGrantError, "Refresh token is no longer valid"
152
177
  end
153
178
 
179
+ # The successor to a revoked token, when re-issuing from it is safer than
180
+ # revoking the family — otherwise nil, and reuse detection proceeds unchanged.
181
+ #
182
+ # Every condition here narrows the window in which a replayed token is
183
+ # honoured:
184
+ #
185
+ # - **Opt-in.** Zero (the default) disables this entirely, so no existing
186
+ # host changes behaviour by upgrading.
187
+ # - **Recently rotated.** Outside the leeway a replay is not a plausible
188
+ # in-flight retry.
189
+ # - **Successor still active, and never itself rotated.** This is the load-
190
+ # bearing one. If the successor has been USED, the legitimate client
191
+ # demonstrably received it, so the token being replayed now is a replay,
192
+ # not a lost response — and the family dies as before.
193
+ #
194
+ # What this deliberately does NOT do is claim to distinguish an attacker
195
+ # from an unlucky client; the server cannot. It bounds the damage instead.
196
+ # An attacker replaying inside the window gets a session, but the real
197
+ # client still holds the successor, and the moment it refreshes, that token
198
+ # is revoked-and-reused: the family dies and the user re-authenticates.
199
+ # The exposure is therefore one refresh interval, not indefinite — versus
200
+ # today, where the honest client is guaranteed to lose its session.
201
+ #
202
+ # The successor cannot simply be RE-DELIVERED, which would be the ideal
203
+ # (idempotent) answer: only its digest is stored, by design, so its token
204
+ # string is unrecoverable. Rotating from it is the closest safe equivalent.
205
+ def graced_successor_for(revoked_record)
206
+ leeway = reuse_leeway_seconds
207
+ return nil unless leeway.positive?
208
+ return nil if revoked_record.revoked_at.blank?
209
+ return nil if revoked_record.revoked_at < leeway.seconds.ago
210
+
211
+ successor = StandardId::RefreshToken.find_by(previous_token_id: revoked_record.id)
212
+ return nil unless successor&.active?
213
+ return nil if StandardId::RefreshToken.exists?(previous_token_id: successor.id)
214
+
215
+ successor
216
+ end
217
+
218
+ # Seconds for which a just-rotated token is still honoured. Absent or
219
+ # non-positive config means OFF — reuse detection behaves exactly as it did
220
+ # before this existed.
221
+ def reuse_leeway_seconds
222
+ config = StandardId.config.oauth
223
+ return 0 unless config.respond_to?(:refresh_token_reuse_leeway)
224
+
225
+ value = config.refresh_token_reuse_leeway
226
+ seconds =
227
+ case value
228
+ when ActiveSupport::Duration then value.to_i
229
+ when Numeric, String then value.to_i
230
+ else 0
231
+ end
232
+ return 0 unless seconds.positive?
233
+
234
+ [seconds, MAX_REUSE_LEEWAY_SECONDS].min
235
+ end
236
+
237
+ # Graced replays are the quiet half of this feature: nothing fails, so
238
+ # without an event the only evidence a host has that the leeway is load-
239
+ # bearing (or mis-tuned) is its absence of complaints.
240
+ def emit_reuse_graced_event(replayed, successor)
241
+ StandardId::Events.publish(
242
+ StandardId::Events::OAUTH_REFRESH_TOKEN_REUSE_GRACED,
243
+ account_id: @refresh_payload[:sub],
244
+ client_id: @refresh_payload[:client_id],
245
+ refresh_token_id: replayed.id,
246
+ successor_refresh_token_id: successor.id
247
+ )
248
+ end
249
+
154
250
  def emit_reuse_detected_event
155
251
  StandardId::Events.publish(
156
252
  StandardId::Events::OAUTH_REFRESH_TOKEN_REUSE_DETECTED,
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.38.0"
2
+ VERSION = "0.39.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.38.0
4
+ version: 0.39.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim