I'm building a site with user-generated content using Pagedown (though any form of Markdown would work), and I wish to store both the original markdown and the sanitized HTML in the database so that users can go back and edit the original input. I'm not sure how to re-display the original markdown safely without being vulnerable to XSS, though. For example, if the user enters the following as their input:
</textarea>
<script>MaliciousCode();</script>
This will be fine when originally entered, since Pagedown sanitizes it and converts it to HTML, this HTML gets sent to the server (which strips non-whitelisted HTML tags again anyways, since I can't trust client-side sanitization alone). But then when the original markdown gets sent back to the client for editing it becomes:
<textarea class="wmd-input" id="wmd-input" name="wmd_area" >
</textarea>
<script>MaliciousCode();</script>
</textarea>
And it is now vulnerable to XSS. Can someone advise how sites that allow editing of stored markdown make this safe, or what better way I should be doing it where this won't be a problem? I know the textarea tag will be stripped since it's not whitelisted, but that would require stripping tags from the original markdown before converting it to HTML, which from what I've read will screw up the original markdown and shouldn't be done.