libmongocrypt-helper 1.21.0.0.1001 → 1.21.2.0.1001

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: cfbe6382cb548a1982646c49a58ea796bf4dcfbc1c69279035744fea2863f799
4
- data.tar.gz: 94417181701f8f1c02d4cdbc1120aefd3ffc8228195df488be240508403cae35
3
+ metadata.gz: 3b286b0a73b451ee7a2e1e694fcbfb085fb778c04f1165d63bc23f775cfce433
4
+ data.tar.gz: 44c87d3e815be8045cca3cb8b140baac86faaa8a1bab99f823bdd8f431df360f
5
5
  SHA512:
6
- metadata.gz: 6e25030a91d60887c6215da9ed953391c8ef5c2b7561bf7d2e4cb686b7efa3b646b036cc5d1d6ed574f6508f27a7b0a2fe078658a122ab12223d3eece7483a17
7
- data.tar.gz: cfe9d46be634bd5c3a449d38539069e5e357811eac73dc6d34152b2e9e7fed573137cc9d5ac49c4de05c2cbac87236c1375af745efd6ba7dbd7d6bf23f716495
6
+ metadata.gz: 8295755f89ed49d90fb7f36eb1531bc95cfcaa81eb95752330ae36b86b70a665e6fe6f4a262226ac9656f8286f9e5270d3cb6e24d15be4a83ec720aa50f50cfa
7
+ data.tar.gz: a990acab5b8df8723d205fd822238e8161e1f55d31b41127f63049e65ab45cdb6bc590d02be6d684d39d07c06206572a5284d33f69147ee8919a5d8dc711ae5a
@@ -1,5 +1,15 @@
1
1
  # ChangeLog
2
2
 
3
+ ## 1.21.2
4
+
5
+ ### Fixed
6
+ - Reject duplicate fields when parsing key vault documents and FLE1 placeholders.
7
+
8
+ ## 1.21.1
9
+
10
+ ### Fixed
11
+ - Validate inputs before interpolating them into a KMS HTTP request.
12
+
3
13
  ## 1.21.0
4
14
 
5
15
  ### Changed
@@ -57,7 +57,7 @@
57
57
  }
58
58
  ],
