DEV Community

Cover image for Vulnerabilities when Using Lambda Authorizers with Caching in API Gateway.
Tycko Franklin
Tycko Franklin

Posted on

Vulnerabilities when Using Lambda Authorizers with Caching in API Gateway.

I'm writing this up to share what almost caught me one time, and seems like it's a potentially huge vulnerability for those who don't know the dangers of it.

This is the vulnerability that can get introduced when you have a Lambda Authorizer, with caching enabled, attached to an API Gateway Rest API. The easiest way to show this is to have two routes that each have their own role that can access their respective route. Below I will get into more detail, but first, I do want to mention that this is known and was documented by Julian Wood in the AWS blog post that announced Lambda Authorizers for HTTP APIs. It's known, and experts know to avoid it, but does the average builder? I had to intentionally search for this to find that article. I haven't seen others mention the issue, so I think it's worth a quick blog post to help share the potential issue!

Now, let's set up our situation. We have warehouse and delivery company. There are warehouse clerks, who load up the delivery items, and there are dispatchers who send the packages out to the correct routes. For the sake of this small example, warehouse clerks can only see their pick lists, and dispatchers can only see the delivery routes. Neither group of folks can view what the other can.

This sets us up with the need for an API that has /getDeliveryRoutes and /getPickingLists (both POST for simplicity). The dispatcher group can use /getDeliveryRoutes, and the warehouse clerks can use /getPickingLists. The API is created and put to use, but then it starts having weird errors for calls that should go through, and there's a suspicion that employees are accessing data they shouldn't be able to.

This leads us to the diagram of how the API is supposed to work, where we have one version with caching (v1) and one without caching (v2) (enjoy the cyberpunk world building, or skip to the diagram below it for clarity):

A cybersecurity analyst with glowing cybernetic implants sips coffee while investigating an API authorization and caching incident in a rugged field lab. Neon-lit monitors display the authorization flowchart and forensic logs. Yellow caution tape, numbered evidence markers, and a bagged storage drive surround the workstation, with rain-soaked mountains and communications antennas outside.

Or, here's a clean diagram without ai art in it (it's cut up to stay large on here):

AWS-style flowchart showing an incoming POST request reaching API Gateway authorization. Version 1 checks for a cached identity policy with a five-second TTL; a cache miss or version 2’s uncached path invokes the shared Lambda authorizer.
The shared Lambda authorizer checks the Authentication header and identity. Dispatchers may access getDeliveryRoutes, while warehouse clerks may access getPickingLists. Matching requests receive an Allow policy; other routes receive Deny. Invalid identities receive 401 Unauthorized. Version 1 caches returned policies.
API Gateway evaluates the selected policy against the current request. Denied requests receive 403 Forbidden; allowed requests invoke either the getDeliveryRoutes or getPickingLists Lambda function.

API V2 has no issues on the caching front, because it doesn't cache anything and every request is authenticated and authorized. V1 on the other hand, has a Lambda Authorizer caching set to 5 seconds and it doesn't include the route in that cache, so it's cached for all routes. This isn't a problem if you move the authorization (what the authenticated user can do) to the end lambda and pass in the authorizer context with the roles the user has; however, that's not what we're doing and misses a nice feature of having authorization centralized in the Lambda Authorizer. The Lambda authorizer is designed in this case to evaluate the same user's roles per route to determine if they should have access or not. We save resources (only the authorizer lambda spins up, not the more expensive ones down the line), we have all (or most) of the authorization code in one place so we don't miss things during upgrades or migrations, and the lambda can usually be very small so we aren't spinning up a lot of resources if we don't cache. I think if the route and method or included in caching, this could remove most of the issues and get some of the caching advantages, but that's outside this blog post's scope.

