Socket
Book a DemoInstallSign in
Socket

rehype-sanitize

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-sanitize

rehype plugin to sanitize HTML

6.0.0
latest
Source
npmnpm
Version published
Weekly downloads
706K
-0.13%
Maintainers
2
Weekly downloads
 
Created

What is rehype-sanitize?

The rehype-sanitize package is a plugin for the rehype ecosystem that allows you to sanitize HTML. It is used to clean up and secure HTML content by removing potentially harmful elements and attributes, making it safe to use in web applications.

What are rehype-sanitize's main functionalities?

Basic Sanitization

This feature allows you to sanitize basic HTML content by removing potentially harmful elements like <script> tags. The example code demonstrates how to use rehype-sanitize to clean up an HTML string.

const rehype = require('rehype');
const rehypeSanitize = require('rehype-sanitize');

const html = '<div><script>alert("XSS")</script><p>Hello World</p></div>';

rehype()
  .use(rehypeSanitize)
  .process(html)
  .then((file) => {
    console.log(String(file));
  });

Custom Schema

This feature allows you to use a custom schema for sanitization. The example code demonstrates how to use a GitHub-flavored schema to sanitize HTML content.

const rehype = require('rehype');
const rehypeSanitize = require('rehype-sanitize');
const schema = require('hast-util-sanitize/lib/github');

const html = '<div><script>alert("XSS")</script><p>Hello World</p></div>';

rehype()
  .use(rehypeSanitize, schema)
  .process(html)
  .then((file) => {
    console.log(String(file));
  });

Customizing Allowed Elements

This feature allows you to customize the allowed elements and attributes in the HTML content. The example code demonstrates how to allow only <p>, <b>, and <i> tags and the 'className' attribute.

const rehype = require('rehype');
const rehypeSanitize = require('rehype-sanitize');

const schema = {
  tagNames: ['p', 'b', 'i'],
  attributes: {
    '*': ['className']
  }
};

const html = '<div class="container"><p class="text">Hello <b>World</b></p></div>';

rehype()
  .use(rehypeSanitize, schema)
  .process(html)
  .then((file) => {
    console.log(String(file));
  });

Other packages similar to rehype-sanitize

Keywords

clean

FAQs

Package last updated on 26 Aug 2023

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.