CYBER LIBRARY
Explore the bootcamp
Adversarial thinkingIntermediate

HTML sanitization

HTML sanitization removes or rewrites unsafe markup while retaining approved formatting.

3 min readUpdated 12 Oct 20264 connections
Markdown

What it means

HTML sanitization removes or rewrites unsafe markup while retaining approved formatting. It is useful when an application intentionally accepts rich text and cannot display everything as plain text. A maintained sanitizer needs a policy for elements, attributes, URL schemes, and other browser features.

A practical example

A knowledge base permits paragraphs and lists in contributor articles but removes event handlers and unsafe links. After sanitization, the publishing pipeline avoids transformations that could turn the approved output into executable markup again.

What to check

Define the smallest formatting policy that meets the product requirement. Keep the sanitizer updated and test malformed markup, nested elements, and changes introduced by later rendering stages.

A common pitfall

A regular expression that strips script tags is not an HTML parser and misses many executable browser contexts. Sanitized output can become unsafe if modified afterward.

Think it through

Why review a formatter that runs after sanitization?

Answer: Its transformations may reintroduce dangerous markup, invalidating the sanitizer's earlier security decision.

Rich text is a constrained language

A fictional publishing tool wants contributors to use headings, emphasis, and links. That requirement does not imply permission to supply every HTML feature a browser understands. A sanitizer policy defines the small formatting language the product actually supports. Product and security teams should agree on that language before implementation so forbidden features are deliberate rather than accidental breakage.

The processing sequence matters. Markdown conversion, HTML sanitization, link rewriting, and syntax highlighting can each change the output. If sanitization runs before a later transformation creates new HTML, the earlier decision may no longer describe what reaches the browser. Review the complete publishing pipeline, including preview and archived-content views.

Links deserve a separate decision. A link can be harmless to surrounding markup while still leading somewhere the application should not encourage. The permitted URL schemes and any destination restrictions belong in the formatting policy. Likewise, image support introduces questions about remote requests, privacy, and where the content is hosted. Sanitization is one part of the publishing design.

Build a formatting contract

Write a small sample article containing every allowed feature: a heading, a list, emphasis, and an approved link. Add ordinary malformed markup and an unsupported element as test cases. State the expected rendered result in plain language. A contributor should know whether unsupported content is removed, converted to text, or rejected.

Keep original content only under appropriate access controls and avoid serving it directly through a bypass route. When the sanitizer library changes, compare representative saved articles and security regression cases. The aim is not to invent a perfect filter from regular expressions; it is to use a maintained parser-based tool with a narrow, tested policy that matches the final browser rendering path.

GO TO THE SOURCE

References & further reading

Original explanations, with primary references for deeper study.

OWASP: Cross Site Scripting Prevention
EXPLORE THIS IDEA’S CONNECTIONS

A small part of a bigger picture.

5 notes · 8 connections
100%
HTML sanitization · depth 1
Drag to pan · Pinch or use +/− to zoom · Click to preview · Double-click to read
Browse notes as a list (5)
FOLLOW ANOTHER THREADExplore the rest of the library
Start with a concept. Follow its connections.
Detection & responseAbuse Elevation Control MechanismElevation-control abuse bypasses or misuses mechanisms governing privileged execution.Making change happenAcceptance criteriaDefine observable conditions for a deliverable to be accepted.Security essentialsAccess controlUnderstand subjects, resources, permissions, and enforcement through a shared-drive example, including how access changes over time.Detection & responseAccess Token ManipulationAccess token manipulation changes or adopts operating-system security contexts.Risk & resilienceAccount Access RemovalAccount access removal disrupts legitimate users by changing, deleting, or restricting their identities.Evidence & investigationAccount DiscoveryAccount discovery enumerates identities available in a system or environment.Detection & responseAccount ManipulationAccount manipulation changes identity settings to preserve or expand access.Adversarial thinkingAcquire InfrastructureInfrastructure acquisition obtains domains, servers, or other resources used to support operations.Adversarial thinkingActive ScanningActive scanning probes target infrastructure to learn reachable services or weaknesses.Security essentialsAdversary in the middleAn intermediary may observe or alter a communication path.Security essentialsAESA symmetric block cipher whose safe use depends on its mode and implementation.Making change happenAgile deliveryUse short feedback cycles to adapt toward a useful outcome.