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.

85 lines
2.2KB

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