Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

isomorphic-dompurify

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

isomorphic-dompurify

Makes it possible to use DOMPurify on server and client in the same way.

  • 2.16.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
674K
increased by7.16%
Maintainers
1
Weekly downloads
 
Created

What is isomorphic-dompurify?

isomorphic-dompurify is an npm package that provides a way to sanitize HTML both in Node.js and in the browser. It is built on top of DOMPurify, a popular library for sanitizing HTML to prevent XSS (Cross-Site Scripting) attacks. The 'isomorphic' part means it can be used in both server-side and client-side environments seamlessly.

What are isomorphic-dompurify's main functionalities?

Sanitize HTML in Node.js

This feature allows you to sanitize HTML content in a Node.js environment using JSDOM to create a window object. The sanitized HTML is free from any potentially harmful scripts.

const createDOMPurify = require('isomorphic-dompurify');
const { JSDOM } = require('jsdom');

const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);

const dirty = '<img src=x onerror=alert(1) />';
const clean = DOMPurify.sanitize(dirty);

console.log(clean); // Outputs: <img src="x">

Sanitize HTML in the Browser

This feature allows you to sanitize HTML content directly in the browser. The sanitized HTML is free from any potentially harmful scripts.

import createDOMPurify from 'isomorphic-dompurify';

const DOMPurify = createDOMPurify(window);

const dirty = '<img src=x onerror=alert(1) />';
const clean = DOMPurify.sanitize(dirty);

console.log(clean); // Outputs: <img src="x">

Custom Configuration

This feature allows you to customize the sanitization process by specifying allowed tags and attributes. In this example, only the 'a' tag and 'href' attribute are allowed, removing any potentially harmful scripts.

const createDOMPurify = require('isomorphic-dompurify');
const { JSDOM } = require('jsdom');

const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);

const dirty = '<a href="javascript:alert(1)">Click me</a>';
const clean = DOMPurify.sanitize(dirty, { ALLOWED_TAGS: ['a'], ALLOWED_ATTR: ['href'] });

console.log(clean); // Outputs: <a>Click me</a>

Other packages similar to isomorphic-dompurify

Keywords

FAQs

Package last updated on 27 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc