two rope with carbiner hooked on brown wooden post
Fri Aug 25

CORS: What Is It and How Does It Work?

If you are a web developer, you have probably encountered the term CORS or Cross-Origin Resource Sharing. But what exactly is CORS and how does it work? In this article, we will explain what CORS is, why it is important, and what problems it solves. We will also describe how CORS works, how to fix common CORS errors, and what are some best practices for implementing CORS securely and efficiently.

What is CORS?

CORS is a web standard that allows browsers to request resources from different origins (domains, protocols, or ports) than the one that the web page was loaded from. For example, if you have a web page hosted on https://example.com that requests an image from https://another.com, this is a cross-origin request.

Why do we need CORS? Well, without CORS, browsers would block cross-origin requests by default, as a security measure to prevent malicious attacks. This is known as the same-origin policy, which only allows web pages to access resources from the same origin.

However, sometimes we do want to access resources from different origins, such as APIs, fonts, images, videos, etc. For example, if you are building a web app that uses Google Maps API, you need to make cross-origin requests to Google’s servers. This is where CORS comes in handy. CORS allows web servers to specify which origins are allowed to access their resources, and which methods, headers, and credentials are supported. This way, browsers can safely make cross-origin requests without compromising security.

How CORS works

The basic mechanism of CORS involves two types of requests: preflight requests and simple requests.

Preflight requests

A preflight request is a special type of request that the browser sends before making a cross-origin request. The purpose of a preflight request is to check whether the server supports CORS and what are the allowed parameters for the actual request.

A preflight request uses the HTTP OPTIONS method and includes two headers: Origin and Access-Control-Request-Method. The Origin header indicates the origin of the web page that is making the request. The Access-Control- Request-Method header indicates the method that will be used for the actual request.

For example, if a web page on https://example.com wants to make a POST request to https://another.com, the browser will first send a preflight request like this:

OPTIONS /some-resource HTTP/1.1
Origin: https://example.com
Access-Control-Request-Method: POST

The server will then respond with either a success or an error response. A success response will include an Access-Control-Allow-Origin header that specifies which origins are allowed to access the resource. Optionally, it may also include other headers such as Access-Control-Allow-Methods, Access- Control-Allow-Headers, Access-Control-Max-Age, etc., that indicate which methods, headers, and credentials are supported, and how long the browser can cache the preflight response.

For example, a success response from the server may look like this:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 86400

This response means that the server allows cross-origin requests from https://example.com, supports the POST method, accepts the Content-Type header, and allows the browser to cache the preflight response for 86400 seconds (one day).

If the server responds with an error response or does not include an Access- Control-Allow-Origin header, the browser will reject the cross-origin request and throw a CORS error.

Simple requests

A simple request is a type of cross-origin request that does not require a preflight request. A simple request can use one of the following methods: GET, HEAD, or POST. Additionally, a simple request can only include certain headers, such as Accept, Accept-Language, Content-Language, Content- Type, etc. The Content-Type header can only have one of the following values: application/x-www-form-urlencoded, multipart/form-data, or text/plain.

For example, if a web page on https://example.com wants to make a GET request to https://another.com, the browser will send a simple request like this:

GET /some-resource HTTP/1.1
Origin: https://example.com

The server will then respond with either a success or an error response. A success response will include an Access-Control-Allow-Origin header that specifies which origins are allowed to access the resource. Optionally, it may also include other headers such as Access-Control-Expose-Headers, Access- Control-Allow-Credentials, etc., that indicate which headers are exposed to the web page and whether cookies are supported.

For example, a success response from the server may look like this:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://example.com
Access-Control-Expose-Headers: X-Custom-Header
Access-Control-Allow-Credentials: true

This response means that the server allows cross-origin requests from https://example.com, exposes the X-Custom-Header to the web page, and supports cookies.

If the server responds with an error response or does not include an Access- Control-Allow-Origin header, the browser will reject the cross-origin request and throw a CORS error.

CORS errors and how to fix them

Sometimes, you may encounter CORS errors when making cross-origin requests. CORS errors are usually caused by one of the following reasons:

  • The server does not support CORS or does not include an Access-Control-Allow-Origin header in the response.
  • The server does not allow the origin, method, header, or credential that the web page is using for the request.
  • The server does not respond to the preflight request or responds with an error.
  • The browser does not support CORS or has a bug.

To fix CORS errors, you need to check the following things:

  • Make sure that the server supports CORS and includes an Access-Control-Allow-Origin header in the response. You can use tools such as CORS Anywhere to test if your server supports CORS.
  • Make sure that the server allows the origin, method, header, or credential that the web page is using for the request. You can use tools such as Test CORS to test if your server allows your request parameters.
  • Make sure that the server responds to the preflight request and does not return an error. You can use tools such as Postman or Curl to send preflight requests and check the response headers.
  • Make sure that your browser supports CORS and does not have a bug. You can use tools such as Can I Use to check if your browser supports CORS.

CORS best practices

To implement CORS securely and efficiently, you should follow some best practices:

  • Use HTTPS for both your web page and your server. HTTPS ensures that your cross-origin requests are encrypted and authenticated, preventing man-in-the-middle attacks and spoofing.
  • Be specific about which origins, methods, headers, and credentials you allow. Do not use wildcards (*) or allow all origins, methods, headers, or credentials. This reduces the risk of exposing sensitive data or allowing unauthorized access to your resources.
  • Use caching for preflight requests. Preflight requests add an extra round-trip time to your cross-origin requests, which can affect performance. By using caching, you can reduce the number of preflight requests and improve performance. You can use the Access-Control-Max-Age header to specify how long the browser can cache the preflight response.
  • Use simple requests whenever possible. Simple requests do not require preflight requests, which can improve performance and reduce complexity. To use simple requests, you need to use one of the supported methods (GET, HEAD, or POST), limit your headers to the allowed ones, and use one of the supported content types (application/x-www-form-urlencoded, multipart/form-data, or text/plain).

CORS libraries and tools

To help you with CORS configuration and testing, you can use some popular libraries and tools:

For servers:

  • Express CORS: A Node.js middleware that enables CORS for Express applications.
  • Flask CORS: A Flask extension that enables CORS for Flask applications.
  • Spring Boot CORS: A Spring Boot feature that enables CORS for Spring Boot applications.
  • Django CORS Headers: A Django app that enables CORS for Django applications.

For clients:

  • Axios: A promise-based HTTP client that supports CORS requests.
  • Fetch API: A native JavaScript API that supports CORS requests.
  • jQuery AJAX: A jQuery method that supports CORS requests.

For testing:

  • CORS Anywhere: A proxy server that adds CORS headers to any request.
  • Test CORS: A web tool that tests if a cross-origin request works and what are the response headers.

Conclusion

CORS is a web standard that allows browsers to make cross-origin requests safely and securely. CORS works by using preflight requests and simple requests, and by using headers to indicate which origins, methods, headers, and credentials are allowed. CORS can help you access resources from different origins, such as APIs, fonts, images, videos, etc.

However, you need to be careful about CORS errors and follow some best practices to implement CORS securely and efficiently. You can also use some libraries and tools to help you with CORS configuration and testing. We hope this article has helped you understand what CORS is and how it works.