Same-Origin vs. Cross-Origin


CORS Protocol Overview

The CORS protocol is enforced only by browsers to ensure secure cross-origin requests. It involves a set of HTTP headers exchanged between the client (browser) and the cross-origin server to determine whether the response should be accessible to the client.


Steps in the CORS Process

  1. Browser Detects Request Type:

    When a request for a resource is made from a web page, the browser determines whether it is a same-origin or cross-origin request. If it’s a cross-origin request, the browser applies the CORS policy.

  2. Sending the Origin Header:

    The browser includes an Origin header in the request to the cross-origin server, specifying the origin of the client making the request.

    Example Request Header:

    Origin: <http://example.com>
    
    
  3. Server Response with CORS Headers:

    The cross-origin server processes the request and responds with a header called Access-Control-Allow-Origin, indicating which origins are allowed to access the resource.

    Example Response Header:

    Access-Control-Allow-Origin: <http://example.com>
    
    
  4. Browser Validates Response:

    The browser compares the value of the Access-Control-Allow-Origin response header with the Origin header sent in the request.


Example Request and Response Headers

Simple Cross-Origin GET Request

Request Headers (sent by the browser):