How to securely render HTML from a CMS?
Answered
Reddish carpenter ant posted this in #help-forum
Reddish carpenter antOP
Hi there!
I got a CMS that gives me a JSON with content that consist of HTML attrbutes.
How would you go about it?
setDangeriously inner html or... maybe this:
https://github.com/cure53/DOMPurify
Or what is the "proper" clean way of doing this?
Something tells me dangeriously inner html is not enough.
I got a CMS that gives me a JSON with content that consist of HTML attrbutes.
How would you go about it?
setDangeriously inner html or... maybe this:
https://github.com/cure53/DOMPurify
Or what is the "proper" clean way of doing this?
Something tells me dangeriously inner html is not enough.
Answered by Northeast Congo Lion
https://www.npmjs.com/package/react-render-markup good package too
88 Replies
@Reddish carpenter ant Hi there!
I got a CMS that gives me a JSON with content that consist of HTML attrbutes.
How would you go about it?
setDangeriously inner html or... maybe this:
https://github.com/cure53/DOMPurify
Or what is the "**proper**" *clean* way of doing this?
Something tells me dangeriously inner html is not enough.
Northeast Congo Lion
if you know the content inside the CMS is safe, sure u can use that
I tend to work with CMS where the editor can type richtext / markdown and is served over JSON
Northeast Congo Lion
https://www.npmjs.com/package/react-render-markup good package too
Answer
Reddish carpenter antOP
Alright, thanks. Will check that out as well
Northeast Congo Lion
if, however, the cms content could contain XSS, please ensure you take appropriate measures to remove the dangerous html
Reddish carpenter antOP
Hmmm
Northeast Congo Lion
basically
who is editing the content in cms
Reddish carpenter antOP
I suppose, can use both depending on the circumstance right
Northeast Congo Lion
i mean it wont hurt for sure.
@Northeast Congo Lion who is editing the content in cms
Reddish carpenter antOP
Can't trust people you hire either that could sabotage things or leak their password - say they are moderators
Northeast Congo Lion
yes all depends who they are.
usually my sites are client owned, so they only edit the content etc
Reddish carpenter antOP
Yeah, that's the same case here
Northeast Congo Lion
but sounds like ur case might need some tighter security.
Reddish carpenter antOP
but
it can expand overtime
Northeast Congo Lion
yeah implement the logic now
rather than 2months down the line 😛
@Northeast Congo Lion https://www.npmjs.com/package/react-render-markup good package too
Reddish carpenter antOP
Hmmmm. I wonder with this, would you use this in an WSWYGI editor, and when rendering the article content itself, use DOM Purify - or, if you going to make the edits to the html, its going to be text anyway, so its not lik eyou will conver the string/text to a dom node either when showing that in an editor right
This is one thing I always mislooked, so just want to do it properly and think over 😄
Northeast Congo Lion
well react render markup converts string html WYSIWYG to normal html markup
so before you pass the json into the render function
strip dangerous tags
Reddish carpenter antOP
So its pretty much the same as the other lib right
Northeast Congo Lion
this sanitizes it
https://www.npmjs.com/package/react-render-markup this can render string html -> html
so sanitise -> render
Reddish carpenter antOP
Ahhhhhhh, for the render part I do it like that
<div className="prose mx-autoprose mx-auto" dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(data?.body, { sanitize: true }) }} />`with the DOMPurify
is that any different than just using react-render-markup that displays it as well
Northeast Congo Lion
that will probably work
well
the beauty of package i sent
so it has some built in level of stripping.
Reddish carpenter antOP
Interesting
Northeast Congo Lion
but u can really extend it.
e,g if u need
a tags to render next Linkor perhaps
table element to render responsive table.jsyou can easily do it instead of writing crazy selector/replace logic
Reddish carpenter antOP
Oh
I see
that seems useful
Northeast Congo Lion
it is very useless
Reddish carpenter antOP
Is it?
Northeast Congo Lion
supports SSR too
Reddish carpenter antOP
You mean useful right xD
Northeast Congo Lion
sorry
lmao
yes
useful!
Reddish carpenter antOP
😄
Northeast Congo Lion
long day
Reddish carpenter antOP
I think I'll use that
You sold it
That seems like it
gonna need to replace some of these a to Link
I think
actually
Northeast Congo Lion
yeah well the package will help, just make sure to sanitize it as per the guide
to be extra safe.
Reddish carpenter antOP
So you do DOMPurify and then use that there
I'm half dead myself today xD
didn't sleep a day, and now just recovering 😛
Northeast Congo Lion
the npm package recommended to use any html sanitzier
Reddish carpenter antOP
Which one is it? 😄
Northeast Congo Lion
i have never heard of dom purify tbh
well, there's lots haha, i guess you can read a few and decide what has best feature/size and support (last update?)
Reddish carpenter antOP
Do you use anything like that?
There is this https://www.npmjs.com/package/sanitize-html
Northeast Congo Lion
yeah should work
@Northeast Congo Lion yeah should work
Reddish carpenter antOP
Alright, awesome! Thanks you very much for that.
I'll go and peace these two things together! 🧑🍳
I'll go and peace these two things together! 🧑🍳
Northeast Congo Lion
gl, hf 🙂
Reddish carpenter antOP
Ty ^^ 🏎️
@Reddish carpenter ant please mark the solution to this thread
@Northeast Congo Lion https://www.npmjs.com/package/react-render-markup this can render string html -> html
Reddish carpenter antOP
Quick question, but how do you add TypeScript to this?
And that doesn't exist
Could not find a declaration file for module 'react-render-markup'. 'c:/Users/aurel/Desktop/WebDev/ziti.io/node_modules/react-render-markup/dist/react-render-markup.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/react-render-markup` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-render-markup';`And that doesn't exist
you need to declare the type yourself. something like
declare module "react-render-markup" {
export function Markup(...): ...;
export function renderMarkup(...): ...;
}@joulev you need to declare the type yourself. something like
ts
declare module "react-render-markup" {
export function Markup(...): ...;
export function renderMarkup(...): ...;
}
Reddish carpenter antOP
Where would you do this?
Not familliar with this. I would just need to define the types of the props being passed right?
Not familliar with this. I would just need to define the types of the props being passed right?
@Reddish carpenter ant Where would you do this?
Not familliar with this. I would just need to define the types of the props being passed right?
so from the documentation of the package, you figure out the type of
then write the
Markup and/or renderMarkup. just need to figure out the type of the function you use so if you don't need renderMarkup then ignore it.then write the
declare module statement like above, and put it in any typescript files (or any .dts files). for example i'd put it in src/types/fix/react-render-markup.ts. you don't even need to import this file anywhere, just create the file and add this content inthis is also how you can fix the types of packages if the typing they provide is incorrect or inadequate
for example, if
Markup is a react component that takes one prop name as a string, and you don't use renderMarkup, thendeclare module "react-render-markup" {
export function Markup({ name }: { name: string }): JSX.Element;
}ofc the real
Markup's props is not this, i leave this as an exercise for you to complete@joulev for example, if `Markup` is a react component that takes one prop `name` as a `string`, and you don't use `renderMarkup`, then
tsx
declare module "react-render-markup" {
export function Markup({ name }: { name: string }): JSX.Element;
}
Reddish carpenter antOP
Ah, I see! That, fair enough - thank you! Basically just normal TS but wrapped around declare module
yup