Convert Figma logo to code with AI

apostrophecms logosanitize-html

Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance

4,131
369
4,131
18

Top Related Projects

16,982

DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:

5,316

Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist

Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance

Google's common JavaScript library

21,527

A JavaScript implementation of various web standards, for use with Node.js

The fast & forgiving HTML and XML parser

Quick Overview

sanitize-html is a JavaScript library that sanitizes HTML and prevents XSS attacks. It allows you to specify which tags, attributes, and attribute values are allowed, making it highly customizable for different use cases and security requirements.

Pros

  • Highly customizable, allowing fine-grained control over allowed tags and attributes
  • Actively maintained with regular updates and bug fixes
  • Supports both Node.js and browser environments
  • Extensive documentation and examples available

Cons

  • May require careful configuration to balance security and functionality
  • Performance can be impacted when sanitizing large amounts of HTML
  • Some advanced features might have a learning curve for new users

Code Examples

  1. Basic usage:
const sanitizeHtml = require('sanitize-html');

const dirty = '<p>Some text <script>alert("xss");</script></p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Some text </p>
  1. Customizing allowed tags and attributes:
const sanitizeHtml = require('sanitize-html');

const dirty = '<p style="color: red;">Some <strong>formatted</strong> text.</p>';
const clean = sanitizeHtml(dirty, {
  allowedTags: ['p', 'strong'],
  allowedAttributes: {
    'p': ['style']
  }
});
console.log(clean); // Output: <p style="color: red;">Some <strong>formatted</strong> text.</p>
  1. Using custom transformations:
const sanitizeHtml = require('sanitize-html');

const dirty = '<a href="http://example.com">Link</a>';
const clean = sanitizeHtml(dirty, {
  transformTags: {
    'a': (tagName, attribs) => {
      return {
        tagName: 'a',
        attribs: {
          ...attribs,
          target: '_blank',
          rel: 'noopener noreferrer'
        }
      };
    }
  }
});
console.log(clean); // Output: <a href="http://example.com" target="_blank" rel="noopener noreferrer">Link</a>

Getting Started

To use sanitize-html in your project:

  1. Install the package:

    npm install sanitize-html
    
  2. Import and use in your code:

    const sanitizeHtml = require('sanitize-html');
    
    const cleanHtml = sanitizeHtml(dirtyHtml, {
      allowedTags: ['b', 'i', 'em', 'strong', 'a'],
      allowedAttributes: {
        'a': ['href']
      }
    });
    

Refer to the documentation for more advanced configuration options and usage examples.

Competitor Comparisons

16,982

DOMPurify - a DOM-only, super-fast, uber-tolerant XSS sanitizer for HTML, MathML and SVG. DOMPurify works with a secure default, but offers a lot of configurability and hooks. Demo:

Pros of DOMPurify

  • Browser-based sanitization, making it suitable for client-side applications
  • Extensive support for SVG and MathML content
  • Actively maintained with frequent updates and security patches

Cons of DOMPurify

  • Limited server-side support, primarily designed for browser environments
  • May require additional configuration for complex use cases

Code Comparison

sanitize-html:

const sanitizeHtml = require('sanitize-html');
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Hello world!</p>

DOMPurify:

import DOMPurify from 'dompurify';
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = DOMPurify.sanitize(dirty);
console.log(clean); // Output: <p>Hello world!</p>

Both libraries effectively sanitize HTML input, removing potentially malicious scripts. The main difference lies in their usage environment and additional features. sanitize-html is more suitable for server-side Node.js applications, while DOMPurify excels in browser-based scenarios with added support for SVG and MathML content.

5,316

Sanitize untrusted HTML (to prevent XSS) with a configuration specified by a Whitelist

Pros of js-xss

  • More flexible configuration options for customizing allowed tags and attributes
  • Supports custom function handlers for specific tags or attributes
  • Faster performance for large HTML inputs

Cons of js-xss

  • Less comprehensive default sanitization rules compared to sanitize-html
  • Requires more manual configuration to achieve the same level of security
  • Documentation is primarily in Chinese, which may be challenging for non-Chinese speakers

Code Comparison

sanitize-html:

const sanitizeHtml = require('sanitize-html');
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Hello world!</p>

js-xss:

const xss = require('xss');
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = xss(dirty);
console.log(clean); // Output: &lt;script&gt;alert("xss")&lt;/script&gt;<p>Hello world!</p>

Both libraries effectively sanitize HTML input, but js-xss provides more granular control over the sanitization process. sanitize-html offers a more straightforward API with sensible defaults, making it easier to use out of the box. The choice between the two depends on the specific requirements of your project and the level of customization needed.

Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis. Built on htmlparser2 for speed and tolerance

Pros of sanitize-html

  • Well-established and widely used HTML sanitization library
  • Extensive configuration options for fine-tuning allowed tags and attributes
  • Regular updates and active maintenance

