<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhinav Galodha</title>
    <description>The latest articles on DEV Community by Abhinav Galodha (@abhinavgalodha).</description>
    <link>https://dev.to/abhinavgalodha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33851%2Fdae7fff2-ba44-4d5b-89be-c832dada4b3f.jpg</url>
      <title>DEV Community: Abhinav Galodha</title>
      <link>https://dev.to/abhinavgalodha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhinavgalodha"/>
    <language>en</language>
    <item>
      <title>All you wanted to know about CORS</title>
      <dc:creator>Abhinav Galodha</dc:creator>
      <pubDate>Tue, 12 Mar 2019 02:07:55 +0000</pubDate>
      <link>https://dev.to/abhinavgalodha/all-you-wanted-to-know-about-cors-2c4f</link>
      <guid>https://dev.to/abhinavgalodha/all-you-wanted-to-know-about-cors-2c4f</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Do you want to understand CORS?&lt;/li&gt;
&lt;li&gt;Does it sound too complicated to understand and the explanation you find is mostly confusing and hard to follow?&lt;/li&gt;
&lt;li&gt;Are you building Microservices and worried about integrating with multiple frontend application?&lt;/li&gt;
&lt;li&gt;Have you encountered the following error, and never understood why the error happens?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A8BFrGFG7TPUJCzGj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A8BFrGFG7TPUJCzGj.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article will provide a &lt;strong&gt;simplistic&lt;/strong&gt; explanation of CORS using real-world analogy. We will go into the details of what you need to understand &amp;amp; troubleshoot CORS Issue. The article will also describe &lt;em&gt;how to add&lt;/em&gt; the CORS support in an Asp.net core web API to enable multiple clients in different domains to interact with the API. Later, we will demonstrate an interaction between a static webpage accessing an API on a different origin.&lt;/p&gt;

&lt;p&gt;This article is the first part in a series of two articles for CORS. This part provides an introduction to CORS which will help you grasp the CORS Concept in an effortless way and allow you to better design, understand, and troubleshoot CORS issue.&lt;/p&gt;

&lt;p&gt;In the next part, we will go into further details, write code and apply the knowledge learned in the first part to build a Client &amp;amp; Server application which can communicate using CORS. The link would be added for the second part once available.&lt;/p&gt;

&lt;h3&gt;
  
  
  Importance of CORS &amp;amp; How Microservices are shaping the modern Application development?
&lt;/h3&gt;

&lt;p&gt;In modern software development, with the advent of the microservices and the rise of the Distributed systems, more components than ever before are being developed in isolation. The monolith applications are being architectured to build smaller more manageable components in the form of the Asp.net core web API. The microservices can be deployed and scaled independently of the frontend. Also, since the Frontend needs to communicate with the API, it is the API responsibility to ensure that it allows the clients to interact and send appropriate data to clients to enable secure communication. CORS forms a major specification which allows an API to accept multiple incoming requests from different websites.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is CORS?
&lt;/h3&gt;

&lt;p&gt;CORS abbreviation is CROSS ORIGIN RESOURCE SHARING.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F970%2F0%2ApGTarhPbC18tWosQ.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F970%2F0%2ApGTarhPbC18tWosQ.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Credit: Getty Images&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cors is a SECURITY mechanism employed by browsers like (Firefox, Chrome, IE etc.) to prevent the browsers from making calls to another Website.&lt;/strong&gt; A request for a resource (like an image or a font) outside of the origin is known as a cross-origin request. CORS (cross-origin resource sharing) is a specification which manages cross-origin requests.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A more naive explanation — It’s like the Security Guard which prevents a malicious person from entering your premises until they possess certain Authorization and hence keeps your family Safe.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Analogy of an Apartment Community
&lt;/h3&gt;

&lt;p&gt;Let’s take an analogy to understand CORS. Imagine, You are living in a &lt;strong&gt;secure&lt;/strong&gt; Housing Apartment Community and have access to all the facilities and having a great time 😄. The access to the community is restricted to its tenants only to ensure safety. If you are a tenant, then you can come in/out at any time. For anyone else, permission is denied.&lt;/p&gt;

&lt;p&gt;You are thinking about your &lt;strong&gt;Birthday party&lt;/strong&gt; in your community swimming pool and want to invite your friends. Since the access to the pool is only restricted to tenants, how would you sneak in your friends?&lt;/p&gt;

&lt;p&gt;In order for them to enter the apartment community, &lt;strong&gt;you need to establish a mechanism to allow your friends to come inside the Apartment Complex&lt;/strong&gt;. One of the mechanism might be giving your friends some Unique Passes which the security would trust and allow access.&lt;/p&gt;

