How CloudFront caching works with pre-signed URLs and CORS

0

Hi,

I'm looking for help with understanding how everything works in the following scenario:

  1. I have an S3 bucket with origin access control configured for my CloudFront distribution.
  2. S3 and CloudFront have configured CORS with Access-Control-Allow-Origin: * for this specific environment to simplify it.
  3. CloudFront behaviour has the following settings: Cache policy: CachingOptimized, Origin request policy: CORS-S3Origin, Response header policy: SimpleCORS
  4. CloudFront has a public key configured and provides access only for pre-signed URLs.

How does caching work in this case? If I understand it, someone will request a file without query parameters ?Expires=111111&Signature=xxxxx it will cache 403 response and all following requests will fail because of the CachingOptimized policy, which doesn't check query parameters. In what order is CloudFront processing request? If it first checks the pre-signed URL, and if all is good, then provides the file from the cache without query parameters, all should be ok, but I see some strange behaviour of the cache in my case.

profile picture
專家
已提問 4 個月前檢視次數 406 次
1 個回答
0
已接受的答案

Hi,

You should look at this page to understand the full mechanism: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html

Section "How signed URLs work" say:

CloudFront uses the public key to validate the signature and confirm that the URL hasn't 
been tampered with. If the signature is invalid, the request is rejected.

If the signature is valid, CloudFront looks at the policy statement in the URL (or constructs 
one if you're using a canned policy) to confirm that the request is still valid. For example, 
if you specified a beginning and ending date and time for the URL, CloudFront confirms that 
the user is trying to access your content during the time period that you want to allow access.

If the request meets the requirements in the policy statement, CloudFront does the standard 
operations: determines whether the file is already in the edge cache, forwards the request to 
the origin if necessary, and returns the file to the user.

Best,

Didier

profile pictureAWS
專家
已回答 4 個月前
profile picture
專家
已審閱 1 個月前
  • Hi Didier,

    Thank you for pointing this out; I've been on this page but missed these last steps describing the procedure. Based on this, would it be the correct assumption that after CloudFront validates the signature and expiry and other related access parameters, it will strip all these parameters and look in the cache only the file? For two recurring requests with different signatures to the same object from the same location, my first request will be forwarded to the origin, and the second, I will get from the cache, even if my Caching policy is CachingOptimized.

  • Hi, thanks for accepting my answer. Yes, my understanding is that CF looks in the cache if the policy allows and then goes to origin if needed. Re CachingOptimized doc says "CloudFront doesn't include any query strings or cookies in the cache key, and only includes the normalized Accept-Encoding header" see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/using-managed-cache-policies.html#managed-cache-caching-optimized

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南