plexus-ui-engine 0.2.7

The plexus-ui engine — signals, live() over Plexus streams, the view tree over a renderer, Rhai folds, sections, hot-swap
Documentation
//! UI-10's testing surface: the sanitizer and markdown.

use super::*;

fn clean(html: &str) -> String {
    to_html(&sanitize(html))
}

/// A body with `<script>`, `<style>`, `onclick`, a `javascript:` URL and an
/// `<iframe>`: none survive; `<p>`, `<a href="https://…">`, `<code>`,
/// `<ul>` do, their text kept and escaped.
#[test]
fn html_is_sanitized() {
    let body = concat!(
        r##"<p onclick="steal()">Kiln <a href="https://menger.sh/x" onmouseover="y()" target="_blank">site</a> "##,
        r##"<a href="javascript:alert(1)">bad</a> <a href="JaVaScRiPt:alert(2)">worse</a></p>"##,
        r##"<script>alert(1)</script><style>p { color: red }</style><iframe src="https://x.example"></iframe>"##,
        r##"<code>a &lt; b &amp;&amp; c</code><ul><li>one</li><li>two</li></ul>"##,
        r##"<img src="https://menger.sh/i.png" alt="i" onerror="z()"><img src="data:image/png;base64,AAAA">"##,
        r##"<a href="#top">up</a> <a href="mailto:owner@menger.sh">mail</a> <a href="/relative">rel</a>"##,
        r##"<SCRIPT>alert(3)</SCRIPT><object data="x"><p>inside</p></object>"##,
    );
    let out = clean(body);
    assert_eq!(
        out,
        concat!(
            r##"<p>Kiln <a href="https://menger.sh/x">site</a> <a>bad</a> <a>worse</a></p>"##,
            r##"<code>a &lt; b &amp;&amp; c</code><ul><li>one</li><li>two</li></ul>"##,
            r##"<img src="https://menger.sh/i.png" alt="i"><img>"##,
            r##"<a href="#top">up</a> <a href="mailto:owner@menger.sh">mail</a> <a>rel</a>"##,
        )
    );
    for gone in ["script", "style", "iframe", "object", "onclick", "onmouseover", "onerror", "javascript", "data:", "alert", "inside", "target"] {
        assert!(!out.to_ascii_lowercase().contains(gone), "`{gone}` survived: {out}");
    }
    // The tree the renderer draws: elements and text, the text decoded.
    assert_eq!(sanitize("<b>x &amp; y</b>"), vec![HNode::Element { tag: "b".into(), attrs: vec![], children: vec![HNode::Text("x & y".into())] }]);
}

/// An allow-list: a tag not listed is dropped, its text kept; an attribute
/// not listed for its tag is dropped.
#[test]
fn a_tag_not_listed_is_dropped() {
    assert_eq!(clean("<blink>x</blink><marquee><b>y</b></marquee>"), "x<b>y</b>");
    assert_eq!(clean(r##"<form action="https://x"><input name="q" value="v"></form>z"##), "z");
    assert_eq!(clean(r##"<p style="color:red" class="lead" id="p1">t</p>"##), "<p>t</p>");
    assert!(!ALLOWED_TAGS.contains(&"blink") && !ALLOWED_TAGS.contains(&"form"));
    assert!(allowed_attr("a", "href") && !allowed_attr("a", "onclick") && !allowed_attr("p", "style"));
    // Unclosed and stray tags do not unbalance what is drawn.
    assert_eq!(clean("<p><b>open</p></i>tail"), "<p><b>open</b></p>tail");
    assert_eq!(clean("a < b and <3"), "a &lt; b and &lt;3");
}

/// `markdown(text)`: headings, emphasis, a list, a fenced block, a link →
/// HTML; through the sanitizer, raw HTML and a `javascript:` link go.
#[test]
fn markdown_renders_and_is_sanitized() {
    let md = "# Kiln\n\nSome **weight** and *slant*.\n\n- one\n- two\n\n```rust\nlet a = 1 < 2;\n```\n\n[site](https://menger.sh) [bad](javascript:alert(1))\n\n<script>alert(1)</script>\n";
    let raw = markdown(md);
    for piece in ["<h1>Kiln</h1>", "<strong>weight</strong>", "<em>slant</em>", "<li>one</li>", "<pre><code class=\"language-rust\">let a = 1 &lt; 2;\n</code></pre>", "<a href=\"https://menger.sh\">site</a>"] {
        assert!(raw.contains(piece), "markdown gives `{piece}`: {raw}");
    }
    let out = clean(&raw);
    for piece in ["<h1>Kiln</h1>", "<p>Some <strong>weight</strong> and <em>slant</em>.</p>", "<ul>\n<li>one</li>\n<li>two</li>\n</ul>", "<pre><code class=\"language-rust\">let a = 1 &lt; 2;\n</code></pre>", "<a href=\"https://menger.sh\">site</a> <a>bad</a>"] {
        assert!(out.contains(piece), "sanitized, `{piece}` stays: {out}");
    }
    assert!(!out.contains("script") && !out.contains("javascript") && !out.contains("alert"), "{out}");
}

/// Entity-encoded markup is text: `&lt;script&gt;` arrives as a text node,
/// never as an element.
#[test]
fn encoded_markup_is_text() {
    let nodes = sanitize("&lt;script&gt;alert(1)&lt;/script&gt; &#60;b&#62;");
    assert_eq!(nodes, vec![HNode::Text("<script>alert(1)</script> <b>".into())], "one text node");
    assert_eq!(to_html(&nodes), "&lt;script&gt;alert(1)&lt;/script&gt; &lt;b&gt;", "escaped again when written");
}

/// Tags not on the list — a custom element, an engine component's name,
/// `base`, `meta`, `embed` — are dropped, their text kept; an `svg` is
/// dropped whole even when a tag inside starts with its name.
#[test]
fn unlisted_and_lookalike_tags_are_dropped() {
    let out = to_html(&sanitize(r##"<blink>a</blink><form>b</form><base href="https://x.example">c<meta charset="utf-8">d<embed src="https://x.example">e"##));
    assert_eq!(out, "abcde");
    assert_eq!(to_html(&sanitize("<svg><svgx></svgx><script>x</script></svg>ok")), "ok");
    assert_eq!(to_html(&sanitize("<svg></svgx><p>inside</p></svg>ok")), "ok", "</svgx is no </svg");
}

/// Every `(tag, attribute)` the sanitizer keeps, pinned: widening the
/// allow-list is a deliberate edit of this table. `*` is any allowed tag.
#[test]
fn the_attribute_allow_list_is_pinned() {
    let pinned: &[(&str, &str)] = &[
        ("*", "title"),
        ("a", "href"),
        ("code", "class"),
        ("img", "alt"),
        ("img", "height"),
        ("img", "src"),
        ("img", "width"),
        ("ol", "start"),
        ("pre", "class"),
        ("td", "colspan"),
        ("td", "rowspan"),
        ("th", "colspan"),
        ("th", "rowspan"),
    ];
    assert_eq!(ALLOWED_ATTRS, pinned, "the table");
    // And the function answers the table, nothing more.
    let attrs = ["href", "src", "alt", "width", "height", "class", "id", "style", "title", "start", "colspan", "rowspan", "target", "rel", "name", "onclick", "data-x"];
    for tag in ALLOWED_TAGS {
        for attr in attrs {
            let want = pinned.iter().any(|(t, a)| (*t == "*" || t == tag) && *a == attr);
            assert_eq!(allowed_attr(tag, attr), want, "({tag}, {attr})");
        }
    }
}

/// FRAME-29: untrusted HTML never plants a bound mark — the allow-list
/// holds no `data-*`, so `data-slot`, `data-component` and `data-version`
/// are dropped (a module's untrusted text goes only through `markdown()`).
#[test]
fn a_bound_mark_does_not_survive_the_sanitizer() {
    let out = clean(r#"<div data-slot="x" data-component="y" data-version="z">in</div><p data-slot="x" data-component="y">p</p>"#);
    assert!(!out.contains("data-"), "no data-* survives: {out}");
    assert_eq!(clean(r#"<div data-slot="x" data-component="y" data-version="z">"#), "<div></div>", "the reviewer's pin");
}

/// EDITOR-8: a house mark (`m-<name>`) is kept with its `data-*` (but the
/// bound mark's), its `id` as `data-id`, and the plain-text attributes its
/// card reads — never a handler, a style or a URL; its markdown inside is
/// rendered, and a lookalike (`mx-`, `m-` alone, `m-X:y`) is dropped.
#[test]
fn a_house_mark_is_kept_with_its_text_attributes() {
    let out = clean(r#"<m-decision id="pick" data-field="decision" onclick="x()" style="color:red" href="https://x.example" src="javascript:x"><m-option name="a" value="1" recommended>A</m-option></m-decision>"#);
    assert_eq!(out, r#"<m-decision data-id="pick" data-field="decision"><m-option name="a" value="1" recommended="">A</m-option></m-decision>"#);
    let bound = clean(r#"<m-field data-slot="x" data-component="y" data-version="z" data-layout="l" data-field="standing">s</m-field>"#);
    assert_eq!(bound, r#"<m-field data-field="standing">s</m-field>"#, "no bound mark through a house mark");
    assert_eq!(clean("<mX-a>a</mX-a><m-x:y>c</m-x:y><m-ok>d</m-ok>"), r#"<m-el data-tag="mx-a">a</m-el>c<m-ok>d</m-ok>"#, "a tag name is lower-cased; `:` is no custom name");
    assert_eq!(clean(r#"<m-stat data-id="mine" id="theirs">1</m-stat>"#), r#"<m-stat data-id="mine">1</m-stat>"#, "a data-id of its own wins");
    let md = to_html(&sanitize(&markdown("<m-summary>\n\n<m-field data-field=\"standing\">**bold**</m-field>\n\n</m-summary>\n")));
    assert_eq!(md, "<m-summary>\n<p><m-field data-field=\"standing\"><strong>bold</strong></m-field></p>\n</m-summary>\n");
    assert!(house_tag("m-summary") && !house_tag("m-") && !house_tag("mx") && !house_tag("m-A"));
    assert!(house_attr("data-field") && !house_attr("data-slot") && !house_attr("onclick") && !house_attr("style") && !house_attr("href") && !house_attr("id"));
}

/// EDITOR-9: any other custom element (`volt-poll`, `ticket-row`) is kept inert as `m-el`, its own
/// name as `data-tag` (never as itself: a name the page may define is never planted), its text and
/// the house's attributes kept, its end tag matched by its own name; FRAME-29's bound mark still dropped.
#[test]
fn a_custom_tag_is_kept_inert_as_m_el() {
    assert_eq!(
        clean(r#"<volt-poll label="Poll" onclick="x()" data-slot="s" data-tag="forged"><b>in</b></volt-poll>after"#),
        r#"<m-el data-tag="volt-poll" label="Poll"><b>in</b></m-el>after"#
    );
    assert_eq!(clean("<a-b><c-d>x</c-d>y</a-b>z"), r#"<m-el data-tag="a-b"><m-el data-tag="c-d">x</m-el>y</m-el>z"#, "each end tag closes its own");
    assert_eq!(clean("<x-new/>t"), r#"<m-el data-tag="x-new"></m-el>t"#);
    assert!(custom_tag("volt-poll") && !custom_tag("blink") && !custom_tag("m:x") && !custom_tag("-x"));
}

/// COMPS-4: a tag names a component and its attributes are its props — so a custom tag's or a house
/// mark's other plain attributes are kept, inert, as `data-prop-<name>` (a `data-prop-*` written as
/// such too); never a handler, a style, a URL or the bound mark's.
#[test]
fn a_tags_other_attributes_are_kept_as_props() {
    assert_eq!(
        clean(r#"<x-counter start="3" step="2" label="Clicks" onclick="x()" style="color:red" href="https://x.example" data-slot="s" data-prop-max="9">n</x-counter>"#),
        r#"<m-el data-tag="x-counter" data-prop-start="3" data-prop-step="2" label="Clicks" data-prop-max="9">n</m-el>"#
    );
    assert_eq!(clean(r#"<m-chart points="1,2,3" kind="bar"></m-chart>"#), r#"<m-chart data-prop-points="1,2,3" kind="bar"></m-chart>"#);
    assert_eq!(clean(r#"<x-a start="1" data-prop-start="2"></x-a>"#), r#"<m-el data-tag="x-a" data-prop-start="1"></m-el>"#, "one value a prop");
    assert!(prop_attr("start") && prop_attr("page-size") && !prop_attr("onload") && !prop_attr("style") && !prop_attr("src") && !prop_attr("data-slot") && !prop_attr("Start") && !prop_attr("x:y"));
}