To demo what can happen, I deployed this api with the AWS CDK, opened the API Gateway main stage in the browser, and then ran JavaScript from the console. The script I made has the two routes, and the two roles. The calls go out once a second for nearly a minute, randomly switching between the combinations. For simplicity, the roles are just passed as "warehouseClerk" and "dispatcher" in the authorization header, but pretend they are bearer tokens to authenticate and authorize that will result in the roles being extracted or looked up. The possible outcomes where: EXPECTED (authorized or rejected as they should be), EXPOSED (user was authorized when they shouldn't be), UNEXPECTED DENIAL (user was rejected when they should have been authorized), and ERROR.

See the results below:

(index) role route expected status outcome
0 dispatcher getDeliveryRoutes 200 200 EXPECTED
1 dispatcher getPickingLists 403 200 EXPOSED
2 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
3 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
4 dispatcher getDeliveryRoutes 200 200 EXPECTED
5 dispatcher getPickingLists 403 200 EXPOSED
6 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
7 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
8 dispatcher getDeliveryRoutes 200 200 EXPECTED
9 dispatcher getPickingLists 403 403 EXPECTED
10 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
11 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
12 dispatcher getDeliveryRoutes 200 403 UNEXPECTED DENIAL
13 dispatcher getPickingLists 403 403 EXPECTED
14 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
15 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
16 dispatcher getDeliveryRoutes 200 403 UNEXPECTED DENIAL
17 dispatcher getPickingLists 403 403 EXPECTED
18 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
19 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
20 dispatcher getDeliveryRoutes 200 200 EXPECTED
21 dispatcher getPickingLists 403 200 EXPOSED
22 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
23 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
24 dispatcher getDeliveryRoutes 200 200 EXPECTED
25 dispatcher getPickingLists 403 200 EXPOSED
26 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
27 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
28 dispatcher getDeliveryRoutes 200 200 EXPECTED
29 dispatcher getPickingLists 403 200 EXPOSED
30 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
31 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
32 dispatcher getDeliveryRoutes 200 200 EXPECTED
33 dispatcher getPickingLists 403 200 EXPOSED
34 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
35 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
36 dispatcher getDeliveryRoutes 200 200 EXPECTED
37 dispatcher getPickingLists 403 200 EXPOSED
38 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
39 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
40 dispatcher getDeliveryRoutes 200 200 EXPECTED
41 dispatcher getPickingLists 403 200 EXPOSED
42 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
43 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
44 dispatcher getDeliveryRoutes 200 200 EXPECTED
45 dispatcher getPickingLists 403 200 EXPOSED
46 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
47 warehouseClerk getPickingLists 200 403 UNEXPECTED DENIAL
48 dispatcher getDeliveryRoutes 200 200 EXPECTED
49 dispatcher getPickingLists 403 200 EXPOSED
50 warehouseClerk getDeliveryRoutes 403 403 EXPECTED
(index) route TOTAL EXPECTED EXPOSED UNEXPECTED DENIAL ERROR
0 getDeliveryRoutes 26 24 0 2 0
1 getPickingLists 25 3 10 12 0

and again if I reversed the call order:

(index) route TOTAL EXPECTED EXPOSED UNEXPECTED DENIAL ERROR
0 getDeliveryRoutes 27 6 11 10 0
1 getPickingLists 27 24 2 1 0

As the data shows there are a lot of wrong results even just doing this once a second with caching for 5 seconds. When I was first working with this I was thinking "caching is good, let's do it!" we choose 30 second to cache. If the team hadn't been paying attention, we would have left ourselves open to this vulnerability and likely would have taken a while to figure out what was up because who would test different roles within that time frame? Anyways, it was figured out, AWS has at least one blog post with the warning in there, but I figured it could easily slip by without people noticing so I added to the content out here. This could be a very hard to track down bug if you don't know what caused it, and people out there could easily test your system if they know routes they shouldn't have access to.

In conclusion, when enabling features that sound good (who doesn't want to cache things?), make sure you're thinking big picture and what vulnerabilities or unintended consequences might show up once you enable it.

note: images generated using Codex.

Top comments (1)

Collapse
 
nilesharma0108 profile image
Nilesh Sharma •

Your results table makes the mechanism clear: with the authorizer cache on, the policy generated for one route gets reused for the other, which accounts for both the EXPOSED and the UNEXPECTED DENIAL rows. A commonly used mitigation is having the authorizer return a policy that covers every route the role is allowed to call instead of only the single requested method, or turning caching off for authorizers whose decision depends on the route. Worth testing any cached authorizer with mixed-route traffic like your script does, since single-route testing would never show this.