1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTMLTest do
10 <b>this is in bold</b>
11 <p>this is a paragraph</p>
12 this is a linebreak<br />
13 this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
14 this is a link with not allowed "rel" attribute: <a href="http://example.com/" rel="tag noallowed">example.com</a>
15 this is an image: <img src="http://example.com/image.jpg"><br />
16 <script>alert('hacked')</script>
19 @html_onerror_sample """
20 <img src="http://example.com/image.jpg" onerror="alert('hacked')">
23 @html_span_class_sample """
24 <span class="animate-spin">hi</span>
27 @html_span_microformats_sample """
28 <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
31 @html_span_invalid_microformats_sample """
32 <span class="h-card"><a class="u-url mention animate-spin">@<span>foo</span></a></span>
35 describe "StripTags scrubber" do
36 test "works as expected" do
41 this is a link with allowed "rel" attribute: example.com
42 this is a link with not allowed "rel" attribute: example.com
47 assert expected == HTML.strip_tags(@html_sample)
50 test "does not allow attribute-based XSS" do
53 assert expected == HTML.strip_tags(@html_onerror_sample)
57 describe "TwitterText scrubber" do
58 test "normalizes HTML as expected" do
61 <p>this is a paragraph</p>
62 this is a linebreak<br />
63 this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
64 this is a link with not allowed "rel" attribute: <a href="http://example.com/">example.com</a>
65 this is an image: <img src="http://example.com/image.jpg" /><br />
69 assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.TwitterText)
72 test "does not allow attribute-based XSS" do
74 <img src="http://example.com/image.jpg" />
77 assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.TwitterText)
80 test "does not allow spans with invalid classes" do
86 HTML.filter_tags(@html_span_class_sample, Pleroma.HTML.Scrubber.TwitterText)
89 test "does allow microformats" do
91 <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
95 HTML.filter_tags(@html_span_microformats_sample, Pleroma.HTML.Scrubber.TwitterText)
98 test "filters invalid microformats markup" do
100 <span class="h-card"><a>@<span>foo</span></a></span>
105 @html_span_invalid_microformats_sample,
106 Pleroma.HTML.Scrubber.TwitterText
111 describe "default scrubber" do
112 test "normalizes HTML as expected" do
114 <b>this is in bold</b>
115 <p>this is a paragraph</p>
116 this is a linebreak<br />
117 this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
118 this is a link with not allowed "rel" attribute: <a href="http://example.com/">example.com</a>
119 this is an image: <img src="http://example.com/image.jpg" /><br />
123 assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.Default)
126 test "does not allow attribute-based XSS" do
128 <img src="http://example.com/image.jpg" />
131 assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.Default)
134 test "does not allow spans with invalid classes" do
139 assert expected == HTML.filter_tags(@html_span_class_sample, Pleroma.HTML.Scrubber.Default)
142 test "does allow microformats" do
144 <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
148 HTML.filter_tags(@html_span_microformats_sample, Pleroma.HTML.Scrubber.Default)
151 test "filters invalid microformats markup" do
153 <span class="h-card"><a>@<span>foo</span></a></span>
158 @html_span_invalid_microformats_sample,
159 Pleroma.HTML.Scrubber.Default