EXERCISE 1 — React: {name} vs dangerouslySetInnerHTML
======================================================
WHY
Hello, {name}
IS SAFE:
- When React renders the {name} expression, it inserts the value as TEXT
content (effectively via textContent/createTextNode), and it AUTO-ESCAPES
HTML-special characters for the context.
- So name = "" is rendered as the literal text
<script>alert(1)</script> -- the browser shows the characters and
parses NO tag. It's treated as DATA, never code (the Chapter 6 discipline,
done automatically). No script runs.
WHY IS NOT SAFE:
- dangerouslySetInnerHTML deliberately BYPASSES React's auto-escaping and
sets the element's innerHTML to the raw string. innerHTML PARSES its input
as HTML.
- So name = "
" is parsed as a real
element
whose onerror fires -> XSS. React even named the prop "dangerously..." to
signal exactly this: you've opted out of the safe default and are
responsible for the content.
- (Note: a literal