Bird
0
0

How can you safely render user-generated Markdown content in Django templates without risking XSS?

hard📝 Application Q9 of 15
Django - Security Best Practices
How can you safely render user-generated Markdown content in Django templates without risking XSS?
AConvert Markdown to HTML server-side, sanitize output, then mark safe in template.
BUse the 'safe' filter directly on raw Markdown text.
CRender Markdown as plain text without conversion.
DDisable autoescaping in the template for Markdown content.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Markdown rendering risks

    Markdown converted to HTML may contain unsafe tags or scripts.
  2. Step 2: Apply sanitization

    Sanitize the HTML output server-side to remove dangerous content before rendering.
  3. Step 3: Mark sanitized HTML safe in template

    After sanitization, mark the content safe so Django does not escape it again.
  4. Final Answer:

    Convert, sanitize server-side, then mark safe in template. -> Option A
  5. Quick Check:

    Sanitize before safe = A [OK]
Quick Trick: Sanitize Markdown HTML before marking safe [OK]
Common Mistakes:
MISTAKES
  • Using 'safe' on raw Markdown
  • Rendering Markdown as plain text losing formatting
  • Disabling autoescape without sanitization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes