Reputation: 12650
I'm trying to set a Content-Security-Policy header for an html file I'm serving via s3/cloudfront. I'm using the web-based AWS console. Whenever I try to add the header:
it doesn't seem to respect it. What can I do to make sure this header is served?
Upvotes: 15
Views: 35427
Reputation: 1527
In CloudFront > Policies > Response headers > Custom policies
, create a new policy, where:
Security headers
, enable Content-Security-Policy
default-src 'self'
In Distributions > ${distributionName} > Behaviours > Default (*)
Response headers policy
, choose the Custom policy created in step 1.Upvotes: 2
Reputation: 1309
not on S3 but you can do it on CloudFront...
go to your distribution -->Behaviours--> default (or if you are using a different one) -> Edit --> Under "Response headers policy" click "View Policy" ... then edit your response policy.. save it then our CF distribution will use this new response headers policy
Upvotes: 1
Reputation: 1004
S3/CloudFront takes any headers that the origin set and forward those to the client, but you can't set custom headers on you response directly.
You can use Lambda@Edge function that can inject security headers through CloudFront.
Here is how the process works: (reference aws blog)
Below is the blog from aws on how to do this step by step.
Upvotes: 6
Reputation: 400
If you are testing through CloudFront, have you made sure you have invalidated the cached objects? Can you try to upload a completely new file and then try accessing it via CF and see if the header is still not there?
Update
Seems like custom metadata will not work as expected as per DOC. Any metadata other than the ones supported by S3 (the ones displayed in the dropdown) will have to be prefixed with x-amz-meta-
Upvotes: 5
Reputation: 2880
I'm having the same problem (using S3/CloudFront) and it appears there is currently no way to set this up easily.
S3 has a whitelist of the headers permitted, and Content-Security-Policy is not on it. Whilst it is true you can use the prefixed x-amz-meta-Content-Security-Policy, this is unhelpful as there is no browser support for it.
There are two options I can see.
1) you can serve the html content from a webserver on an EC2 instance and set that up as another CloudFront origin. Not really a great solution.
2) include the CSP as a meta tag within your html document:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src http://*.foobar.com 'self'">
...
This option is not as widely supported by browsers, but it appears to work with both Webkit and Firefox, so the current Chrome, Firefox, Safari (and IOS 7 Safari) seem to support it.
I chose 2 as it was the simpler/cheaper/faster solution and I hope AWS will add the CSP header in the future.
Upvotes: 10