Skip to content
BENIBIX Learn

Cross-Origin Resource Sharing (CORS)

Development🔐 SecurityDefences

Also known as CORS, CORS Error, Access-Control-Allow-Origin

A browser rule. It decides if a page on one site may read another site's data.

Think of it like

Think of it like a guest list. The server says which other sites may come in and ask for data.

Example

Jack's shop page calls his API on a different address. The browser blocks it until the API sends a header that names the shop.

Why it matters

Most web apps that call their own API from another address meet CORS on day one.

Definition

CORS is a browser rule that says if a page may read another site's data. By default, a browser stops a page from reading replies from another site. The server can relax that with a header that names the sites it trusts. If the header is missing or wrong, the browser shows a CORS error. It protects users. It does not protect the server from scripts or tools.

Example code

http
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://shop.example.com
The API tells the browser that the shop may read this reply.

Common mistake

Fixing a CORS error by allowing every site with a star. That lets any page read the reply, including with logins.

Key takeaways

  1. 1
    A browser rule. Tools like curl ignore it.
  2. 2
    The server decides. With a header.
  3. 3
    Name the sites. Avoid the star.

Compare