CORS
What an origin is, how browsers use CORS and preflight requests, and how to configure CORS on an S3 bucket.
What it is
Web browsers protect users with the same-origin policy: JavaScript on a page from one website can't freely read responses from another website. CORS (Cross-Origin Resource Sharing) is how the other website says "requests from that origin are allowed".
An origin is the scheme (protocol) + host (domain) + port. In https://www.example.com, the port is implied: 443 for HTTPS, 80 for HTTP.
- Same origin:
http://example.com/app1andhttp://example.com/app2. - Different origins:
http://www.example.comandhttp://other.example.com.
Requests to another origin aren't fulfilled by the browser unless the other origin allows them, using CORS headers such as Access-Control-Allow-Origin.
How the browser checks
- The page on
https://www.example.comwants to callhttps://www.other.com. - For anything beyond a simple GET, the browser first sends a preflight request: an HTTP
OPTIONSrequest with the headerOrigin: https://www.example.com. - The other server answers with CORS headers, such as
Access-Control-Allow-Origin: https://www.example.comandAccess-Control-Allow-Methods: GET, PUT, DELETE. - If the origin and method are allowed, the browser makes the real request.
CORS is enforced by browsers. Other clients, such as the AWS CLI or a server, ignore it.
CORS on Amazon S3
If a client makes a cross-origin request to a bucket, the right CORS headers must be turned on for that bucket. It's a popular exam question.
- The browser loads
index.htmlfrom the website bucket. - The page needs
coffee.jpgfrom a second, assets bucket. The browser asks for it, sending itsOrigin. - The assets bucket has a CORS configuration that allows that origin, so it answers with
Access-Control-Allow-Origin, and the browser accepts the image.
A CORS rule can allow a specific origin or * (all origins). A CORS configuration on the bucket looks like:
[
{
"AllowedOrigins": ["http://www.example.com"],
"AllowedMethods": ["GET"],
"AllowedHeaders": ["*"],
"MaxAgeSeconds": 3000
}
]The CORS settings go on the bucket that receives the cross-origin request (the assets bucket), not on the bucket that served the page.
Real-world examples
- A web app at
app.example.comuploads files straight from the browser to an S3 bucket. The bucket's CORS rules allowPUTfromhttps://app.example.com. - A static site loads fonts and images from a separate assets bucket; that bucket allows the site's origin.
Exam tips
- "Website on one bucket can't load files from another bucket", "browser blocks the request", "CORS error" → add a CORS configuration to the bucket being requested, allowing the website's origin.
- "Allow any website to load these public files" → CORS with
AllowedOrigins: ["*"].
Common traps
- "Fix CORS with a bucket policy." Bucket policies control permissions; CORS is a separate bucket setting.
- "Configure CORS on the bucket that serves the HTML." It goes on the bucket the browser is calling cross-origin.
Hands-on lab: two buckets and a CORS rule
Goal: see a browser block a cross-origin request, then allow it with CORS.
Costs money
Two small website buckets and a few requests cost less than a cent. Any Region works.
Before you start: set up a website bucket as in the static website lab.
Create the assets bucket
Create a second bucket saa-assets-<random>, make it a public static website the same way, and upload extra.html containing <p>Loaded from the other bucket</p>.
Fetch it from the first site
Replace index.html in the first bucket with:
<h1>Hello from S3</h1>
<div id="x"></div>
<script>
fetch('http://saa-assets-<random>.s3-website-us-east-1.amazonaws.com/extra.html')
.then(r => r.text()).then(t => document.getElementById('x').innerHTML = t);
</script>Open the first site and the browser's developer console: a CORS error, and the text doesn't appear.
Add the CORS rule
On saa-assets-<random>, Permissions → Cross-origin resource sharing (CORS) → Edit. Paste the configuration above with AllowedOrigins set to your first site's website endpoint (starting http://, no trailing slash). Save and reload the first site: the text from the other bucket appears.
Expected result: the browser only accepts the cross-origin response once the assets bucket allows the site's origin.
Clean up: empty and delete both buckets.
AWS documentation
Encryption
S3 server-side encryption with SSE-S3, SSE-KMS, DSSE-KMS and SSE-C, KMS quotas and Bucket Keys, client-side encryption, encryption in transit, and forcing encryption with bucket policies.
MFA Delete and access logs
MFA Delete for protecting object versions, and S3 server access logs, including the logging-loop warning.