<?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: Nikita Sarkania</title>
    <description>The latest articles on DEV Community by Nikita Sarkania (@sarkanianikita).</description>
    <link>https://dev.to/sarkanianikita</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%2F1324400%2F6994aa31-1166-42bf-843f-e8b4dac0e152.JPG</url>
      <title>DEV Community: Nikita Sarkania</title>
      <link>https://dev.to/sarkanianikita</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarkanianikita"/>
    <language>en</language>
    <item>
      <title>Security Risks of Inline Event Handlers in React.js (And How to Avoid Them)</title>
      <dc:creator>Nikita Sarkania</dc:creator>
      <pubDate>Wed, 02 Oct 2024 17:13:38 +0000</pubDate>
      <link>https://dev.to/sarkanianikita/security-risks-of-inline-event-handlers-in-reactjs-and-how-to-avoid-them-3mgj</link>
      <guid>https://dev.to/sarkanianikita/security-risks-of-inline-event-handlers-in-reactjs-and-how-to-avoid-them-3mgj</guid>
      <description>&lt;p&gt;If you’re a React.js developer, chances are you’ve dropped an inline event handler into your code without even thinking twice. It’s so easy to do! You might write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; alert('You clicked me!')}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom—mission accomplished, right? The button works, everyone’s happy. But wait… did you just leave the back door open for hackers? 😱&lt;/p&gt;

&lt;p&gt;Yes, as convenient as inline event handlers seem, they come with hidden security risks, especially when dealing with user input. Let’s dive into why inline handlers in React.js can be dangerous, and more importantly, how to avoid those risks—without losing your cool!&lt;/p&gt;




&lt;h3&gt;
  
  
  What’s the Big Deal About Inline Event Handlers?
&lt;/h3&gt;

&lt;p&gt;In React, inline event handlers let you pass functions directly into JSX elements. It's so simple and quick:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; console.log('Clicked!')}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works like a charm, right? But… here’s the catch: when you mix user input with inline event handlers, you open up your app to Cross-Site Scripting (XSS) attacks.&lt;/p&gt;




&lt;h3&gt;
  
  
  XSS Attacks: The Villain of the Web
&lt;/h3&gt;

&lt;p&gt;Okay, let's say you’ve got a list of items, and you’re letting users click a button to show an alert based on the item’s name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const items = [
  { name: 'Safe Item' },
  { name: '&amp;lt;script&amp;gt;alert("Hacked!")&amp;lt;/script&amp;gt;' }
];

return items.map((item, index) =&amp;gt; (
  &amp;lt;div key={index}&amp;gt;
    &amp;lt;span&amp;gt;{item.name}&amp;lt;/span&amp;gt;
    &amp;lt;button onClick={() =&amp;gt; alert(`You clicked on ${item.name}`)}&amp;gt;Click Me&amp;lt;/button&amp;gt;
  &amp;lt;/div&amp;gt;
));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Harmless, right? Not quite. The second item contains a sneaky &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag. If a user clicks the button next to it, that malicious script runs in the browser. You’ve just been hacked, my friend!!.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uwuuceld2j7dvcp8dpd.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6uwuuceld2j7dvcp8dpd.gif" alt="sneaky-toes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s an XSS attack in action. Hackers can inject code into your app and make it run as if it’s part of your site. This can lead to stolen user data, altered content, or worse.&lt;/p&gt;




&lt;h3&gt;
  
  
  Wait… But Isn’t React Supposed to Protect Me?
&lt;/h3&gt;

&lt;p&gt;Great question! React does protect you from XSS in most cases. For example, when rendering text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;span&amp;gt;{item.name}&amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if &lt;code&gt;item.name&lt;/code&gt; contains a script tag, React automatically escapes it. Instead of executing the script, it just displays it as plain text. Crisis averted!&lt;/p&gt;

&lt;p&gt;But here’s the twist: React won’t save you if you’re using untrusted data inside an inline event handler like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={() =&amp;gt; alert(`You clicked on ${item.name}`)}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the code inside the event handler runs directly, React can’t stop the malicious script from executing. And that’s where things can get tricky.&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Avoid Security Risks in React.js
&lt;/h3&gt;

&lt;p&gt;So, what’s the solution? You don’t have to abandon inline event handlers altogether, but here’s how to avoid security disasters while still keeping your code neat and tidy.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Don’t Use Untrusted Data Directly in Inline Handlers
&lt;/h4&gt;

&lt;p&gt;The first rule: &lt;strong&gt;never&lt;/strong&gt; put user input or untrusted data straight into an event handler. That’s like giving a stranger the keys to your house.&lt;/p&gt;

&lt;p&gt;Instead, sanitize the data before using it in the handler. You can use libraries like &lt;strong&gt;DOMPurify&lt;/strong&gt; to clean up any potentially dangerous content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import DOMPurify from 'dompurify';

const safeData = DOMPurify.sanitize(item.name);

&amp;lt;button onClick={() =&amp;gt; alert(`You clicked on ${safeData}`)}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, even if someone tries to sneak a script into the &lt;code&gt;name&lt;/code&gt;, it won’t execute. It’s like giving your app a little protective shield 🛡️.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Move Event Handlers Out of JSX
&lt;/h4&gt;

&lt;p&gt;Instead of defining the event handler inside the JSX, create a separate function. Not only does this keep things clean, but it’s also easier to manage and secure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleClick = (name) =&amp;gt; {
  alert(`You clicked on ${name}`);
};

