Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Configuring CORS on the Backend | Backend CORS Configuration and Credential Handling
CORS Internals and Security

Configuring CORS on the Backend

Scorri per mostrare il menu

When you set up a backend server that will be accessed from web browsers, configuring CORS ("Cross-Origin Resource Sharing") is a critical step for both functionality and security. CORS controls which web origins can interact with your server’s resources, and how they can do so. If configured incorrectly, CORS can unintentionally expose sensitive data or allow malicious sites to interact with your backend. Understanding how to properly set CORS headers helps you strike a balance between enabling necessary cross-origin access and minimizing security risks.

Backend CORS settings determine who can access your server’s resources and under what conditions. The most common settings involve specifying allowed origins, HTTP methods, and headers. You can use a wildcard origin (*) to allow any site to access your resources, or you can restrict access to specific trusted domains.

  • A wildcard origin (*) allows any website to access your resources;
  • A restrictive policy requires you to list allowed origins explicitly, reducing the risk of unwanted cross-origin access.

A permissive policy like a wildcard makes development easier but can introduce security vulnerabilities. Choosing the right configuration depends on your application’s needs and the sensitivity of the data being served.

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json

{
  "message": "This is a CORS-enabled response."
}

While using Access-Control-Allow-Origin: * makes your server accessible from any origin, it significantly increases your attack surface. Any website, including potentially malicious ones, can make requests to your server and access its responses. This is especially dangerous if your server handles sensitive data or user-specific information. For secure applications, you should avoid wildcards and instead specify only the trusted origins that should have access. By doing so, you reduce the risk of data leaks and unauthorized actions triggered from untrusted sites.

question mark

What is a potential risk of setting Access-Control-Allow-Origin to '*'?

Seleziona la risposta corretta

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Sezione 2. Capitolo 1
some-alt