CORS: Securing Your Web Applications

Alex U
6 min readMar 9, 2023

--

CORS protocol, from its role in securing sensitive information to its importance in enabling better collaboration between different web applications. Read more about the technical details of the protocol, various parts of a CORS request and response, common CORS vulnerabilities, and best practices for implementing the protocol to prevent security threats.

Photo by Alina Grubnyak on Unsplash

As a web developer, you might have come across a scenario where you want to access information from another website in your own web application. But, have you ever thought about the security implications of doing so?

That’s where Cross-Origin Resource Sharing (CORS) comes in.

CORS stands for Cross-Origin Resource Sharing, which is a mechanism for allowing a web page from one domain (origin) to access resources on a different domain. By default, web browsers enforce a same-origin policy, which only allows a web page to make requests to the same domain as the page itself. This policy is designed to prevent malicious websites from accessing sensitive information on other domains.

The origin of a page is defined by the combination of its scheme (e.g., “http” or “https”), host (e.g., “example.com”), and port (e.g., 80 or 443). If a request is made from a page with a different origin than the server, it is considered a cross-origin request.

CORS provides a way for a server to explicitly allow cross-origin requests from specific domains. This is done by including specific headers in the HTTP response, such as “Access-Control-Allow-Origin”. These headers tell the browser which domains are allowed to access the resource, and can also include other information, such as the methods (e.g., GET, POST) and headers that are allowed in the request.

By implementing CORS, a server can enable a web page from one domain to access its resources, while still preserving the security of the same-origin policy and limiting access to trusted domains.

A CORS request consists of two parts:

  • the preflight request
  • the actual request

CORS preflight request

A CORS preflight request is a special type of request that is sent by a web browser to a server as part of the CORS protocol. The purpose of the preflight request is to determine whether a cross-origin request is safe to send, by checking with the server whether it allows requests from the domain that is making the request and what methods and headers are allowed.

When a web page makes a cross-origin request that falls outside the scope of simple requests (e.g. using certain HTTP methods or headers), the browser will first send an OPTIONS request to the server to ask for permission. This OPTIONS request is known as the preflight request. The preflight request includes an “Origin” header that indicates the domain that is making the request, as well as information about the request, such as the HTTP method and headers that will be used.

The server will respond to the preflight request with a set of headers that indicate whether the cross-origin request is allowed, and what methods and headers are permitted. If the server allows the request, the browser will then send the actual request.

The use of preflight requests in CORS is an important security feature that helps prevent unauthorized access to resources and protects against certain types of attacks, such as cross-site scripting (XSS) and cross-site request forgery (CSRF).

The preflight request consists of several parts:

  • HTTP method: typically the OPTIONS method
  • Request headers: any custom headers that will be included in the actual request
  • Origin header: the domain that is making the request
  • Access-Control-Request-Method header: the HTTP method that will be used in the actual request
  • Access-Control-Request-Headers header: any custom headers that will be included in the actual request

CORS actual request

A CORS actual request is the second part of a cross-origin request that is sent by a web browser after a preflight request has been successfully completed.

Once the browser has received a response from the server to the preflight request indicating that the cross-origin request is allowed, it will send the actual request to the server. The actual request is similar to a regular HTTP request, but it includes an additional “Origin” header that identifies the domain that is making the request.

The server receiving the actual request will check the “Origin” header to ensure that it matches the domain that was approved in the preflight request. If the “Origin” header does not match, the server will reject the request.

Assuming that the “Origin” header matches, the server will then process the actual request and return the requested resource back to the browser. The server may include additional CORS headers in its response, such as “Access-Control-Allow-Origin”, to indicate that the resource is allowed to be accessed from the requesting domain.

CORS actual requests are an important part of the CORS protocol, as they allow web developers to build powerful web applications that can access resources from multiple domains while maintaining a high level of security.

The actual request consists of next parts:

  • HTTP method: the HTTP method to be used for the request
  • Request headers: any custom headers that will be included in the request
  • Origin header: the domain that is making the request

In addition to these parts, both the preflight and actual requests may include additional CORS headers in the response, such as “Access-Control-Allow-Origin”, “Access-Control-Allow-Methods”, and “Access-Control-Allow-Headers”, to indicate whether the cross-origin request is allowed, and what methods and headers are permitted.

Photo by Carl Nenzen Loven on Unsplash

So in a few words, CORS is really important and there are several reasons why:

  1. Security: By default, the same-origin policy prevents malicious websites from accessing sensitive information on other domains, which helps to prevent various types of attacks, such as cross-site scripting (XSS) and cross-site request forgery (CSRF).
  2. Cross-domain data sharing: CORS provides a way for web pages from different domains to access resources on another domain, which is essential for enabling cross-domain data sharing and collaboration in the modern web.
  3. Interoperability: With CORS, web developers can build applications that work seamlessly across multiple domains, making it easier to integrate data and services from different sources.
  4. Better user experience: By allowing for cross-domain data sharing, CORS enables web developers to build more sophisticated and interactive web applications that provide a better user experience.
  5. API access: CORS is also important for web-based APIs, as it enables developers to access these APIs from JavaScript code running on a different domain, which is essential for building modern web applications.

But is CORS secure?

CORS vulnerabilities can occur when web developers do not properly implement the CORS protocol, which can leave web applications vulnerable to security threats such as cross-site scripting (XSS) and cross-site request forgery (CSRF).

One example of a CORS vulnerability is when a web application allows cross-origin requests from any domain (i.e., by using the wildcard character “*”) instead of explicitly specifying which domains are allowed. This can allow attackers to make unauthorized requests to the application and potentially access sensitive information or execute malicious code.

Another example of a CORS vulnerability is when the web application does not properly validate the “Origin” header in the request, which can allow attackers to send requests that appear to come from a trusted domain. This can be exploited in a CSRF attack, where an attacker tricks a user into performing actions on the web application that they did not intend to.

To prevent CORS vulnerabilities, web developers should ensure that their applications properly implement the CORS protocol and only allow cross-origin requests from trusted domains. Additionally, developers should properly validate and sanitize user input to prevent XSS and CSRF attacks, as these vulnerabilities can be exploited in conjunction with CORS vulnerabilities to create a more powerful attack. Regular security audits and testing can also help identify and address any potential vulnerabilities in the application.

Summary

it’s important to understand the role of CORS in web development because it helps ensure the security of sensitive information on websites. CORS allows a server to explicitly allow cross-domain requests and also provides a way to protect against unauthorized access to resources and protect against certain types of attacks such as XSS and CSRF.

--

--