# CORS

> An HTTP-header based mechanism that allows a server to indicate any origins other than its own from which a browser should permit loading resources.

Subject: Internet | Lesson 18 of 35 | 4 min read
Interactive lesson: https://codewithanimation.in/internet/cors
Previous: [Same-Origin Policy](https://codewithanimation.in/internet/same-origin-policy.md) | Next: [Preflight Requests](https://codewithanimation.in/internet/preflight-requests.md)

## What it is

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources.

It acts as a controlled, server-approved relaxation of the browser's restrictive Same-Origin Policy (SOP).

## Why it matters

Modern web applications frequently fetch resources-such as data from REST APIs, web fonts, or images-that reside on different domains than the one serving the front-end (e.g., `frontend.com` requesting data from `api.backend.com`).

Without CORS, the browser's Same-Origin Policy automatically blocks these cross-origin read requests to prevent malicious scripts from accessing sensitive data. Understanding CORS is essential to successfully connecting a front-end to third-party APIs or separated back-end services.

## How it works

When a front-end makes a cross-origin request, the browser automatically attaches an `Origin` HTTP header indicating where the request is coming from.

The server inspects this header and, if it wants to allow the request, responds with an `Access-Control-Allow-Origin` header (containing the allowed origin, or `*` for any origin).

The browser sees this header. If it matches, the browser allows the front-end JavaScript to read the response. If not, the browser throws a CORS error.

## Common mistake: Common mistake

**Trying to fix CORS errors entirely by changing the front-end code** (e.g., blindly adding `mode: 'no-cors'` to a `fetch` request, which actually makes the response completely unreadable).

**The painful truth**: CORS is a security mechanism enforced by the *browser*, but the rules are set by the *server*. If the server doesn't send the correct headers, the browser will block the response. You usually have to fix CORS on the back-end, not the front-end.

## Tip: Pro tip

Use the **Bouncer and the VIP List** analogy.

Think of the browser as a strict bouncer at an exclusive club (your webpage). The Same-Origin Policy is the default rule: 'Only locals allowed.' When a guest from another domain tries to enter with data, the bouncer immediately stops them. To verify if they can enter, the bouncer calls the club manager (the server). The manager checks their VIP list (`Access-Control-Allow-Origin`). If the guest's origin is on that list, the manager tells the bouncer to let them in. If not, the bouncer aggressively blocks them, and the front-end never gets the data.

## Try it: Try it

Play with the **Nightclub Door Simulator**. Toggle the server's CORS settings on and off and watch how the browser's bouncer reacts to incoming data packets!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/cors

## Check yourself

### Question 1

What is the primary purpose of CORS?

- A. To prevent servers from being attacked by malicious front-end applications.
- B. To encrypt data sent between the front-end and a different domain.
- C. To allow front-end code to bypass the server's security rules completely.
- D. To allow servers to specify which origins are permitted to access their resources, relaxing the browser's Same-Origin Policy.

Correct answer: D

CORS is an HTTP-header based mechanism that provides a secure, server-approved way to bypass the restrictive Same-Origin Policy for specific origins.

- Why not A: CORS protects the user and the browser, not the server. A server can still be directly attacked via tools like cURL where CORS doesn't apply.
- Why not B: Data encryption in transit is handled by HTTPS/TLS, not CORS.
- Why not C: CORS does not bypass server security; it actively requires the server to explicitly grant permission.

### Question 2

You encounter a CORS error when your front-end tries to fetch data from an API. How should you generally fix this?

- A. Add { mode: 'no-cors' } to your fetch request configuration.
- B. Disable web security policies in your browser settings.
- C. Send Access-Control-Allow-Origin: * in the headers of your front-end fetch request.
- D. Configure the back-end server to return the appropriate Access-Control-Allow-Origin headers.

Correct answer: D

CORS errors happen because the browser blocks the response when the server doesn't give permission. The proper fix is configuring the backend API to include the correct CORS headers in its response.

- Why not A: While this stops the error in the console, it creates an 'opaque response'-your JavaScript won't be able to read any of the data returned by the API.
- Why not B: This is a massive security risk for the developer and doesn't fix the problem for actual users visiting your site.
- Why not C: This header must be sent by the server in its response. Sending it from the client in the request does absolutely nothing.

## Remember this

- CORS is a controlled relaxation of the Same-Origin Policy.
- It relies on the server sending Access-Control-Allow-Origin headers.
- CORS errors are enforced by the browser, but must be fixed on the server.
