Sub topic 13
Adding security and CORS headers with response headers policies
This chapter assumes you have a working distribution in front of a private S3 bucket, as covered earlier. You should also know what an HTTP response header is and what CORS (cross-origin resource sharing) does in a browser.
Why add headers at CloudFront
S3 can return CORS headers, but only through a per-bucket CORS configuration. It cannot add security headers such as Strict-Transport-Security. A response headers policy is a CloudFront setting that adds or overrides headers on responses at the edge. You write it once, attach it to one or more cache behaviors, and every response from that behavior carries the headers. This works whether the response comes from the cache or from S3.
A response headers policy is separate from the cache policy. It does not change the cache key, so you can edit it without affecting what gets cached.
Managed policies
CloudFront provides managed response headers policies, so you can start without writing anything. Common ones include:
| Managed policy | What it does |
|---|---|
| `SimpleCORS` | Adds `Access-Control-Allow-Origin: *` for simple cross-origin GET and HEAD requests |
| `CORS-With-Preflight` | Adds CORS headers that also answer preflight OPTIONS requests |
| `SecurityHeadersPolicy` | Adds a standard set of security headers |
| `CORS-and-SecurityHeadersPolicy` | Combines the CORS and security sets |
The * origin in the managed CORS policies allows any website to read your content from a browser. That is acceptable for public fonts or images but not for anything you want to limit. For a specific origin, write a custom policy.
A custom policy
The following CloudFormation resource allows one origin and adds three security headers. It also adds a Cache-Control header only when the origin did not send one, because Override: false keeps the origin's value.
Resources:
SiteHeadersPolicy:
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties:
ResponseHeadersPolicyConfig:
Name: site-headers
CorsConfig:
AccessControlAllowCredentials: false
AccessControlAllowHeaders:
Items:
- Content-Type
AccessControlAllowMethods:
Items:
- GET
- HEAD
- OPTIONS
AccessControlAllowOrigins:
Items:
- https://app.example.com
AccessControlMaxAge: 600
OriginOverride: true
SecurityHeadersConfig:
StrictTransportSecurity:
AccessControlMaxAgeSec: 31536000
IncludeSubdomains: true
Override: true
ContentTypeOptions:
Override: true
FrameOptions:
FrameOption: DENY
Override: true
CustomHeadersConfig:
Items:
- Header: Cache-Control
Value: public,max-age=300
Override: falseAttach the policy by setting ResponseHeadersPolicyId: !Ref SiteHeadersPolicy inside DefaultCacheBehavior of your distribution, or inside any other cache behavior. Different path patterns can use different policies.
The Override setting
Each header group has an override flag. If the origin already sends the header and Override is true, CloudFront replaces the origin's value. If it is false, CloudFront keeps the origin's value and uses its own only when the origin sent none.
Common errors
- CORS headers missing. CloudFront adds CORS headers only when the request carries an
Originheader. Test withcurl -I -H "Origin: https://app.example.com" https://dxxxx.cloudfront.net/file.png. - Wrong origin in a policy. The value must match the page's origin exactly, including scheme and port.
http://app.example.comandhttps://app.example.comdiffer. - Preflight fails. Include
OPTIONSin the allowed methods, and make sure the behavior's allowed methods cover the preflight request. - Changes not visible. A policy edit propagates to edge locations over a few minutes. You do not need an invalidation, because headers are added at response time.
Exercise
Your fonts at https://cdn.example.com load on https://app.example.com but fail on https://www.example.com with a CORS error. The policy allows only https://app.example.com. What do you change, and do you need an invalidation?
Show answer
Add https://www.example.com to AccessControlAllowOrigins in the response headers policy (or, if the fonts are public, use a managed policy such as SimpleCORS). No invalidation is needed, because CloudFront applies the policy at response time. Wait for the policy change to finish deploying.