keycardai-oauth 0.11.0 → 0.11.2

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: 013a407a3595ae3e9b735f95e216187d3534d9ff9cf52298cbd4071a1cd92994
4
- data.tar.gz: 33080c7a3643832e8601d5d20eb95fec4fd556d2e69cacb984772da6f6dc7e4b
3
+ metadata.gz: bfa5ca1a750441c826d0534dba37195836c84689d881851aa317e114a5b0c1db
4
+ data.tar.gz: 89b2adb8d071b31f6ecd79894bddacdd8915bc1fa4d209a3f59a032969784e24
5
5
  SHA512:
6
- metadata.gz: '01392024fcce36fb9788c654f66611d0d88ba635616d794c00f3a644231d64f965a70c54f5f005dc690540784a49f3e64289599781c8042de40ca88ec45a9b8d'
7
- data.tar.gz: 295acc954c463bffa89b83db3781aeff4b8d59bba93ce1c645b99f5ae8930dd9bd600c9f624abb3a55e676a282014b30b3680d422aee17179cd41bc89d0ccb83
6
+ metadata.gz: c59d40c970a41e9e4d45ffa06f2298a9e477900865da326a081f7283aa30d663ad64bb3dc14c42a64817f29530332344f743b48729dcb018506e351ca1246f17
7
+ data.tar.gz: df07a17a41cfed346146555fb82745fc311f6ec08241f54c4a2f282e29544d5a6f23aaf76c60ab6e31d060e1c4e4123bd5dfe0904e1b730120cd56a7b7fbf4a9
data/CHANGELOG.md CHANGED
@@ -15,6 +15,31 @@ the loopback flow (RFC 8252), JWT signing and verification with a caching JWKS
15
15
  keyring, the three application credentials (ClientSecret with multi-zone,
16
16
  WebIdentity, WorkloadIdentity with pluggable token sources), and AccessContext.
17
17
 
