keycardai-oauth 0.11.1 → 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 +4 -4
- data/CHANGELOG.md +16 -0
- data/lib/keycardai/oauth/device_authorization.rb +4 -0
- data/lib/keycardai/oauth/errors.rb +28 -1
- data/lib/keycardai/oauth/http.rb +143 -28
- data/lib/keycardai/oauth/jwks_keyring.rb +1 -1
- data/lib/keycardai/oauth/token_sources.rb +1 -1
- data/lib/keycardai/oauth/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bfa5ca1a750441c826d0534dba37195836c84689d881851aa317e114a5b0c1db
|
|
4
|
+
data.tar.gz: 89b2adb8d071b31f6ecd79894bddacdd8915bc1fa4d209a3f59a032969784e24
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c59d40c970a41e9e4d45ffa06f2298a9e477900865da326a081f7283aa30d663ad64bb3dc14c42a64817f29530332344f743b48729dcb018506e351ca1246f17
|
|
7
|
+
data.tar.gz: df07a17a41cfed346146555fb82745fc311f6ec08241f54c4a2f282e29544d5a6f23aaf76c60ab6e31d060e1c4e4123bd5dfe0904e1b730120cd56a7b7fbf4a9
|
data/CHANGELOG.md
CHANGED
|
@@ -15,6 +15,22 @@ 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
|
+
|
|
18
34
|
## 0.11.1-keycardai-oauth (2026-11-25)
|
|
19
35
|
|
|
20
36
|
|
|
@@ -382,6 +382,10 @@ module Keycardai
|
|
|
382
382
|
rescue NetworkError => e
|
|
383
383
|
# RFC 8628 §3.5: wait longer after a connection timeout.
|
|
384
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)
|
|
385
389
|
end
|
|
386
390
|
|
|
387
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
|
|
data/lib/keycardai/oauth/http.rb
CHANGED
|
@@ -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
|
-
|
|
165
|
-
|
|
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?
|