Home / Blog / Website Security / What is the difference between CORS and Content Security Policy?
Answers

What is the difference between CORS and Content Security Policy?

27 November 2026 · By John Carlsson
CORS and Content Security Policy do different jobs. CORS is a set of response headers that lets a server relax the same-origin policy so that scripts on other origins can read its responses. Content Security Policy is a response header that restricts what your own page may load and execute, mainly to limit cross-site scripting. CORS opens a controlled door to your data, while CSP locks down your page.

What CORS and Content Security Policy each control

CORS and Content Security Policy are both enforced by the browser, but they answer different questions. Both headers are enforced by the browser, but they answer different questions.

CORS answers the question: may a script running on another origin read this response? Browsers apply the same-origin policy, where an origin is the combination of scheme, host and port. By default, `fetch()` on one origin cannot read a response from another. A server can opt in to sharing by sending headers such as `Access-Control-Allow-Origin`. MDN describes CORS as a controlled exception to the same-origin policy, not its removal.

Content Security Policy answers a different question: what may this page load and run? The site sends a `Content-Security-Policy` header with directives that list allowed sources for scripts, styles, images, frames and more. MDN describes it as a defence in depth measure, mainly against cross-site scripting, that does not replace proper input sanitisation.

In short:

How CORS works in practice

For a simple GET request, the browser sends an `Origin` header and checks whether the response carries a matching `Access-Control-Allow-Origin`. For requests that could change data, such as those with custom headers or methods like PUT, the browser first sends an `OPTIONS` preflight request. The server replies with the methods and headers it accepts, and only then does the real request go ahead.

A typical response from an API that serves one front end looks like this:

```http Access-Control-Allow-Origin: https://app.example.com Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Credentials: true ```

Two points are worth remembering. First, CORS does not stop requests from being sent; it controls whether the browser lets scripts read the response. Second, when cookies or HTTP authentication are involved, the server must name an exact origin, not `*`. MDN also notes that the detailed reason for a CORS failure only appears in the browser console, not to the script.

How CSP works in practice

A CSP is a list of directives separated by semicolons. `default-src` sets the fallback for any resource type without its own rule, `script-src` controls JavaScript, and `frame-ancestors` controls which sites may embed your page, which helps against clickjacking and replaces the older `X-Frame-Options` header.

MDN recommends a strict policy based on nonces or hashes rather than long lists of allowed hosts:

```http Content-Security-Policy: script-src 'nonce-RANDOM'; object-src 'none'; base-uri 'none'; frame-ancestors 'none' ```

Here the server generates a fresh random nonce for every response and adds it to each legitimate script tag. Injected scripts without the nonce will not run. Other useful directives include `upgrade-insecure-requests`, which upgrades HTTP resource requests to HTTPS, and `report-to`, which sends violation reports to an endpoint you define.

A quick way to decide which header you need:

  1. Another site or app must read data from your server: configure CORS on that server.
  2. You want to stop injected or unexpected scripts running on your pages: add a CSP to those pages.
  3. You run both a public site and an API: you will probably need both, set in different places.

Our tool

The AI Web Page SEO Security Audit reads the security headers a site sends, including Content-Security-Policy, HSTS, frame protection, Referrer-Policy and Permissions-Policy, along with the TLS certificate, cookies, mixed content and DNS records, and scores each risk. It needs a free account and is for domains you own or have permission to scan.

Sources

Frequently asked questions

Can a CORS misconfiguration weaken CORS and Content Security Policy protection?

Yes. If a server reflects any requesting origin in Access-Control-Allow-Origin and also allows credentials, another site may be able to read data meant only for signed-in users. MDN notes that credentialed requests need an explicit origin rather than a wildcard. Review which origins genuinely need access and list only those, rather than echoing whatever the browser sends.

Should I start with a report-only Content Security Policy?

Usually, yes. The Content-Security-Policy-Report-Only header reports violations without blocking anything, so you can see which scripts, styles and frames your pages really use. Once the reports are clean, move the same policy to the enforcing Content-Security-Policy header. MDN notes that a report-only policy cannot be delivered through a meta element, so it must be sent as an HTTP header.

💬 Got a problem?

aiwebpageseo.com is a data-driven SEO and AEO platform providing a free suite of technical website tools.