didit-sdk 2.13.0 → 2.13.1

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: 88b2a67f60c7c6817959479e213a2458a19e281c0b1d25ce2e2d5e5b20e04cc7
4
- data.tar.gz: a5bd939f7fe5bbc01a41fd3e6b563c549a72282f348648f10c55dad73ce230d6
3
+ metadata.gz: fdd7206643757259988718350d8738ab3fc1e407a502ef263a3fb1fb7cd2b95e
4
+ data.tar.gz: 7958618527442627b4e9420afbc37f467689e848134d0b5c114730adb015e2d1
5
5
  SHA512:
6
- metadata.gz: 15ba2f596d5b7014f98963637698fd4b5dac96dd547f8fb9fc9414d2f7d6d2aef8d8f5302dce36e41619421c9d4b89f31f3b7db51407d292eac02d76861ab348
7
- data.tar.gz: 8fae87cb3eddd611a32b34162415ab6951f5684022bddee23776514d74c783efe62c77680aa4b8f6fbea7ecda076a2630573d0e782742f1fb0fe097850f5e563
6
+ metadata.gz: ef3b873d60dc67cbe542d85427c0abeddbca437238c32c6495a7b32d144006f095a0792e1b668ffbc79d4fc8e10441553096630a7a110e25b07f225b7144c332
7
+ data.tar.gz: e15b2941758352a511710b652108ddd7502e056975ed6c1f9bda01e3a7a1f3bd07f192669017be1fa3a68b3cdcc2a7c3e51ff3964e4fd4affb85076ff9ab90d0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.13.1](https://github.com/didit-protocol/sdk-ruby/compare/v2.13.0...v2.13.1) (2026-11-12)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **authentication:** a body nested past the depth limit is not JSON ([c0cb38e](https://github.com/didit-protocol/sdk-ruby/commit/c0cb38edf6f8302c640bfac2bac2e8191b3c123f))
9
+ * **authentication:** a failure to encode a consent request has a fixed message ([2f2bdb7](https://github.com/didit-protocol/sdk-ruby/commit/2f2bdb7302fb6b56db5b1b29cd90cf447ef588a5))
10
+ * **authentication:** a malformed 2xx consent error keeps no parser exception as its cause ([e9cad91](https://github.com/didit-protocol/sdk-ruby/commit/e9cad918b725708f14ee37f5f07922ab78d074be))
11
+ * **authentication:** a malformed 2xx consent message reads the content type from the sanitized response ([fa0e897](https://github.com/didit-protocol/sdk-ruby/commit/fa0e897c76d810f473b2f4c69cd6025883ba1ce5))
12
+ * **authentication:** collect consent secrets from the JSON the request carries ([ada5949](https://github.com/didit-protocol/sdk-ruby/commit/ada5949ad9afd041cbf8f2766e5889f66600dae7))
13
+ * **authentication:** read consent secrets from the bytes that were sent ([02a0be4](https://github.com/didit-protocol/sdk-ruby/commit/02a0be4ef2688595a66aeda5f03e512f20109b94))
14
+ * **authentication:** redact every string of the consent assertion from errors ([c1ed630](https://github.com/didit-protocol/sdk-ruby/commit/c1ed63076c21fd9a66939045b026bb68592ad580))
15
+ * **authentication:** redact the parsed agent-consent response, not its raw text ([9d8dfef](https://github.com/didit-protocol/sdk-ruby/commit/9d8dfef87e9c2d2f5433551b75de28a98608cd1b))
16
+ * **authentication:** sanitize the whole consent response once on receipt ([d7f875d](https://github.com/didit-protocol/sdk-ruby/commit/d7f875deabe3cb4a0311ced9d6bf416a4c7ef19d))
17
+ * **authentication:** successful consent responses reach the caller unchanged; fold header-name case ([9becd6d](https://github.com/didit-protocol/sdk-ruby/commit/9becd6d166d122deeec244248890a0a4f5c8b847))
18
+ * **authentication:** the approval-options call encodes through the same fixed-message wrapper ([570043e](https://github.com/didit-protocol/sdk-ruby/commit/570043e36c63e494708c2f7cc8ab0ae412633c1f))
19
+ * **authentication:** treat only assertion strings of 16+ characters as secrets ([e072900](https://github.com/didit-protocol/sdk-ruby/commit/e072900a01bb52e865159ef88811c2398bd442e2))
20
+ * **authentication:** withhold transport causes, redact empty bodies, headers and request id, keep safe key names ([d966dc2](https://github.com/didit-protocol/sdk-ruby/commit/d966dc27fe9dcc0909446272961258cdb22178fb))
21
+
3
22
  ## [2.13.0](https://github.com/didit-protocol/sdk-ruby/compare/v2.12.0...v2.13.0) (2026-11-11)
4
23
 
5
24
 
@@ -3,7 +3,7 @@
3
3
  module Didit
4
4
  class Authentication
5
5
  # Builds and checks the body of +POST agents/consent/+, and names the strings of it that
6
- # must never reach an error message: the challenge and the assertion's response members.
6
+ # must never reach an error: the challenge and every string of the assertion.
7
7
  #
8
8
  # @api private
9
9
  module AgentConsentRequest
@@ -12,9 +12,6 @@ module Didit
12
12
  UUID = /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
13
13
  LIFETIMES = [86_400, 604_800, 2_592_000].freeze
14
14
  DECISIONS = %w[approve deny].freeze
15
- # The assertion members that are secrets.
16
- ASSERTION_SECRETS = %w[clientDataJSON authenticatorData signature userHandle].freeze
17
-
18
15
  # @return [Array(Hash, Array<String>)] the JSON body and its secret strings
19
16
  # @raise [Didit::ConfigurationError]
20
17
  def self.build(challenge, decision, assertion, scopes, extras = {})
@@ -25,17 +22,30 @@ module Didit
25
22
 
26
23
  # @type var body: Hash[String, untyped]
27
24
  body = { "challenge" => challenge, "decision" => decision }
28
- secrets = [challenge]
29
- unless assertion.nil?
30
- body["assertion"] = checked_assertion(assertion)
31
- secrets.concat(assertion_secrets(assertion))
32
- end
25
+ body["assertion"] = checked_assertion(assertion) unless assertion.nil?
33
26
  body["scopes"] = checked_scopes(scopes) unless scopes.nil?
34
27
  organization_ids = extras[:organization_ids]
35
28
  lifetime = extras[:lifetime]
36
29
  body["organization_ids"] = checked_organization_ids(organization_ids) unless organization_ids.nil?
37
30
  body["lifetime"] = checked_lifetime(lifetime) unless lifetime.nil?
38
- [body, secrets]
31
+ # Encoded exactly once: the plain body decoded from these bytes is what is sent, and the secrets are
32
+ # read from it, so a caller's object is never serialized a second time.
33
+ body = encode_once(body)
34
+ [body, [challenge, *assertion_secrets(body["assertion"])]]
35
+ end
36
+
37
+ # A nested object's serializer may put the challenge or a credential in its error message: any failure
38
+ # is a fixed-message error with no cause.
39
+ def self.encode_once(body)
40
+ encoded = nil
41
+ begin
42
+ encoded = JSON.parse(JSON.generate(body))
43
+ rescue StandardError, SystemStackError
44
+ encoded = nil
45
+ end
46
+ raise ConfigurationError, "could not encode the agent consent request." if encoded.nil?
47
+
48
+ encoded
39
49
  end
40
50
 
41
51
  def self.checked_organization_ids(ids)
@@ -63,12 +73,19 @@ module Didit
63
73
  raise ConfigurationError, "scopes must be a non-empty array of strings such as [\"read\", \"act\"]."
64
74
  end
65
75
 
66
- def self.assertion_secrets(assertion)
67
- response = assertion["response"] || assertion[:response]
68
- return [] unless response.is_a?(Hash)
76
+ # Strings shorter than this are not secrets: field names such as "id" and values such as "public-key"
77
+ # would otherwise erase error codes like "invalid_grant" (docs/DESIGN.md section 10).
78
+ # Sensitive values are 16+ chars: user handles are 16-byte ids (22 base64url), credential ids 16+ bytes.
79
+ MIN_SECRET_LENGTH = 16
69
80
 
70
- response.filter_map do |key, value|
71
- value if ASSERTION_SECRETS.include?(key.to_s) && value.is_a?(String) && !value.empty?
81
+ # Every string of at least 16 characters inside the assertion, at any depth: object keys and values,
82
+ # array items.
83
+ def self.assertion_secrets(value)
84
+ case value
85
+ when String then value.length >= MIN_SECRET_LENGTH ? [value] : []
86
+ when Hash then value.flat_map { |key, item| assertion_secrets(key.to_s) + assertion_secrets(item) }
87
+ when Array then value.flat_map { |item| assertion_secrets(item) }
88
+ else []
72
89
  end
73
90
  end
74
91
  end
@@ -24,13 +24,17 @@ module Didit
24
24
  # @return [Array<String>] tokens, codes and verifiers redacted from errors
25
25
  attr_reader :secrets
26
26
 
27
- def initialize(http_method, uri, bearer: nil, form: nil, json: nil, secrets: [])
27
+ # @return [Boolean] redact errors from the parsed body, never from its raw text
28
+ attr_reader :structured
29
+
30
+ def initialize(http_method, uri, bearer: nil, form: nil, json: nil, secrets: [], structured: false)
28
31
  @http_method = http_method
29
32
  @uri = uri
30
33
  @bearer = bearer
31
34
  @form = form
32
35
  @json = json
33
36
  @secrets = secrets
37
+ @structured = structured
34
38
  freeze
35
39
  end
36
40
 
@@ -61,9 +65,21 @@ module Didit
61
65
  # @return [Didit::Internal::Transport::Response]
62
66
  # @raise [Didit::APIConnectionError, Didit::APITimeoutError] when no response arrives
63
67
  def perform(call)
64
- @transport.execute(VERBS.fetch(call.http_method), call.uri, headers_for(call), body_for(call), @timeout)
65
- rescue Internal::Transport::Failure => e
66
- error_class = e.timeout? ? APITimeoutError : APIConnectionError
68
+ error_class = APIConnectionError
69
+ begin
70
+ verb = VERBS.fetch(call.http_method)
71
+ response = @transport.execute(verb, call.uri, headers_for(call), body_for(call), @timeout)
72
+ # A successful response reaches the caller unchanged; errors are built from a sanitized copy only.
73
+ return response
74
+ rescue Internal::Transport::Failure => e
75
+ error_class = e.timeout? ? APITimeoutError : APIConnectionError
76
+ rescue StandardError
77
+ # A request that carries secrets reports a fixed error for anything a transport raises.
78
+ raise unless call.structured
79
+
80
+ error_class = APIConnectionError
81
+ end
82
+ # Raised outside the handlers, so no cause carries the transport's text, which may quote the request.
67
83
  raise error_class.new(http_method: call.http_method, url: call.display_url)
68
84
  end
69
85
 
@@ -82,18 +98,26 @@ module Didit
82
98
  def read(call, response)
83
99
  body = response.body.to_s
84
100
  unless (200..299).cover?(response.status)
101
+ response = error_response(call, response)
102
+ body = response.body.to_s
85
103
  raise Internal::Errors.status_error(
86
- status: response.status, headers: response.headers, raw_body: body, redact: call.secrets
104
+ status: response.status, headers: response.headers, raw_body: body, redact: redactions(call)
87
105
  )
88
106
  end
89
107
  return nil if response.status == 204 || body.empty?
90
108
 
109
+ parsed = nil
110
+ failed = false
91
111
  begin
92
- JSON.parse(text_of(body))
112
+ parsed = JSON.parse(text_of(body))
93
113
  rescue JSON::ParserError
94
- content_type = response.headers.fetch("content-type", "no content type")
95
- raise validation_error(call, response, "Expected a JSON response from #{call.describe}, got #{content_type}.")
114
+ failed = true
96
115
  end
116
+ return parsed unless failed
117
+
118
+ # Raised outside the rescue: the parser's error quotes the raw input, so it is never the cause.
119
+ content_type = error_response(call, response).headers.fetch("content-type", "no content type")
120
+ raise validation_error(call, response, "Expected a JSON response from #{call.describe}, got #{content_type}.")
97
121
  end
98
122
 
99
123
  # The JSON object of a 2xx response.
@@ -108,16 +132,27 @@ module Didit
108
132
 
109
133
  # @return [Didit::APIResponseValidationError]
110
134
  def validation_error(call, response, message)
135
+ response = error_response(call, response)
111
136
  body = response.body.to_s
137
+ shown = body.empty? ? nil : Internal::Util.redact(text_of(body), redactions(call))
112
138
  APIResponseValidationError.new(
113
- message, status: response.status, headers: response.headers,
114
- body: body.empty? ? nil : Internal::Util.redact(text_of(body), call.secrets),
139
+ message, status: response.status, headers: response.headers, body: shown,
115
140
  request_id: Internal::Util.request_id(response.headers)
116
141
  )
117
142
  end
118
143
 
119
144
  private
120
145
 
146
+ # The response an error is built from: a sanitized copy for a consent call, else the response.
147
+ def error_response(call, response)
148
+ call.structured ? Internal::Errors.sanitized_response(response, call.secrets) : response
149
+ end
150
+
151
+ # A structured call's response is already sanitized, so nothing is replaced again.
152
+ def redactions(call)
153
+ call.structured ? [] : call.secrets
154
+ end
155
+
121
156
  def headers_for(call)
122
157
  headers = {
123
158
  "Accept" => "application/json",
@@ -296,7 +296,8 @@ module Didit
296
296
  def create_agent_approval_options(access_token, challenge:)
297
297
  challenge = required_text(challenge, "challenge")
298
298
  AgentApprovalOptions.new(
299
- agent_request(AgentConsentRequest::OPTIONS_PATH, access_token, { "challenge" => challenge }, [challenge])
299
+ agent_request(AgentConsentRequest::OPTIONS_PATH, access_token,
300
+ AgentConsentRequest.encode_once({ "challenge" => challenge }), [challenge])
300
301
  )
301
302
  end
302
303
 
@@ -488,16 +489,18 @@ module Didit
488
489
  TokenResponse.new(send_object(call))
489
490
  end
490
491
 
491
- def bearer_call(http_method, uri, access_token, json: nil, secrets: [])
492
+ def bearer_call(http_method, uri, access_token, json: nil, secrets: [], structured: false)
492
493
  token = required_text(access_token, "access_token")
493
494
  # Sent in a header: printable ASCII only, so it can never split or extend the request.
494
495
  raise ConfigurationError, "access_token contains invalid characters." if token.b.match?(/[^\x21-\x7e]/n)
495
496
 
496
- Http::Call.new(http_method, uri, bearer: token, json: json, secrets: [token, *secrets])
497
+ Http::Call.new(http_method, uri, bearer: token, json: json, secrets: [token, *secrets],
498
+ structured: structured)
497
499
  end
498
500
 
499
501
  def agent_request(path, access_token, body, secrets)
500
- send_object(bearer_call("POST", issuer_uri(path), access_token, json: body, secrets: secrets))
502
+ send_object(bearer_call("POST", issuer_uri(path), access_token, json: body, secrets: secrets,
503
+ structured: true))
501
504
  end
502
505
 
503
506
  def send_object(call)
@@ -32,6 +32,38 @@ module Didit
32
32
  klass.new(message, status: status, headers: headers, body: body, request_id: Util.request_id(headers))
33
33
  end
34
34
 
35
+ # The response of a call whose answer may echo secrets (the agent consent calls), sanitized once on
36
+ # receipt: status kept; each header whose name contains a secret dropped and the other values redacted;
37
+ # the body redacted structurally, never from its raw text (the parsed JSON with every string that
38
+ # contains a secret replaced whole, "[redacted]" when it is not JSON, cut at the size limit or empty).
39
+ # Every error of these calls is built from this response only.
40
+ #
41
+ # @return [Didit::Internal::Transport::Response]
42
+ def sanitized_response(response, secrets)
43
+ # @type var headers: Hash[String, String]
44
+ headers = {}
45
+ response.headers.each do |name, value|
46
+ # Header names are case-insensitive: compare case-folded.
47
+ next if secrets.any? { |secret| secret && !secret.empty? && name.downcase.include?(secret.downcase) }
48
+
49
+ headers[name] = Util.redact_structured(value, secrets)
50
+ end
51
+ Transport::Response.new(status: response.status, headers: headers,
52
+ body: sanitized_body(response.body.to_s, secrets))
53
+ end
54
+
55
+ # @return [String] the redacted JSON text, or "[redacted]"
56
+ def sanitized_body(raw_body, secrets)
57
+ return Util::REDACTED if raw_body.empty?
58
+
59
+ text = raw_body.dup.force_encoding(Encoding::UTF_8)
60
+ text = text.scrub unless text.valid_encoding?
61
+ JSON.generate(Util.redact_structured(JSON.parse(text), secrets))
62
+ rescue JSON::ParserError, SystemStackError
63
+ # Not JSON, or nested past the parser's or the walk's depth limit: not JSON.
64
+ Util::REDACTED
65
+ end
66
+
35
67
  # The parsed JSON body, else the text, else nil.
36
68
  def parse_body(raw_body)
37
69
  return nil if raw_body.nil? || raw_body.empty?
@@ -39,6 +39,40 @@ module Didit
39
39
  redact_value(value, secrets)
40
40
  end
41
41
 
42
+ # Redacts a parsed JSON value structurally: every string (object key or value) that contains a secret
43
+ # anywhere in it becomes "[redacted]" as a whole. Strings are compared as decoded, so the spelling
44
+ # of the raw JSON text does not matter. A redacted key that collides with another key is numbered.
45
+ def redact_structured(value, secrets)
46
+ secrets = secrets.compact.reject(&:empty?)
47
+ case value
48
+ when String then secrets.any? { |secret| value.include?(secret) } ? REDACTED : value
49
+ when Array then value.map { |item| redact_structured(item, secrets) }
50
+ when Hash then redact_structured_hash(value, secrets)
51
+ else value
52
+ end
53
+ end
54
+
55
+ def redact_structured_hash(value, secrets)
56
+ # Reserve every safe key name first, so a placeholder never takes one.
57
+ taken = value.keys.select { |key| redact_structured(key, secrets) == key }
58
+ number = 1
59
+ # @type var result: Hash[untyped, untyped]
60
+ result = {}
61
+ value.each do |key, item|
62
+ name = key
63
+ if redact_structured(key, secrets) != key
64
+ loop do
65
+ name = number == 1 ? REDACTED : "#{REDACTED}#{number}"
66
+ number += 1
67
+ break unless taken.include?(name)
68
+ end
69
+ taken << name
70
+ end
71
+ result[name] = redact_structured(item, secrets)
72
+ end
73
+ result
74
+ end
75
+
42
76
  def redact_value(value, secrets)
43
77
  case value
44
78
  when String then secrets.reduce(value) { |text, secret| text.gsub(secret, REDACTED) }
data/lib/didit/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Didit
4
- VERSION = "2.13.0" # x-release-please-version
4
+ VERSION = "2.13.1" # x-release-please-version
5
5
  end
@@ -6,14 +6,15 @@ module Didit
6
6
  UUID: Regexp
7
7
  LIFETIMES: Array[Integer]
8
8
  DECISIONS: Array[String]
9
- ASSERTION_SECRETS: Array[String]
9
+ MIN_SECRET_LENGTH: Integer
10
10
 
11
11
  def self.build: (String challenge, untyped decision, untyped assertion, untyped scopes, ?Hash[Symbol, untyped] extras) -> [Hash[String, untyped], Array[String]]
12
+ def self.encode_once: (Hash[String, untyped] body) -> Hash[String, untyped]
12
13
  def self.checked_assertion: (untyped assertion) -> Hash[untyped, untyped]
13
14
  def self.checked_organization_ids: (untyped ids) -> Array[String]
14
15
  def self.checked_lifetime: (untyped lifetime) -> Integer
15
16
  def self.checked_scopes: (untyped scopes) -> Array[String]
16
- def self.assertion_secrets: (Hash[untyped, untyped] assertion) -> Array[String]
17
+ def self.assertion_secrets: (untyped value) -> Array[String]
17
18
  end
18
19
  end
19
20
  end
@@ -8,9 +8,10 @@ module Didit
8
8
  attr_reader form: Hash[String, String]?
9
9
  attr_reader json: Hash[String, untyped]?
10
10
  attr_reader secrets: Array[String]
11
+ attr_reader structured: bool
11
12
 
12
13
  def initialize: (String http_method, URI::Generic uri, ?bearer: String?, ?form: Hash[String, String]?,
13
- ?json: Hash[String, untyped]?, ?secrets: Array[String]) -> void
14
+ ?json: Hash[String, untyped]?, ?secrets: Array[String], ?structured: bool) -> void
14
15
  def display_url: () -> String
15
16
  def describe: () -> String
16
17
  end
@@ -31,6 +32,8 @@ module Didit
31
32
 
32
33
  def headers_for: (Call call) -> Hash[String, String]
33
34
  def body_for: (Call call) -> String?
35
+ def error_response: (Call call, Internal::Transport::Response response) -> Internal::Transport::Response
36
+ def redactions: (Call call) -> Array[String]
34
37
  def text_of: (String body) -> String
35
38
  end
36
39
  end
@@ -72,7 +72,7 @@ module Didit
72
72
  def token_request: (Hash[String, String] form, Array[String] secrets) -> TokenResponse
73
73
  def bearer_call: (String http_method, URI::Generic uri, untyped access_token,
74
74
  ?json: Hash[String, untyped]?,
75
- ?secrets: Array[String]) -> Http::Call
75
+ ?secrets: Array[String], ?structured: bool) -> Http::Call
76
76
  def agent_request: (String path, String access_token, Hash[String, untyped] body, Array[String] secrets) -> Hash[String, untyped]
77
77
  def send_object: (Http::Call call) -> Hash[String, untyped]
78
78
  def issuer_uri: (String path) -> URI::Generic
@@ -11,6 +11,8 @@ module Didit
11
11
  def self?.client_header: () -> String
12
12
  def self?.request_id: (Hash[String, String] headers) -> String?
13
13
  def self?.redact: [T] (T value, Array[String?] secrets) -> T
14
+ def self?.redact_structured: (untyped value, Array[String?] secrets) -> untyped
15
+ def self?.redact_structured_hash: (Hash[untyped, untyped] value, Array[String] secrets) -> Hash[untyped, untyped]
14
16
  def self?.redact_value: (untyped value, Array[String] secrets) -> untyped
15
17
  def self?.origin: (URI::Generic | String uri) -> String
16
18
  def self?.display_url: (URI::Generic | String uri) -> String
@@ -23,6 +25,8 @@ module Didit
23
25
  DETAIL_KEYS: Array[String]
24
26
 
25
27
  def self?.status_error: (status: Integer, headers: Hash[String, String], raw_body: String?, ?redact: Array[String?]) -> APIStatusError
28
+ def self?.sanitized_response: (Transport::Response response, Array[String?] secrets) -> Transport::Response
29
+ def self?.sanitized_body: (String raw_body, Array[String?] secrets) -> String
26
30
  def self?.parse_body: (String? raw_body) -> untyped
27
31
  def self?.message_for: (Integer status, untyped body) -> String
28
32
  def self?.detail_of: (untyped body) -> String?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: didit-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.13.0
4
+ version: 2.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Didit