standard_id 0.34.0 → 0.35.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: 984924490081d96a84ec4570541802d70414cac9dcf9dd325b8aba6ee7af7be2
4
- data.tar.gz: c132d58daeea53090156fe42bfd3b6a7b8d8a2393d7ae7a3766e0c4ece0e8a6d
3
+ metadata.gz: e5e5289f6f264b4175abb757c1423d9fc656e5b2e5057239e7486d843a3eaf7a
4
+ data.tar.gz: 209d3a72e8a16049ef1e633216c38939e69518e526baea0110dd99567ac23083
5
5
  SHA512:
6
- metadata.gz: 65dac8337e2bfa0784d43308d4c29eeeb8208c3b06b721a3f1a2f040eec1916ba35c00b8e69945f72c80a7240b95cb17ed4bf46c6939fe8c78508c44bf3363b9
7
- data.tar.gz: eb1acc6703201fa5955494aec635f1821ea4d9689b49f73703736f853deb166b5eeaec1adcb20fe79c083f15903301e60829da0497ec0e138af49d2f406e4b02
6
+ metadata.gz: bdc0a65782aa7ea1299b49b0eb946dc72e56391de6e7d5132b65e59e27281d9d16a5ef28e1d75fadca282768d853a95d703b1c81278b8008699ddfeb05ab02e1
7
+ data.tar.gz: ed73c2092285091f034f9d0b33423e4013004a6e129c37220d8a004f94e3c9bb79b1d9d95679f4e18f126db28243201f80cc7126780225e5114b6439a74ff214
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.35.0] - 2026-07-31
11
+
12
+ ### Security
13
+
14
+ - **A refresh token is now refused when its parent session is revoked or expired.** `Oauth::RefreshTokenFlow` validated the `RefreshToken` row only — found, not revoked, not expired — and never consulted the session it belongs to.
15
+
16
+ So "revoking a session ends that session's access" was not a property of the gem. It was an emergent consequence of every caller reaching for `Session#revoke!` (a transaction that ALSO does `refresh_tokens.active.update_all(revoked_at:)`) rather than the obvious-looking `session.update!(revoked_at: ...)`, which revokes the session row and leaves its refresh tokens live. Two apps wrote the second form independently and shipped it (rarebit-one/nutripod-web#1100, luminalityai/luminality-web#1048), because the natural spec asserts the session's own `revoked_at` — exactly the half that does work.
17
+
18
+ On the device path that meant a refresh-token holder kept minting access tokens after the user had explicitly signed that device out. Checking the parent in the flow makes the property true by construction: it now holds however the session was revoked — `revoke!`, a bare `update!`, a bulk `update_all`, a DBA, a data fix — and covers session expiry too.
19
+
20
+ **Unaffected:** refresh tokens with no session (`session_id` nil) — the machine-to-machine shape, including `client_credentials`. There is no parent to outlive, so nothing is checked. No configuration flag was added because no flow in the gem legitimately needs a refresh to outlive its session; every token that carries a `session_id` was minted against that session, and rotation carries the same `session_id` forward.
21
+
22
+ The refusal reuses the existing `invalid_grant` / `"Refresh token is no longer valid"` response verbatim (RFC 6749 §5.2). A distinct error would be an oracle — it would tell a holder that the token itself is still good and only the session was pulled.
23
+
24
+ The parent session is fetched by `eager_load` in the same query as the token row, so the hot `/oauth/token` path gains no extra round trip.
25
+
26
+ Tracked in rarebit-one/rarebit-ops#297.
27
+
28
+ - **`POST /oauth/introspect` reports the same refresh token as `active: false`.** The RFC 7662 endpoint (off by default, behind `config.oauth.introspection_enabled`) checked the `RefreshToken` row and not its session, so it had the identical gap. Left alone it would have reported `active: true` for a token `/oauth/token` now refuses — introspection contradicting the endpoint it describes. Access tokens are unchanged and still introspect as active until their `exp`; that documented limit is unaffected, because they are stateless and carry no `sid`.
29
+
10
30
  ## [0.34.0] - 2026-07-31
11
31
 
12
32
  ### Added
@@ -32,8 +32,12 @@ module StandardId
32
32
  # lifetime.
33
33
  #
34
34
  # Refresh tokens ARE persisted (as `SHA256(jti)`), so those are checked
35
- # against the row: a revoked or expired refresh token introspects as
36
- # inactive, correctly and immediately.
35
+ # against the row AND against the parent session: a refresh token that is
36
+ # itself revoked or expired — or whose session is — introspects as
37
+ # inactive, correctly and immediately. That second half matters because
38
+ # the session can be revoked without the cascade ever running (a bare
39
+ # `update!`, a bulk `update_all`); see Oauth::RefreshTokenFlow and
40
+ # rarebit-one/rarebit-ops#297.
37
41
  class IntrospectionsController < BaseController
38
42
  public_controller
39
43
 
@@ -86,8 +90,21 @@ module StandardId
86
90
  return render_inactive if payload.nil?
87
91
 
88
92
  # Persisted refresh tokens are checkable; access tokens are not.
89
- persisted = StandardId::RefreshToken.find_by_jti(payload[:jti].to_s) if payload[:jti].present?
93
+ #
94
+ # eager_load the parent session in the same query: the answer must
95
+ # match what Oauth::RefreshTokenFlow would do with the same token, and
96
+ # that flow refuses a refresh whose session is revoked or expired
97
+ # (rarebit-one/rarebit-ops#297). Reporting `active: true` here for a
98
+ # token /oauth/token would refuse would make introspection an
99
+ # authority that disagrees with the endpoint it describes.
100
+ #
101
+ # A nil session means no parent — the machine-to-machine shape — and
102
+ # is not a reason to call the token inactive.
103
+ if payload[:jti].present?
104
+ persisted = StandardId::RefreshToken.eager_load(:session).find_by_jti(payload[:jti].to_s)
105
+ end
90
106
  return render_inactive if persisted && !persisted.active?
107
+ return render_inactive if persisted&.session && !persisted.session.active?
91
108
 
92
109
  render json: active_response(payload, persisted), status: :ok
93
110
  end
@@ -47,7 +47,11 @@ module StandardId
47
47
  # once all pre-jti tokens have expired (refresh_token_lifetime after deploy).
48
48
  return if jti.blank?
49
49
 
50
- @current_refresh_token_record = StandardId::RefreshToken.find_by_jti(jti)
50
+ # eager_load (not includes/lazy) so the parent session arrives in the
51
+ # SAME query via a LEFT OUTER JOIN. #validate_parent_session! below
52
+ # reads it on every refresh, and /oauth/token is a hot path — a lazy
53
+ # `.session` would add a second SELECT per request.
54
+ @current_refresh_token_record = StandardId::RefreshToken.eager_load(:session).find_by_jti(jti)
51
55
 
52
56
  unless @current_refresh_token_record
53
57
  raise StandardId::InvalidGrantError, "Refresh token not found"
@@ -63,6 +67,41 @@ module StandardId
63
67
  unless @current_refresh_token_record.active?
64
68
  raise StandardId::InvalidGrantError, "Refresh token is no longer valid"
65
69
  end
70
+
71
+ validate_parent_session!
72
+ end
73
+
74
+ # Refuse a refresh whose parent session is no longer active.
75
+ #
76
+ # Without this, "revoking a session ends that session's access" was not a
77
+ # property of the gem at all — it was an emergent consequence of every
78
+ # caller reaching for Session#revoke! (a transaction that ALSO does
79
+ # `refresh_tokens.active.update_all(revoked_at:)`) rather than the
80
+ # obvious-looking `session.update!(revoked_at:)`, which revokes the
81
+ # session row and leaves its refresh tokens live. Two apps wrote the
82
+ # second form independently and shipped it, because a spec asserting the
83
+ # session's own `revoked_at` passes against the buggy code.
84
+ #
85
+ # Checking the parent here makes the property true by construction: it
86
+ # holds however the session was revoked — `revoke!`, a bare `update!`, a
87
+ # bulk `update_all`, a DBA, a data fix — and it covers expiry as well.
88
+ # See rarebit-one/rarebit-ops#297.
89
+ #
90
+ # A refresh token with no session (`session_id` nil) is unaffected: that
91
+ # is the machine-to-machine shape (client_credentials and any other grant
92
+ # the gem issues without persisting a session), where there is no parent
93
+ # to outlive and nothing to check.
94
+ #
95
+ # The message is deliberately IDENTICAL to the inactive-token case above.
96
+ # A distinct error would be an oracle: it would tell an attacker holding a
97
+ # stolen refresh token that the token itself is still good and only the
98
+ # session was pulled. RFC 6749 §5.2 wants `invalid_grant` either way.
99
+ def validate_parent_session!
100
+ session = @current_refresh_token_record.session
101
+ return if session.nil?
102
+ return if session.active?
103
+
104
+ raise StandardId::InvalidGrantError, "Refresh token is no longer valid"
66
105
  end
67
106
 
68
107
  # Atomically revoke the current token as part of rotation.
@@ -1,3 +1,3 @@
1
1
  module StandardId
2
- VERSION = "0.34.0"
2
+ VERSION = "0.35.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.34.0
4
+ version: 0.35.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim