CORS

Updated at:

A "blocked by CORS policy" error occurs when a website loads OSS resources because the browser's same-origin policy allows access only to resources with the same protocol, domain, and port. Configure Cross-Origin Resource Sharing (CORS) rules for OSS to securely authorize specified websites to access OSS resources.

How it works

The browser's same-origin policy allows a page to access only resources with the same protocol, domain, and port. A difference in any one of them makes the request cross-origin:

Current page

Requested resource

Cross-origin

Reason

https://www.example.com

https://www.example.com/api

No

Same origin

https://www.example.com

http://www.example.com/api

Yes

Different protocol

https://www.example.com

https://api.example.com/data

Yes

Different domain

https://www.example.com

https://www.example.com:8080/api

Yes

Different port

When a webpage requests OSS resources, the OSS domain differs from the webpage domain. The browser checks whether OSS explicitly allows the cross-origin access. If OSS has no matching CORS rule, the browser rejects the request and reports an error.

Configure CORS rules for OSS to tell the browser which cross-origin requests are allowed. If a rule matches, OSS returns headers such as Access-Control-Allow-Origin in the response. The browser allows the webpage to access the resource only after it receives these headers.

Configuration method

  1. Go to the Bucket list and click the target bucket.

  2. In the left-side navigation pane, choose Content Security > CORS, click Create Rule, and configure CORS rule parameters as needed.

    • Origin: Specify the websites that can access resources cross-origin, such as https://www.example.com.

    • Allowed Methods: Specify the allowed HTTP methods, such as GET, PUT, and POST.

    • Allowed Headers: Specify the allowed request headers, such as Authorization and x-oss-*.

    • Exposed Headers: Specify the response headers that frontend JavaScript can read, such as ETag.

    • Cache Timeout (Seconds): Specify how long to cache preflight results.

    • Vary: Origin: Enable this option for multiple origins or wildcard characters to prevent cache pollution.

  3. Click OK to complete the configuration.

For detailed parameter descriptions, see Parameters of CORS rules. For specific settings, see the following scenario examples.

Scenario examples

The following scenarios show how to configure CORS for different business requirements.

Load static website resources

The website at https://www.example.com needs to load images, CSS, and JavaScript files from OSS.

Parameter

Value

Description

Origin

https://www.example.com

Allow cross-origin requests only from this website.

Allowed Methods

GET, HEAD

GET downloads resources, and HEAD validates caches.

Allowed Headers

Leave blank

This is a simple request, which does not trigger preflight. Therefore, this setting is not used.

Exposed Headers

ETag, Content-Length

ETag validates caches, and Content-Length displays loading progress.

Cache Timeout (Seconds)

86400

Cache preflight results for 24 hours to reduce preflight requests.

Vary: Origin

Clear

The origin is single and fixed, so multi-domain caching is not a concern.

Upload files directly from the frontend

Users upload avatars, documents, and other files to OSS from the webpage at https://app.example.com.

Parameter

Value

Description

Origin

https://app.example.com

Make sure that only this authorized application can upload files.

Allowed Methods

PUT, POST

PUT or POST is required to upload files.

Allowed Headers

*

Temporary signatures (STS or presigned URLs) secure direct frontend uploads. Use * to support the various headers sent by SDKs.

Exposed Headers

ETag, x-oss-request-id

ETag verifies uploaded-file integrity, and x-oss-request-id helps troubleshoot issues.

Cache Timeout (Seconds)

600

Upload operations are relatively infrequent. Caching for 10 minutes reduces preflight requests while allowing configuration changes to take effect quickly.

Vary: Origin

Select

Prepare for potential multi-domain deployment and prevent CDN cache pollution.

Support for multiple environments

Multiple subdomains for development, testing, and production, such as dev.example.com and app.example.com, need to access the same OSS resources.

Parameter

Value

Description

Origin

https://*.example.com

Use the * wildcard character to authorize all HTTPS subdomains under example.com at once.

Allowed Methods

GET, PUT, POST

Allow resource reads and uploads to meet multi-environment testing requirements.

Allowed Headers

*

In multi-environment development, different environments can introduce different custom headers. Use * to avoid frequent CORS rule changes.

Exposed Headers

ETag, x-oss-request-id

ETag validates caches and upload integrity, and x-oss-request-id helps troubleshoot issues.

Cache Timeout (Seconds)

3600

A one-hour cache duration provides flexibility when switching and debugging multiple environments.

Vary: Origin

Select

