<?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>Forem: Aditya Srivastava</title>
    <description>The latest articles on Forem by Aditya Srivastava (@jerkeyray).</description>
    <link>https://forem.com/jerkeyray</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%2F2990401%2Fd795e77c-6e37-4187-84b6-72ad353c8e7f.jpg</url>
      <title>Forem: Aditya Srivastava</title>
      <link>https://forem.com/jerkeyray</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jerkeyray"/>
    <language>en</language>
    <item>
      <title>Why 0.1 + 0.2 isn't always 0.3 : Floating Point Explained</title>
      <dc:creator>Aditya Srivastava</dc:creator>
      <pubDate>Wed, 18 Jun 2025 11:04:45 +0000</pubDate>
      <link>https://forem.com/jerkeyray/why-01-02-isnt-always-03-floating-point-explained-31gp</link>
      <guid>https://forem.com/jerkeyray/why-01-02-isnt-always-03-floating-point-explained-31gp</guid>
      <description>&lt;p&gt;Go ahead and type out &lt;code&gt;0.1 + 0.2&lt;/code&gt; in your IDE. You expect &lt;code&gt;0.3&lt;/code&gt;, but instead, you get &lt;code&gt;0.30000000000000004&lt;/code&gt;. You stare at the screen, knowing something’s off. Is your computer broken? Is your code faulty? Well, news flash, it's intentional and it's all a part of floating point arithmetic. In this blog, we’ll look into why floating-point numbers behave this way, how computers represent them, and the real-world situations where precision is critical. &lt;/p&gt;

&lt;p&gt;Let’s start with a quick experiment. Run this Python code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Output: 0.30000000000000004
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that tiny &lt;code&gt;0.00000000000000004&lt;/code&gt; trailing at the end of &lt;code&gt;0.1 + 0.2&lt;/code&gt;? That’s not a bug—it’s a fundamental quirk of how computers deal with decimal numbers. Computers don’t naturally understand decimals the way humans do; they speak &lt;strong&gt;binary&lt;/strong&gt;, and most decimal numbers, like 0.1, are messy in that world. Enter &lt;strong&gt;floating-point arithmetic&lt;/strong&gt;, the system computers use to approximate real numbers (like 3.14 or 0.001). It’s a clever workaround, but it comes with a catch: &lt;strong&gt;it's not precise&lt;/strong&gt;. Because computers can’t store most decimals exactly, these tiny errors can lead to surprising results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Precision Is an Illusion
&lt;/h2&gt;

&lt;p&gt;To understand why &lt;code&gt;0.1 + 0.2 might not always be 0.3&lt;/code&gt;, let’s think about fractions. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4gerw0uddlqwyyb4v3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4gerw0uddlqwyyb4v3p.png" alt="analogy on why these errors kinda compound" width="800" height="707"&gt;&lt;/a&gt;&lt;br&gt;
(pardon the shit hand writing)&lt;/p&gt;

&lt;p&gt;If you limit precision to, say, four decimal places, you get &lt;code&gt;0.9999&lt;/code&gt; instead of &lt;code&gt;1&lt;/code&gt;. Computers face a similar problem when converting decimal numbers to &lt;strong&gt;binary&lt;/strong&gt;. Numbers like &lt;code&gt;0.1&lt;/code&gt; and &lt;code&gt;0.2&lt;/code&gt; look simple in base-10, but in binary, they become infinite repeating fractions. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0.1&lt;/code&gt; in binary is approximately &lt;code&gt;0.000110011001100...&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0.2&lt;/code&gt; in binary is approximately &lt;code&gt;0.00110011001100...&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Computers use a finite number of bit's to store these numbers, so they’re rounded off, leading to small errors. When you add &lt;code&gt;0.1 + 0.2&lt;/code&gt;, those tiny errors combine, giving you &lt;code&gt;0.30000000000000004&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The IEEE 754 Standard
&lt;/h2&gt;

&lt;p&gt;How do computers manage to store a vast, infinite range of numbers with just a handful of bits? Meet the &lt;strong&gt;IEEE 754 standard&lt;/strong&gt;, the clever blueprint behind floating-point arithmetic in nearly all modern computers. Think of IEEE 754 as a clever packing system that squeezes an infinite range of numbers into just 32 or 64 bits.&lt;/p&gt;