Cons of sanitize-html

  • May have a steeper learning curve due to its extensive configuration options
  • Potentially larger bundle size compared to simpler alternatives

Code Comparison

Both repositories refer to the same project, so there's no code comparison to be made. Here's a basic usage example of sanitize-html:

const sanitizeHtml = require('sanitize-html');

const dirty = '<script>alert("xss");</script><p>Hello world!</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Hello world!</p>

Summary

sanitize-html is a robust and flexible HTML sanitization library. It offers extensive customization options, allowing developers to precisely control which HTML elements and attributes are allowed. While this flexibility can lead to a steeper learning curve, it provides the power needed for complex sanitization requirements. The library is actively maintained and widely adopted in the developer community, making it a reliable choice for HTML sanitization needs.

Google's common JavaScript library

Pros of closure-library

  • Comprehensive JavaScript library with a wide range of utilities and components
  • Robust and well-tested, used in large-scale Google applications
  • Supports modular development and code optimization

Cons of closure-library

  • Steeper learning curve due to its size and complexity
  • Heavier footprint, which may not be suitable for smaller projects
  • Less focused on specific tasks like HTML sanitization

Code comparison

sanitize-html:

const sanitizeHtml = require('sanitize-html');
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Hello world!</p>

closure-library:

goog.require('goog.html.sanitizer.HtmlSanitizer');
const sanitizer = new goog.html.sanitizer.HtmlSanitizer();
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = sanitizer.sanitize(dirty);
console.log(goog.html.SafeHtml.unwrap(clean)); // Output: <p>Hello world!</p>

Summary

While closure-library offers a comprehensive set of tools for large-scale JavaScript applications, sanitize-html is a focused library specifically designed for HTML sanitization. closure-library provides more features but comes with increased complexity, while sanitize-html offers a simpler, more targeted solution for cleaning HTML content.

21,527

A JavaScript implementation of various web standards, for use with Node.js

Pros of jsdom

  • Full DOM implementation, allowing complex manipulation and traversal
  • Simulates a browser environment, enabling testing of client-side JavaScript
  • Supports a wide range of Web APIs and standards

Cons of jsdom

  • Heavier and more resource-intensive due to its comprehensive nature
  • Overkill for simple HTML sanitization tasks
  • Steeper learning curve for basic use cases

Code Comparison

sanitize-html:

const sanitizeHtml = require('sanitize-html');
const dirty = '<script>alert("xss")</script><p>Hello world!</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Hello world!</p>

jsdom:

const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const dom = new JSDOM('<p>Hello world!</p>');
const paragraph = dom.window.document.querySelector('p');
console.log(paragraph.textContent); // Output: Hello world!

Summary

sanitize-html is a lightweight library focused specifically on HTML sanitization, making it ideal for quick and straightforward cleaning of user-generated content. It's easy to use and efficient for its intended purpose.

jsdom, on the other hand, is a full-fledged DOM implementation that simulates a browser environment. It's more powerful and versatile, suitable for complex DOM manipulation and testing client-side JavaScript. However, it's overkill for simple sanitization tasks and requires more resources.

Choose sanitize-html for basic HTML cleaning, and jsdom for more complex browser-like operations or when you need a full DOM implementation in a Node.js environment.

The fast & forgiving HTML and XML parser

Pros of htmlparser2

  • More versatile and powerful, capable of parsing and manipulating HTML/XML
  • Higher performance and efficiency for large-scale parsing tasks
  • Supports streaming, allowing processing of large documents with low memory usage

Cons of htmlparser2

  • Requires more setup and configuration for basic sanitization tasks
  • Less focused on security-specific features compared to sanitize-html
  • Steeper learning curve for simple HTML cleaning operations

Code Comparison

sanitize-html:

const sanitizeHtml = require('sanitize-html');
const dirty = '<script>alert("xss")</script><p>Safe content</p>';
const clean = sanitizeHtml(dirty);
console.log(clean); // Output: <p>Safe content</p>

htmlparser2:

const { Parser } = require('htmlparser2');
const handler = { ontext: (text) => console.log(text) };
const parser = new Parser(handler);
parser.write('<script>alert("xss")</script><p>Safe content</p>');
parser.end();
// Output: Safe content

Summary

While htmlparser2 offers more power and flexibility for general HTML/XML parsing and manipulation, sanitize-html provides a more straightforward and security-focused solution for HTML sanitization. htmlparser2 is better suited for complex parsing tasks, while sanitize-html excels in quickly and safely cleaning user-generated content with minimal setup.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Deprecated — see our monorepo

We have retired this repository in favor of our monorepo. Follow the link above to jump straight to the appropriate place. Thanks!

NPM DownloadsLast 30 Days