AWS Security ChangesHomeSearch

AWS AmazonCloudFront: Harden CloudFront Functions JWT verification example

Service: AmazonCloudFront · 2026-10-29 · Security-related high

File: AmazonCloudFront/latest/DeveloperGuide/example_cloudfront_functions_kvs_jwt_verify_section.md · Type: authz

Summary

Rewrites the CloudFront Functions JWT example: replaces jwt_decode (which accepted a noVerify flag and a caller-supplied algorithm) with a jwtVerify function that always verifies the HS256 signature using a constant-time comparison, requires the exp claim, and cleans up the 401 handling and secret-key naming.

Security assessment

The old example exposed a `noVerify` parameter and an algorithm argument that could let callers skip or weaken signature validation, and it only checked `exp` when present. The new code unconditionally recomputes the HMAC-SHA256 signature, compares it in constant time to avoid timing side channels, and rejects tokens with a missing or expired `exp`, directly strengthening token authentication in the sample.

Evidence

+        if (!_constantTimeEquals(segments[2], expected)) {

Diff

diff --git a/AmazonCloudFront/latest/DeveloperGuide/example_cloudfront_functions_kvs_jwt_verify_section.md b/AmazonCloudFront/latest/DeveloperGuide/example_cloudfront_functions_kvs_jwt_verify_section.md
index 03438c2..71662b7 100644
--- a//AmazonCloudFront/latest/DeveloperGuide/example_cloudfront_functions_kvs_jwt_verify_section.md
+++ b//AmazonCloudFront/latest/DeveloperGuide/example_cloudfront_functions_kvs_jwt_verify_section.md
@@ -22 +21,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    import crypto from 'crypto';
@@ -23,0 +23 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+    import crypto from 'crypto';
@@ -25,5 +25,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    //Response when JWT is not valid.
-    const response401 = {
-        statusCode: 401,
-        statusDescription: 'Unauthorized'
-    };
+    // Response when JWT is not valid
+    const response401 = { statusCode: 401, statusDescription: 'Unauthorized' };
@@ -37,2 +34 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    
-    function jwt_decode(token, key, noVerify, algorithm) {
+    function jwtVerify(token, key) {
@@ -49,16 +45,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        // All segment should be base64
-        const headerSeg = segments[0];
-        const payloadSeg = segments[1];
-        const signatureSeg = segments[2];
-    
-        // base64 decode and parse JSON
-        const payload = JSON.parse(_base64urlDecode(payloadSeg));
-    
-        if (!noVerify) {
-            const signingMethod = 'sha256';
-            const signingType = 'hmac';
-    
-            // Verify signature. `sign` will return base64 string.
-            const signingInput = [headerSeg, payloadSeg].join('.');
-    
-            if (!_verify(signingInput, key, signingMethod, signingType, signatureSeg)) {
+        // Verify the HS256 signature
+        const expected = crypto.createHmac('sha256', key).update(`${segments[0]}.${segments[1]}`).digest('base64url');
+        if (!_constantTimeEquals(segments[2], expected)) {
@@ -67,0 +51,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+        // base64 decode and parse JSON
+        const payload = JSON.parse(Buffer.from(segments[1], 'base64url'));
+    
@@ -74,3 +60,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            if (payload.exp && Date.now() > payload.exp*1000) {
-                throw new Error('Token expired');
-            }
+        if (!payload.exp || Date.now() > payload.exp*1000) {
+            throw new Error('Token expired or missing exp');
@@ -82,2 +67 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    //Function to ensure a constant time comparison to prevent
-    //timing side channels.
+    // Constant-time comparison to prevent timing side channels
@@ -85 +69 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        if (a.length != b.length) {
+        if (a.length !== b.length) {
@@ -91,12 +74 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        xor |= (a.charCodeAt(i) ^ b.charCodeAt(i));
-        }
-    
-        return 0 === xor;
-    }
-    
-    function _verify(input, key, method, type, signature) {
-        if(type === "hmac") {
-            return _constantTimeEquals(signature, _sign(input, key, method));
-        }
-        else {
-            throw new Error('Algorithm type not recognized');
+            xor |= a.charCodeAt(i) ^ b.charCodeAt(i);
@@ -104,8 +76 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-    }
-    
-    function _sign(input, key, method) {
-        return crypto.createHmac(method, key).update(input).digest('base64url');
-    }
-    
-    function _base64urlDecode(str) {
-        return Buffer.from(str, 'base64url')
+        return xor === 0;
@@ -115 +80 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        let request = event.request;
+        const request = event.request;
@@ -117,3 +82,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        //Secret key used to verify JWT token.
-        //Update with your own key.
-        const secret_key = await getSecret()
+        // Secret key used to verify the JWT, read from KeyValueStore
+        const secretKey = await getSecret();
@@ -121 +85 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        if(!secret_key) {
+        if (!secretKey) {
@@ -125 +89 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        // If no JWT token, then generate HTTP redirect 401 response.
+        // No JWT in the query string, return 401
@@ -134,4 +98,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-            jwt_decode(jwtToken, secret_key);
-        }
-        catch(e) {
-            log(e);
+            jwtVerify(jwtToken, secretKey);
+        } catch (e) {
+            log(e.message);
@@ -141 +104 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
-        //Remove the JWT from the query string if valid and return.
+        // Remove the JWT from the query string if valid and return