logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

html_test.exs (9802B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.HTMLTest do
  5. alias Pleroma.HTML
  6. alias Pleroma.Object
  7. alias Pleroma.Web.CommonAPI
  8. use Pleroma.DataCase, async: true
  9. import Pleroma.Factory
  10. @html_sample """
  11. <b>this is in bold</b>
  12. <p>this is a paragraph</p>
  13. this is a linebreak<br />
  14. this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
  15. this is a link with not allowed "rel" attribute: <a href="http://example.com/" rel="tag noallowed">example.com</a>
  16. this is an image: <img src="http://example.com/image.jpg"><br />
  17. this is an inline emoji: <img class="emoji" src="http://example.com/image.jpg"><br />
  18. <script>alert('hacked')</script>
  19. """
  20. @html_onerror_sample """
  21. <img src="http://example.com/image.jpg" onerror="alert('hacked')">
  22. """
  23. @html_stillimage_sample """
  24. <img class="still-image" src="http://example.com/image.jpg">
  25. """
  26. @html_span_class_sample """
  27. <span class="animate-spin">hi</span>
  28. """
  29. @html_span_microformats_sample """
  30. <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
  31. """
  32. @html_span_invalid_microformats_sample """
  33. <span class="h-card"><a class="u-url mention animate-spin">@<span>foo</span></a></span>
  34. """
  35. @mention_hashtags_sample """
  36. <a href="https://mastodon.example/tags/linux" class="mention hashtag" rel="tag">#<span>linux</span></a>
  37. """
  38. describe "StripTags scrubber" do
  39. test "works as expected" do
  40. expected = """
  41. this is in bold
  42. this is a paragraph
  43. this is a linebreak
  44. this is a link with allowed &quot;rel&quot; attribute: example.com
  45. this is a link with not allowed &quot;rel&quot; attribute: example.com
  46. this is an image:
  47. this is an inline emoji:
  48. alert(&#39;hacked&#39;)
  49. """
  50. assert expected == HTML.strip_tags(@html_sample)
  51. end
  52. test "does not allow attribute-based XSS" do
  53. expected = "\n"
  54. assert expected == HTML.strip_tags(@html_onerror_sample)
  55. end
  56. end
  57. describe "TwitterText scrubber" do
  58. test "normalizes HTML as expected" do
  59. expected = """
  60. this is in bold
  61. <p>this is a paragraph</p>
  62. this is a linebreak<br/>
  63. this is a link with allowed &quot;rel&quot; attribute: <a href="http://example.com/" rel="tag">example.com</a>
  64. this is a link with not allowed &quot;rel&quot; attribute: <a href="http://example.com/">example.com</a>
  65. this is an image: <img src="http://example.com/image.jpg"/><br/>
  66. this is an inline emoji: <img class="emoji" src="http://example.com/image.jpg"/><br/>
  67. alert(&#39;hacked&#39;)
  68. """
  69. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.TwitterText)
  70. end
  71. test "does not allow attribute-based XSS" do
  72. expected = """
  73. <img src="http://example.com/image.jpg"/>
  74. """
  75. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.TwitterText)
  76. end
  77. test "does not allow spans with invalid classes" do
  78. expected = """
  79. <span>hi</span>
  80. """
  81. assert expected ==
  82. HTML.filter_tags(@html_span_class_sample, Pleroma.HTML.Scrubber.TwitterText)
  83. end
  84. test "does not allow images with invalid classes" do
  85. expected = """
  86. <img src="http://example.com/image.jpg"/>
  87. """
  88. assert expected ==
  89. HTML.filter_tags(@html_stillimage_sample, Pleroma.HTML.Scrubber.TwitterText)
  90. end
  91. test "does allow microformats" do
  92. expected = """
  93. <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
  94. """
  95. assert expected ==
  96. HTML.filter_tags(@html_span_microformats_sample, Pleroma.HTML.Scrubber.TwitterText)
  97. end
  98. test "filters invalid microformats markup" do
  99. expected = """
  100. <span class="h-card"><a>@<span>foo</span></a></span>
  101. """
  102. assert expected ==
  103. HTML.filter_tags(
  104. @html_span_invalid_microformats_sample,
  105. Pleroma.HTML.Scrubber.TwitterText
  106. )
  107. end
  108. test "does allow mention hashtags" do
  109. expected = """
  110. <a href="https://mastodon.example/tags/linux" class="mention hashtag" rel="tag">#<span>linux</span></a>
  111. """
  112. assert expected ==
  113. HTML.filter_tags(@mention_hashtags_sample, Pleroma.HTML.Scrubber.Default)
  114. end
  115. end
  116. describe "default scrubber" do
  117. test "normalizes HTML as expected" do
  118. expected = """
  119. <b>this is in bold</b>
  120. <p>this is a paragraph</p>
  121. this is a linebreak<br/>
  122. this is a link with allowed &quot;rel&quot; attribute: <a href="http://example.com/" rel="tag">example.com</a>
  123. this is a link with not allowed &quot;rel&quot; attribute: <a href="http://example.com/">example.com</a>
  124. this is an image: <img src="http://example.com/image.jpg"/><br/>
  125. this is an inline emoji: <img class="emoji" src="http://example.com/image.jpg"/><br/>
  126. alert(&#39;hacked&#39;)
  127. """
  128. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.Default)
  129. end
  130. test "does not allow attribute-based XSS" do
  131. expected = """
  132. <img src="http://example.com/image.jpg"/>
  133. """
  134. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.Default)
  135. end
  136. test "does not allow spans with invalid classes" do
  137. expected = """
  138. <span>hi</span>
  139. """
  140. assert expected == HTML.filter_tags(@html_span_class_sample, Pleroma.HTML.Scrubber.Default)
  141. end
  142. test "does not allow images with invalid classes" do
  143. expected = """
  144. <img src="http://example.com/image.jpg"/>
  145. """
  146. assert expected ==
  147. HTML.filter_tags(@html_stillimage_sample, Pleroma.HTML.Scrubber.TwitterText)
  148. end
  149. test "does allow microformats" do
  150. expected = """
  151. <span class="h-card"><a class="u-url mention">@<span>foo</span></a></span>
  152. """
  153. assert expected ==
  154. HTML.filter_tags(@html_span_microformats_sample, Pleroma.HTML.Scrubber.Default)
  155. end
  156. test "filters invalid microformats markup" do
  157. expected = """
  158. <span class="h-card"><a>@<span>foo</span></a></span>
  159. """
  160. assert expected ==
  161. HTML.filter_tags(
  162. @html_span_invalid_microformats_sample,
  163. Pleroma.HTML.Scrubber.Default
  164. )
  165. end
  166. test "does allow mention hashtags" do
  167. expected = """
  168. <a href="https://mastodon.example/tags/linux" class="mention hashtag" rel="tag">#<span>linux</span></a>
  169. """
  170. assert expected ==
  171. HTML.filter_tags(@mention_hashtags_sample, Pleroma.HTML.Scrubber.Default)
  172. end
  173. end
  174. describe "extract_first_external_url_from_object" do
  175. test "extracts the url" do
  176. user = insert(:user)
  177. {:ok, activity} =
  178. CommonAPI.post(user, %{
  179. status:
  180. "I think I just found the best github repo https://github.com/komeiji-satori/Dress"
  181. })
  182. object = Object.normalize(activity, fetch: false)
  183. url = HTML.extract_first_external_url_from_object(object)
  184. assert url == "https://github.com/komeiji-satori/Dress"
  185. end
  186. test "skips mentions" do
  187. user = insert(:user)
  188. other_user = insert(:user)
  189. {:ok, activity} =
  190. CommonAPI.post(user, %{
  191. status:
  192. "@#{other_user.nickname} install misskey! https://github.com/syuilo/misskey/blob/develop/docs/setup.en.md"
  193. })
  194. object = Object.normalize(activity, fetch: false)
  195. url = HTML.extract_first_external_url_from_object(object)
  196. assert url == "https://github.com/syuilo/misskey/blob/develop/docs/setup.en.md"
  197. refute url == other_user.ap_id
  198. end
  199. test "skips hashtags" do
  200. user = insert(:user)
  201. {:ok, activity} =
  202. CommonAPI.post(user, %{
  203. status: "#cofe https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140"
  204. })
  205. object = Object.normalize(activity, fetch: false)
  206. url = HTML.extract_first_external_url_from_object(object)
  207. assert url == "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140"
  208. end
  209. test "skips microformats hashtags" do
  210. user = insert(:user)
  211. {:ok, activity} =
  212. CommonAPI.post(user, %{
  213. status:
  214. "<a href=\"https://pleroma.gov/tags/cofe\" rel=\"tag\">#cofe</a> https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140",
  215. content_type: "text/html"
  216. })
  217. object = Object.normalize(activity, fetch: false)
  218. url = HTML.extract_first_external_url_from_object(object)
  219. assert url == "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=72255140"
  220. end
  221. test "does not crash when there is an HTML entity in a link" do
  222. user = insert(:user)
  223. {:ok, activity} = CommonAPI.post(user, %{status: "\"http://cofe.com/?boomer=ok&foo=bar\""})
  224. object = Object.normalize(activity, fetch: false)
  225. assert nil == HTML.extract_first_external_url_from_object(object)
  226. end
  227. test "skips attachment links" do
  228. user = insert(:user)
  229. {:ok, activity} =
  230. CommonAPI.post(user, %{
  231. status:
  232. "<a href=\"https://pleroma.gov/media/d24caa3a498e21e0298377a9ca0149a4f4f8b767178aacf837542282e2d94fb1.png?name=image.png\" class=\"attachment\">image.png</a>"
  233. })
  234. object = Object.normalize(activity, fetch: false)
  235. assert nil == HTML.extract_first_external_url_from_object(object)
  236. end
  237. end
  238. end