&lt;p&gt;Above analogy was a simplistic explanation to understand the overall concept of the CORS. The notion of security for a Housing apartment is similar to the security implemented by the browsers using the &lt;strong&gt;same origin policy&lt;/strong&gt;. I will refer to this example later while explaining the CORS concept in this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F245%2F0%2A7eGHiR9lVgrfFEs7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F245%2F0%2A7eGHiR9lVgrfFEs7.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s understand what is all this fuss about Origin and what exactly is Origin in the next section. This is the most important Concept to decipher CORS easily.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an Origin?
&lt;/h3&gt;

&lt;p&gt;Let’s take a look at what exactly is Origin. An Origin is made up of the following three parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Protocol/Scheme&lt;/strong&gt;  : (Http/https)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Host&lt;/strong&gt;  : The server/domain name&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port number&lt;/strong&gt;  : The numeric value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Therefore a URL like &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt;, represents a Origin.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example of Same Origin
&lt;/h4&gt;

&lt;p&gt;Let’s consider the following two Url’s which belongs to same origin, &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://galodha.com/image1.jpg" rel="noopener noreferrer"&gt;https://galodha.com/image1.jpg&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://galodha.com/image2.jpg" rel="noopener noreferrer"&gt;https://galodha.com/image2.jpg&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above Url’s are having the &lt;strong&gt;same Protocol&lt;/strong&gt; (https), &lt;strong&gt;Host&lt;/strong&gt; (galodha.com), and &lt;strong&gt;Port Number&lt;/strong&gt; (80 by default for HTTP communication).&lt;/p&gt;

&lt;h4&gt;
  
  
  Example of Different Origin
&lt;/h4&gt;

&lt;p&gt;Following are the example of Origins which are having different Origin than &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt;. One of the Url’s is having a different protocol and other belongs to a different Host.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://galodha.com/image1.jpg" rel="noopener noreferrer"&gt;http://galodha.com/image1.jpg&lt;/a&gt; (Different protocol)&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/image1.jpg" rel="noopener noreferrer"&gt;https://github.com/image1.jpg&lt;/a&gt; (Different host)&lt;/p&gt;

&lt;p&gt;Now, after gaining the understanding of Origin, let look at what exactly is the Same-Origin Policy.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is meant by Same Origin Policy?
&lt;/h3&gt;

&lt;p&gt;The same-origin policy is a &lt;strong&gt;security measure standardized among browsers&lt;/strong&gt;. &lt;em&gt;It prevents different origins from interacting with each other&lt;/em&gt;, to prevent attacks such as Cross-Site Request Forgery. Referring to our analogy, the Same origin is like the tenants belonging to the same apartment community. You can trust a tenant in your Apartment Community, but wouldn’t trust another person in other Apartment Community, unless they are your friends.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example: The Same Origin Allowing communication
&lt;/h4&gt;

&lt;p&gt;Following image shows the communication between client and browser in the same origin. A Client browsing website &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt; can make the calls to &lt;a href="https://galodha.com/projects" rel="noopener noreferrer"&gt;https://galodha.com/projects&lt;/a&gt; API to get the data. The origin &lt;strong&gt;(&lt;/strong&gt;&lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;&lt;strong&gt;https://galodha.com&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;)&lt;/strong&gt; is same for the client and the server and hence communication is allowed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2Ax3b-gpu0BXCZmMWl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2Ax3b-gpu0BXCZmMWl.jpg"&gt;&lt;/a&gt;Sharing Resources&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The same-origin policy is very restrictive.&lt;/strong&gt; This prevents JavaScript from making requests across different Origin. Although the same-origin policy is effective in preventing resources from different origins, it also prevents legitimate interactions between a server and clients of a known and trusted origin.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example: Cross-Origin (Different Origin) Forbidding communication.
&lt;/h4&gt;

&lt;p&gt;Following image shows the communication between 2 different origin. Client browser is browsing the website at an origin, &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt; and making ajax requests to &lt;a href="https://anotherwebsite.com/api/users" rel="noopener noreferrer"&gt;https://anotherwebsite.com/api/users&lt;/a&gt; which is at origin `&lt;a href="https://anotherwebsite.com" rel="noopener noreferrer"&gt;https://anotherwebsite.com&lt;/a&gt;. Since the client and server are at different Origin, the communication is forbidden.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2APEvH_OmTCseu5cdg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2APEvH_OmTCseu5cdg.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Why browsers enforced Same-Origin Policy?
&lt;/h4&gt;

&lt;p&gt;The same Origin policy was enforced in order to prevent security attacks like CSRF (Cross Request Forgery).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you are browsing a bank website and in another tab, while watching your favorite video, you have an advertisement, &lt;strong&gt;“You Won the lottery”&lt;/strong&gt; , you are enticed by the ad and click the advertisement, it opens another window. The Advertisement is a malicious website and has ajax code to send the money from your account to Malicious site owners account.&lt;/p&gt;

&lt;p&gt;$.post('&lt;a href="https://yourfavouriteBANK.com/transfer" rel="noopener noreferrer"&gt;https://yourfavouriteBANK.com/transfer&lt;/a&gt;', { to: 'maliciousOwner', amount: '10000000' }),&lt;/p&gt;

&lt;p&gt;Without further security measures, this would work because authentication cookies from yourfavouriteBANK.com would be sent and authenticate you. All the communication from one Origin to another is possible without restriction and can lead to the above attacks.&lt;/p&gt;

&lt;p&gt;For more information, you may read &lt;a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CORS allows circumventing the Same origin policy while not compromising on the security.&lt;/p&gt;

&lt;p&gt;Let’s break the CORS into smaller pieces to understand it better.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cross-Origin
&lt;/h4&gt;

&lt;p&gt;Cross-origin means that the &lt;strong&gt;origin&lt;/strong&gt; of the request can be &lt;em&gt;different&lt;/em&gt; from the domain that made the request. In Simple words, when a user browsing website X makes another request to website B, it is considered as Cross-Origin.&lt;/p&gt;

&lt;p&gt;In our analogy, Origin refers to Apartment Community. 2 tenants from the same Apartment Community belong to same Origin. However, your friends residing in another Apartment community are in different Origin or Cross Origin.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AC6PFtEzEVNxKQdsz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F800%2F0%2AC6PFtEzEVNxKQdsz.png"&gt;&lt;/a&gt;Different Origin&lt;/p&gt;

&lt;h4&gt;
  
  
  Resource
&lt;/h4&gt;

&lt;p&gt;A resource is like an Image, font, Videos, Data etc. When we are making an Ajax call, we are requesting for some data which is Resource as per the terminology.&lt;/p&gt;

&lt;p&gt;In our analogy, the swimming pool was the resource. This is the valued possession which other people are interested to access.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sharing
&lt;/h4&gt;

&lt;p&gt;CORS defines various headers which allows the browser and server to communicate about which requests are (and are not) allowed and enable the resources to be shared.&lt;/p&gt;

&lt;p&gt;In our analogy, swimming pool was to be shared.&lt;/p&gt;

&lt;p&gt;In our Analogy, we had a Secured Apartment Community which only allowed the tenants access to the resources. Imagine if the access to the apartment resources is not restricted, a malicious person can enter the Community and damage the swimming pool or other resources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F403%2F0%2AHTHruhxPMV6tRqmq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F403%2F0%2AHTHruhxPMV6tRqmq.jpg"&gt;&lt;/a&gt;Sharing Resources&lt;/p&gt;

&lt;h3&gt;
  
  
  How CORS allows to bypass the Same-Origin Policy?
&lt;/h3&gt;

&lt;p&gt;CORS specification provides a list of &lt;strong&gt;Headers&lt;/strong&gt; values which &lt;strong&gt;browser&lt;/strong&gt; and &lt;strong&gt;server&lt;/strong&gt; communicate and understand to ensure that the different Origin can share resources.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In our Analogy, this was the&lt;/em&gt; &lt;strong&gt;&lt;em&gt;Unique pass&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;which allows your friends to inform the Security that you have permission to enter into the Apartment community.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The Client sends Requests to server and server responds by providing the information of the resources that are allowed and how the resources can be accessed. The Server sends the information in the Response Headers. Each Response header signifies an attribute for the resource sharing. For e.g. The header Access-Control-Allow-Methods specifies the list of HTTP methods (GET, POST, DELETE etc.) which are allowed by the Server.&lt;/p&gt;

&lt;p&gt;Following image shows how CORS response headers allow communication between 2 different Origin.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server: &lt;a href="https://localhost:5001" rel="noopener noreferrer"&gt;https://localhost:5001&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Client: &lt;a href="https://localhost:44343" rel="noopener noreferrer"&gt;https://localhost:44343&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to same origin policy, the Ajax request is blocked to a different Origin. However, We have enabled the CORS on the API at &lt;a href="https://localhost:5001" rel="noopener noreferrer"&gt;https://localhost:5001&lt;/a&gt; by adding the response headers Access-Control-Allow-Origin: &lt;a href="https://localhost:44343" rel="noopener noreferrer"&gt;https://localhost:44343&lt;/a&gt;, which is allowing the API at &lt;a href="https://localhost:5001" rel="noopener noreferrer"&gt;https://localhost:5001&lt;/a&gt; to receive any request from Origin &lt;a href="https://localhost:44343" rel="noopener noreferrer"&gt;https://localhost:44343&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AnP0fhds-wexKU3U5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AnP0fhds-wexKU3U5.png"&gt;&lt;/a&gt;Various Parts in Action in CORS&lt;/p&gt;

&lt;p&gt;Most of the headers are prefixed with ‘Access-Control-Allow’. Few Examples are shown below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access-Control-Allow-Origin&lt;/li&gt;
&lt;li&gt;Access-Control-Allow-Headers&lt;/li&gt;
&lt;li&gt;Access-Control-Allow-Methods&lt;/li&gt;
&lt;li&gt;Access-Control-Allow-Credentials&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most important of these is Access-Control-Allow-Origin, Let's explore this header.&lt;/p&gt;

&lt;h4&gt;
  
  
  Access-Control-Allow-Origin
&lt;/h4&gt;

&lt;p&gt;The Access-Control-Allow-Origin header allows servers to specify a list of Origins with which the server will share the resources.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Referring back to our analogy, This basically allows specifying the list of friends which are allowed in your pool party. You are allowed to specify the list of the names that you would want to allow for the party. If your friend name is not in the list, they will not be allowed to come inside.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;During the development stage, the value can be set to *, meaning that Server will share the requested resources with any domain on the Internet. But please refrain from using this setting beyond local development.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;

&lt;p&gt;A browser client from Origin, &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt;, wants to send a request to server &lt;a href="https://api.github.com" rel="noopener noreferrer"&gt;https://api.github.com&lt;/a&gt; Due to Same origin policy, the Client can't send the request to the server. However, when the Server responds with a response header Access-Control-Allow-Origin: &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt; to the client, The Client Browsers allows making a request to Origin &lt;a href="https://api.github" rel="noopener noreferrer"&gt;https://api.github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AjBMQ_Vnbz8XHhN9X.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2AjBMQ_Vnbz8XHhN9X.jpg"&gt;&lt;/a&gt;CORS Headers Allows Communication among different Origin&lt;/p&gt;

&lt;h3&gt;
  
  
  Pre-flight requests
&lt;/h3&gt;

&lt;p&gt;Certain times, an additional Server Request is made by the browser &lt;strong&gt;before&lt;/strong&gt; the actual request, which is also known as the Pre-Flight Request. Preflight requests use the OPTIONS header.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does the browser send an additional request in form of a pre-flight request? Isn’t it an overhead?
&lt;/h4&gt;

&lt;p&gt;The first time, I read about the Pre-flight request, I couldn’t make much sense of the concept, why do we need an additional Request to send before the actual Request. Isn’t it a overhead of making an additional request?&lt;/p&gt;

&lt;p&gt;The Pre-flight Request was added to the CORS Specification to allow communication with the &lt;em&gt;Old&lt;/em&gt; Servers which don’t understand CORS and safeguard against the Potentially dangerous requests like Delete.&lt;/p&gt;

&lt;p&gt;Following screenshot from the Chrome Developer tools shows the OPTIONS Request before the actual request is made. This is the pre-flight request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F989%2F0%2AQe0--64SphgpmkhS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F989%2F0%2AQe0--64SphgpmkhS.png"&gt;&lt;/a&gt;Pre Flight Request in CORS&lt;/p&gt;

&lt;h4&gt;
  
  
  Example : Why Pre-flight request was added?
&lt;/h4&gt;

&lt;p&gt;Let’s go back in past, and Assume a time when CORS Specification wasn’t defined. &lt;strong&gt;Servers were not aware of the CORS Specification&lt;/strong&gt; but did understand the Same-Origin Specification and allowed requests from the Same Origin Only.&lt;/p&gt;

&lt;p&gt;An Origin like &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt; is used by a server X for hosting blogs at &lt;a href="https://galodha.com/blogs" rel="noopener noreferrer"&gt;https://galodha.com/blogs&lt;/a&gt;. This Server X knows about the same Origin policy and allows the operation like &lt;strong&gt;Delete&lt;/strong&gt; a blog post from the same origin.&lt;/p&gt;

&lt;p&gt;Now, CORS specification is launched. A new server Y is setup to manage projects at URL &lt;a href="https://galodha.com/projects" rel="noopener noreferrer"&gt;https://galodha.com/projects&lt;/a&gt;. The server Y supports the Get/Post/Delete operations on projects on the same origin.&lt;/p&gt;

&lt;p&gt;The Projects are getting popular and other websites are interested to list the projects on their websites. So, you will need to allow for a Get Operation from the origin &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt;. Also, there are open source contributors, so you need to offer the Delete operation as well from other websites/origin. Fortunately, the CORS specification has been launched and you know that by using the CORS header Access-Control-Allow-Origin: &lt;a href="https://anotherwebsite.com" rel="noopener noreferrer"&gt;https://anotherwebsite.com&lt;/a&gt;, we can allow the requests from another website/origin. Also, using the CORS header, Access-Control-Allow-Methods: Get, POST, Delete, we can allow the GET/POST/DELETE operations from other websites.&lt;/p&gt;

&lt;p&gt;So far so good, everything is going good and Your Server Y is getting a lot of traffic from other websites.&lt;/p&gt;

&lt;p&gt;Next, A malicious user enters, and is using &lt;a href="https://anotherwebsite.com" rel="noopener noreferrer"&gt;https://anotherwebsite.com&lt;/a&gt; and tries to perform a &lt;strong&gt;DELETE&lt;/strong&gt; operation on the URL &lt;a href="https://galodha.com/blogs" rel="noopener noreferrer"&gt;https://galodha.com/blogs&lt;/a&gt; on Server X. The origin &lt;a href="https://galodha.com" rel="noopener noreferrer"&gt;https://galodha.com&lt;/a&gt; already allows requests from other websites for Server Y at &lt;a href="https://galodha.com/Projects" rel="noopener noreferrer"&gt;https://galodha.com/Projects&lt;/a&gt;. Note that the Same origin policy considers &lt;em&gt;only&lt;/em&gt; the Scheme, HostName and port number, It doesn't consider the full path of the URL. So, a client can make a request to both &lt;a href="https://galodha.com/blogs" rel="noopener noreferrer"&gt;https://galodha.com/blogs&lt;/a&gt; and &lt;a href="https://galodha.com/projects" rel="noopener noreferrer"&gt;https://galodha.com/projects&lt;/a&gt; as the browser thinks that both belong to the same origin.&lt;/p&gt;

&lt;p&gt;Since Server X allowed Delete operations from the same Origin and It doesn’t know anything about the new CORS specification, what should be the behavior for a DELETE operation on server X requested from another website/origin?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should it allow to delete a resource?.&lt;/strong&gt; Wouldn’t it be wonderful if server X can tell that it doesn’t support CORS? Can’t we make an additional Request to check if a Server support CORS?&lt;/p&gt;

&lt;p&gt;Yes, You are in Good luck, The CORS specification defines the Preflight Request which does the same things as we mentioned above. The PreFlight Request makes an additional request to ensure that the Server understands the request or not. If server doesn’t understand the request, then the client will not make the actual Request. However, if the server understands the request, it will return the appropriate response mentioning that what it allows and then client can make the actual request.&lt;/p&gt;

&lt;h4&gt;
  
  
  What conditions Trigger a PreFlight Request?
&lt;/h4&gt;

&lt;p&gt;A Pre-flight request is not made for all the Requests, but is only applicable in certain cases. Following is the list of the conditions which govern if a pre-flight request is made or not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the actual request is any HTTP Method other than GET, POST, or HEAD.&lt;/li&gt;
&lt;li&gt;If a POST request’s content type is anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain.&lt;/li&gt;
&lt;li&gt;Also, if the request contains any custom headers, then a preflight request is required. Headers like Accept, Accept-Language, Content-Language etc doesn't trigger a Preflight request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are certain additional conditions which can trigger the Preflight request. For the complete list reference the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We gained a better understanding of the Origin, What makes and Origin (Scheme, Host and Port Number). Why browser enforced Same origin Policy and how CORS Specification allows the communication between different Origin. We looked at various CORS Headers which Server needs to send to the client to allow the communication. I hope the analogy of the Apartment Community was helpful to understand the security concern and how the Security Pass help us get around it. Lastly, we covered the Pre-Flight requests which might be confusing the first time you see the request popped up in the network Toolbar.&lt;/p&gt;

&lt;p&gt;I hope you have a better understanding of CORS now and can easily sneak in your friends into your secure Apartment Community. All thanks to CORS. In the next article, we will take a look at the code that needs to be added at the Server side and also take a look at a simple Javascript client which will communicate over a different Origin.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This article was originally published &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/15381105/cors-what-is-the-motivation-behind-introducing-preflight-requests" rel="noopener noreferrer"&gt;https://stackoverflow.com/questions/15381105/cors-what-is-the-motivation-behind-introducing-preflight-requests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://enable-cors.org/" rel="noopener noreferrer"&gt;https://enable-cors.org/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>webdev</category>
      <category>networking</category>
      <category>cors</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
