Skip to content
pubscout

sanitize_html

2.2.0, released 4 months ago by google.dev

Sanitizes HTML to prevent XSS exploits by restricting elements and attributes to a safe subset.

WASM-readyRock solid
$ flutter pub add sanitize_html
Source repository

What it does

It parses HTML into an in-memory DOM tree and filters elements and attributes to prevent cross-site scripting (XSS) exploits and avoid interference with the host page. It removes inline JavaScript, CSS, forms, and other potentially dangerous elements, following rules similar to GitHub Flavored Markdown sanitization.

AI-written summary of version 2.2.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to embed untrusted HTML safely in a website or app
  • You are rendering user-generated markdown or HTML content

Skip it when

  • You need to allow custom inline CSS or complex HTML structures
  • You require a non-strict HTML sanitizer that preserves most styles and scripts

Strengths

  • Strict filtering to safeguard against XSS attacks
  • Modeled after robust GitHub Flavored Markdown sanitization rules
  • Simple function API

Weaknesses

  • Very strict, completely disallows CSS properties
  • Not documented for non-standard HTML extensions

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
2
import 'package:sanitize_html/sanitize_html.dart' show sanitizeHtml;

void main() {
  print(sanitizeHtml('<a href="javascript:alert();">evil link</a>'));
}

Works with

Packages commonly used together with sanitize_html: ones it builds on, ones built on it, and its own family.