18
+ ## 0.11.2-keycardai-oauth (2026-11-25)
19
+
20
+
21
+ - fix(keycardai-oauth): cap decoded authorization-server responses at 1 MiB (#81)
22
+ - * fix(keycardai-oauth): cap decoded authorization-server responses at 1 MiB
23
+ - Co-Authored-By: Larry Osakwe <larry@keycard.ai>
24
+ - * fix(keycardai-oauth): fix chunked overflow cleanup and multi-member gzip inflation
25
+ - Co-Authored-By: Larry Osakwe <larry@keycard.ai>
26
+ - * fix(keycardai-oauth): capture non-BufError Zlib errors inside BoundedBodyReader
27
+ - Co-Authored-By: Larry Osakwe <larry@keycard.ai>
28
+ - * fix(keycardai-oauth): map oversized metadata server replies and cover device polling overflows
29
+ - ---------
30
+ - Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
31
+ Co-authored-by: Larry Osakwe <larry@keycard.ai>
32
+ Co-authored-by: Larry Osakwe <23560354+Larry-Osakwe@users.noreply.github.com>
33
+
34
+ ## 0.11.1-keycardai-oauth (2026-11-25)
35
+
36
+
37
+ - fix(keycardai-oauth): pass the explicit issuer to the device credential (#80)
38
+ - When both issuer and metadata are given and match ignoring a trailing slash, DeviceAuthorizationClient now passes the issuer as supplied to the credential's client_authentication instead of metadata.issuer. A mismatch still raises ConfigurationError at construction.
39
+ - Implements keycard-sdk-spec#140, specs/oauth-client/device-authorization.md unit row 35. SDK-124.
40
+ - Co-authored-by: devin-ai-keycard <devin-ai@keycard.ai>
41
+ Co-authored-by: Larry Osakwe <larry@keycard.ai>
42
+
18
43
  ## 0.11.0-keycardai-oauth (2026-11-25)
19
44
 
20
45
 
@@ -127,8 +127,9 @@ module Keycardai
127
127
  CANCELLATION_CHECK_INTERVAL = 0.5
128
128
 
129
129
  # @param issuer [String, nil] the zone's issuer URL; required unless
130
- # metadata is given. With metadata, the client uses metadata.issuer
131
- # and this must match it, ignoring a trailing slash.
130
+ # metadata is given. With metadata, this must match metadata.issuer,
131
+ # ignoring a trailing slash, and the credential gets this spelling.
132
+ # Without it, the credential gets metadata.issuer.
132
133
  # @param metadata [AuthorizationServerMetadata, nil] pre-discovered
133
134
  # metadata; no discovery request is made
134
135
  # @param credential [#client_authentication, nil] authenticates every
@@ -298,7 +299,8 @@ module Keycardai
298
299
  return issuer
299
300
  end
300
301
  raise ConfigurationError, "the metadata has no issuer" unless metadata.issuer.is_a?(String)
301
- return metadata.issuer if issuer.nil? || issuer.chomp("/") == metadata.issuer.chomp("/")
302
+ return metadata.issuer if issuer.nil?
303
+ return issuer if issuer.chomp("/") == metadata.issuer.chomp("/")
302
304
 
303
305
  raise ConfigurationError, "issuer #{issuer} does not match the metadata issuer #{metadata.issuer}"
304
306
  end
@@ -380,6 +382,10 @@ module Keycardai
380
382
  rescue NetworkError => e
381
383
  # RFC 8628 §3.5: wait longer after a connection timeout.
382
384
  raise DevicePollError.new("device token poll failed: #{e.message}", interval: interval * 2)
385
+ rescue ResponseBodyTooLargeError => e
386
+ raise unless e.status == 429 || e.status.to_i >= 500
387
+
388
+ raise DevicePollError.new("device token poll failed: #{e.message}", interval: interval)
383
389
  end
384
390
 
385
391
  # The next step after a poll that raised +error+: poll again, or stop.
@@ -82,6 +82,10 @@ module Keycardai
82
82
  # request after this.
83
83
  class CancelledError < Keycardai::Error; end
84
84
 
85
+ # Maximum decoded byte length read from any authorization-server HTTP
86
+ # response (1 MiB).
87
+ MAX_RESPONSE_BODY_BYTES = 1_048_576
88
+
85
89
  # The server's response violates the protocol contract: metadata with a
86
90
  # mismatched or missing issuer, a token response with no access token.
87
91
  # The +code+ discriminates the case (issuer_mismatch, invalid_metadata,
@@ -89,10 +93,33 @@ module Keycardai
89
93
  class ProtocolError < Keycardai::Error
90
94
  # @return [String]
91
95
  attr_reader :code
96
+ # @return [Integer, nil]
97
+ attr_reader :status
92
98
 
93
- def initialize(message, code:)
99
+ def initialize(message, code:, status: nil)
94
100
  super(message)
95
101
  @code = code
102
+ @status = status
103
+ end
104
+
105
+ # @return [Boolean] whether the failure is transient based on HTTP status
106
+ def retryable?
107
+ return false unless status
108
+
109
+ (200..299).cover?(status) || status == 429 || status >= 500
110
+ end
111
+ end
112
+
113
+ # Raised when a decoded authorization-server HTTP response body exceeds
114
+ # MAX_RESPONSE_BODY_BYTES while streaming.
115
+ class ResponseBodyTooLargeError < ProtocolError
116
+ # @return [Integer]
117
+ attr_reader :limit
118
+
119
+ def initialize(status:, limit: MAX_RESPONSE_BODY_BYTES)
120
+ @limit = limit
121
+ super("response body exceeded the #{limit}-byte decoded size limit (HTTP #{status})",
122
+ code: "invalid_response", status: status)
96
123
  end
97
124
  end
98
125
 
@@ -61,6 +61,122 @@ module Keycardai
61
61
  end
62
62
  private_class_method :percent_encode
63
63
 
64
+ # Streams a Net::HTTP response body up to MAX_RESPONSE_BODY_BYTES
65
+ # decoded bytes, inflating gzip/deflate incrementally so an oversized
66
+ # response stops before buffering or parsing.
67
+ module BoundedBodyReader
68
+ DECOMPRESSED = %w[gzip x-gzip deflate].freeze
69
+
70
+ module_function
71
+
72
+ def read(response, limit: MAX_RESPONSE_BODY_BYTES)
73
+ if DECOMPRESSED.include?(response["content-encoding"]&.downcase)
74
+ read_compressed(response, limit: limit)
75
+ else
76
+ read_plain(response, limit: limit)
77
+ end
78
+ end
79
+
80
+ def read_plain(response, limit:)
81
+ decoded = String.new(encoding: Encoding::BINARY)
82
+ each_chunk(response) { |chunk| append(decoded, chunk, response, limit) }
83
+ [decoded, nil]
84
+ end
85
+
86
+ def read_compressed(response, limit:)
87
+ decoded = String.new(encoding: Encoding::BINARY)
88
+ state = {
89
+ saw_bytes: false, raw_bytes: 0, body_error: nil,
90
+ response: response, limit: limit, inflate: Zlib::Inflate.new(32 + Zlib::MAX_WBITS),
91
+ gzip: %w[gzip x-gzip].include?(response["content-encoding"]&.downcase)
92
+ }
93
+ begin
94
+ each_chunk(response) { |chunk| consume_compressed(chunk, decoded, state) }
95
+ finish_compressed(decoded, state)
96
+ ensure
97
+ state[:inflate].close
98
+ end
99
+ return ["", state[:body_error]] if state[:body_error]
100
+
101
+ strip_encoding_headers(response, decoded)
102
+ [decoded, nil]
103
+ end
104
+
105
+ def consume_compressed(chunk, decoded, state)
106
+ return if chunk.empty?
107
+
108
+ state[:saw_bytes] = true
109
+ state[:raw_bytes] += chunk.bytesize
110
+ if state[:body_error]
111
+ check_limit(state[:raw_bytes], state[:response], state[:limit])
112
+ return
113
+ end
114
+ inflate_loop(chunk, decoded, state)
115
+ rescue Zlib::BufError
116
+ raise
117
+ rescue Zlib::Error => e
118
+ state[:body_error] = e
119
+ end
120
+
121
+ def inflate_loop(data, decoded, state)
122
+ pending = data
123
+ until pending.empty?
124
+ state[:inflate].inflate(pending) { |out| append(decoded, out, state[:response], state[:limit]) }
125
+ break unless state[:inflate].finished?
126
+
127
+ pending = next_gzip_member(state[:inflate].finish, decoded, state)
128
+ end
129
+ end
130
+
131
+ def next_gzip_member(leftover, decoded, state)
132
+ return "" if leftover.empty?
133
+
134
+ stripped = state[:gzip] ? leftover.sub(/\A\x00+/, "") : ""
135
+ unless stripped.start_with?("\x1f\x8b".b)
136
+ check_limit(decoded.bytesize + leftover.bytesize, state[:response], state[:limit])
137
+ return ""
138
+ end
139
+ state[:inflate].close
140
+ state[:inflate] = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
141
+ stripped
142
+ end
143
+
144
+ def finish_compressed(decoded, state)
145
+ return unless state[:saw_bytes] && state[:body_error].nil? && !state[:inflate].finished?
146
+
147
+ state[:inflate].finish { |out| append(decoded, out, state[:response], state[:limit]) }
148
+ rescue Zlib::BufError
149
+ raise
150
+ rescue Zlib::Error => e
151
+ state[:body_error] = e
152
+ end
153
+
154
+ def each_chunk(response, &)
155
+ if response.respond_to?(:read_body)
156
+ response.read_body(&)
157
+ else
158
+ yield response.body.to_s
159
+ end
160
+ end
161
+
162
+ def strip_encoding_headers(response, decoded)
163
+ response.delete("content-encoding")
164
+ response["content-length"] = decoded.bytesize.to_s if response["content-length"]
165
+ end
166
+
167
+ def append(decoded, chunk, response, limit)
168
+ check_limit(decoded.bytesize + chunk.bytesize, response, limit)
169
+ decoded << chunk
170
+ end
171
+
172
+ def check_limit(size, response, limit)
173
+ return if limit.nil? || size <= limit
174
+
175
+ raise ResponseBodyTooLargeError.new(status: response.code.to_i, limit: limit)
176
+ end
177
+ end
178
+ private_constant :BoundedBodyReader
179
+
64
180
  # Default transport backed by Net::HTTP. TLS is used for https URLs.
65
181
  #
66
182
  # Sessions are kept open and reused per instance, per thread, per
@@ -99,7 +215,8 @@ module Keycardai
99
215
  # or a zlib header for each.
100
216
  DECOMPRESSED = %w[gzip x-gzip deflate].freeze
101
217
 
102
- def initialize
218
+ def initialize(max_response_bytes: MAX_RESPONSE_BODY_BYTES)
219
+ @max_response_bytes = max_response_bytes
103
220
  @registry = {}
104
221
  @registry_mutex = Mutex.new
105
222
  end
@@ -161,8 +278,31 @@ module Keycardai
161
278
 
162
279
  def perform(uri, request, timeout)
163
280
  http = session_for(uri)
164
- # Timeouts are per request; a request without one gets the default
165
- # back rather than the previous request's values.
281
+ configure_http(http, timeout, request)
282
+ response, body, body_error = execute_request(http, request)
283
+ build_response(response, body, body_error)
284
+ rescue *TRANSPORT_ERRORS => e
285
+ raise NetworkError, "request to #{uri.host} failed: #{e.class}"
286
+ end
287
+
288
+ def execute_request(http, request)
289
+ yielded = nil
290
+ body = nil
291
+ body_error = nil
292
+ returned = http.request(request) do |res|
293
+ yielded = res
294
+ body, body_error = BoundedBodyReader.read(res, limit: @max_response_bytes)
295
+ end
296
+ return [yielded, body, body_error] if yielded
297
+
298
+ body, body_error = BoundedBodyReader.read(returned, limit: @max_response_bytes)
299
+ [returned, body, body_error]
300
+ rescue ResponseBodyTooLargeError
301
+ finish(http)
302
+ raise
303
+ end
304
+
305
+ def configure_http(http, timeout, request)
166
306
  http.open_timeout = timeout || DEFAULT_TIMEOUT
167
307
  http.read_timeout = timeout || DEFAULT_TIMEOUT
168
308
  http.write_timeout = timeout || DEFAULT_TIMEOUT
@@ -171,31 +311,6 @@ module Keycardai
171
311
  # client decompresses the body itself. Assigning Accept-Encoding
172
312
  # turns off Net::HTTP's decompression.
173
313
  request["Accept-Encoding"] = ACCEPT_ENCODING if request.decode_content
174
- response = http.request(request)
175
- build_response(response, decompress(response))
176
- rescue *TRANSPORT_ERRORS => e
177
- raise NetworkError, "request to #{uri.host} failed: #{e.class}"
178
- rescue Zlib::Error => e
179
- build_response(response, "", e)
180
- end
181
-
182
- # Returns the body, decompressed when its Content-Encoding is gzip or
183
- # deflate. Raises Zlib::BufError when the compressed stream ends
184
- # early, and Zlib::DataError when the body is not compressed data.
185
- def decompress(response)
186
- body = response.body.to_s
187
- return body if body.empty? || !DECOMPRESSED.include?(response["content-encoding"]&.downcase)
188
-
189
- inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
190
- begin
191
- decoded = inflate.inflate(body)
192
- raise Zlib::BufError, "the compressed stream ends early" unless inflate.finished?
193
- ensure
194
- inflate.close
195
- end
196
- response.delete("content-encoding")
197
- response["content-length"] = decoded.bytesize.to_s if response["content-length"]
198
- decoded
199
314
  end
200
315
 
201
316
  def build_response(response, body, body_error = nil)
@@ -172,7 +172,7 @@ module Keycardai
172
172
  def fetch_jwks(jwks_uri)
173
173
  response = begin
174
174
  @http_client.get(jwks_uri, headers: { "Accept" => "application/json" }, timeout: @fetch_timeout)
175
- rescue NetworkError => e
175
+ rescue NetworkError, ResponseBodyTooLargeError => e
176
176
  raise JWKSFetchError, "JWKS fetch from #{jwks_uri} failed: #{e.message}"
177
177
  end
178
178
  raise JWKSFetchError, "JWKS fetch from #{jwks_uri} returned HTTP #{response.status}" unless response.success?
@@ -98,7 +98,7 @@ module Keycardai
98
98
  url = "#{@metadata_url}#{IDENTITY_PATH}?#{URI.encode_www_form("audience" => @audience, "format" => "full")}"
99
99
  response = begin
100
100
  @http_client.get(url, headers: { "Metadata-Flavor" => "Google" }, timeout: @timeout)
101
- rescue NetworkError => e
101
+ rescue NetworkError, ResponseBodyTooLargeError => e
102
102
  raise WorkloadIdentityRuntimeError.new("metadata server unreachable: #{e.message}", source: "gcp-metadata")
103
103
  end
104
104
  unless response.success?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Keycardai
4
4
  module OAuth
5
- VERSION = "0.11.0"
5
+ VERSION = "0.11.2"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keycardai-oauth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keycard