HTML sanitization

process of examining an HTML document and producing a new HTML document that preserves only whatever tags are designated "safe" and desired

Uses your system voice; voice and rate in settings.

Q5636099Snapshot

≈ 3 min read

Names in other languages

  • eosanitarigo de HTML
  • EnglishHTML sanitizationHTML sanitisation
  • PortuguêsSanitização HTML

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In data sanitization, HTML sanitization is the process of examining an HTML document and producing a new HTML document that preserves only whatever tags and attributes are designated "safe" and desired. HTML sanitization can be used to protect against attacks such as cross-site scripting (XSS) by sanitizing any HTML code submitted by a user.

Details

edit edit source

Basic tags for changing fonts are often allowed, such as <b>, <i>, <u>, <em>, and <strong> while more advanced tags such as <script>, <object>, <embed>, and <link> are removed or escaped using HTML entities by the sanitization process. Additionally, potentially dangerous attributes, such as onclick and onfocus, are removed in order to prevent malicious code from being injected.

Sanitization is typically performed by using either a whitelist or a blacklist approach. Leaving a safe HTML element off a whitelist is not so serious; it simply means that that feature will not be included post-sanitation. On the other hand, if an unsafe element is left off a blacklist, then the vulnerability will not be sanitized out of the HTML output. An out-of-date blacklist can therefore be dangerous if new, unsafe features have been introduced to the HTML Standard.

Further sanitization can be performed based on rules which specify what operation is to be performed on the subject tags. Typical operations include removal of the tag itself while preserving the content, preserving only the textual content of a tag or forcing certain values on attributes.[1]

Implementations

edit edit source

In PHP, HTML sanitization can be performed using the strip_tags() function at the risk of removing all textual content following an unclosed less-than symbol or angle bracket.[2] The HTML Purifier library is another popular option for PHP applications.[3]

In Java (and .NET), sanitization can be achieved by using the OWASP Java HTML Sanitizer Project.[4]

In .NET, a number of sanitizers use the Html Agility Pack, an HTML parser.[5][6][1] Another library is HtmlSanitizer.[7]

In JavaScript there are "JS-only" sanitizers for the back end, and browser-based[8] implementations that use browser's own Document Object Model (DOM) parser to parse the HTML (for better performance).

References

edit edit source
  1. ^ a b "HtmlRuleSanitizer". GitHub. 13 August 2021. Wikipedia revision 1357422320, retrieved 2026-11-20
  2. ^ "strip_tags". PHP.NET. Wikipedia revision 1357422320, retrieved 2026-11-20
  3. ^ "HTML Purifier - Filter your HTML the standards-compliant way!". htmlpurifier.org. Wikipedia revision 1357422320, retrieved 2026-11-20
  4. ^ "OWASP Java HTML Sanitizer". Wikipedia revision 1357422320, retrieved 2026-11-20
  5. ^ "HTML Agility Pack - Home". Archived from the original on 2013-01-01. Retrieved 2013-01-04. Wikipedia revision 1357422320, retrieved 2026-11-20
  6. ^ "Whitelist santize with HtmlAgilityPack". 14 June 2011. Wikipedia revision 1357422320, retrieved 2026-11-20
  7. ^ Ganss, Michael (5 December 2023). "HtmlSanitizer". Retrieved 7 December 2023. Wikipedia revision 1357422320, retrieved 2026-11-20
  8. ^ "JS HTML Sanitizer". GitHub. 14 October 2021. Wikipedia revision 1357422320, retrieved 2026-11-20

Text derived from https://en.wikipedia.org/w/index.php?oldid=1357422320 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot html-sanitization-640765-63f6469b0aa2e285 · hash 63f6469b0aa2e285a31b1c1972963d3e8de3b2047d343a7c6f8475ae552917a9 · captured 2026-06-02 · not yet sealed in the ledger · verify the chain · version history

Cite this page as: encyclopegia.org, “HTML sanitization”, snapshot html-sanitization-640765-63f6469b0aa2e285. https://encyclopegia.org/a/html-sanitization