&amp;lt;button onClick={() =&amp;gt; handleClick(item.name)}&amp;gt;Click Me&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the event handler is separate from your JSX, which keeps things organized—and makes it easier to spot potential security issues!&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Sanitize and Escape User Inputs
&lt;/h4&gt;

&lt;p&gt;User inputs are notorious for being &lt;code&gt;sneaky little security risks&lt;/code&gt;. If your app is rendering any user-generated content (like comments, form inputs, etc.), always sanitize the input before using it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import DOMPurify from 'dompurify';

const cleanInput = DOMPurify.sanitize(userInput);

&amp;lt;span&amp;gt;{cleanInput}&amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that no matter what shady stuff a user might try to input, it won’t end up as executable code in your app.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Trust React’s Built-In XSS Protections (For Rendering)
&lt;/h4&gt;

&lt;p&gt;React is pretty good at protecting you from XSS when it comes to rendering content. So when it comes to displaying user data in your app, just trust React’s default behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;span&amp;gt;{item.name}&amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React will escape any HTML tags for you, so you don’t have to worry about rendering malicious scripts. Just avoid inline event handlers that mix untrusted data.&lt;/p&gt;




&lt;h3&gt;
  
  
  Keep It Fun, Keep It Safe
&lt;/h3&gt;

&lt;p&gt;Inline event handlers in React.js can be tempting because they’re quick and easy to write, but as we’ve seen, they come with some big security risks if you’re not careful. The good news? You can still keep your code clean and fun &lt;em&gt;while&lt;/em&gt; protecting your app from XSS attacks.&lt;/p&gt;

&lt;p&gt;By following these best practices—sanitizing user data, separating your handlers, and letting React do its thing—you’ll be writing safe, secure, and maintainable code like a true React pro. 🙌&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu49qa1ash9d77wh2o4kf.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu49qa1ash9d77wh2o4kf.gif" alt="cool-dev"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Got questions or want to share your own experiences with security in React? Drop a comment below!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Elements vs. Tags: A Friendly Guide</title>
      <dc:creator>Nikita Sarkania</dc:creator>
      <pubDate>Thu, 29 Aug 2024 18:47:23 +0000</pubDate>
      <link>https://dev.to/sarkanianikita/elements-vs-tags-a-friendly-guide-boj</link>
      <guid>https://dev.to/sarkanianikita/elements-vs-tags-a-friendly-guide-boj</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;strong&gt;Ever wondered what the difference is between an element and a tag in HTML?&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Let's break it down in a way that's easy to understand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Imagine a House
&lt;/h3&gt;

&lt;p&gt;Think of a house. The house is like an &lt;em&gt;&lt;strong&gt;element&lt;/strong&gt;&lt;/em&gt; in HTML. It's the whole building. Inside the house, you have different rooms. These rooms are like &lt;em&gt;&lt;strong&gt;tags&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;House (Element)&lt;/strong&gt;: The entire building.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rooms (Tags)&lt;/strong&gt;: Different sections within the house, like bedrooms, kitchens, and living rooms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Elements: The Big Picture
&lt;/h3&gt;

&lt;p&gt;In HTML, elements are like the building blocks of a webpage. They define the structure and content of the page. Some common elements include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt;: Paragraph&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;: Division&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;h1-h6&amp;gt;&lt;/code&gt;: Heading levels&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;ul&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;ol&amp;gt;&lt;/code&gt; : Unordered and ordered lists&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt;: Image&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tags: The Labels
&lt;/h3&gt;

&lt;p&gt;Tags are like labels that tell the browser what kind of element it's dealing with. They are enclosed in angle brackets (&lt;code&gt;&amp;lt;&amp;gt;&lt;/code&gt;). For example, &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; is the tag for a paragraph.&lt;/p&gt;

&lt;h3&gt;
  
  
  Putting It Together
&lt;/h3&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
&lt;strong&gt;Element&lt;/strong&gt;: The whole sentence.&lt;br&gt;
&lt;strong&gt;Tags&lt;/strong&gt;: The words that make up the sentence.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;This is a paragraph.&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Element&lt;/strong&gt;: &lt;code&gt;&amp;lt;p&amp;gt;This is a paragraph.&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Tags&lt;/strong&gt;: &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;: Elements are made up of tags and their content. Tags tell the browser how to interpret the content.&lt;/p&gt;

&lt;p&gt;I know this is just the basics, but understanding the difference between elements and tags can clear up a lot of confusion for beginners. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Keep exploring and happy coding!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hello World!!!</title>
      <dc:creator>Nikita Sarkania</dc:creator>
      <pubDate>Thu, 29 Aug 2024 16:00:27 +0000</pubDate>
      <link>https://dev.to/sarkanianikita/hello-world-214o</link>
      <guid>https://dev.to/sarkanianikita/hello-world-214o</guid>
      <description>&lt;p&gt;Hello everyone!&lt;/p&gt;

&lt;p&gt;I'm excited to start this new journey and share my thoughts, experiences, and learnings with all of you. This is my first blog post, and I'm eager to see where it takes me.&lt;/p&gt;

&lt;p&gt;As I continue to explore and grow, I plan to use this platform to document my progress, share tips and tricks, and connect with like-minded individuals. I believe that sharing knowledge and experiences can be a powerful way to learn and inspire others.&lt;/p&gt;

&lt;p&gt;I hope you'll join me on this adventure!&lt;/p&gt;

&lt;p&gt;Best, Nikita&lt;/p&gt;

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