This option is required. It tells the CDN to distinguish cached content by the Origin header and prevents cache conflicts between environments.

Make API-style calls with authentication

The frontend application at https://api.example.com needs to include custom headers such as Authorization to access protected OSS resources.

Parameter

Value

Description

Origin

https://api.example.com

For requests that include authentication information, the origin must be a precise, trusted domain.

Allowed Methods

GET, PUT, DELETE

Support full lifecycle management for reading, updating, and deleting private resources.

Allowed Headers

authorization, content-type, x-oss-*

Do not use *. Explicitly list every required request header and follow the principle of least privilege.

Exposed Headers

ETag, x-oss-request-id

ETag validates caches and integrity, and x-oss-request-id helps troubleshoot issues.

Cache Timeout (Seconds)

600

For authenticated requests, a shorter preflight cache duration helps security policy changes take effect faster.

Vary: Origin

Select

Tell the CDN to cache requests with different origins separately to prevent conflicts.

Types of CORS requests

Browsers classify cross-origin requests as simple requests or preflight requests based on their characteristics. The two types require slightly different CORS settings.

Type

Trigger conditions

Required CORS settings

Simple request

All of the following conditions are met:

  • The request uses GET/HEAD/POST.

  • POST requests have a Content-Type of text/plain, application/x-www-form-urlencoded, or multipart/form-data

  • The request has no custom headers.

Origin and allowed methods

Preflight request

Any of the following conditions is met:

  • The request uses a method other than GET/HEAD/POST, such as PUT/DELETE.

  • POST requests have a Content-Type with another value.

  • The request has custom headers, such as x-oss-*.

Origin, allowed methods, and allowed headers

Simple requests

Simple requests can be sent directly and are processed as follows:

  1. The browser adds the Origin header to the request. The header contains the resource origin, for example, Origin: https://www.example.com.

  2. OSS compares the HTTP method of the request and the value of the Origin header against the target bucket's CORS configuration. If a matching item exists, OSS includes the Access-Control-Allow-Origin header in the response.

  3. The browser receives the response and checks whether the Access-Control-Allow-Origin value matches the domain of the original request. If it matches, the request succeeds; otherwise, the request fails.

Preflight requests

A preflight request first asks whether the server allows the request and is processed as follows:

  1. The browser sends an OPTIONS request that includes the method (Access-Control-Request-Method) and headers (Access-Control-Request-Headers) of the main request.

  2. OSS uses the CORS configuration to check whether the request method and headers are allowed. If any method or header value in the preflight request is not in the target resource's set of allowed methods and headers, the request fails and the main request is not sent.

  3. After the preflight succeeds, the same process as a simple request is performed.

Parameters of CORS rules

OSS checks rules from top to bottom in their configured order and uses the first rule that matches. After a rule matches, OSS does not check later rules.

Parameter

Required

Description

Origin

Yes

Specify the websites or IP addresses that can access OSS resources cross-origin.

  • Use the format protocol://domain or IP address[:port], for example, https://www.example.com or http://192.168.1.1:8080.

  • The * wildcard character is supported, but each origin can contain only one.

    • Valid examples: https://*.example.com, http://localhost:*, or http://192.168.1.1:8080.

    • Invalid examples: https://*.example.* or https://*.

  • Multiple origins are allowed, one per line.

Allowed Methods

Yes

Specify the allowed HTTP methods.

  • Valid values: GET, PUT, POST, DELETE, and HEAD.

  • Multiple methods are allowed.

Allowed Headers

No

Specify the HTTP headers allowed in actual requests. This setting applies to preflight requests.

  • The * wildcard character is supported and allows all headers.

  • Multiple headers are allowed, one per line, and are case-insensitive.

Exposed Headers

No

Specify which headers in OSS responses frontend JavaScript can access.

  • The * wildcard character is not supported.

  • Multiple headers are allowed, one per line.

To obtain ETag or x-oss-request-id in JavaScript, add it here.

Cache Timeout (Seconds)

No

Specify how long, in seconds, the browser caches an OPTIONS preflight result.

During the cache validity period, identical cross-origin requests for the same resource do not send another preflight request.

Vary: Origin

No

Determine whether to add the Vary: Origin header to the response.

This header tells CDNs and other intermediate caches to distinguish cached versions by the request Origin header to avoid cache pollution when multiple origins access the resource.

Important

You must enable this option when the origin has multiple domains or a wildcard character. Enabling it can reduce the CDN cache hit ratio.

Apply in production

