Skip to Content
Asterrr's Handbook
3 · Infrastructure security

S3 Cross-Origin Resource Sharing

How S3 CORS rules tell browsers which origins may call a bucket, and what CORS does not protect.

Exam tasks: 3.1 (S3 CORS)

The decision: is the failure a missing CORS rule for a browser call, or does the requirement really need bucket policy or OAC instead?

Browsers apply the same-origin policy and block JavaScript from reading responses from a different origin. A CORS configuration on the bucket tells the browser which origins, methods and headers S3 allows. It is a browser instruction, not an access control.

JavaScriptOPTIONS preflightWeb app onwww.example.comBrowserS3 bucket withCORS rules
The browser asks first with a preflight OPTIONS request. S3 matches it against the bucket's CORS rules and answers with Access-Control headers. The browser then allows or blocks the real request.

CORS rule elements

ElementMeaning
AllowedOriginsOrigins allowed to call the bucket. Include the scheme, such as https://www.example.com. One * wildcard is allowed per string, so https://*.example.com is valid
AllowedMethodsGET, PUT, POST, DELETE, HEAD
AllowedHeadersRequest headers allowed in the preflight. One * per string, for example x-amz-*
ExposeHeadersResponse headers the page script may read, such as ETag or x-amz-request-id
MaxAgeSecondsHow long the browser caches the preflight answer

Up to 100 rules can be in one configuration. The S3 console accepts it as JSON (the API also accepts XML). AllowedOrigins of * lets every site call the bucket.

What CORS does not do

MisconceptionReality
"CORS protects my objects"Only browsers enforce it. curl, mobile apps and server code ignore CORS
"CORS grants access"Bucket policies, IAM and Block Public Access still decide who can read. CORS only lets the browser expose the response
"* is fine because the bucket is private"Still avoid it. Use exact origins and HTTPS

Troubleshooting

SymptomLikely cause
No Access-Control-Allow-Origin headerNo CORS configuration, or the origin string does not match exactly (scheme, host, port)
Method not allowedThe method is missing from AllowedMethods
Preflight failsA custom request header is not covered by AllowedHeaders
Works direct, fails through CloudFrontCloudFront must forward the Origin header to S3 (cache policy or origin request policy), or add the CORS headers in CloudFront instead

Common patterns are static sites loading assets from S3, single-page apps reading S3 data, and direct browser uploads with presigned URLs.

Exam signal

"Browser console shows a cross-origin error when a web app fetches from S3" means add or fix the bucket CORS configuration, not a bucket policy.

Exam trap

Making CORS permissive never makes a private bucket readable, and tightening CORS never stops curl. If the question asks to prevent unauthorized downloads, use IAM, bucket policy or presigned URLs.

Troubleshooting notes

  • S3 answers the browser's OPTIONS preflight from the first matching rule.
  • A CORS configuration never grants S3 permissions. curl and servers ignore it.

Wildcard origins

AllowedOrigins: * with PUT or DELETE lets script on any website read responses and send writes to the bucket from a visitor's browser, as long as that script holds a presigned URL or credentials. Name the exact origins. And if the question says "users can download files directly by URL", the fix is bucket policy or OAC, not CORS.

Further reading

On this page