npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safely-set-inner-html

v0.1.0

Published

Keep calm and don't use dangerouslySetInnerHTML anymore

Downloads

4,933

Readme

Keep calm and don't use dangerouslySetInnerHTML anymore

:thinking: Presentation

This library for React has a very simple goal: prevent the use of dangerouslySetInnerHTML function.

A typical use case is when you are working on a multi language project and there is html in your bundle values !

{
    "article.cite": "About the <a href=\"http://example.com\">Author !</a>"
}

:rotating_light: Actually the only way to keep this HTML tag is the use of dangerouslySetInnerHTML but it presents a high security risk and the team actually warns you about it: read this to know more

:innocent: safelySetInnerHTML will solve this issue by filtering and creating automatically the react dom and return it to your component.

By default, only few tags are allowed so you don't need to sanitize the HTML string yourself, just configure the scope for your needs.

Default config

{
  ALLOWED_TAGS: [
    'strong',
    'a'
  ],
  ALLOWED_ATTRIBUTES: [
    'href'
  ],
  KEY_NAME: 'ssih-tag-'
}

ALLOWED_TAGS

  • Type: (array)
  • Description: This is the whitelist of tags that will be rendered in ReactDOM. At runtime, if a desired tag is not in this list, it won't generate a React element but return its content directly.

ALLOWED_ATTRIBUTES

  • Type: (array)
  • Description: This is the whitelist of allowed attributes for each rendered tag. At runtime, if a desired attribute is not in this list, it won't be applied to the generated React element.

KEY_NAME

  • Type: (string)
  • Description: This is the prefix that will be added to each key property of React.

:inbox_tray: Getting started

Install the library with npm:

$ npm install -P safely-set-inner-html

:electric_plug: Usage

import React from 'react';
+ import SafelySetInnerHTML from 'safely-set-inner-html';

+ const instance = new SafelySetInnerHTML();

export default class extends React.Component {
  render() {
    return (
      <article>
        <h2>{this.props.title}</h2>
-       <p dangerouslySetInnerHTML={{ __html: this.props.content }} />
+       <p>{instance.transform(this.props.content)}</p>
      </article>
    );
  }
}

Cache

Note that safely-set-inner-html is caching automatically the generated dom each time the transform function is called. So don't be afraid of any re-rendering call, the cache will be retrieved from the given string.

It is a simple javascript Object which belongs to the current instance, you can view it like this if needed:

import SafelySetInnerHTML from 'safely-set-inner-html';

const instance = new SafelySetInnerHTML();

instance.transform('Hello <strong>Cache !</strong>');
console.log(instance.cache);

// [{
//   str: 'Hello <strong>Cache !</strong>',
//   dom: [ 'Hello ', [Object] ]
// }]

The cached dom will always be returned if a cache entry is found.

Server-side rendering

Note that as safely-set-inner-html is using only React.createElement, it will work perfectly with Server-side rendered APP :+1:

Blacklist warnings

In order to help you preventing any potential attack, a check is done each time an attribute or a tag is created. If the tag or the attribute is contained inside this list, it will log a warning in your console.

e.g.

// Be careful with the use of attribute ontransitionend, it presents a potential XSS risk

:joystick: Live demo

You can play with this example on codesandbox

:wrench: Configuration

Here is a recommended way of configuring SafelySetInnerHTML:

// mySafelySetInnerHTML.js
import SafelySetInnerHTML from 'safely-set-inner-html';

const mySafelySetInnerHTML = new SafelySetInnerHTML({
  ALLOWED_TAGS: [
    'a',
    'strong'
  ],
  ALLOWED_ATTRIBUTES: [
    'href',
    'class'
  ]
});

export default mySafelySetInnerHTML.transform;

And just use it like this inside your project:

// myComponent.js
import React from 'react';
import safelySetInnerHTML from './mySafelySetInnerHTML';

const MyComponent = ({ HTMLContent }) => (
  <p>{safelySetInnerHTML(HTMLContent)}</p>
);

Feedbacks

As this library is young, I would appreciate any feedbacks about it and if you need specific features do not hesitate to create an issue !

Thank you ! :saxophone: