CORS (Cross-Origin Resource Sharing) is a security feature in web browsers that controls access to resources across different domains. It ensures that only trusted domains can access resources, preventing unauthorized access and protecting user data and actions. It extends and adds flexibility to the same-origin policy (SOP). However, it also provides potential for cross-domain attacks, if a website's CORS policy is poorly configured and implemented. CORS is not a protection against cross-origin attacks such as cross-site request forgery (CSRF).
For security reasons, browsers restrict cross-origin HTTP requests initiated from scripts. For example, XMLHttpRequest and the Fetch API follow the same-origin policy.
But in modern Web we know everything is Tangled and interconnected, so we need some relaxations on the SOP. And for this we use the CORS OPTIONS method.
Here we will study limited scope of CORS to understand How it is related to cookies and CSRF. That’s it.
Sec-Fetch-* headers are sent by the browser in cross-origin requests. Their purpose is to indicate the relationship between a request initiator’s origin and the origin of the requested resource.
The malicious CSRF request is sent with the following headers by the browser:
Sec-Fetch-Dest: empty
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Simple HTTP requests are the requests that we can trigger using just HTML. Anything other than that is considered as a PreFlighted Request.