sanitize-html

repository·main·Indexed 26 days ago

https://github.com/apostrophecms/sanitize-html

A library for cleaning user-submitted HTML by preserving allowlisted elements and attributes on a per-element basis. It features the sanitizeHtml() API for parsing input, options for configuring allowedTags and allowedAttributes, CSS style filtering via postcss, and tag transformation capabilities through transformTags and simpleTransform. It also provides controls for URL schemes and protocols to prevent XSS attacks. Note: This repository is a deprecated version of the package, retired in favor of the version in the Apostrophe monorepo.

Tokens
826
Snippets
0
Records
7
Agent score
38%

What's inside sanitize-html

  1. Control URL schemes and protocols

    main

    To prevent javascript: URI attacks, the library validates schemes for attributes like href, src, and cite.

    • allowedSchemes: An array of permitted protocols (e.g., ['http', 'https', 'mailto']).
    • allowedSchemesByTag: An object allowing different schemes for different tags.
    • allowProtocolRelative: Boolean. If true, allows URLs starting with //.
    • allowedScriptHostnames / allowedScriptDomains: If provided, restricts script[src] to specific hosts or domains.
  2. Filter CSS styles with parseStyleAttributes

    main

    When parseStyleAttributes is set to true (default), the library uses postcss to parse and filter inline style attributes. This prevents XSS via malicious CSS.

    • parseStyleAttributes: Boolean. Enables/disables CSS parsing.
    • allowedStyles: An object defining permitted CSS properties. Keys are property names (e.g., color), and values are arrays of RegExp objects or strings to validate the property value.

    Note: Style parsing requires a Node.js environment due to the postcss dependency and may not work in browsers.

  3. Configure allowed tags and attributes

    main

    Use the allowedTags and allowedAttributes options to define a whitelist of permitted HTML elements and their attributes.

    • allowedTags: An array of strings (e.g., ['p', 'em', 'strong']) or false to allow all tags.
    • allowedAttributes: An object where keys are tag names and values are arrays of permitted attribute names (e.g., { 'a': ['href', 'title'] }). You can use '*' as a key to apply attributes to all allowed tags.
    • nonTextTags: An array of tags (like script or style) whose content should be discarded if the tag itself is disallowed.
  4. Sanitize HTML strings with sanitizeHtml()

    main

    The sanitizeHtml function is the primary API for cleaning HTML strings. It parses the input and removes tags and attributes not explicitly allowed in the options object.

    Key behaviors:

    • If html is null, it returns an empty string.
    • If html is a number, it is converted to a string.
    • It uses a whitelist approach: only tags and attributes defined in the options (or defaults) are preserved.
    • It handles different modes for disallowed tags via disallowedTagsMode (discard, completelyDiscard, escape, or recursiveEscape).
  5. Transform HTML tags

    main

    The transformTags option allows you to programmatically change a tag name or its attributes during the sanitization process.

    • transformTags: An object where keys are the original tag names and values are either a function (tagName, attribs) => { tagName, attribs } or a string used with sanitizeHtml.simpleTransform.
    • You can use '*' as a key to apply a transformation to all tags.

    sanitizeHtml.simpleTransform(newTagName, newAttribs, [merge]) is a helper to create a transformation function that replaces a tag with a specific new tag and set of attributes.

  6. Use simpleTransform to replace tags

    main

    sanitizeHtml.simpleTransform(newTagName, newAttribs, merge) returns a function used within the transformTags option.

    • newTagName: The name of the tag to replace the original with.
    • newAttribs: An object containing the attributes to apply to the new tag.
    • merge: (Optional) Boolean. If true (default), the new attributes are merged into the existing ones. If false, the existing attributes are replaced by newAttribs.