logo

pleroma

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

normalize_markup_test.exs (2565B)


  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.Web.ActivityPub.MRF.NormalizeMarkupTest do
  5. use Pleroma.DataCase, async: true
  6. alias Pleroma.Web.ActivityPub.MRF
  7. alias Pleroma.Web.ActivityPub.MRF.NormalizeMarkup
  8. @html_sample """
  9. <b>this is in bold</b>
  10. <p>this is a paragraph</p>
  11. this is a linebreak<br />
  12. this is a link with allowed "rel" attribute: <a href="http://example.com/" rel="tag">example.com</a>
  13. this is a link with not allowed "rel" attribute: <a href="http://example.com/" rel="tag noallowed">example.com</a>
  14. this is an image: <img src="http://example.com/image.jpg"><br />
  15. <script>alert('hacked')</script>
  16. """
  17. @expected """
  18. <b>this is in bold</b>
  19. <p>this is a paragraph</p>
  20. this is a linebreak<br/>
  21. this is a link with allowed &quot;rel&quot; attribute: <a href="http://example.com/" rel="tag">example.com</a>
  22. this is a link with not allowed &quot;rel&quot; attribute: <a href="http://example.com/">example.com</a>
  23. this is an image: <img src="http://example.com/image.jpg"/><br/>
  24. alert(&#39;hacked&#39;)
  25. """
  26. test "it filter html tags" do
  27. message = %{"type" => "Create", "object" => %{"content" => @html_sample}}
  28. assert {:ok, res} = NormalizeMarkup.filter(message)
  29. assert res["object"]["content"] == @expected
  30. end
  31. test "history-aware" do
  32. message = %{
  33. "type" => "Create",
  34. "object" => %{
  35. "content" => @html_sample,
  36. "formerRepresentations" => %{"orderedItems" => [%{"content" => @html_sample}]}
  37. }
  38. }
  39. assert {:ok, res} = MRF.filter_one(NormalizeMarkup, message)
  40. assert %{
  41. "content" => @expected,
  42. "formerRepresentations" => %{"orderedItems" => [%{"content" => @expected}]}
  43. } = res["object"]
  44. end
  45. test "works with Updates" do
  46. message = %{
  47. "type" => "Update",
  48. "object" => %{
  49. "content" => @html_sample,
  50. "formerRepresentations" => %{"orderedItems" => [%{"content" => @html_sample}]}
  51. }
  52. }
  53. assert {:ok, res} = MRF.filter_one(NormalizeMarkup, message)
  54. assert %{
  55. "content" => @expected,
  56. "formerRepresentations" => %{"orderedItems" => [%{"content" => @expected}]}
  57. } = res["object"]
  58. end
  59. test "it skips filter if type isn't `Create` or `Update`" do
  60. message = %{"type" => "Note", "object" => %{}}
  61. assert {:ok, res} = NormalizeMarkup.filter(message)
  62. assert res == message
  63. end
  64. end