<?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: Ashish Jha</title>
    <description>The latest articles on DEV Community by Ashish Jha (@ashish_jha_178859fb4618e6).</description>
    <link>https://dev.to/ashish_jha_178859fb4618e6</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%2F2905376%2Fa57f5c45-971d-4c58-a5b7-729fbaca8812.jpg</url>
      <title>DEV Community: Ashish Jha</title>
      <link>https://dev.to/ashish_jha_178859fb4618e6</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashish_jha_178859fb4618e6"/>
    <language>en</language>
    <item>
      <title>"But It Works on My Machine! 🤷‍♂️ How package-lock.json Saves the Day"</title>
      <dc:creator>Ashish Jha</dc:creator>
      <pubDate>Mon, 03 Mar 2025 10:51:16 +0000</pubDate>
      <link>https://dev.to/ashish_jha_178859fb4618e6/but-it-works-on-my-machine-how-package-lockjson-saves-the-day-1bjk</link>
      <guid>https://dev.to/ashish_jha_178859fb4618e6/but-it-works-on-my-machine-how-package-lockjson-saves-the-day-1bjk</guid>
      <description>&lt;h2&gt;
  
  
  BASICS
&lt;/h2&gt;

&lt;p&gt;Ever had a project work perfectly on your machine, but when your friend (or worse, your CI/CD pipeline) runs it, everything falls apart like a Jenga tower? Yeah, blame dependencies.&lt;/p&gt;

