CORS misconfigurations — how Access-Control-Allow-Origin: * combined with credentials wrecks your API

Cross-Origin Resource Sharing (CORS) is a critical security feature for web applications, designed to control how resources on a web server can be requested by content from another

Published August 12, 2024 · 3 min read

CORS Misconfigurations — How Access-Control-Allow-Origin: * Combined with Credentials Wrecks Your API

Cross-Origin Resource Sharing (CORS) is a critical security feature for web applications, designed to control how resources on a web server can be requested by content from another domain. However, improper configuration can lead to severe security vulnerabilities. One of the most common pitfalls is using Access-Control-Allow-Origin: * in conjunction with credentials. Let's dive into why this is a security misstep and how to configure CORS correctly.

Understanding CORS and Its Purpose

CORS is a protocol that allows web applications on one domain to request resources from another domain. It is an essential part of the web security model, enforcing same-origin policies to prevent malicious websites from accessing sensitive data on other domains. The Access-Control-Allow-Origin header is at the core of this mechanism, specifying which domains are permitted to access resources.

Common CORS Headers

  • Access-Control-Allow-Origin: Specifies which origin sites can access the resource.
  • Access-Control-Allow-Credentials: Indicates whether the response to the request can be exposed when the credentials flag is true.
  • Access-Control-Allow-Methods: Lists the HTTP methods that the server allows for cross-origin requests.
  • Access-Control-Allow-Headers: Lists the headers that can be used during the actual request.

The Problem with Access-Control-Allow-Origin: *

Setting Access-Control-Allow-Origin: * allows any domain to access the resources. This might seem convenient, but it poses a significant security risk, especially when combined with credentials.

Why * is Dangerous

When you use Access-Control-Allow-Origin: *, you open up your API to requests from any domain. This becomes particularly dangerous when the Access-Control-Allow-Credentials: true header is also set. This combination allows cross-origin requests to include credentials like cookies, authorization headers, or TLS client certificates, which can lead to unauthorized access.

Example of a Misconfiguration

Consider the following server response headers:

Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

This configuration permits any website to perform authenticated requests on behalf of the user, potentially leading to data breaches or unauthorized actions.

Configuring CORS Correctly

To avoid these vulnerabilities, it's crucial to configure CORS with a specific origin rather than using *. Here’s how you can do it safely:

Specify Allowed Origins

Instead of using a wildcard, specify the exact origins that should be allowed. For example:

Access-Control-Allow-Origin: https://trusted.example.com
Access-Control-Allow-Credentials: true

Implement Dynamic Origin Checking

For more flexibility without sacrificing security, implement a server-side check to dynamically set the Access-Control-Allow-Origin header based on a whitelist of trusted domains.

const allowedOrigins = ['https://trusted.example.com', 'https://another-trusted.com'];

app.use((req, res, next) => {
  const origin = req.headers.origin;
  if (allowedOrigins.includes(origin)) {
    res.setHeader('Access-Control-Allow-Origin', origin);
  }
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  next();
});

Considerations for Credentials

When Access-Control-Allow-Credentials is set to true, ensure that the origins allowed are trustworthy and necessary for your application’s functionality. This minimizes the risk of exposing sensitive user data.

The Impact of Misconfiguration

Failing to configure CORS properly can lead to significant security issues, including:

  • Cross-Site Scripting (XSS): Attackers can exploit a misconfigured CORS policy to perform XSS attacks, accessing sensitive data or executing malicious scripts.
  • Session Hijacking: With Access-Control-Allow-Credentials: true, attackers can hijack user sessions if they can exploit a vulnerability in a trusted origin.

The Role of Security Testing

Regular security testing, such as penetration tests, can help identify CORS misconfigurations before they become a problem. At CyberGrid, our penetration testing services include thorough checks of CORS policies to ensure your APIs are secure.

Conclusion

CORS misconfigurations, particularly the combination of Access-Control-Allow-Origin: * with credentials, can severely compromise your API's security. By specifying allowed origins, implementing dynamic checks, and conducting regular security assessments, you can protect your web applications from these vulnerabilities. For a comprehensive approach to security, consider integrating continuous security practices and regular assessments as part of your development lifecycle.

Want to see this in practice?

Run a free single-domain scan in three minutes — same engine, smaller scope, no signup. We'll email you the PDF.

Run a free scan