Frontend School

Understanding and Resolving Cross-Origin Resource Sharing (CORS) in Zomato

Theory
easy

Understanding and Resolving Cross-Origin Resource Sharing (CORS) in Zomato

Easy
Frontend
Web Security
CORS
HTTP

Overview

Zomato's web application is a complex system, often interacting with various backend services, microservices, and third-party APIs across different domains or subdomains. Ensuring secure and controlled communication between these origins is paramount for protecting user data and maintaining application integrity. This problem explores a fundamental web security mechanism that facilitates this controlled interaction.

What you need to do / Task

Your task is to demonstrate your understanding of Cross-Origin Resource Sharing (CORS) by addressing the following points:

  1. Explain CORS: Clearly define what Cross-Origin Resource Sharing (CORS) is.
  2. Security Importance: Describe why CORS is a critical web security mechanism and how it protects users.
  3. Scenario & Issue: Illustrate a common scenario in a Zomato-like application where a frontend request would trigger a CORS error.
  4. Resolution: Detail the typical steps a developer would take to resolve this specific CORS error.

The question

Imagine you're developing a new feature for Zomato's customer-facing web application (www.zomato.com). You're building a frontend component that needs to fetch user preferences from a newly deployed backend service (user-preferences.api.zomato.com). Initially, your requests to this new service are failing with a CORS error. Explain what Cross-Origin Resource Sharing (CORS) is, why it's a critical web security mechanism, and how it protects users. Then, describe the common scenario where your frontend request to user-preferences.api.zomato.com might encounter a CORS issue and outline the steps a developer would typically take to resolve it.

What a strong answer covers

  • A clear and accurate definition of CORS, including its relationship with the Same-Origin Policy (SOP).
  • A thorough explanation of the security implications and how CORS safeguards user data and application integrity.
  • A concrete, realistic Zomato-related scenario that clearly demonstrates a CORS issue.
  • A step-by-step, practical resolution strategy, focusing on server-side configuration (e.g., HTTP headers) to enable the cross-origin request.

Examples

1

Example 1

Input:

Explain CORS, its security importance, and how to resolve a common Zomato-specific CORS issue.

Output:

  • CORS Definition: Explanation of CORS as a browser security feature relaxing SOP, allowing controlled cross-origin requests.
  • Security: How CORS prevents unauthorized data access and CSRF by requiring explicit server opt-in.
  • Scenario: www.zomato.com (origin) trying to fetch from user-preferences.api.zomato.com (different origin/subdomain).
  • Resolution: Backend (user-preferences.api.zomato.com) needs to configure Access-Control-Allow-Origin header to include https://www.zomato.com in its HTTP responses.

Note:

This example outlines the key components expected in a strong answer, covering definition, security rationale, a specific scenario, and the primary resolution method.

Constraints

Constraints & assumptions

  • Focus on fundamental concepts and practical application.
  • Assume a standard web browser environment for the frontend.
  • Assume the backend service (user-preferences.api.zomato.com) is under Zomato's control and can be configured.
  • Expected depth: Explanations should go beyond simple definitions, demonstrating understanding of why CORS works the way it does and its practical implications.
  • Suggested length: Approximately 300-500 words.

Out of scope

  • Detailed code implementation for backend CORS configuration (focus on conceptual steps and HTTP headers).
  • Complex CORS preflight request specifics (e.g., specific headers for preflight beyond general understanding of when they occur).
  • Advanced web security topics like Content Security Policy (CSP) or Subresource Integrity (SRI) unless directly relevant to the CORS context.

What interviewers evaluate

  • Understanding of core web security principles (Same-Origin Policy, CORS).
  • Ability to articulate technical concepts clearly and concisely.
  • Practical problem-solving approach to common web development issues.
  • Relevance and accuracy of examples in a Zomato context.

Loading editor...