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.
CORS rule elements
| Element | Meaning |
|---|---|
AllowedOrigins | Origins 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 |
AllowedMethods | GET, PUT, POST, DELETE, HEAD |
AllowedHeaders | Request headers allowed in the preflight. One * per string, for example x-amz-* |
ExposeHeaders | Response headers the page script may read, such as ETag or x-amz-request-id |
MaxAgeSeconds | How 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
| Misconception | Reality |
|---|---|
| "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
| Symptom | Likely cause |
|---|---|
No Access-Control-Allow-Origin header | No CORS configuration, or the origin string does not match exactly (scheme, host, port) |
| Method not allowed | The method is missing from AllowedMethods |
| Preflight fails | A custom request header is not covered by AllowedHeaders |
| Works direct, fails through CloudFront | CloudFront 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
OPTIONSpreflight from the first matching rule. - A CORS configuration never grants S3 permissions.
curland 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.
Related
Further reading
CloudFront Edge Security
Protecting S3 and custom origins behind CloudFront with OAC, signed URLs and cookies, response headers policies, geo restriction and field-level encryption.
Elastic Load Balancer Security
TLS termination, SNI and SAN certificates, forward secrecy, mutual TLS and WAF on an ALB, plus isolating a compromised target.