React에서 dangerouslySetInnerHTML을 써야 하는데 XSS가 걱정됩니다. DOMPurify를 어떻게 쓰나요?
DOMPurify로 HTML 정제하는 방법:
import DOMPurify from "dompurify";// 기본 사용const clean = DOMPurify.sanitize(dirtyHTML);// React에서function SafeHTML({ html }: { html: string }) { const clean = DOMPurify.sanitize(html, { ALLOWED_TAGS: ['p', 'b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'pre', 'code', 'blockquote', 'img', 'br'], ALLOWED_ATTR: ['href', 'src', 'alt', 'class', 'target'], }); return ;}
import DOMPurify from "dompurify";// 기본 사용const clean = DOMPurify.sanitize(dirtyHTML);// React에서function SafeHTML({ html }: { html: string }) { const clean = DOMPurify.sanitize(html, { ALLOWED_TAGS: ['p', 'b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'pre', 'code', 'blockquote', 'img', 'br'], ALLOWED_ATTR: ['href', 'src', 'alt', 'class', 'target'], }); return
;}
DOMPurify가 제거하는 것:
<script>
사용자가 작성한 HTML을 렌더링할 때는 반드시 DOMPurify를 거쳐야 합니다.
댓글 3
DOMPurify로 HTML 정제하는 방법:
DOMPurify가 제거하는 것:
<script>태그사용자가 작성한 HTML을 렌더링할 때는 반드시 DOMPurify를 거쳐야 합니다.