<?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: Someshwar Tripathi</title>
    <description>The latest articles on DEV Community by Someshwar Tripathi (@lordneo).</description>
    <link>https://dev.to/lordneo</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%2F613863%2F37f0e3d7-0a86-4d7b-8c7a-0f25a6b70519.jpg</url>
      <title>DEV Community: Someshwar Tripathi</title>
      <link>https://dev.to/lordneo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lordneo"/>
    <language>en</language>
    <item>
      <title>CSRF Explained Like You’re Five (But Smarter) 🔥</title>
      <dc:creator>Someshwar Tripathi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 23:30:23 +0000</pubDate>
      <link>https://dev.to/lordneo/csrf-explained-like-youre-five-but-smarter-3mke</link>
      <guid>https://dev.to/lordneo/csrf-explained-like-youre-five-but-smarter-3mke</guid>
      <description>&lt;p&gt;Imagine you’re at a restaurant. You order a pizza, but some prankster sneaks in and changes your order to &lt;strong&gt;pineapple pizza&lt;/strong&gt; 🍍🍕(the horror!). Worse, they do it without you even noticing.  &lt;/p&gt;

&lt;p&gt;That, my friend, is how &lt;strong&gt;Cross-Site Request Forgery (CSRF)&lt;/strong&gt; works on the web.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What is CSRF?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;CSRF (pronounced "sea-surf") stands for &lt;strong&gt;Cross-Site Request Forgery&lt;/strong&gt;. It’s a sneaky attack where a bad actor tricks your browser into making &lt;strong&gt;unwanted requests&lt;/strong&gt; to another site &lt;strong&gt;where you’re already logged in&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;Basically, CSRF exploits the fact that browsers &lt;strong&gt;automatically&lt;/strong&gt; include cookies with requests.  &lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Let’s say you’re logged into your online banking (&lt;strong&gt;mybank.com&lt;/strong&gt;).  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A hacker sends you a &lt;strong&gt;phishing email&lt;/strong&gt; with a link to an evil website.
&lt;/li&gt;
&lt;li&gt;That evil website secretly sends a request like this:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://mybank.com/transfer?amount=1000&amp;amp;to=hacker"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Your browser &lt;strong&gt;automatically includes your banking cookies&lt;/strong&gt;, making the bank think &lt;em&gt;you&lt;/em&gt; made the request.
&lt;/li&gt;
&lt;li&gt;Boom. You just lost $1000.
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Why Does CSRF Work?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browsers send cookies automatically&lt;/strong&gt; – If you’re logged in, your cookies get sent with every request, no matter where it came from.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No user interaction needed&lt;/strong&gt; – Just &lt;strong&gt;visiting a malicious page&lt;/strong&gt; can trigger CSRF.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Servers trust authenticated users&lt;/strong&gt; – The server thinks requests with valid cookies are legit.
&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;How Do We Stop CSRF?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that we know how it works, let’s talk about &lt;strong&gt;fixes&lt;/strong&gt;.  &lt;/p&gt;
&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;1. CSRF Tokens (The Secret Handshake)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;CSRF token&lt;/strong&gt; is a &lt;strong&gt;random, secret value&lt;/strong&gt; that your app generates and checks with every request.  &lt;/p&gt;

&lt;p&gt;Here’s how it works:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you load a form/page, the server includes a &lt;strong&gt;hidden token&lt;/strong&gt; in it.
&lt;/li&gt;
&lt;li&gt;When you submit the form, that token must be sent back.
&lt;/li&gt;
&lt;li&gt;The server checks if the token is valid—if not, the request is rejected.
&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 html"&gt;&lt;code&gt;
&lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/transfer"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;  
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"hidden"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"csrf_token"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"a1b2c3d4"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;  
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Send Money&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;  
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🚀 &lt;strong&gt;Why it works:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The attacker’s evil site &lt;strong&gt;can’t guess the CSRF token&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Even if they trick your browser into making a request, &lt;strong&gt;it will be missing the token&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;2. SameSite Cookies (The Browser’s Defense)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;SameSite&lt;/code&gt; cookie attribute tells browsers &lt;strong&gt;not&lt;/strong&gt; to send cookies with requests coming from other sites.  &lt;/p&gt;

