MORTIMERDEV LABS / AWS / S3 CORS FREE · BETA
Why is S3 blocking
my browser request?
Paste your bucket's CORS configuration and describe the request. See which rule S3 picks, what it sends back and why the browser blocks it. Then build a rule that works, and the bucket policy CloudFront needs to read a private bucket, as JSON and CDK in TypeScript and Python.
Check a request
RESULT
Check these
Rule by rule
What goes over the wire
Build a CORS rule
Bucket policy for CloudFront (OAC)
What is S3 CORS?
Browsers stop a page from reading responses from another origin unless the server says it may. An S3 CORS configuration is how a bucket says so: a list of rules, and S3 uses the first one that matches.
- Who
- The page origins allowed, with at most one * each
- What
- GET, PUT, POST, DELETE or HEAD
- Preflight
- Request headers the browser may send
- Readable
- Response headers your script can see
Two different questions
- May this page read the response? That's CORS, and only browsers enforce it.
- May this request reach the object? That's permissions: a bucket policy, a presigned URL, or CloudFront with Origin Access Control. CORS never grants access.
Easy to get wrong
- A trailing slash in AllowedOrigins never matches:
browsers send
https://www.example.com. - OPTIONS isn't a valid AllowedMethod. S3 answers preflights from the method the browser asks about.
- Testing with curl shows no CORS headers unless you
send an
Originheader.
Read more: AWS: using CORS with S3AWS: elements of a CORS configurationAWS: troubleshooting CORSAWS: restrict access to an S3 origin (OAC)CDK: CorsRule
TEST YOURSELF
How well do you know S3 CORS?
Ten questions on rule matching, preflights, ExposeHeaders and OAC bucket policies, with an explanation and a link to the reference after each one. Three ask whether S3 allows a request, answered by the same rules as the checker.
S3 CORS REFERENCE
S3 CORS and bucket policies, in plain English.
How S3 matches a request
First matching ruleS3 checks the rules in order and uses the first one that matches the request.
A rule matches when the Origin header matches one of its AllowedOrigins, the method is one of its AllowedMethods and, for a preflight, every requested header matches AllowedHeaders. Later rules are ignored, so their ExposeHeaders and MaxAgeSeconds don't apply.
Test with an Origin headerS3 matches the request's Origin header against AllowedOrigins, so a test needs one.
Browsers add Origin to cross-origin requests; curl doesn't. In practice S3 sends no CORS headers to a request without Origin, so test with curl -i -H 'Origin: https://www.example.com' and check the response.
When nothing matchesIf no rule matches, S3 sends no CORS headers and the browser blocks the response.
AWS documents 403 Forbidden “This CORS request is not allowed” when no rule matches, and “CORS is not enabled for this bucket” when there's no configuration. After a failed preflight the browser never sends the real request. The console's CORS message doesn't include S3's error body, so check the Network tab or test with curl.
Preflight (OPTIONS)S3 answers a preflight using Access-Control-Request-Method and Access-Control-Request-Headers.
The browser sends OPTIONS before a PUT or DELETE, or a request with headers that aren't CORS-safelisted (such as Content-Type: application/json or Authorization). S3 checks the method it's asked about against AllowedMethods and each requested header against AllowedHeaders. You never list OPTIONS yourself.
The parts of a rule
AllowedOriginsThe origins (scheme, host and port) allowed to make requests, such as https://www.example.com.
Each value can contain at most one * wildcard, such as https://*.example.com, or be * for any origin. Browsers send Origin with no path or trailing slash, so https://www.example.com/ never matches. AWS doesn't document case handling, so write origins in lower case, as browsers send them.
AllowedMethodsGET, PUT, POST, DELETE and HEAD: the only values S3 accepts.
PATCH and OPTIONS aren't valid. List every method your page uses; a PUT upload needs PUT here even though the preflight itself is an OPTIONS request.
AllowedHeadersRequest headers allowed in a preflight's Access-Control-Request-Headers.
Each value can contain at most one *, such as x-amz-*. A single * allows any header. Only preflights are checked against AllowedHeaders; the actual request, simple or not, isn't.
ExposeHeadersResponse headers your script is allowed to read, such as ETag or x-amz-version-id.
Without it, scripts only see the CORS-safelisted response headers (Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified and Pragma). Multipart uploads from the browser usually need ETag.
MaxAgeSecondsHow long the browser may cache the preflight response, sent as Access-Control-Max-Age.
Browsers cap it (Chrome at two hours, Firefox at a day), so a very large value doesn't help. It only saves preflights; it doesn't cache the object.
LimitsUp to 100 rules, and the configuration can be at most 64 KB.
The console takes JSON (a list of rules), the CLI takes {"CORSRules": [...]} and the REST API takes XML. They describe the same configuration.
What the browser does
AllowedOrigin * and credentialsCredentialed requests need the exact origin echoed back, which a rule of * doesn't give you.
A fetch with credentials: "include", or an image or script with crossorigin="use-credentials", needs Access-Control-Allow-Origin set to the page's origin plus Access-Control-Allow-Credentials: true. For AllowedOrigin * S3 is observed to answer * (AWS doesn't document it), which browsers reject. List your origins instead. Presigned URLs don't use cookies, so * works for them.
Simple requestsGET, HEAD and POST with only CORS-safelisted headers skip the preflight.
The browser sends the request straight away with an Origin header and checks the response. Adding Authorization, or a Content-Type other than form, multipart or text/plain, turns it into a preflighted request.
CORS isn't access controlCORS only tells browsers which pages may read a response. It doesn't grant permission to the bucket.
A request still needs permission: a public-read policy, a presigned URL, or CloudFront with Origin Access Control. A request that's allowed by CORS but not by permissions gets 403 Access Denied, and anything outside a browser (curl, scripts) ignores CORS entirely.
Bucket policies and CloudFront
Origin Access Control (OAC)Lets one CloudFront distribution read a private bucket, signing each request with SigV4.
The bucket stays private with Block Public Access on. The bucket policy allows the service principal cloudfront.amazonaws.com, with an AWS:SourceArn condition naming your distribution, so other distributions can't use it. OAC replaces the legacy origin access identity (OAI).
AWS:SourceArn conditionLimits the CloudFront service principal to requests on behalf of one distribution.
The value is arn:aws:cloudfront::ACCOUNT_ID:distribution/DISTRIBUTION_ID. Without it, any CloudFront distribution, including someone else's, could be pointed at the bucket.
SSE-KMS bucketsObjects encrypted with a KMS key also need a statement in the key policy.
Add a statement allowing cloudfront.amazonaws.com kms:Decrypt (plus kms:Encrypt and kms:GenerateDataKey* for uploads), with the same AWS:SourceArn condition. It goes in the KMS key policy, not the bucket policy.
Website endpointsOAC only works with the bucket's REST endpoint, not its static website endpoint.
A website endpoint (bucket.s3-website-region.amazonaws.com) is a custom origin and must be public. With OAC, use the REST endpoint and set a default root object on the distribution.
CORS through CloudFrontFor S3's CORS rules to apply through CloudFront, CloudFront has to forward the CORS headers.
Forward Origin, Access-Control-Request-Headers and Access-Control-Request-Method (the managed CORS-S3Origin origin request policy does this), allow OPTIONS on the cache behavior. Also put Origin in the cache key with a cache policy, or a response cached without CORS headers can be served to CORS requests. Or let CloudFront add CORS headers itself with a response headers policy.
Follows S3's documented rules.
Matching follows the S3 User Guide and API reference. Where AWS
doesn't document a detail, such as the Allow-Origin value for a rule
of *, the result says it's observed behaviour. Test the real bucket
with
curl -H "Origin: …" before relying on it.
Nothing leaves this page.
Everything is worked out in your browser and nothing is saved. We count anonymous page views and button use, never what you enter.
Beta feedback helps shape Labs. Tell Andy what would help
Rules last checked , against S3: using cross-origin resource sharing (how S3 evaluates CORS), S3: elements of a CORS configuration, S3: troubleshooting CORS, S3 API: PutBucketCors, CloudFront: restrict access to an Amazon S3 origin (OAC), CloudFront: cache content based on request headers (CORS) and S3: testing CORS. Re-checked weekly.
