Skip to Content

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/app1 and http://example.com/app2.
  • Different origins: http://www.example.com and http://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

  1. The page on https://www.example.com wants to call https://www.other.com.
  2. For anything beyond a simple GET, the browser first sends a preflight request: an HTTP OPTIONS request with the header Origin: https://www.example.com.
  3. The other server answers with CORS headers, such as Access-Control-Allow-Origin: https://www.example.com and Access-Control-Allow-Methods: GET, PUT, DELETE.
  4. 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

CORS: a browser loads index.html from the www.example.com bucket, then asks the assets.example.com bucket for coffee.jpg with an Origin header; the assets bucket has a CORS rule for www.example.com, so it answers with Access-Control-Allow-Origin and the browser allows the response CORS: a browser loads index.html from the www.example.com bucket, then asks the assets.example.com bucket for coffee.jpg with an Origin header; the assets bucket has a CORS rule for www.example.com, so it answers with Access-Control-Allow-Origin and the browser allows the response

The second bucket must allow the first bucket's origin.

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.

  1. The browser loads index.html from the website bucket.
  2. The page needs coffee.jpg from a second, assets bucket. The browser asks for it, sending its Origin.
  3. 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.com uploads files straight from the browser to an S3 bucket. The bucket's CORS rules allow PUT from https://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

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt