<?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: Sarim Nadeem</title>
    <description>The latest articles on DEV Community by Sarim Nadeem (@sarim_nadeem_888180307df8).</description>
    <link>https://dev.to/sarim_nadeem_888180307df8</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%2F3759481%2F31d5b18a-a9d7-4370-aa09-9d4dfb454448.jpeg</url>
      <title>DEV Community: Sarim Nadeem</title>
      <link>https://dev.to/sarim_nadeem_888180307df8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarim_nadeem_888180307df8"/>
    <language>en</language>
    <item>
      <title>The Internet’s Bouncer: A Clear Guide to SOP and CORS</title>
      <dc:creator>Sarim Nadeem</dc:creator>
      <pubDate>Tue, 14 Apr 2026 11:06:57 +0000</pubDate>
      <link>https://dev.to/sarim_nadeem_888180307df8/the-internets-bouncer-a-clear-guide-to-sop-and-cors-1954</link>
      <guid>https://dev.to/sarim_nadeem_888180307df8/the-internets-bouncer-a-clear-guide-to-sop-and-cors-1954</guid>
      <description>&lt;h1&gt;
  
  
  The Internet’s Bouncer: A Clear Guide to SOP and CORS
&lt;/h1&gt;

&lt;p&gt;If you’ve ever seen a red error message in your browser console shouting about "Cross-Origin Request Blocked," you’ve met the web’s most important security duo: &lt;strong&gt;SOP&lt;/strong&gt; and &lt;strong&gt;CORS&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Here is everything you need to know to understand how they work together to keep the web safe.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Same-Origin Policy (SOP)?
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Same-Origin Policy (SOP)&lt;/strong&gt; is a fundamental security feature implemented by web browsers. It’s like the internet’s bouncer, preventing web pages from making requests to different origins unless they match. &lt;/p&gt;

&lt;p&gt;Without SOP, malicious websites could easily access sensitive data on other tabs you’ve got open—imagine your bank account details hanging out for all to see!&lt;/p&gt;

&lt;h3&gt;
  
  
  What Defines an "Origin"?
&lt;/h3&gt;

&lt;p&gt;If any of these three components differ between two URLs, they are considered to have different origins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Protocol&lt;/strong&gt; (e.g., &lt;code&gt;http&lt;/code&gt; vs. &lt;code&gt;https&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain&lt;/strong&gt; (e.g., &lt;code&gt;example.com&lt;/code&gt; vs. &lt;code&gt;api.example.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port&lt;/strong&gt; (e.g., &lt;code&gt;:80&lt;/code&gt; vs. &lt;code&gt;:443&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com:443/page1  ✅ Same origin as https://example.com:443/page2
http://example.com/page1       ❌ Different origin from https://example.com/page2 (different protocol)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  How SOP Restricts Interactions
&lt;/h2&gt;

&lt;p&gt;To protect your data, the browser restricts what scripts can do across origins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cookies:&lt;/strong&gt; You can only access cookies for your own origin.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage:&lt;/strong&gt; &lt;code&gt;LocalStorage&lt;/code&gt; and &lt;code&gt;SessionStorage&lt;/code&gt; are origin-specific. No peeking at someone else’s data!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DOM Access:&lt;/strong&gt; A script from &lt;code&gt;Origin A&lt;/code&gt; cannot access the DOM of a page from &lt;code&gt;Origin B&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AJAX/Fetch Requests:&lt;/strong&gt; Requests are blocked by default unless explicitly allowed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  What Is CORS (Cross-Origin Resource Sharing)?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;CORS&lt;/strong&gt; is a security feature that allows or restricts resources on a web server to be requested from a different domain. It’s like giving permission to certain websites to knock on your server’s door and grab some data. &lt;/p&gt;

&lt;p&gt;Without CORS, web pages are locked in their own origin-sandbox, unable to communicate with external APIs, CDNs, or other resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Is CORS Important?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Enforces SOP:&lt;/strong&gt; It makes the strictness of SOP flexible for legitimate communication.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Controlled Sharing:&lt;/strong&gt; Servers can whitelist specific domains to keep data safe.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Prevents Attacks:&lt;/strong&gt; It helps protect against attacks like &lt;em&gt;Cross-Site Request Forgery (CSRF)&lt;/em&gt; and &lt;em&gt;Cross-Site Scripting (XSS)&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  The CORS Workflow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Simple Requests
&lt;/h3&gt;

&lt;p&gt;These include &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, and &lt;code&gt;HEAD&lt;/code&gt; requests that don’t require special handling. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The browser sends the request with an &lt;code&gt;Origin&lt;/code&gt; header.&lt;/li&gt;
&lt;li&gt;If the server returns an &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; header that matches, all is good.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Preflight Requests
&lt;/h3&gt;

&lt;p&gt;These are like asking the server, &lt;em&gt;"Hey, is it cool if I send a DELETE request?"&lt;/em&gt; before making the actual request. This is done via an &lt;code&gt;OPTIONS&lt;/code&gt; call.&lt;/p&gt;




&lt;h2&gt;
  
  
  CORS Headers You Should Know
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;&lt;/strong&gt;: Specifies which origins are allowed access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt;&lt;/strong&gt;: Defines permitted HTTP methods (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt;&lt;/strong&gt;: Specifies which custom headers can be used.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt;&lt;/strong&gt;: Allows cookies or authorization headers to be included.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Wildcard Policy
&lt;/h2&gt;

&lt;p&gt;A wildcard &lt;code&gt;*&lt;/code&gt; is appropriate when an API response is intended to be accessible to any code on any site, such as &lt;a href="https://fonts.google.com/" rel="noopener noreferrer"&gt;Google Fonts&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The value of &lt;code&gt;*&lt;/code&gt; is special because it &lt;strong&gt;does not&lt;/strong&gt; allow requests to supply credentials. This means it won't allow HTTP authentication, SSL certificates, or cookies to be sent in the cross-domain request.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SOP&lt;/strong&gt; is the "Default Deny" policy that keeps your browser secure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CORS&lt;/strong&gt; is the "Explicit Allow" protocol that lets modern web apps talk to each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>discuss</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