&lt;p&gt;Set your cookies like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;
Set-Cookie: sessionid=xyz; Secure; HttpOnly; SameSite=Strict  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🚀 &lt;strong&gt;Why it works:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If an attacker’s site tries to make a request, the browser &lt;strong&gt;won’t include your cookies&lt;/strong&gt;, making the request useless.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;3. Requiring Re-Authentication&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before doing &lt;strong&gt;sensitive actions&lt;/strong&gt;, ask the user to &lt;strong&gt;re-enter their password&lt;/strong&gt; or use &lt;strong&gt;2FA&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;🚀 &lt;strong&gt;Why it works:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Even if a CSRF attack tricks your browser, it &lt;strong&gt;can’t bypass authentication prompts&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The Takeaway&lt;/strong&gt; 🎯
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSRF tricks your browser&lt;/strong&gt; into making &lt;strong&gt;unwanted requests&lt;/strong&gt; on your behalf.
&lt;/li&gt;
&lt;li&gt;The best defenses are &lt;strong&gt;CSRF tokens, SameSite cookies, and requiring re-authentication&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;If you’re building a web app, &lt;strong&gt;always protect sensitive actions&lt;/strong&gt; from CSRF.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;CSRF is sneaky, but now &lt;strong&gt;you’re smarter than the hackers&lt;/strong&gt;. Stay safe out there! 🔒🚀  &lt;/p&gt;

&lt;p&gt;Ever run into CSRF issues before? Drop your stories below! 👇🔥  &lt;/p&gt;

</description>
    </item>
    <item>
      <title>CORS Explained Like You’re Five (But Smarter) 🚀</title>
      <dc:creator>Someshwar Tripathi</dc:creator>
      <pubDate>Tue, 18 Mar 2025 23:22:18 +0000</pubDate>
      <link>https://dev.to/lordneo/cors-explained-like-youre-five-but-smarter-1hon</link>
      <guid>https://dev.to/lordneo/cors-explained-like-youre-five-but-smarter-1hon</guid>
      <description>&lt;p&gt;If you've ever built a web app that fetches data from an API, there's a good chance you've run into &lt;strong&gt;CORS errors&lt;/strong&gt;. They’re like that annoying bouncer at the club who won’t let you in, even though your friend is already inside waving at you.  &lt;/p&gt;

&lt;p&gt;So, what is CORS, why does it exist, and how do we deal with it without losing our minds? Let’s break it down.  &lt;/p&gt;




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

&lt;p&gt;CORS stands for &lt;strong&gt;Cross-Origin Resource Sharing&lt;/strong&gt;. It’s a security feature built into web browsers that controls which websites (origins) can request resources from another website’s server.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Origins? What’s That?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;origin&lt;/strong&gt; is simply the &lt;strong&gt;protocol + domain + port&lt;/strong&gt; of a website. For example:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;https://myapp.com&lt;/code&gt; (origin: &lt;code&gt;https&lt;/code&gt;, &lt;code&gt;myapp.com&lt;/code&gt;, default port &lt;code&gt;443&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http://localhost:3000&lt;/code&gt; (origin: &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;localhost&lt;/code&gt;, &lt;code&gt;3000&lt;/code&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your frontend is running on &lt;code&gt;http://localhost:3000&lt;/code&gt; and tries to fetch data from &lt;code&gt;https://api.example.com&lt;/code&gt;, that’s a &lt;strong&gt;cross-origin request&lt;/strong&gt;—meaning CORS rules apply.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Does CORS Exist?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;security guard for your browser&lt;/strong&gt;. Without CORS, any random website could make requests to &lt;strong&gt;your bank, email, or private APIs&lt;/strong&gt; and steal your data. Not cool.  &lt;/p&gt;

&lt;p&gt;CORS is the browser’s way of saying:&lt;br&gt;&lt;br&gt;
❌ "Hey, you’re trying to talk to a different website. Are you sure you’re allowed to?"  &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;server&lt;/strong&gt; being contacted must explicitly say, &lt;strong&gt;“Yes, you are allowed”&lt;/strong&gt; by including special headers in the response. If it doesn’t, the browser &lt;strong&gt;blocks the request&lt;/strong&gt;.  &lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Common CORS Errors &amp;amp; Fixes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now, let’s talk about those &lt;strong&gt;frustrating CORS errors&lt;/strong&gt; and how to deal with them.  &lt;/p&gt;
&lt;h3&gt;
  
  
  🚨 &lt;strong&gt;Error: No ‘Access-Control-Allow-Origin’ header&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;💡 &lt;strong&gt;What’s happening?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Your frontend made a request to another domain, but the server didn’t include an &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; header.  &lt;/p&gt;

&lt;p&gt;🛠 &lt;strong&gt;How to fix it:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you own the API, update the backend to send the header:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;  Access-Control-Allow-Origin: *
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  🚨 &lt;strong&gt;Error: Preflight request blocked&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;💡 &lt;strong&gt;What’s happening?&lt;/strong&gt; &lt;br&gt;
Some requests (like POST, PUT, or requests with custom headers) trigger a preflight request. The browser first sends an OPTIONS request to check if the API allows it. If the API doesn’t respond correctly, the main request is blocked.&lt;/p&gt;

&lt;p&gt;🛠 &lt;strong&gt;How to fix it:&lt;/strong&gt; &lt;br&gt;
Make sure the server handles OPTIONS requests and responds with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
```

`
(This tells the browser, “Yes, I accept these request types.”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
    </item>
  </channel>
</rss>
