“CORS Unchained: Strengthening Web Security with Preflight Requests”
Introduction: In the ever-changing landscape of web development, Cross-Origin Resource Sharing (CORS) plays a vital role in enabling secure communication between different domains. However, there’s a lesser-known yet crucial aspect called “Preflight Requests” that complements CORS. In this article, we’ll explore the significance of CORS and Preflight Requests, unveiling how they work together to ensure seamless and protected interactions in modern web applications.
Understanding CORS: Cross-Origin Resource Sharing (CORS) is a browser security feature that permits servers to specify which domains can access their resources. By controlling cross-origin requests, CORS safeguards sensitive data and mitigates potential security risks.
The Role of Preflight Requests: When a web app initiates a cross-origin request, the browser first sends a “Preflight Request” with an HTTP OPTIONS method. This preliminary request includes headers describing the actual request to follow. The server responds with CORS headers, such as Access-Control-Allow-Origin and Access-Control-Allow-Methods, confirming whether the actual request is allowed.
Importance of CORS and Preflight Requests: CORS, in tandem with Preflight Requests, fortifies web applications against unauthorized access. Preflight Requests act as a safety checkpoint, ensuring that the server is aware of the client’s intent and grants permission for subsequent requests. This layered approach enhances security while enabling feature-rich web apps.

To understand let us take a Scenario:
You are building a web application that fetches data from an API hosted on a different domain (api.example.com). Your web application (app.example.com) needs to access this API to display information to the users. However, due to browser security restrictions, direct cross-origin requests from app.example.com to api.example.com are blocked by default.
Solution using CORS and Preflight Requests: To enable secure communication between your web application and the API, you implement CORS and Preflight Requests on the server-side (api.example.com).
CORS Configuration on Server (api.example.com):
On the server hosting the API, you configure CORS headers in the response to allow cross-origin requests from your web application’s domain (app.example.com).
Example Response Headers on api.example.com:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Preflight Request:
When your web application (app.example.com) wants to make a cross-origin request to the API (api.example.com), the browser sends a Preflight Request to api.example.com with the HTTP OPTIONS method.
Example Preflight Request Headers:
OPTIONS /api/data HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: Authorization
Preflight Response (api.example.com):
The server (api.example.com) receives the Preflight Request, checks its CORS configuration, and responds with the appropriate CORS headers, allowing or denying access to the API.
Example Preflight Response Headers:
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Actual Request (GET Request to api.example.com):
After receiving a successful Preflight Response, the browser proceeds with the actual GET request to the API (api.example.com).
Example Actual Request Headers:
GET /api/data HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>
By allowing the specified domain (app.example.com) in the CORS headers and handling the Preflight Request, the API (api.example.com) ensures that only trusted origins can access its resources securely. This setup prevents unauthorized access from other domains and safeguards sensitive data, maintaining a robust security layer for your web application.
Countering Malicious Attacks:
Unauthorized Access: CORS restricts access to resources, ensuring only trusted origins can make cross-origin requests. This prevents malicious websites from stealing sensitive information from unsuspecting users.
CSRF (Cross-Site Request Forgery): Preflight Requests act as an essential defense against CSRF attacks. Malicious sites cannot forge requests without receiving proper authorization from the server during the Preflight phase.
Information Disclosure: CORS prevents information leakage by blocking cross-origin requests that try to access restricted data.
Cookie Theft: By disallowing unauthorized origins from accessing cookies, CORS mitigates the risk of cookie theft and session hijacking.
Conclusion
In the ever-evolving landscape of web development, CORS and Preflight Requests stand as stalwart defenders against malicious attacks. Their combined strength ensures secure communication, protects sensitive data, and maintains user trust. By leveraging these security mechanisms effectively, developers can create web applications that thrive on integrity, privacy, and resilience against potential threats. Embrace the power of CORS and Preflight Requests to fortify your web applications and foster a safer digital ecosystem for users worldwide.
If this sounds valuable, enter your email to receive notification for the next post