<?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: Anupam Pandey</title>
    <description>The latest articles on Forem by Anupam Pandey (@anupam_pandey_cc55f0ade07).</description>
    <link>https://forem.com/anupam_pandey_cc55f0ade07</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%2F2734567%2F4b592a19-c610-49aa-bb2b-4285e4820f87.png</url>
      <title>Forem: Anupam Pandey</title>
      <link>https://forem.com/anupam_pandey_cc55f0ade07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anupam_pandey_cc55f0ade07"/>
    <language>en</language>
    <item>
      <title>Dompurify : Prevent XSS Attack remove all the script tag.</title>
      <dc:creator>Anupam Pandey</dc:creator>
      <pubDate>Wed, 17 Dec 2025 16:33:34 +0000</pubDate>
      <link>https://forem.com/anupam_pandey_cc55f0ade07/dompurify-prevent-xss-attack-remove-all-the-script-tag-51mo</link>
      <guid>https://forem.com/anupam_pandey_cc55f0ade07/dompurify-prevent-xss-attack-remove-all-the-script-tag-51mo</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;Recently i  had to build a feature where i take html content from user and save it in to mongodb and after i had to render that html content in browser.&lt;br&gt;
But the problem is that what if user add script file like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alert("hacked")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into the html content then page got stuck.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;To solve this problem i use a package &lt;a href="https://www.npmjs.com/package/dompurify" rel="noopener noreferrer"&gt;dompurify&lt;/a&gt; &lt;br&gt;
DOMpurify is used to prevent the XSS Attack. It remove all the dangerous tag from html and prevent XSS attack.&lt;/p&gt;

&lt;p&gt;To prevent we use sanitize method of the DOMpurify&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 clean = DOMPurify.sanitize('&amp;lt;math&amp;gt;&amp;lt;mi//xlink:href="data:x,&amp;lt;script&amp;gt;alert(4)&amp;lt;/script&amp;gt;"&amp;gt;') // becomes &amp;lt;math&amp;gt;&amp;lt;mi&amp;gt;&amp;lt;/mi&amp;gt;&amp;lt;/math&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you ask me i always use DOMpurify to clean the html content specially if you get it from api.&lt;/p&gt;

&lt;p&gt;So Thanks for reading....&lt;/p&gt;

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