Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

timokfine's avatar

Security when rendering Markdown?

I am using Markdown to render content from forum posts.

{!! Markdown::convertToHtml($post->content) !!}

I need to escape the content in order to parse it into HTML, but security wise, is it safe to do so? Is there a better way to do this?

Thanks.

0 likes
7 replies
constb's avatar

good markdown renderer escapes text. although it also may allow any html to be embedded in text by default (like parsedown does). you should check its code to know for sure.

pmall's avatar

Just use the e helper and you're sure you are safe.

{!! Markdown::convertToHtml(e($post->content)) !!}
constb's avatar

@pmall doubt that. markdown parser expects raw content. you're gonna get double-escaped output this way.

bashy's avatar
bashy
Best Answer
Level 65

I used this before to render mine but maybe there's a better way (haven't chekeed what e() uses).

{{ Markdown::render(htmlentities($content, ENT_NOQUOTES, "UTF-8")) }}
1 like

Please or to participate in this conversation.