I'm attempting to use signed cookies to access private content on AWS Cloudfront.
My app successfully sets the correct cookies required by cloudfront, and each cookie has the correct value. For reference, the three cookies that need to be set are:
I know that the values are correct as if I grab those cookies and perform a curl request using them, it is successful (values replaced with ... for the sake of brevity):
curl -v -b "CloudFront-Signature=...; CloudFront-Policy==...; CloudFront-Key-Pair-Id==...;" http://mydistribution.cloudfront.net/myfile.jpg
When I use the browser, whilst I can see the cookies have been set in my original request to the server http://localhost:3000/, my requests to Cloudfront urls (for example, in my image tags) do not pass on any of these cookies. This results in
<Error><Code>MissingKey</Code><Message>Missing Key-Pair-Id query parameter or cookie value</Message></Error>
Any ideas why my cookies are not being sent to cloudfront? Advice and support appreciated :)
If you want to receive cookies at your origin but you don't want CloudFront to cache the Set-Cookie headers in your origin's responses, configure your origin to add a Cache-Control header with a no-cache directive that specifies Set-Cookie as a field name. For example: Cache-Control: no-cache="Set-Cookie" .
If your CloudFront distribution isn't caching based on the custom values that you set on cache behaviors, then check the origin. Verify whether the origin has any conflicting caching headers.
Right click your distribution. Select "Get Cloudfront invalidation list" Then select "Create" to create a new invalidation list. Select the files to invalidate, and click "Invalidate." Wait 5-15 minutes.
After a request returns the "X-Cache:Miss from CloudFront" response, the browser might serve the same response to subsequent requests because it's stored in the browser cache. To verify if the response is stored in the browser cache, clear the browser cache and make a new request for the same object.
This is normal: each cookie has an associated domain and for each request the browser will only send cookies that match the URL the request is being made for.
Furthermore you cannot set cookies for arbitrary domains - if your application was on example.com then it can set cookies for example.com and any subdomains, but not for other domains (eg other-domain.com)
Therefore if your app is being accessed by the browser as localhost then it can't set cookies sent to foo.cloudfront.net. The signed cookies function in cloudfront is pretty new so I'm not sure what the recommended approach is here, however you can configure cloudfront to pass certain paths through to your app (check the cloudfront documentation on behaviour and origins) at this point the browser thinks it is talking to cloudfront so it will let you set cookies that will be sent in subsequent requests to your cloudfront distribution.
Your app does need to be reachable from cloudfront for this to work though, so I don't think this will work in development.
Alternatively use a CNAME so that your cloudfront distribution and your app are served from the same domain. This will however mean that you can't use the default cloudfront ssl certificate, and unless SNI is acceptable, you'll need to pay extra to use your own certificate.
Yes, this is the way we are setting up, using CNAME alias and set the cookies on the base domain, then you will be able to pass your cookie.
Let's put more detail to it in case people want to know what would be the next step is, let's use the above example :-
If you are using javascript to get the cdn assets, make sure in your JS code, you need to pass withCredentials option, or it won't work. For example, if you are using jQuery, you will need something like the following :-
$.ajax({
url: a_cross_domain_url,
xhrFields: {
withCredentials: true
}
});
And if the request is successful, you should get a response header from CloudFront with "Access-Control-blah-blah".
Hope it helps people if they search this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With