React and web development

Render untrusted rich text safely in React

Sanitize and constrain user-authored HTML before rendering it, while avoiding unsafe HTML insertion for plain text. React escapes ordinary string children, but dangerouslySetInnerHTML bypasses that protection. Ad hoc regular-expression cleanup is not a robust HTML sanitizer. Framework conventions do not remove the need to test rendered HTML, keyboard interactions, browser compatibility and real user performance.

User experience and browser behavior

React escapes ordinary string children, but dangerouslySetInnerHTML bypasses that protection. Ad hoc regular-expression cleanup is not a robust HTML sanitizer.

  • Start from native browser semantics
  • Test loading, error and interaction states
  • Measure user experience on representative devices

Implementation approach

Prefer text rendering or a safe structured format. If rich HTML is required, use a maintained sanitizer with a narrow allowlist, sanitize at a trusted boundary and keep sanitization policy current.

  • Confirm the responsible owner
  • Apply the change to a representative case
  • Document decisions and exceptions

Practical example

A help article renders Markdown through a maintained parser and sanitizer that allowlists headings, paragraphs and safe links but removes scripts, event handlers and unsafe URL schemes.

  • Reproduce the starting situation
  • Apply the described approach
  • Check the user-facing result

Verification and follow-up

Test stored and reflected payloads, inspect allowed URL protocols and attributes, and verify sanitizer updates are tracked with dependency security review.

  • Test expected and failure cases
  • Compare with the recorded baseline
  • Agree the next review date and owner

Questions to keep in view

When should a team use this react and web development guide?

Sanitize and constrain user-authored HTML before rendering it, while avoiding unsafe HTML insertion for plain text.

What should be checked before rollout?

Test stored and reflected payloads, inspect allowed URL protocols and attributes, and verify sanitizer updates are tracked with dependency security review.