Skip to content

Commit 4ee477c

Browse files
authored
MONGOCRYPT-963 Inputs not sanitized for KMS HTTP requests (#1253)
1 parent cdf20ff commit 4ee477c

9 files changed

Lines changed: 914 additions & 12 deletions

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ChangeLog
22

3+
## 1.21.1
4+
5+
### Fixed
6+
- Validate inputs before interpolating them into a KMS HTTP request.
7+
38
## 1.21.0
49

510
### Changed

‎kms-message/src/kms_azure_request.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,53 @@
2121
#include "kms_request_opt_private.h"
2222
#include "kms_request_str.h"
2323

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+
2471
/*
2572
* Request has the following form:
2673
*
@@ -65,6 +112,14 @@ kms_azure_request_oauth_new (const char *host,
65112
goto done;
66113
}
67114

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+
68123
if (kms_request_get_error (req)) {
69124
goto done;
70125
}
@@ -123,6 +178,16 @@ _wrap_unwrap_common (const char *wrap_unwrap,
123178
goto done;
124179
}
125180

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+
126191
if (kms_request_get_error (req)) {
127192
goto done;
128193
}

‎kms-message/src/kms_encrypt_request.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@
2020
#include "kms_message/kms_b64.h"
2121
#include "kms_request_str.h"
2222

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+
2350
kms_request_t *
2451
kms_encrypt_request_new (const uint8_t *plaintext,
2552
size_t plaintext_length,
@@ -36,6 +63,10 @@ kms_encrypt_request_new (const uint8_t *plaintext,
3663
goto done;
3764
}
3865

66+
if (!_key_id_chars_are_valid (request, key_id)) {
67+
goto done;
68+
}
69+
3970
if (!(kms_request_add_header_field (
4071
request, "Content-Type", "application/x-amz-json-1.1") &&
4172
kms_request_add_header_field (

‎kms-message/src/kms_gcp_request.c‎

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,31 @@
2020
#include "kms_message_private.h"
2121
#include "kms_request_opt_private.h"
2222

23-
#include <ctype.h>
24-
2523
/* Set a default expiration of 5 minutes for JSON Web Tokens (GCP allows up to
2624
* one hour) */
2725
#define JWT_EXPIRATION_SECS 5 * 60
2826

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+
2948
kms_request_t *
3049
kms_gcp_request_oauth_new (const char *host,
3150
const char *email,
@@ -57,6 +76,11 @@ kms_gcp_request_oauth_new (const char *host,
5776
goto done;
5877
}
5978

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+
6084
/* Produce the signed JWT <base64url header>.<base64url claims>.<base64url
6185
* signature> */
6286
issued_at = time (NULL);
@@ -163,18 +187,31 @@ kms_gcp_request_oauth_new (const char *host,
163187
* - location is a Cloud location name (e.g. "global", "us-east1").
164188
* - keyVersion is a positive integer.
165189
*
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+
*
166193
* `value` is not included in the error message: it is untrusted, and the error may be logged. */
167194
static bool
168195
_check_key_identifier (kms_request_t *req, const char *name, const char *value)
169196
{
170197
const char *c;
198+
bool has_alnum = false;
171199

172200
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 == '~')) {
175204
KMS_ERROR (req, "Invalid character in GCP KMS key identifier: %s", name);
176205
return false;
177206
}
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;
178215
}
179216
return true;
180217
}
@@ -222,15 +259,22 @@ _encrypt_decrypt_common (const char *encrypt_decrypt,
222259
goto done;
223260
}
224261

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. */
229266
if (!_check_key_identifier (req, "projectId", project_id) ||
230267
!_check_key_identifier (req, "location", location) ||
231268
!_check_key_identifier (req, "keyRing", key_ring_name) ||
232269
!_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)) {
234278
goto done;
235279
}
236280

0 commit comments

Comments
 (0)