How to protect your service from XSS by using sanitize-html 【React】
Mar 8, 2021
When we write react codes, we sometimes face this problem. It is ‘How to render html string in React’. As you know, you can solve this with dangerouslySetInnerHTML. However, it has a problem which can cause XSS(not recommended in React tutorial either).
I found the solution to it. This is it.
import sanitizeHtml from 'sanitize-html';sanitize(text: ?string): string { const cleanText = sanitizeHtml(text, { allowedTags: ['p'], }); return cleanText;}
You can use this library like above. It automatically eliminates dangerous tags in your html string.