arrow_backAll termssecurity

CORS

CORS (Cross-Origin Resource Sharing) is a browser security mechanism that allows web pages to request resources from a different domain than the one that serve…

Also available: Auf Deutsch

CORS, or Cross-Origin Resource Sharing, is a crucial browser security feature that dictates how web pages can request resources (like fonts, scripts, or data from APIs) from a different domain than the one that served the original page. By default, browsers enforce a Same-Origin Policy, which restricts web pages from making requests to a different origin (domain, protocol, or port) to prevent malicious scripts from one site accessing sensitive data on another.

However, modern web applications often need to access resources from different origins (e.g., a frontend application on app.example.com fetching data from an API on api.example.com). CORS provides a secure way to relax the Same-Origin Policy. It works by adding special HTTP headers to responses from the server, indicating which origins are permitted to access its resources. When a browser detects a cross-origin request, it first sends a "preflight" OPTIONS request to the server to check these headers.

Key CORS headers include:

  • Access-Control-Allow-Origin: Specifies which origins are allowed to access the resource (e.g., Access-Control-Allow-Origin: https://app.example.com or * for any origin).
  • Access-Control-Allow-Methods: Specifies the HTTP methods allowed (e.g., GET, POST, PUT).
  • Access-Control-Allow-Headers: Specifies which HTTP headers can be used in the actual request.

Properly configuring CORS is essential for the security and functionality of many web applications. Misconfigurations can either block legitimate requests or, worse, expose your API to unauthorized access from any origin, creating security vulnerabilities.

Related terms

Audit your site on all of these?

Pantra scans you in 8 seconds. Free, no signup.

Scan my sitearrow_forward