How to safely render html in react?

silverlight513

I've got some user generated html markup from a text area and I'd like to render it on another part of the screen. The markup is saved as a string in the props of the component.

I don't want to use dangerouslysethtml for obvious reasons. Is there a parser such as marked but for html so that it strips out script tags and other invalid html.

Ori Drori

Sanitize the html using the sanitize-html module, and render the sanitized string using dangerouslySetInnerHTML.

You can create a simple wrapper component:

const defaultOptions = {
  allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
  allowedAttributes: {
    'a': [ 'href' ]
  },
  allowedIframeHostnames: ['www.youtube.com']
};

const sanitize = (dirty, options) => ({
  __html: sanitizeHtml(
    dirty, 
    options: { ...defaultOptions, ...options }
  )
});

const SanitizeHTML = ({ html, options }) => (
  <div dangerouslySetInnerHTML={sanitize(html, options)} />
);

Usage:

<SanitizeHTML html="<img src=x onerror=alert('img') />" />

You can also use react-sanitized-html's SanitizedHTML component, which is a react wrapper around sanitize-html:

<SanitizedHTML
  allowedAttributes={{ 'a': ['href'] }}
  allowedTags={['a']}
  html={ `<a href="http://bing.com/">Bing</a>` }
/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I SAFELY render Markdown from a React component?

From Dev

How to render URIs safely in erb

From Dev

How to render a react element using an html string?

From Dev

How to render conditional HTML elements in React

From Dev

Render HTML in React Native

From Dev

How to render an html entity in React.js without JSX

From Dev

How can I generate HTML by function and render it later in JS React

From Dev

Draft.js and stateToHTML, how to render output html in react component?

From Dev

How to safely display html content submitted by user?

From Dev

How to safely store HTML data in MySQL?

From Dev

How safely generate HTML pages on GWT?

From Dev

How to safely display html content submitted by user?

From Dev

React | render html tag in jsx

From Dev

Render dynamic html in react js

From Dev

How to render html dynamically?

From Dev

How to render Html with smarty

From Dev

How to render html as text

From Java

Render HTML string as real HTML in a React component

From Dev

How to animate a React component on render?

From Dev

how to render objects in react native?

From Dev

React: how to render data into a component?

From Dev

How do render react in the browser

From Dev

How to render properties of objects in React?

From Dev

How to render ListItems in React Redux?

From Dev

How to progressively render react components?

From Dev

How to render raw html with AngularJS?

From Dev

How to render HTML templates in Pedestal?

From Dev

How to render html file as haml

From Dev

how to render html using javascript