AWS AmazonCloudFront: CloudFront CWT docs: warn validateToken() does not check claims
Summary
Adds an Important note clarifying that cf.cwt.validateToken() only validates token structure and tag, not claims (exp, nbf, aud, catu), and instructs functions to validate claims themselves. Also updates API signatures, return object shape (protected/unprotected/payload), key base64 handling, COSE algorithm identifier, and example code/status codes.
Security assessment
The added Important note documents a security-relevant limitation: token validation does not enforce claim checks (expiry, not-before, audience, CAT restrictions), which if ignored could allow replay or unauthorized access. It is guidance/hardening rather than a fix for a specific disclosed vulnerability, so it is security documentation of medium severity.
Evidence
`validateToken()` checks only the structure of the token and its tag. It doesn't check any claims, such as `exp`, `nbf`, or `aud`, or Common Access Token (CAT) claims, such as `catu`. Your function must check the claims that it relies on.
Diff
diff --git a/AmazonCloudFront/latest/DeveloperGuide/cwt-support-cloudfront-functions.md b/AmazonCloudFront/latest/DeveloperGuide/cwt-support-cloudfront-functions.md index 3fef821..18106f7 100644 --- a//AmazonCloudFront/latest/DeveloperGuide/cwt-support-cloudfront-functions.md +++ b//AmazonCloudFront/latest/DeveloperGuide/cwt-support-cloudfront-functions.md @@ -18 +18 @@ To use this module, create a CloudFront Function using JavaScript runtime 2.0 an -The methods associated to this module are accessible through (where * is a wildcard representing the different functions present in the module): +The methods associated with this module are accessible through (where * is a wildcard representing the different functions present in the module): @@ -25 +25 @@ For more information, see [JavaScript runtime 2.0 features for CloudFront Functi -Currently, the module only support MAC0 structure with HS256 (HMAC-SHA256) algorithm with a limit of 1KB for the maximum token size. +Currently, the module only supports MAC0 structure with HS256 (HMAC-SHA256) algorithm with a limit of 1 KB for the maximum token size. @@ -29 +29 @@ Currently, the module only support MAC0 structure with HS256 (HMAC-SHA256) algor -This section covers the token structure that is expected by the CWT module. The module expects the token to be correctly tagged and identifiable (for example COSE MAC0). Moreover, as for the structure of the token, the module follows the standards set by [CBOR Object Signing and Encryption (COSE) [RFC 8152]](https://datatracker.ietf.org/doc/html/rfc8152). +This section covers the token structure that is expected by the CWT module. The module expects the token to be correctly tagged and identifiable (for example COSE MAC0). For the token structure, the module follows the standards set by [CBOR Object Signing and Encryption (COSE) [RFC 8152]](https://datatracker.ietf.org/doc/html/rfc8152). @@ -50 +50 @@ This section covers the token structure that is expected by the CWT module. The - 1: 4 // algorithm : HMAC-256-64 + 1: 5 // algorithm @@ -63 +63 @@ This section covers the token structure that is expected by the CWT module. The - h'093101ef6d789200' // tag + h'ac243fa30d37386146b4e559fdc56d28840c453e7caf58126e819cbc3c299b3a' // tag @@ -67,0 +68,2 @@ This section covers the token structure that is expected by the CWT module. The +The tag in this example is the HMAC-SHA256 of the token, computed with the example key `Symmetric256` from [RFC 8392](https://datatracker.ietf.org/doc/html/rfc8392). + @@ -74 +76,7 @@ The CWT tag is optional when generating tokens. However, the COSE structure tag -The function decodes and validates a CWT token using the specified key. If validation is successful, it returns the decoded CWT token. Otherwise, it throws an error. Please note that this function does no validation on the claim set. +The function decodes and validates a CWT token using the specified key. If validation is successful, it returns the decoded CWT token. Otherwise, it throws an error. + +###### Important + +`validateToken()` checks only the structure of the token and its tag. It doesn't check any claims, such as `exp`, `nbf`, or `aud`, or Common Access Token (CAT) claims, such as `catu`. Your function must check the claims that it relies on. + +To check a `catu` query restriction, compare it with the unparsed query string, which `request.rawQueryString()` returns. For more information, see [rawQueryString() method](./general-helper-methods.html#raw-query-string-method). @@ -79 +87 @@ The function decodes and validates a CWT token using the specified key. If valid - cf.cwt.validateToken(token, handlerContext{key}) + cf.cwt.validateToken(token, handlerContext) @@ -83 +91 @@ The function decodes and validates a CWT token using the specified key. If valid -**token (required)** +**token (Required)** @@ -96 +104 @@ A JavaScript Object that stores context for the validateToken call. At present, -Secret key for message digest computation. Can be provided either as a string or JavaScript Buffer. +Secret key for message digest computation. Can be provided either as a string or JavaScript Buffer. The bytes of a string are used as the key, so if you store the key base64-encoded, decode it first, for example with `Buffer.from(key, 'base64')`. @@ -100 +108 @@ Secret key for message digest computation. Can be provided either as a string or -When the `validateToken()` method returns a successfully validated token, the response from the function is a `CWTObject` in the following format. Once decoded, all claim keys are represented as strings. +When the `validateToken()` method returns a successfully validated token, the response from the function is a JavaScript object in the following format. Once decoded, all claim keys are represented as strings of their numeric labels, such as `"4"` for `exp`. @@ -103,3 +111,3 @@ When the `validateToken()` method returns a successfully validated token, the re - CWTObject { - protectedHeaders, - unprotectedHeaders, + { + protected, + unprotected, @@ -109 +117 @@ When the `validateToken()` method returns a successfully validated token, the re -### Example - Validate token with kid sent as part of the token +### Example - Validate token with kid sent in a request header @@ -111 +119 @@ When the `validateToken()` method returns a successfully validated token, the re -This example demonstrates CWT token validation, where the kid is extracted from the header. The kid is then passed into CloudFront Functions KeyValueStore to fetch the secret key used to validate the token. +This example demonstrates CWT token validation, where the `kid` is read from the `x-cwt-kid` request header. The `kid` is then passed into CloudFront Functions KeyValueStore to fetch the secret key used to validate the token. @@ -143 +151 @@ This example demonstrates CWT token validation, where the kid is extracted from - if (cwtObj[CwtClaims.exp] && cwtObj[CwtClaims.exp] < currentTime) { + if (cwtObj.payload[CwtClaims.exp] && cwtObj.payload[CwtClaims.exp] < currentTime) { @@ -157 +165 @@ This example demonstrates CWT token validation, where the kid is extracted from - statusCode: 402, + statusCode: 500, @@ -171 +179 @@ This function generates a new CWT token using the provided payload and context s - cf.cwt.generateToken(generatorContext, payload) + cf.cwt.generateToken(tokenContents, generatorContext) @@ -174,0 +183,5 @@ This function generates a new CWT token using the provided payload and context s +**tokenContents (Required)** + + +The contents of the token to encode, as a JavaScript object with exactly three properties: `protected`, `unprotected`, and `payload`. This is the same format as the object that `validateToken()` returns. + @@ -178 +191 @@ This function generates a new CWT token using the provided payload and context s -This a JavaScript Object that is used as the context for generating the token and contains the following key value pairs: +This is a JavaScript Object that is used as the context for generating the token and contains the following key value pairs: @@ -195,5 +207,0 @@ Secret key to compute message digest. This value can be either a string or JavaS -**payload (Required)** - - -Token payload for encoding. The payload must be in `CWTObject` format. - @@ -204 +212 @@ Returns a JavaScript Buffer containing the encoded token. -###### Example: Generate a CWT token +###### Example: Generate a token @@ -216,4 +224,4 @@ Returns a JavaScript Buffer containing the encoded token. - catu: 401, - catnip: 402, - catm: 403, - catr: 404 + catnip: 311, + catu: 312, + catm: 313, + catr: 323 @@ -224,2 +232,2 @@ Returns a JavaScript Buffer containing the encoded token. - path: 2, - ext: 3 + path: 3, + ext: 8 @@ -228,0 +237 @@ Returns a JavaScript Buffer containing the encoded token. + exact_match: 0, @@ -230,2 +239 @@ Returns a JavaScript Buffer containing the encoded token. - suffix_match: 2, - exact_match: 3 + suffix_match: 2 @@ -235,3 +243,11 @@ Returns a JavaScript Buffer containing the encoded token. - renewal_method: 1, - next_renewal_time: 2, - max_uses: 3 + type: 0, + expadd: 1, + deadline: 2, + header_name: 4 + }; + + const CatrTypes = { + automatic: 0, + cookie: 1, + header: 2, + redirect: 3 @@ -250 +266 @@ Returns a JavaScript Buffer containing the encoded token. - 1: "5", + 1: 5, @@ -281,3 +297,4 @@ Returns a JavaScript Buffer containing the encoded token. - [Catr.renewal_method]: "header_renewal", - [Catr.next_renewal_time]: 1750000000, - [Catr.max_uses]: 5 + [Catr.type]: CatrTypes.header, + [Catr.expadd]: 3600, + [Catr.deadline]: 60, + [Catr.header_name]: "CTA-Common-Access-Token" @@ -287,0 +305 @@ Returns a JavaScript Buffer containing the encoded token. + const request = event.request; @@ -312 +330 @@ Returns a JavaScript Buffer containing the encoded token. - statusCode: 401, + statusCode: 500, @@ -318 +336 @@ Returns a JavaScript Buffer containing the encoded token. - statusCode: 402, + statusCode: 500, @@ -354,3 +372,3 @@ Returns a JavaScript Buffer containing the encoded token. - if (cwtJSON[CwtClaims.exp] && cwtJSON[CwtClaims.exp] < currentTime) { - // We can regnerate the token and add 8 hours to the expiry time - cwtJSON[CwtClaims.exp] = Math.floor(Date.now() / 1000) + (8 * 60 * 60); + if (cwtJSON.payload[CwtClaims.exp] && cwtJSON.payload[CwtClaims.exp] < currentTime) { + // We can regenerate the token and add 8 hours to the expiry time + cwtJSON.payload[CwtClaims.exp] = Math.floor(Date.now() / 1000) + (8 * 60 * 60); @@ -364 +382 @@ Returns a JavaScript Buffer containing the encoded token. - request.headers['x-cwt-regenerated-token'] = newTokenBuffer.toString('base64url'); + request.headers['x-cwt-regenerated-token'] = { value: newTokenBuffer.toString('base64url') }; @@ -375 +393 @@ Returns a JavaScript Buffer containing the encoded token. - statusCode: 402, + statusCode: 500,