&lt;p&gt;In a &lt;strong&gt;Node.js&lt;/strong&gt; project, &lt;strong&gt;package.json&lt;/strong&gt; is like your shopping list—it says, "I need React, but I’m cool with any version above 18.2.0.(denoted by ^)" Meanwhile, &lt;strong&gt;package-lock.json&lt;/strong&gt; is the receipt—it says, "You got React 18.2.3 from this exact store, and don’t even think about changing it!" It locks the exact versions of not just your dependencies, but also their dependencies’ dependencies (yep, it's dependencies all the way down). This means that when someone else runs npm install, they get the exact same versions as you—no surprise updates, no weird bugs, no "But it works on my machine!" moments.&lt;/p&gt;

&lt;p&gt;Now, let’s break down exactly why package-lock.json is the hero we never knew we needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  package.json
&lt;/h2&gt;

&lt;p&gt;Purpose: Defines the project's dependencies, metadata, scripts, and configurations.&lt;/p&gt;

&lt;p&gt;Contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Project name, version, and description.&lt;/li&gt;
&lt;li&gt;Scripts (npm start, npm test, etc.).&lt;/li&gt;
&lt;li&gt;Dependencies (dependencies, devDependencies, peerDependencies, etc.).&lt;/li&gt;
&lt;li&gt;Version ranges for dependencies, like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"dependencies": {
  "react": "^18.2.0",
  "axios": "~1.4.0"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Versioning:&lt;/strong&gt;&lt;br&gt;
Uses semantic versioning (SemVer) ranges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"^1.2.3" → Allows updates up to &amp;lt;2.0.0 (i.e., 1.x.x updates).&lt;/li&gt;
&lt;li&gt;"~1.2.3" → Allows updates up to &amp;lt;1.3.0 (i.e., 1.2.x updates).&lt;/li&gt;
&lt;li&gt;"1.2.3" → Locks the dependency to exactly 1.2.3.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  package-lock.json
&lt;/h2&gt;

&lt;p&gt;Purpose: Ensures deterministic installs by locking exact versions of dependencies and all nested dependencies.&lt;/p&gt;

&lt;p&gt;Contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exact versions of direct and transitive (nested) dependencies.&lt;/li&gt;
&lt;li&gt;Resolved URLs from where dependencies were installed.&lt;/li&gt;
&lt;li&gt;Integrity checksums for security and verification.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Do We Need Both?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;package.json:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines what dependencies your project needs.&lt;/li&gt;
&lt;li&gt;Allows flexibility for minor updates.&lt;/li&gt;
&lt;li&gt;Useful for open-source projects to let users install compatible 
versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;package-lock.json&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures that everyone working on the project installs the exact 
same versions.&lt;/li&gt;
&lt;li&gt;Prevents "&lt;em&gt;works on my machine&lt;/em&gt;" problems.&lt;/li&gt;
&lt;li&gt;Improves performance by caching resolved dependency versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Happens If You Delete package-lock.json?
&lt;/h2&gt;

&lt;p&gt;Running npm install will generate a new &lt;strong&gt;package-lock.json&lt;/strong&gt; file with potentially different versions for dependencies, especially if they were specified with version ranges in package.json.&lt;/p&gt;

&lt;h2&gt;
  
  
  Should You Commit package-lock.json?
&lt;/h2&gt;

&lt;p&gt;✅ Yes, especially for teams and production apps, to ensure consistency across environments.&lt;br&gt;
🚫 No, if you're building a reusable package/library (e.g., an npm package), because the consumer should decide which versions to use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;br&gt;
If you're using &lt;strong&gt;yarn&lt;/strong&gt;, it creates a &lt;strong&gt;yarn.lock&lt;/strong&gt; file instead of &lt;strong&gt;package-lock.json&lt;/strong&gt;, serving the same purpose.&lt;/p&gt;

&lt;p&gt;And that’s the magic of &lt;strong&gt;package-lock.json&lt;/strong&gt;—keeping your project safe from the dreaded &lt;strong&gt;“But it works on my machine!”&lt;/strong&gt; chaos. Hopefully, now you see why this little file is your best friend when it comes to dependency management.&lt;/p&gt;

&lt;p&gt;Got questions? Confused about something? Or just want to rant about npm breaking your build at the worst possible time? Drop your thoughts in the comments! I’d love to hear them (and maybe cry with you over dependency nightmares). 😆👇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>node</category>
      <category>npm</category>
    </item>
    <item>
      <title>Understanding and Preventing XSS Attacks: A Frontend Developer’s Guide</title>
      <dc:creator>Ashish Jha</dc:creator>
      <pubDate>Sat, 01 Mar 2025 14:44:11 +0000</pubDate>
      <link>https://dev.to/ashish_jha_178859fb4618e6/understanding-and-preventing-xss-attacks-a-frontend-developers-guide-1abm</link>
      <guid>https://dev.to/ashish_jha_178859fb4618e6/understanding-and-preventing-xss-attacks-a-frontend-developers-guide-1abm</guid>
      <description>&lt;p&gt;Cross-Site Scripting (XSS) is one of the most prevalent security vulnerabilities in web applications. As front-end developers, understanding how XSS attacks work and how to mitigate them is crucial for building secure applications.&lt;/p&gt;

&lt;p&gt;By the end, you'll not only understand XSS inside out but also gain practical strategies to secure your applications—without the usual jargon overload. Stick with me, and you'll never have to second-guess your app's security again!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is XSS?
&lt;/h2&gt;

&lt;p&gt;XSS (Cross-Site Scripting) is a security vulnerability that allows attackers to inject malicious JavaScript code into a web application. This code executes in the context of the victim’s browser, enabling attackers to steal sensitive data, hijack sessions, or deface websites.&lt;/p&gt;

&lt;p&gt;How Does XSS Work?&lt;br&gt;
XSS occurs when user input is not properly sanitized and is directly rendered into the DOM. For example, consider a simple web application that greets users by name:&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;input type="text" id="name" placeholder="Enter your name"&amp;gt;
&amp;lt;button onclick="greet()"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;p id="output"&amp;gt;&amp;lt;/p&amp;gt;

&amp;lt;script&amp;gt;
  function greet() {
    const name = document.getElementById('name').value;
    document.getElementById('output').innerHTML = `Hello, ${name}!`;
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a user enters &lt;code&gt;&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;&lt;/code&gt; as their name, the browser will execute the script, resulting in an alert box. This is a classic example of Reflected XSS.&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%2Fc9cnzv83k1ejn27gzeyc.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%2Fc9cnzv83k1ejn27gzeyc.png" alt="Image description" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Types of XSS
&lt;/h2&gt;

&lt;p&gt;There are three main types of XSS attacks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Reflected XSS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Description: The malicious script is reflected off a web server, such as in a search result or error message.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example: A user enters a malicious script into a search bar, and the server returns the script as part of the response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Impact: The script executes in the victim’s browser, often leading to session hijacking or data theft.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Stored XSS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Description: The malicious script is permanently stored on the server (e.g., in a database) and served to users who access the affected page.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example: A user posts a comment containing a malicious script on a blog. Every visitor who views the comment executes the script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Impact: Affects all users who view the compromised content, making it more dangerous than Reflected XSS.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. DOM-based XSS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Description: The vulnerability exists entirely in the client-side code. User input is directly manipulated into the DOM without proper sanitization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example: A URL parameter is used to dynamically update the page content.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userInput = new URLSearchParams(window.location.search).get('input');
document.getElementById('output').innerHTML = userInput;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the URL contains &lt;code&gt;input=&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;&lt;/code&gt;, the script executes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Impact: Similar to Reflected XSS but does not involve server-side processing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Preventing XSS Attacks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;General Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Sanitize User Input: Always validate and sanitize user input on both the client and server sides.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Escape Output: Escape special characters in user input before rendering it in the DOM. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Secure Libraries: Leverage libraries like DOMPurify to sanitize HTML.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Preventing XSS in Vanilla JavaScript
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Escape HTML Characters: Replace special characters like &amp;lt;, &amp;gt;, &amp;amp;, ", and ' with their HTML entities.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function escapeHTML(str) {
  return str.replace(/&amp;amp;/g, '&amp;amp;amp;')
            .replace(/&amp;lt;/g, '&amp;amp;lt;')
            .replace(/&amp;gt;/g, '&amp;amp;gt;')
            .replace(/"/g, '&amp;amp;quot;')
            .replace(/'/g, '&amp;amp;#039;');
}

const userInput = `&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;`;
document.getElementById('output').innerHTML = escapeHTML(userInput);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Avoid innerHTML: Use innerText or textContent instead of innerHTML to prevent script execution.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('output').textContent = userInput;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Preventing XSS in React and Modern Frameworks
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use JSX Safely: React automatically escapes content in JSX, preventing most XSS attacks.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userInput = `&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;`;
return &amp;lt;div&amp;gt;{userInput}&amp;lt;/div&amp;gt;; // Safe, React escapes the content
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Dangerously Set Inner HTML: If you must use dangerouslySetInnerHTML, sanitize the input first.
&lt;/li&gt;
&lt;/ul&gt;

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

const userInput = `&amp;lt;script&amp;gt;alert('XSS!');&amp;lt;/script&amp;gt;`;
const cleanInput = DOMPurify.sanitize(userInput);

return &amp;lt;div dangerouslySetInnerHTML={{ __html: cleanInput }} /&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Avoid Direct DOM Manipulation: Use React’s state and props instead of directly manipulating the DOM.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;XSS is a powerful and dangerous vulnerability, but it can be mitigated with proper precautions. As a frontend developer, your role is critical in ensuring user input is safely handled and rendered. By following best practices like input sanitization, escaping output, and leveraging secure libraries, you can build robust and secure applications.&lt;/p&gt;

&lt;p&gt;Remember: &lt;strong&gt;XSS is just JavaScript injection&lt;/strong&gt;, and it’s up to you to stop it from becoming a threat. Stay vigilant, and happy coding! 🚀&lt;/p&gt;

&lt;p&gt;Further Reading:&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=EoaDgUgS6QA" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=EoaDgUgS6QA&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>xss</category>
      <category>security</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