Security best practices

  • Configure precise origins: Unless the bucket is fully public, do not set the origin to *. Specify website domains precisely.

  • Restrict allowed methods: Expose only the HTTP methods required by your application. For read-only scenarios, configure only GET and HEAD.

  • Explicitly list allowed headers: Follow the principle of least privilege. Explicitly list the required request headers where possible, and avoid using *.

Performance best practices

  • Optimize the preflight cache: Set a reasonable cache duration, such as 86400 seconds, to reduce preflight requests, latency, and request costs.

  • Evaluate the effect of Vary: Origin: Enabling this option prevents cache pollution, but can reduce the CDN cache hit ratio and increase back-to-origin traffic costs. Enable it only for multiple origins or wildcard characters.

Use with CDN

If a bucket has CDN acceleration enabled and you access it through an accelerated domain name, cross-origin requests first reach a CDN point of presence. You can configure CORS in either of the following ways:

  • Configure CORS in CDN: Configure CORS rules in the CDN console so that CDN directly returns CORS headers. For more information, see Configure cross-origin resource sharing.

  • Pass through CORS headers from the origin: CDN passes through the CORS headers returned by OSS. In this case, configure CORS rules in OSS.

CORS settings on OSS take effect only when requests directly access the OSS origin domain name or when CDN is configured to pass through origin response headers.

Limits

You can configure up to 20 CORS rules for each bucket.

FAQ

Why does the error “No 'Access-Control-Allow-Origin' header is present on the requested resource” occur?

This error usually occurs because the browser cached an old response without a CORS header, or the CORS rules are configured so that no rule matches the request. Troubleshoot the issue as follows:

  1. Clear the browser cache and test again.

  2. Check whether the CORS rule origin and allowed methods match the actual request.

  3. Temporarily create a permissive CORS rule for troubleshooting: set the origin to *, select all allowed methods, set allowed headers to *, set exposed headers to ETag and x-oss-request-id, set the cache duration to 0, and select Vary: Origin.

  4. If the issue persists, run the following command on a server to view the cross-origin request headers:

    curl -v -H 'Origin: <origin-address>' '<OSS-resource-URL>' -o output.txt

    Troubleshoot according to the output:

    • If the response contains one cross-origin header that matches the configuration, the browser cache might be the cause. Press Ctrl+F5 to clear the browser cache, or set the cache duration to 0.

    • If the response contains two cross-origin headers or a header that does not match the configuration, OSS might be accelerated by CDN.

      • Log on to the CDN console and temporarily disable CDN acceleration to confirm that no cross-origin issue exists.

      • After confirmation, configure a custom response header in Cache > Modify Outgoing Response Header for the CDN-accelerated domain name.

  5. If the issue remains unresolved, see Common errors and solutions for OSS Cross-Origin Resource Sharing (CORS) for further troubleshooting.

Why does the error “The 'Access-Control-Allow-Origin' header has a value '...' that is not equal to the supplied origin” occur?

The server returned the Access-Control-Allow-Origin header, but its value does not match the Origin of the current request. This is usually a cache issue: when multiple website domains access OSS, the browser or CDN might retain access permission for another website.

Solution: Enable Vary: Origin in the CORS rule, or clear the browser cache and access the resource again.

Why does the error “The value of the 'Access-Control-Allow-Origin' header must not be the wildcard '*' when credentials mode is 'include'” occur?

The frontend code sends a request with credentials (Access-Control-Allow-Credentials is True), but the origin is configured as the wildcard character *. For security reasons, browsers do not allow wildcard origins in this case.

Solution:

  • If Credentials must remain in the request, change the origin from * to a specific domain, such as https://example.com, and make sure that Access-Control-Allow-Credentials is set to true on the server.

  • If Credentials are not needed, set xhr.withCredentials to false in the frontend code, and make sure that Access-Control-Allow-Credentials is set to false or is not returned by the server.

Why does object-level Access-Control-Allow-Origin not take effect?

Object-level Access-Control-Allow-Origin takes effect only when no bucket-level CORS rules are configured. If bucket-level CORS rules are configured, object-level Access-Control-Allow-Origin is ignored regardless of whether the request matches the rules.

How can you improve slow cross-origin loading from OSS?

The loading speed of cross-origin requests depends on the physical network path between the client and the OSS bucket, not on CORS itself. Long-distance access can occur when the client is in China (Hong Kong) and the bucket is in the Chinese mainland. Use OSS transfer acceleration to access the bucket.