&lt;p&gt;Let’s break down how IEEE 754 works with a simplified 32-bit example (real systems often use 64 bits for more precision):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sign bit (1 bit)&lt;/strong&gt;: Indicates positive (&lt;code&gt;0&lt;/code&gt;) or negative (&lt;code&gt;1&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exponent (8 bits)&lt;/strong&gt;: Represents the scale of the number (like (2^n) in scientific notation).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mantissa (23 bits)&lt;/strong&gt;: Stores the significant digits.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The catch? Only a limited number of bits are available, so most numbers are approximations. IEEE 754 also defines special values like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NaN&lt;/strong&gt; (Not a Number): For invalid operations like &lt;code&gt;0/0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infinity&lt;/strong&gt;: For numbers too large to represent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;-0&lt;/strong&gt;: A negative zero, distinct from positive zero.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The biased exponent in IEEE 754 adds a constant (like 127 for 32-bit) to the raw exponent, shifting it to a positive range for storage. This allows representation of both positive and negative powers of 2 within the 8-bit exponent field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzf9hlfkzw0stoyie674.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzf9hlfkzw0stoyie674.png" alt="IEEE 754 explained" width="800" height="1232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41steu2mkdr84rcyb2xd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F41steu2mkdr84rcyb2xd.png" alt="IEEE 754 explained" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(this blog ain't about IEEE-754 cause that's a big topic to go into by itself but there's a very cool video explaining such compression algorithms for anyone interested linked at the end)&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Impacts of Floating-Point Errors
&lt;/h2&gt;

&lt;p&gt;You might think, “Who cares about a tiny error like &lt;code&gt;0.00000000000000004&lt;/code&gt;?” For many applications, like video games or weather simulations, these errors are negligible. But in some fields, they can be catastrophic. Here are some real-world examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Financial Systems
&lt;/h3&gt;

&lt;p&gt;In finance, tiny floating-point errors can add up fast. A bank processing millions of transactions might see small discrepancies, like 0.1 + 0.2 yielding 0.30000000000000004, causing significant losses or overcharges. To avoid this, financial systems often use fixed-point arithmetic or specialised libraries for exact decimal calculations.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Scientific Computing
&lt;/h3&gt;

&lt;p&gt;In scientific simulations, like climate modelling, floating-point errors can distort results. Small miscalculations in iterative processes may predict clear skies when a storm is coming. Scientists use high-precision formats and error-correction methods to minimise these issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Aerospace and Engineering
&lt;/h3&gt;

&lt;p&gt;In aerospace, precision is critical. The 1991 Patriot Missile failure, where a 0.1-second timing error grew due to floating-point rounding in a 24-bit system, led to a deadly miss.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwtb7wz3u5ms72c7k66jq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwtb7wz3u5ms72c7k66jq.png" alt="photo of article on patriot missile failiure" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Machine Learning
&lt;/h3&gt;

&lt;p&gt;Machine learning models perform billions of calculations, and floating-point errors can affect accuracy. Using lower-precision formats for efficiency can lead to unstable training, so techniques like mixed-precision training balance speed and reliability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Floating-Point Errors
&lt;/h2&gt;

&lt;p&gt;Those tiny errors, like 0.1 + 0.2 giving 0.30000000000000004, can trip you up. Here’s how to stay out of trouble:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;fixed-point arithmetic&lt;/strong&gt; or &lt;strong&gt;decimal libraries&lt;/strong&gt; for money-related stuff where precision is critical.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid comparing floats directly. Check if the difference is within a small &lt;strong&gt;epsilon&lt;/strong&gt; (like 0.000000001).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &lt;strong&gt;higher precision types&lt;/strong&gt; (like float64) when accuracy matters more than speed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test carefully when building systems that rely on precision, like aerospace or scientific applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Floating-point arithmetic isn’t a perfect. It’s more like a clever negotiation between your code and the computer’s binary brain. That annoying &lt;code&gt;0.00000000000000004&lt;/code&gt; when adding &lt;code&gt;0.1 + 0.2&lt;/code&gt; shows the system’s limits, but the IEEE 754 standard still manages to cram an infinite range of numbers into a few bits. &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;I did a shit job at explaining this stuff so just go and look at these videos for a better explanation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/wPBjd-vb9eI?si=3ipOPzeFrrnoAO74" rel="noopener noreferrer"&gt;This dude implemented IEEE 754 in JS&lt;/a&gt;&lt;br&gt;
&lt;a href="https://youtu.be/PZRI1IfStY0?si=RknxesFfqbpyCGsa" rel="noopener noreferrer"&gt;Tom Scott explains it like Tom Scott&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
    <item>
      <title>CORS : The Browser Bouncers Explained</title>
      <dc:creator>Aditya Srivastava</dc:creator>
      <pubDate>Wed, 21 May 2025 17:13:29 +0000</pubDate>
      <link>https://forem.com/jerkeyray/cors-the-browser-bouncers-explained-26a7</link>
      <guid>https://forem.com/jerkeyray/cors-the-browser-bouncers-explained-26a7</guid>
      <description>&lt;h2&gt;
  
  
  Why You Should Know About CORS
&lt;/h2&gt;

&lt;p&gt;Knowing CORS inside out doesn’t just make you feel smarter—it’s critical for &lt;strong&gt;building secure systems on the internet&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cross-Origin Resource Sharing (CORS)&lt;/strong&gt; is like a big bouncer at the gate of your browser’s exclusive club, saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;“SORRY, YOU’RE NOT ON THE LIST.”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;...to any uninvited requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftg4g0tpo80l4j3jr4530.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftg4g0tpo80l4j3jr4530.webp" alt="Bouncer denying entry" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are many ways to learn about CORS. In this blog, we’re &lt;strong&gt;ditching the frontend perspective&lt;/strong&gt; (and the cybersecurity deep dive) to focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;What CORS actually does&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why it exists&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;How &lt;strong&gt;you—the backend dev—are in charge of making it behave&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Same-Origin Policy (SOP)
&lt;/h2&gt;

&lt;p&gt;Before diving into CORS, let’s cover why it exists.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Same-Origin Policy (SOP)&lt;/strong&gt; is a browser-enforced security rule that restricts how sites (technically, &lt;em&gt;origins&lt;/em&gt;) interact. Without it, a site like &lt;a href="https://cutecatphotos.com/" rel="noopener noreferrer"&gt;cutecatphotos.com&lt;/a&gt; could make requests to your bank’s website and read private data just because you’re logged in. Not good.&lt;/p&gt;

&lt;p&gt;In short: SOP prevents websites from &lt;strong&gt;reading data&lt;/strong&gt; from another site unless they share the same origin.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s an “Origin”?
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;origin&lt;/strong&gt; combines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scheme&lt;/strong&gt; (e.g., &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;https&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Host&lt;/strong&gt; (e.g., &lt;code&gt;example.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Port&lt;/strong&gt; (e.g., &lt;code&gt;:3000&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdcierilvh0vjba5330sw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdcierilvh0vjba5330sw.png" alt="Diagram of origin structure" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even a slight difference in any of these creates a different origin.&lt;/p&gt;

&lt;p&gt;The browser only allows full access between resources from the &lt;strong&gt;same origin&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;See the table below to understand what counts as “same origin”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fke13pgo2dkgmq15xi4j0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fke13pgo2dkgmq15xi4j0.png" alt="Table explaining same origin" width="800" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  SOP Blocks Reads, Not Writes
&lt;/h3&gt;

&lt;p&gt;A common misconception: SOP doesn’t stop websites from &lt;strong&gt;sending&lt;/strong&gt; data to other origins—only from &lt;strong&gt;reading&lt;/strong&gt; the response.&lt;/p&gt;

&lt;p&gt;Your app can still:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send a &lt;code&gt;POST&lt;/code&gt; request&lt;/li&gt;
&lt;li&gt;Embed an image, script, or stylesheet from another origin&lt;/li&gt;
&lt;li&gt;Submit a form to a different domain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why is this okay? Because you’re not seeing what comes back—you’re just tossing data over the wall.&lt;/p&gt;

&lt;p&gt;Try reading the response with &lt;code&gt;fetch()&lt;/code&gt; or &lt;code&gt;XMLHttpRequest&lt;/code&gt;? Blocked—&lt;strong&gt;unless&lt;/strong&gt; the server explicitly allows it via CORS.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Quick Analogy
&lt;/h3&gt;

&lt;p&gt;Think of SOP as sending a letter. You can mail it anywhere, no problem. But reading the reply? That’s locked—unless the recipient says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Yep, you’re allowed to read this.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s where &lt;strong&gt;CORS&lt;/strong&gt; comes in.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Cross-Origin Resource Sharing (CORS)&lt;/strong&gt; is the guest list for your browser’s club. It’s how a server tells the browser:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Hey, this site is cool—let them read my stuff.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;SOP blocks JavaScript from reading responses from other origins by default. &lt;strong&gt;CORS is the exception—the permission slip&lt;/strong&gt; that tells the browser it’s safe to share.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5j4glo6nupzanosxrya.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff5j4glo6nupzanosxrya.png" alt="Bouncer allowing trusted origin" width="800" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does CORS Work?
&lt;/h3&gt;

&lt;p&gt;When your browser tries to fetch data from a different origin, it sends an HTTP request with an &lt;code&gt;Origin&lt;/code&gt; header, identifying where the request came from.&lt;/p&gt;

&lt;p&gt;The server checks this origin against its “allowed list” and responds with headers like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;: Which origin(s) can access the resource&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Access-Control-Allow-Credentials&lt;/code&gt;: Whether cookies/credentials are allowed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the server gives the green light, the browser lets your JavaScript access the response. If not, the browser blocks it and throws a CORS error in the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Flow of a CORS Request
&lt;/h2&gt;

&lt;p&gt;CORS requests typically follow one of two flows: &lt;strong&gt;simple&lt;/strong&gt; or &lt;strong&gt;preflighted&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simple CORS Request Flow
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;simple request&lt;/strong&gt; is a cross-origin &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, or &lt;code&gt;HEAD&lt;/code&gt; request using only basic headers (e.g., &lt;code&gt;Content-Type: text/plain&lt;/code&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Flow:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;browser adds an &lt;code&gt;Origin&lt;/code&gt; header&lt;/strong&gt; to the request.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;server checks the &lt;code&gt;Origin&lt;/code&gt;&lt;/strong&gt; and, if allowed, responds with: &lt;code&gt;Access-Control-Allow-Origin: &amp;lt;origin&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;browser inspects the response&lt;/strong&gt;. If the header is present and matches, JavaScript can read the response; otherwise, it’s blocked with a CORS error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Even simple requests are subject to CORS enforcement if the response lacks proper headers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preflighted CORS Request Flow
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;preflight request&lt;/strong&gt; occurs when the browser sends an &lt;code&gt;OPTIONS&lt;/code&gt; request to check if the actual request is safe. This happens for methods other than &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, or &lt;code&gt;HEAD&lt;/code&gt;, or when custom headers are used.&lt;/p&gt;

&lt;h4&gt;
  
  
  Flow:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;browser sends an &lt;code&gt;OPTIONS&lt;/code&gt; request&lt;/strong&gt; with &lt;code&gt;Origin&lt;/code&gt;, &lt;code&gt;Access-Control-Request-Method&lt;/code&gt;, and &lt;code&gt;Access-Control-Request-Headers&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;server responds with allowed origins, methods, and headers&lt;/strong&gt; via:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Methods&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Access-Control-Allow-Headers&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If the preflight response allows it, the &lt;strong&gt;browser sends the actual request&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;server responds with the data&lt;/strong&gt;, including &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;browser allows JavaScript to read the response&lt;/strong&gt; only if headers match; otherwise, it throws a CORS error.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This extra step ensures servers are protected from unsafe requests by verifying permissions first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dynamic Origin Whitelisting: Don’t Let Your CORS Be a Pushover
&lt;/h2&gt;

&lt;p&gt;Imagine your server’s a VIP club, and CORS is the bouncer checking IDs. If the bouncer just &lt;em&gt;trusted&lt;/em&gt; every ID without a guest list, you’d have chaos. That’s what happens when you blindly set &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; to any origin that knocks. Let’s break down why this is a rookie mistake and how to lock it down.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Rookie Mistake: Blindly Trusting Origins
&lt;/h3&gt;

&lt;p&gt;Here’s the kind of code that you don't want to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is like your bouncer saying, “Sure, &lt;code&gt;shadyhacker.com&lt;/code&gt;, you’re legit!” It takes the &lt;code&gt;Origin&lt;/code&gt; header from &lt;em&gt;any&lt;/em&gt; request and mirrors it back. Why’s this bad? Because &lt;em&gt;anyone&lt;/em&gt; can fake an &lt;code&gt;Origin&lt;/code&gt; header. A malicious site like &lt;code&gt;evil.com&lt;/code&gt; could get approved and read your server’s response, potentially stealing sensitive data like user info or session tokens. It’s like handing out backstage passes to every rando in the crowd.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Pro Gamer Move: Whitelist Like You Mean It
&lt;/h3&gt;

&lt;p&gt;As the backstage manager, only let trusted origins in with a whitelist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;whitelist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://trusted.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;whitelist&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is your bouncer with a clipboard, checking every origin against a VIP list. Only &lt;code&gt;https://trusted.com&lt;/code&gt; gets in. If &lt;code&gt;dodgy.net&lt;/code&gt; tries to sneak through? Access denied. The browser sees no &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; (or one that doesn’t match), and the response stays locked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro tip&lt;/strong&gt;: Hardcode the whitelist or load it from a secure config. Never let user input touch it. Keep the list tight—every extra origin is another&lt;/p&gt;

&lt;p&gt;door left ajar.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Backend Guidelines: Be the Boss of Your CORS
&lt;/h2&gt;

&lt;p&gt;You’re running the show, so don’t let CORS be the weak link that trashes your club. Here are five golden rules to keep your server secure and users happy:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Always Validate the Origin
&lt;/h3&gt;

&lt;p&gt;Never trust the &lt;code&gt;Origin&lt;/code&gt; header blindly. Use a whitelist to check every request. If the origin isn’t on your VIP list, don’t set &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt;. This keeps &lt;code&gt;shadyhacker.com&lt;/code&gt; out.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Don’t Use &lt;code&gt;*&lt;/code&gt; with Credentials
&lt;/h3&gt;

&lt;p&gt;Setting &lt;code&gt;Access-Control-Allow-Origin: *&lt;/code&gt; is like leaving the club door wide open—fine for public data, but a disaster with cookies or auth tokens. If you need &lt;code&gt;Access-Control-Allow-Credentials: true&lt;/code&gt;, specify an exact origin (e.g., &lt;code&gt;https://trusted.com&lt;/code&gt;). Mixing &lt;code&gt;*&lt;/code&gt; with credentials? The browser will block the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;whitelist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://trusted.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;whitelist&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Credentials&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Origin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Misconceptions About CORS: Don’t Get It Twisted
&lt;/h2&gt;

&lt;p&gt;CORS can be tricky, and plenty of devs get the wrong idea. Let’s clear up some myths so you don’t look like a rookie at the next dev meetup.&lt;/p&gt;

&lt;h3&gt;
  
  
  CORS Isn’t About Authentication or Authorization
&lt;/h3&gt;

&lt;p&gt;CORS doesn’t care who’s logged in or if they have the right API key. It’s just the browser checking if one origin can &lt;em&gt;read&lt;/em&gt; data from another. Your server still needs to handle auth and permissions. Think of CORS as the club’s guest list, not the VIP pass—your backend still checks IDs at the bar.&lt;/p&gt;

&lt;h3&gt;
  
  
  CORS Protects the Browser, Not Your Backend
&lt;/h3&gt;

&lt;p&gt;CORS is a browser mechanism, not a server fortress. It stops &lt;code&gt;shadyapp.com&lt;/code&gt; from reading your server’s response in a user’s browser, but it doesn’t stop requests from hitting your server. Your backend needs proper auth to keep the riffraff out.&lt;/p&gt;

&lt;h3&gt;
  
  
  Non-Browser Clients Don’t Care About CORS
&lt;/h3&gt;

&lt;p&gt;Tools like &lt;code&gt;curl&lt;/code&gt; or Postman ignore CORS entirely—they’re not browsers. They’ll hit your server and get the response, no questions asked. This is why CORS alone isn’t enough for security—it’s like expecting the bouncer to stop people sneaking through the back alley.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro tip&lt;/strong&gt;: Assume your server’s getting hit directly. CORS is the browser’s gatekeeper, not your backend’s bodyguard.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: CORS is Negotiation, Not Defense
&lt;/h2&gt;

&lt;p&gt;CORS isn’t your server’s shield—it’s a handshake between your backend and the browser, ensuring safe cross-origin sharing. As the backend boss, you write the guest list, deciding who reads your data and how. Get it right, and you enable smooth, secure apps that play nice across domains. Get it wrong, and you’re either locking out legit users or letting &lt;code&gt;evil.com&lt;/code&gt; sneak into the VIP lounge.&lt;/p&gt;

&lt;p&gt;Understanding CORS doesn’t just make you a better dev—it saves you from those soul-crushing “CORS error” bug reports and keeps your app’s users dancing. So, set those headers, test your setup, and run your club like a pro. Your server’s got this. &lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;p&gt;Want to dive deeper into CORS? Check out these resources for more technical details and best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=a3C1DMswClQ&amp;amp;list=PLui3EUkuMTPgZcV0QhQrOcwMPcBCcd_Q1&amp;amp;index=11" rel="noopener noreferrer"&gt;Siriniously's Understanding HTTP for beginners&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=t5FBwq-kudw" rel="noopener noreferrer"&gt;Rana Khalil's CORS guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This was my first try at writing a technical blog and I am by no means an expert in the field, this is just a topic I happened to dive deeper into than I needed to and wanted to share what I thought might help. &lt;br&gt;
Still I hope you guys enjoyed it :)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>http</category>
    </item>
  </channel>
</rss>