59
59
  "metadata": {
60
- "timestamp": "2026-10-13T19:21:15.044599+00:00",
60
+ "timestamp": "2026-10-22T10:30:28.302732+00:00",
61
61
  "tools": [
62
62
  {
63
63
  "externalReferences": [
@@ -100,7 +100,7 @@
100
100
  }
101
101
  ]
102
102
  },
103
- "serialNumber": "urn:uuid:870a42e2-38df-4271-afee-8ffa5c28bcf7",
103
+ "serialNumber": "urn:uuid:11dad630-c86b-4fee-a46b-b20f815b1c93",
104
104
  "version": 1,
105
105
  "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
106
106
  "bomFormat": "CycloneDX",
@@ -21,6 +21,53 @@
21
21
  #include "kms_request_opt_private.h"
22
22
  #include "kms_request_str.h"
23
23
 
24
+ #include <ctype.h>
25
+
26
+ /* _check_path_segment returns true if `value` is safe to use as a URL path
27
+ * segment.
28
+ *
29
+ * Key identifiers may originate from key vault documents or caller-supplied
30
+ * options. They are formatted into the request path, so an unescaped '/', '?',
31
+ * '#', space, or CR/LF would rewrite the request target or inject header lines
32
+ * into the request. A '/' or ".." can address a different key, still
33
+ * authenticated with the caller's access token.
34
+ *
35
+ * Only the RFC 3986 unreserved characters are permitted. This is a superset of
36
+ * what Azure itself accepts, so it does not reject an identifier that would
37
+ * otherwise work:
38
+ * - A key name must match `[a-zA-Z0-9-]{1,127}`. See
39
+ * https://learn.microsoft.com/en-us/azure/key-vault/general/about-keys-secrets-certificates#objects-identifiers-and-versioning
40
+ * - A key version is a 32 character hex string.
41
+ * - A tenant ID is a GUID, or a domain name (e.g. "contoso.onmicrosoft.com").
42
+ *
43
+ * Each identifier must also contain an alphanumeric character. No valid
44
+ * identifier lacks one, and this keeps a value of only dots from forming a
45
+ * dot-segment ("..") in the request path.
46
+ *
47
+ * `value` is not included in the error message: it is untrusted, and the error
48
+ * may be logged. */
49
+ static bool
50
+ _check_path_segment (kms_request_t *req, const char *name, const char *value)
51
+ {
52
+ bool has_alnum = false;
53
+
54
+ for (const char* c = value; *c != '\0'; c++) {
55
+ if (!((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) && *c != '-' && *c != '.' &&
56
+ *c != '_' && *c != '~') {
57
+ KMS_ERROR (req, "Invalid character in Azure KMS identifier: %s", name);
58
+ return false;
59
+ }
60
+ if ((*c >= '0' && *c <= '9') || (*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')) {
61
+ has_alnum = true;
62
+ }
63
+ }
64
+ if (!has_alnum) {
65
+ KMS_ERROR (req, "Invalid Azure KMS identifier: %s", name);
66
+ return false;
67
+ }
68
+ return true;
69
+ }
70
+
24
71
  /*
25
72
  * Request has the following form:
26
73
  *
@@ -65,6 +112,14 @@ kms_azure_request_oauth_new (const char *host,
65
112
  goto done;
66
113
  }
67
114
 
115
+ /* Check the path segments before checking req for an error. tenant_id is
116
+ * formatted into the request path above, so an invalid value may have
117
+ * already failed the generic request line validation in kms_request_new.
118
+ * Report the more specific error naming the field. */
119
+ if (!_check_path_segment (req, "tenantId", tenant_id)) {
120
+ goto done;
121
+ }
122
+
68
123
  if (kms_request_get_error (req)) {
69
124
  goto done;
70
125
  }
@@ -123,6 +178,16 @@ _wrap_unwrap_common (const char *wrap_unwrap,
123
178
  goto done;
124
179
  }
125
180
 
181
+ /* Check the path segments before checking req for an error. See the
182
+ * comment in kms_azure_request_oauth_new. */
183
+ if (!_check_path_segment (req, "keyName", key_name) ||
184
+ /* An empty key version is treated the same as an absent one: both
185
+ * produce the same request path. */
186
+ (key_version && *key_version &&
187
+ !_check_path_segment (req, "keyVersion", key_version))) {
188
+ goto done;
189
+ }
190
+
126
191
  if (kms_request_get_error (req)) {
127
192
  goto done;
128
193
  }
@@ -20,6 +20,33 @@
20
20
  #include "kms_message/kms_b64.h"
21
21
  #include "kms_request_str.h"
22
22
 
23
+ #define KMS_PRINTABLE_ASCII_START ' '
24
+ #define KMS_PRINTABLE_ASCII_END '~'
25
+
26
+ /* _key_id_chars_are_valid returns true if `key_id` is safe to interpolate into the JSON body of the AWS
27
+ * Encrypt request. Reject non-printable ASCII, double quotes, and backslashes: those end the JSON string
28
+ * and let the caller add request members of their own, which are then signed with the SigV4 signature.
29
+ *
30
+ * A key identifier may be a key ID, an ARN, or an alias, none of which contain those characters.
31
+ *
32
+ * `key_id` is not included in the error message: it is untrusted, and the error may be logged.
33
+ *
34
+ * This duplicates _oauth_field_chars_are_valid in kms_gcp_request.c. */
35
+ static bool
36
+ _key_id_chars_are_valid (kms_request_t *req, const char *key_id)
37
+ {
38
+ for (const char *i = key_id; *i != '\0'; i++) {
39
+ const unsigned char uc = (unsigned char) *i;
40
+
41
+ if (uc < KMS_PRINTABLE_ASCII_START || uc > KMS_PRINTABLE_ASCII_END ||
42
+ uc == '"' || uc == '\\') {
43
+ KMS_ERROR (req, "Invalid character in AWS KMS key identifier");
44
+ return false;
45
+ }
46
+ }
47
+ return true;
48
+ }
49
+
23
50
  kms_request_t *
24
51
  kms_encrypt_request_new (const uint8_t *plaintext,
25
52
  size_t plaintext_length,
@@ -36,6 +63,10 @@ kms_encrypt_request_new (const uint8_t *plaintext,
36
63
  goto done;
37
64
  }
38
65
 
66
+ if (!_key_id_chars_are_valid (request, key_id)) {
67
+ goto done;
68
+ }
69
+
39
70
  if (!(kms_request_add_header_field (
40
71
  request, "Content-Type", "application/x-amz-json-1.1") &&
41
72
  kms_request_add_header_field (
@@ -20,12 +20,31 @@
20
20
  #include "kms_message_private.h"
21
21
  #include "kms_request_opt_private.h"
22
22
 
23
- #include <ctype.h>
24
-
25
23
  /* Set a default expiration of 5 minutes for JSON Web Tokens (GCP allows up to
26
24
  * one hour) */
27
25
  #define JWT_EXPIRATION_SECS 5 * 60
28
26
 
27
+ #define KMS_PRINTABLE_ASCII_START ' '
28
+ #define KMS_PRINTABLE_ASCII_END '~'
29
+
30
+ /* _oauth_field_chars_are_valid returns true if `value` is safe to interpolate into the JWT claim set of the
31
+ * GCP OAuth request. Reject non-printable ASCII, double quotes, and backslashes.
32
+ *
33
+ * `value` is not included in the error message: it is untrusted, and the error may be logged. */
34
+ static bool
35
+ _oauth_field_chars_are_valid(kms_request_t *req, const char *name, const char *value)
36
+ {
37
+ for (const char *i = value; *i != '\0'; i++) {
38
+ const unsigned char uc = (unsigned char)*i;
39
+
40
+ if (uc < KMS_PRINTABLE_ASCII_START || uc > KMS_PRINTABLE_ASCII_END || uc == '"' || uc == '\\') {
41
+ KMS_ERROR(req, "Invalid character in GCP OAuth request: %s", name);
42
+ return false;
43
+ }
44
+ }
45
+ return true;
46
+ }
47
+
29
48
  kms_request_t *
30
49
  kms_gcp_request_oauth_new (const char *host,
31
50
  const char *email,
@@ -57,6 +76,11 @@ kms_gcp_request_oauth_new (const char *host,
57
76
  goto done;
58
77
  }
59
78
 
79
+ if (!(_oauth_field_chars_are_valid(req, "email", email) && _oauth_field_chars_are_valid(req, "audience", audience) &&
80
+ _oauth_field_chars_are_valid(req, "scope", scope))) {
81
+ goto done;
82
+ }
83
+
60
84
  /* Produce the signed JWT <base64url header>.<base64url claims>.<base64url
61
85
  * signature> */
62
86
  issued_at = time (NULL);
@@ -163,18 +187,31 @@ done:
163
187
  * - location is a Cloud location name (e.g. "global", "us-east1").
164
188
  * - keyVersion is a positive integer.
165
189
  *
190
+ * Each identifier must also contain an alphanumeric character. No valid identifier lacks one, and
191
+ * this keeps a value of only dots from forming a dot-segment ("..") in the request path.
192
+ *
166
193
  * `value` is not included in the error message: it is untrusted, and the error may be logged. */
167
194
  static bool
168
195
  _check_key_identifier (kms_request_t *req, const char *name, const char *value)
169
196
  {
170
197
  const char *c;
198
+ bool has_alnum = false;
171
199
 
172
200
  for (c = value; *c != '\0'; c++) {
173
- if (!isalnum ((unsigned char) *c) && *c != '-' && *c != '.' && *c != '_' &&
174
- *c != '~') {
201
+ if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') ||
202
+ (*c >= '0' && *c <= '9') || *c == '-' || *c == '.' || *c == '_' ||
203
+ *c == '~')) {
175
204
  KMS_ERROR (req, "Invalid character in GCP KMS key identifier: %s", name);
176
205
  return false;
177
206
  }
207
+ if ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') ||
208
+ (*c >= '0' && *c <= '9')) {
209
+ has_alnum = true;
210
+ }
211
+ }
212
+ if (!has_alnum) {
213
+ KMS_ERROR (req, "Invalid GCP KMS key identifier: %s", name);
214
+ return false;
178
215
  }
179
216
  return true;
180
217
  }
@@ -222,15 +259,22 @@ _encrypt_decrypt_common (const char *encrypt_decrypt,
222
259
  goto done;
223
260
  }
224
261
 
225
- if (kms_request_get_error (req)) {
226
- goto done;
227
- }
228
-
262
+ /* Check the key identifiers before checking req for an error. The
263
+ * identifiers are formatted into the request path above, so an invalid
264
+ * identifier may have already failed the generic request line validation in
265
+ * kms_request_new. Report the more specific error naming the field. */
229
266
  if (!_check_key_identifier (req, "projectId", project_id) ||
230
267
  !_check_key_identifier (req, "location", location) ||
231
268
  !_check_key_identifier (req, "keyRing", key_ring_name) ||
232
269
  !_check_key_identifier (req, "keyName", key_name) ||
233
- (key_version && !_check_key_identifier (req, "keyVersion", key_version))) {
270
+ /* An empty key version is treated the same as an absent one: it is
271
+ * omitted from the path above, so there is nothing to validate. */
272
+ (key_version && *key_version &&
273
+ !_check_key_identifier (req, "keyVersion", key_version))) {
274
+ goto done;
275
+ }
276
+
277
+ if (kms_request_get_error (req)) {
234
278
  goto done;
235
279
  }
236
280
 
@@ -64,6 +64,83 @@ check_and_prohibit_kmip (kms_request_t *req)
64
64
  return true;
65
65
  }
66
66
 
67
+ /* Callers interpolate externally supplied strings (key names, hosts, access
68
+ * tokens) into requests. Validate that they cannot alter the structure of the
69
+ * resulting HTTP message: a CR or LF forges an additional header, terminates
70
+ * the header section, or pipelines an entire second request, and a space in the
71
+ * request line changes the request target. */
72
+
73
+ /* Checks the method of the "METHOD path?query HTTP/1.1" request line. */
74
+ static bool
75
+ check_method (kms_request_t *request, const char *method)
76
+ {
77
+ if (!*method) {
78
+ KMS_ERROR (request, "HTTP method must not be empty");
79
+ return false;
80
+ }
81
+ if (strpbrk (method, " \r\n")) {
82
+ KMS_ERROR (request, "HTTP method contains invalid characters");
83
+ return false;
84
+ }
85
+ return true;
86
+ }
87
+
88
+ /* Checks the request target (path and query) of the request line.
89
+ *
90
+ * A space is prohibited because it ends the request target, except for AWS: the
91
+ * SigV4 conformance suite requires a literal space in the path to be sent
92
+ * verbatim and signed as %20 (normalize-path/get-space). AWS request targets in
93
+ * libmongocrypt are fixed strings, not interpolated from external input. */
94
+ static bool
95
+ check_request_target (kms_request_t *request,
96
+ const char *what,
97
+ const char *str)
98
+ {
99
+ const char *prohibited =
100
+ request->provider == KMS_REQUEST_PROVIDER_AWS ? "\r\n" : " \r\n";
101
+
102
+ if (strpbrk (str, prohibited)) {
103
+ KMS_ERROR (request, "HTTP %s contains invalid characters", what);
104
+ return false;
105
+ }
106
+ return true;
107
+ }
108
+
109
+ /* Checks a header name, or a header value that is not permitted to fold. */
110
+ static bool
111
+ check_header_value (kms_request_t *request, const char *what, const char *str)
112
+ {
113
+ if (strpbrk (str, "\r\n")) {
114
+ KMS_ERROR (request, "HTTP %s contains invalid characters", what);
115
+ return false;
116
+ }
117
+ return true;
118
+ }
119
+
120
+ /* Checks an already stored header value.
121
+ *
122
+ * A CRLF is permitted only as a line fold: the continuation line must begin
123
+ * with a space or horizontal tab. Line folding is deprecated by RFC 7230
124
+ * section 3.2.4, but is still accepted here because the AWS SigV4 test suite
125
+ * exercises it (get-header-value-multiline) by way of
126
+ * kms_request_append_header_field_value, which is the only API able to produce
127
+ * a folded value. */
128
+ static bool
129
+ check_stored_header_value (kms_request_t *request,
130
+ const kms_request_str_t *value)
131
+ {
132
+ const char *p = value->str;
133
+
134
+ while ((p = strpbrk (p, "\r\n"))) {
135
+ if (p[0] != '\r' || p[1] != '\n' || (p[2] != ' ' && p[2] != '\t')) {
136
+ KMS_ERROR (request, "HTTP header value contains invalid characters");
137
+ return false;
138
+ }
139
+ p += 2;
140
+ }
141
+ return true;
142
+ }
143
+
67
144
  kms_request_t *
68
145
  kms_request_new (const char *method,
69
146
  const char *path_and_query,
@@ -113,6 +190,18 @@ kms_request_new (const char *method,
113
190
  request->header_fields = kms_kv_list_new ();
114
191
  request->auto_content_length = true;
115
192
 
193
+ /* Validate once all members are initialized, so the request remains safe to
194
+ * destroy. */
195
+ if (request->failed) {
196
+ return request;
197
+ }
198
+ if (!check_method (request, method)) {
199
+ return request;
200
+ }
201
+ if (!check_request_target (request, "path", path_and_query)) {
202
+ return request;
203
+ }
204
+
116
205
  /* For AWS KMS requests, add a X-Amz-Date header. */
117
206
  if (request->provider == KMS_REQUEST_PROVIDER_AWS &&
118
207
  !kms_request_set_date (request, NULL)) {
@@ -214,6 +303,11 @@ kms_request_set_region (kms_request_t *request, const char *region)
214
303
  if (!check_and_prohibit_kmip (request)) {
215
304
  return false;
216
305
  }
306
+ /* region and service reach the wire in the SigV4 Authorization header's
307
+ * credential scope, and in the Host header derived by finalize(). */
308
+ if (!check_header_value (request, "region", region)) {
309
+ return false;
310
+ }
217
311
  kms_request_str_set_chars (request->region, region, -1);
218
312
  return true;
219
313
  }
@@ -224,6 +318,9 @@ kms_request_set_service (kms_request_t *request, const char *service)
224
318
  if (!check_and_prohibit_kmip (request)) {
225
319
  return false;
226
320
  }
321
+ if (!check_header_value (request, "service", service)) {
322
+ return false;
323
+ }
227
324
  kms_request_str_set_chars (request->service, service, -1);
228
325
  return true;
229
326
  }
@@ -234,6 +331,10 @@ kms_request_set_access_key_id (kms_request_t *request, const char *akid)
234
331
  if (!check_and_prohibit_kmip (request)) {
235
332
  return false;
236
333
  }
334
+ /* access_key_id reaches the wire in the SigV4 Authorization header. */
335
+ if (!check_header_value (request, "access key id", akid)) {
336
+ return false;
337
+ }
237
338
  kms_request_str_set_chars (request->access_key_id, akid, -1);
238
339
  return true;
239
340
  }
@@ -261,6 +362,15 @@ kms_request_add_header_field (kms_request_t *request,
261
362
  return false;
262
363
  }
263
364
 
365
+ /* This API cannot produce a folded value, so CR and LF are prohibited
366
+ * outright. Use kms_request_append_header_field_value to fold. */
367
+ if (!check_header_value (request, "header name", field_name)) {
368
+ return false;
369
+ }
370
+ if (!check_header_value (request, "header value", value)) {
371
+ return false;
372
+ }
373
+
264
374
  k = kms_request_str_new_from_chars (field_name, -1);
265
375
  v = kms_request_str_new_from_chars (value, -1);
266
376
  kms_kv_list_add (request->header_fields, k, v);
@@ -468,6 +578,23 @@ finalize (kms_request_t *request)
468
578
  kms_request_str_destroy (v);
469
579
  }
470
580
 
581
+ /* Every serializing entry point funnels through here, so validate the
582
+ * assembled request as a backstop: the setters above and
583
+ * kms_request_append_header_field_value can all reach the wire. */
584
+ if (!check_method (request, request->method->str) ||
585
+ !check_request_target (request, "path", request->path->str) ||
586
+ !check_request_target (request, "query", request->query->str)) {
587
+ return false;
588
+ }
589
+ for (size_t i = 0; i < lst->len; i++) {
590
+ if (!check_header_value (request, "header name", lst->kvs[i].key->str)) {
591
+ return false;
592
+ }
593
+ if (!check_stored_header_value (request, lst->kvs[i].value)) {
594
+ return false;
595
+ }
596
+ }
597
+
471
598
  return true;
472
599
  }
473
600