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.

81 lines
2.1KB

  1. defmodule Pleroma.HTMLTest do
  2. alias Pleroma.HTML
  3. use Pleroma.DataCase
  4. @html_sample """
  5. <b>this is in bold</b>
  6. <p>this is a paragraph</p>
  7. this is a linebreak<br />
  8. this is an image: <img src="http://example.com/image.jpg"><br />
  9. <script>alert('hacked')</script>
  10. """
  11. @html_onerror_sample """
  12. <img src="http://example.com/image.jpg" onerror="alert('hacked')">
  13. """
  14. describe "StripTags scrubber" do
  15. test "works as expected" do
  16. expected = """
  17. this is in bold
  18. this is a paragraph
  19. this is a linebreak
  20. this is an image:
  21. alert('hacked')
  22. """
  23. assert expected == HTML.strip_tags(@html_sample)
  24. end
  25. test "does not allow attribute-based XSS" do
  26. expected = "\n"
  27. assert expected == HTML.strip_tags(@html_onerror_sample)
  28. end
  29. end
  30. describe "TwitterText scrubber" do
  31. test "normalizes HTML as expected" do
  32. expected = """
  33. this is in bold
  34. <p>this is a paragraph</p>
  35. this is a linebreak<br />
  36. this is an image: <img src="http://example.com/image.jpg" /><br />
  37. alert('hacked')
  38. """
  39. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.TwitterText)
  40. end
  41. test "does not allow attribute-based XSS" do
  42. expected = """
  43. <img src="http://example.com/image.jpg" />
  44. """
  45. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.TwitterText)
  46. end
  47. end
  48. describe "default scrubber" do
  49. test "normalizes HTML as expected" do
  50. expected = """
  51. <b>this is in bold</b>
  52. <p>this is a paragraph</p>
  53. this is a linebreak<br />
  54. this is an image: <img src="http://example.com/image.jpg" /><br />
  55. alert('hacked')
  56. """
  57. assert expected == HTML.filter_tags(@html_sample, Pleroma.HTML.Scrubber.Default)
  58. end
  59. test "does not allow attribute-based XSS" do
  60. expected = """
  61. <img src="http://example.com/image.jpg" />
  62. """
  63. assert expected == HTML.filter_tags(@html_onerror_sample, Pleroma.HTML.Scrubber.Default)
  64. end
  65. end
  66. end