Huge news!Announcing our $20M Series A led by Andreessen Horowitz.Learn more
Log inDemoInstall

← Back to Glossary


Cross-Origin Resource Sharing (CORS)

Introduction to CORS#

Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers that controls how web pages in one domain can request and interact with resources hosted on another domain. At its core, CORS is about defining which external domains are permitted to read data from your site when a request is made.

Historically, browsers blocked web pages from making requests to a different domain than the one hosting the webpage. This restriction, known as the "same-origin policy", was implemented as a security measure to prevent malicious sites from reading sensitive data from another site.

However, with the rise of web services and cloud applications, there arose a need to safely allow cross-origin requests. Enter CORS, which lets servers specify who can access their assets and how.

How Does CORS Work?#

When a web page attempts to fetch a resource from a different origin (domain, protocol, or port), the browser sends the request with an additional Origin header. This header indicates where the request is coming from.

The server, upon receiving this request, can check the Origin against its list of allowed origins. If the origin is on the list, the server responds with an Access-Control-Allow-Origin header, specifying the requesting origin or a wildcard (*), meaning any origin is allowed.

If the browser doesn't receive this header in the response, or if the origin specified in the response doesn't match the originating server, the browser will block the request, ensuring the security policy isn't violated.

Why is CORS Important?#

  • Security: The primary reason CORS exists is to maintain security. By restricting which websites can interact with each other, browsers help prevent attacks like Cross-Site Request Forgery (CSRF) and Cross-Site Script Inclusion (XSSI).
  • Flexibility for Developers: With the proliferation of APIs and web services, developers often need to access resources from different domains. CORS provides a secure method for developers to make these cross-origin requests.
  • Better User Experiences: Without CORS, certain web functionalities that users have come to expect, like integrating third-party login systems or fetching data from external sources, would be far more cumbersome or even impossible to implement securely.

Setting Up CORS on Your Server#

Setting up CORS on your server involves specifying certain headers to inform the browser about which origins are permitted. Here's a basic example using an Express.js server:

const express = require('express');
const app = express();

app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

In this example, only requests from "" will be permitted. You can also use a wildcard (*) to allow any origin, but this approach is less secure and not recommended for sensitive data.

Common CORS Errors and Solutions#

One of the most common CORS-related errors developers encounter is the "No 'Access-Control-Allow-Origin' header is present on the requested resource." This means that either the server didn't include the necessary CORS headers in its response, or the origin from which the request was made isn't on the server's list of permitted origins.


  • Ensure your server configuration correctly specifies the necessary CORS headers.
  • Check that the requesting origin is included in the server's list of allowed origins.
  • If using a frontend development server (like Webpack Dev Server), ensure it's correctly configured to handle CORS.

How Socket Enhances Security With CORS#

At Socket, we understand the balance between usability and security. While our primary focus is detecting supply chain attacks in your dependencies, we recognize the importance of other security mechanisms like CORS.

For instance, when integrating Socket's deep package inspection tools into your CI/CD pipeline, it's essential to ensure secure communication between different services. Leveraging CORS correctly ensures that Socket's tools communicate seamlessly with your infrastructure without compromising security. With the rise of supply chain attacks, safeguarding every touchpoint, including cross-origin requests, is more crucial than ever.

CORS in a Modern Web#

With the advent of Single Page Applications (SPA) and progressive web apps, frontend applications increasingly need to access resources from various origins. Additionally, serverless architectures and third-party APIs have made the web even more interconnected.

In this modern web landscape, understanding and correctly implementing CORS is paramount. It's not just about enabling cross-origin requests but doing so securely. While the web continues to evolve, the foundational principles of security, like CORS, remain constant.

Wrapping Up: Best Practices with CORS#

As you incorporate CORS into your web applications, keep these best practices in mind:

  • Explicitly Define Allowed Origins: Instead of using the wildcard (*), list out the specific domains that should have access. This minimizes potential exposure.
  • Limit Exposed Headers: Only expose headers that are necessary for the frontend functionality to reduce potential information leaks.
  • Use HTTPS: Combining CORS with HTTPS ensures that your cross-origin communications are not only correctly restricted but also encrypted.

With these practices in place, you can harness the power of cross-origin communications while keeping your applications secure. Always remember, security in the digital age requires a combination of cutting-edge solutions like Socket and foundational practices like CORS.

Table of Contents

Introduction to CORSHow Does CORS Work?Why is CORS Important?Setting Up CORS on Your ServerCommon CORS Errors and SolutionsHow Socket Enhances Security With CORSCORS in a Modern WebWrapping Up: Best Practices with CORS
SocketSocket SOC 2 Logo


Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc