Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.HTMLTest do
  5. alias Pleroma.HTML
  6. use Pleroma.DataCase
  7. @html_sample """
  8. <b>this is in bold</b>
  9. <p>this is a paragraph</p>
  10. this is a linebreak<br />
  11. this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
  12. this is a link with not allowed "rel" attribute: <a href="http://example.com/" rel="tag noallowed">example.com</a>
  13. this is an image: <img src="http://example.com/image.jpg"><br />
  14. <script>alert('hacked')</script>
  15. """
  16. @html_onerror_sample """
  17. <img src="http://example.com/image.jpg" onerror="alert('hacked')">
  18. """
  19. describe "StripTags scrubber" do
  20. test "works as expected" do
  21. expected = """
  22. this is in bold
  23. this is a paragraph
  24. this is a linebreak
  25. this is a link with allowed "rel" attribute: example.com
  26. this is a link with not allowed "rel" attribute: example.com
  27. this is an image:
  28. alert('hacked')
  29. """
  30. assert expected == HTML.strip_tags(@html_sample)
  31. end
  32. test "does not allow attribute-based XSS" do
  33. expected = "\n"
  34. assert expected == HTML.strip_tags(@html_onerror_sample)
  35. end
  36. end
  37. describe "TwitterText scrubber" do
  38. test "normalizes HTML as expected" do
  39. expected = """
  40. this is in bold
  41. <p>this is a paragraph</p>
  42. this is a linebreak<br />
  43. this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
  44. this is a link with not allowed "rel" attribute: <a href="http://example.com/">example.com</a>
  45. this is an image: <img src="http://example.com/image.jpg" /><br />
  46. alert('hacked')
  47. """
  48. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.TwitterText)
  49. end
  50. test "does not allow attribute-based XSS" do
  51. expected = """
  52. <img src="http://example.com/image.jpg" />
  53. """
  54. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.TwitterText)
  55. end
  56. end
  57. describe "default scrubber" do
  58. test "normalizes HTML as expected" do
  59. expected = """
  60. <b>this is in bold</b>
  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 />
  66. alert('hacked')
  67. """
  68. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.Default)
  69. end
  70. test "does not allow attribute-based XSS" do
  71. expected = """
  72. <img src="http://example.com/image.jpg" />
  73. """
  74. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.Default)
  75. end
  76. end
  77. end