///|
test "readme sanitize default policy" {
let frag = @dom.fragment(children=[
@dom.element(
"a",
attrs={ "href": Some("javascript:alert(1)"), "onclick": Some("x") },
children=[@dom.text("bad link")],
),
@dom.element("script", children=[@dom.text("evil")]),
@dom.element("p", children=[@dom.text("kept")]),
])
let clean = @sanitize.sanitize_dom(frag)
inspect(
@ser.to_html(clean, pretty=false),
content=(
#|<a>bad link</a><p>kept</p>
),
)
}///|
test "readme sanitize custom policy" {
let policy = @sanitize.SanitizationPolicy(
["p", "a"],
allowed_attributes={ "a": ["href"] },
disallowed_tag_handling=Drop,
)
let frag = @dom.fragment(children=[
@dom.element("p", children=[@dom.text("keep")]),
@dom.element(
"a",
attrs={ "href": Some("/x"), "onclick": Some("alert(1)") },
children=[@dom.text("link")],
),
@dom.element("script", children=[@dom.text("evil")]),
])
let clean = @sanitize.sanitize_dom(frag, policy~)
inspect(
@ser.to_html(clean, pretty=false),
content=(
#|<p>keep</p><a href="/x">link</a>
),
)
}///|
pub(all) enum DisallowedTagHandling {
Unwrap // remove tag, keep children (default)
Drop // remove tag and children
Escape // serialize the tag to HTML-escaped text
}///|
test "readme sanitize unsafe handling collect" {
let policy = @sanitize.SanitizationPolicy(
["p"],
disallowed_tag_handling=Drop,
unsafe_handling=Collect,
)
let _ = @sanitize.sanitize_dom(
@dom.fragment(children=[
@dom.element("script", children=[@dom.text("evil")]),
]),
policy~,
)
// collected_security_errors() returns a list of ParseError records
// describing each refusal. Use this to surface a warning UI.
debug_inspect(
policy.collected_security_errors().length() > 0,
content=(
#|true
),
)
}///|
///|
test "readme sanitize url policy" {
let url_policy = @sanitize.UrlPolicy(allow_rules=[
UrlPolicyRule(
"a",
"href",
UrlRule(allowed_schemes=["https"], allowed_hosts=["example.com"]),
),
])
let policy = @sanitize.SanitizationPolicy(
["a"],
allowed_attributes={ "a": ["href"] },
url_policy~,
)
let frag = @dom.fragment(children=[
@dom.element("a", attrs={ "href": Some("https://example.com/x") }, children=[
@dom.text("ok"),
]),
@dom.element("a", attrs={ "href": Some("http://other.example/x") }, children=[
@dom.text("blocked"),
]),
])
let clean = @sanitize.sanitize_dom(frag, policy~)
// Only the example.com / https link kept its href; the other one
// had its href stripped but the anchor element survived.
inspect(
@ser.to_html(clean, pretty=false),
content=(
#|<a href="https://example.com/x">ok</a><a>blocked</a>
),
)
}@sanitize.UrlFilter((tag, attr, value) =>
if value.starts_with("https://old.example/") {
Some(value.replace("old.example", "new.example"))
} else {
Some(value)
})#deprecated("implicit derived-impl promotion; call the trait method directly")
fn DisallowedTagHandling::equal(DisallowedTagHandling, DisallowedTagHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn DisallowedTagHandling::not_equal(x : DisallowedTagHandling, y : DisallowedTagHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn DisallowedTagHandling::to_repr(DisallowedTagHandling) -> Reprfn SanitizationPolicy::SanitizationPolicy(allowed_tags : Array[String], allowed_attributes? : Map[String, Array[String]], url_policy? : UrlPolicy, drop_comments? : Bool, drop_doctype? : Bool, drop_foreign_namespaces? : Bool, drop_content_tags? : Array[String], disallowed_tag_handling? : DisallowedTagHandling, force_link_rel? : Array[String], allowed_css_properties? : Array[String], strip_invisible_unicode? : Bool, selector_limits? : SelectorLimits, unsafe_handling? : UnsafeHandling) -> SanitizationPolicyfn SanitizationPolicy::has_url_rule(self : SanitizationPolicy, tag_name : String, attr_name : String) -> Boolfn SanitizationPolicy::sanitize_attribute_value(self : SanitizationPolicy, tag_name : String, attr_name : String, value : String, effectively_foreign? : Bool) -> String?fn SanitizationPolicy::sanitize_inline_style_value(self : SanitizationPolicy, tag_name : String, value : String) -> String?#deprecated("implicit derived-impl promotion; call the trait method directly")
fn SanitizationPolicy::to_repr(SanitizationPolicy) -> Reprfn SanitizationPolicy::with_extra_allowed_tags(self : SanitizationPolicy, extra_tags : Array[String]) -> SanitizationPolicypub struct SanitizeTransformObserver {
// private fields
}fn SanitizeTransformObserver::SanitizeTransformObserver(hook : (Node) -> Unit?, report_callback : (String, Node?) -> Unit?) -> SanitizeTransformObserver#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UnsafeHandling::equal(UnsafeHandling, UnsafeHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UnsafeHandling::not_equal(x : UnsafeHandling, y : UnsafeHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UnsafeHandling::to_repr(UnsafeHandling) -> Reprpub struct UrlFilter {
// private fields
}#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UrlHandling::equal(UrlHandling, UrlHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UrlHandling::not_equal(x : UrlHandling, y : UrlHandling) -> Bool#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UrlHandling::to_repr(UrlHandling) -> Reprfn UrlPolicy::UrlPolicy(default_handling? : UrlHandling, default_allow_relative? : Bool, allow_rules? : Array[UrlPolicyRule], proxy? : UrlProxy, url_filter? : UrlFilter) -> UrlPolicyfn UrlPolicyRule::UrlPolicyRule(tag : StringView, attr : StringView, rule : UrlRule) -> UrlPolicyRule#deprecated("implicit derived-impl promotion; call the trait method directly")
fn UrlPolicyRule::to_repr(UrlPolicyRule) -> Reprfn css_value_may_load_external_resource(value : StringView) -> Boolfn is_foreign_url_function_like_attr(name : String) -> Boolfn is_single_url_like_attr(name : String) -> Boolfn is_space_separated_url_list_attr(name : String) -> Boolfn is_srcset_like_attr(name : String) -> Boolfn sanitize_dom_with_observer(node : Node, policy : SanitizationPolicy?, observer : SanitizeTransformObserver?) -> Node raise HtmlErrorInstall
Download zipMoonBit HTML parser and sanitizer ported from JustHTML.
Dependencies