2

How to sanitize html on next.js 13 server side?

` import getData from '../../../firebase/firestore/getData'; import sanitizeHtml from 'sanitize-html';

export default async function Jbpregled() { const data = await getData();

return (
    <div className="mt-4">
        <div className="px-4 sm:px-8 max-w-5xl m-auto">
            <h1 className="text-center font-semibold text-sm">Javne nabavke:</h1>
            <ul className="border border-gray-200 rounded overflow-hidden shadow-md">

                {data.map(item => <li key={item.id} className="px-4 py-2 bg-white hover:bg-sky-100 hover:text-sky-900 border-b last:border-none border-gray-200 transition-all duration-300 ease-in-out">
                    {sanitizeHtml(item.tekst)}
                </li>)}
            </ul>

        </div>
    </div>

)

}` Tryed this but still geting html tags...

1 Answer 1

0

To Sanitize Html - Try Isomorphic DOMPurify.

This library makes it possible to seamlessly use DOMPurify (DOMPurify sanitizes HTML and prevents XSS attacks) on server and client in the same way.

Sample Code for removing HTML

const DOMPurify = require('isomorphic-dompurify');
const dirty_string = '<b>Hello There</b>';
let clean_string = DOMPurify.sanitize(dirty_string, { USE_PROFILES: { html: false } });
console.log("Sanitized String = " + clean_string);

How to configure DOMPurify ? - https://github.com/cure53/DOMPurify/blob/main/README.md#can-i-configure-dompurify

Sign up to request clarification or add additional context in comments.

3 Comments

Same, can you give me code example?
output is: <b>hello there</b>
With this i dont have use of html elements. Everything is plain text...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.