logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

http_security_plug_test.exs (5297B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Config
  7. alias Plug.Conn
  8. describe "http security enabled" do
  9. setup do: clear_config([:http_security, :enabled], true)
  10. test "it sends CSP headers when enabled", %{conn: conn} do
  11. conn = get(conn, "/api/v1/instance")
  12. refute Conn.get_resp_header(conn, "x-xss-protection") == []
  13. refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  14. refute Conn.get_resp_header(conn, "x-frame-options") == []
  15. refute Conn.get_resp_header(conn, "x-content-type-options") == []
  16. refute Conn.get_resp_header(conn, "x-download-options") == []
  17. refute Conn.get_resp_header(conn, "referrer-policy") == []
  18. refute Conn.get_resp_header(conn, "content-security-policy") == []
  19. end
  20. test "it sends STS headers when enabled", %{conn: conn} do
  21. clear_config([:http_security, :sts], true)
  22. conn = get(conn, "/api/v1/instance")
  23. refute Conn.get_resp_header(conn, "strict-transport-security") == []
  24. refute Conn.get_resp_header(conn, "expect-ct") == []
  25. end
  26. test "it does not send STS headers when disabled", %{conn: conn} do
  27. clear_config([:http_security, :sts], false)
  28. conn = get(conn, "/api/v1/instance")
  29. assert Conn.get_resp_header(conn, "strict-transport-security") == []
  30. assert Conn.get_resp_header(conn, "expect-ct") == []
  31. end
  32. test "referrer-policy header reflects configured value", %{conn: conn} do
  33. resp = get(conn, "/api/v1/instance")
  34. assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
  35. clear_config([:http_security, :referrer_policy], "no-referrer")
  36. resp = get(conn, "/api/v1/instance")
  37. assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
  38. end
  39. test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
  40. conn = get(conn, "/api/v1/instance")
  41. [csp] = Conn.get_resp_header(conn, "content-security-policy")
  42. assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
  43. [reply_to] = Conn.get_resp_header(conn, "reply-to")
  44. assert reply_to ==
  45. "{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
  46. end
  47. test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
  48. conn = get(conn, "/api/v1/instance")
  49. [csp] = Conn.get_resp_header(conn, "content-security-policy")
  50. assert csp =~ "media-src 'self' https:;"
  51. assert csp =~ "img-src 'self' data: blob: https:;"
  52. end
  53. end
  54. describe "img-src and media-src" do
  55. setup do
  56. clear_config([:http_security, :enabled], true)
  57. clear_config([:media_proxy, :enabled], true)
  58. clear_config([:media_proxy, :proxy_opts, :redirect_on_failure], false)
  59. end
  60. test "media_proxy with base_url", %{conn: conn} do
  61. url = "https://example.com"
  62. clear_config([:media_proxy, :base_url], url)
  63. assert_media_img_src(conn, url)
  64. end
  65. test "upload with base url", %{conn: conn} do
  66. url = "https://example2.com"
  67. clear_config([Pleroma.Upload, :base_url], url)
  68. assert_media_img_src(conn, url)
  69. end
  70. test "with S3 public endpoint", %{conn: conn} do
  71. url = "https://example3.com"
  72. clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
  73. assert_media_img_src(conn, url)
  74. end
  75. test "with captcha endpoint", %{conn: conn} do
  76. clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
  77. assert_media_img_src(conn, "https://captcha.com")
  78. end
  79. test "with media_proxy whitelist", %{conn: conn} do
  80. clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
  81. assert_media_img_src(conn, "https://example7.com https://example6.com")
  82. end
  83. # TODO: delete after removing support bare domains for media proxy whitelist
  84. test "with media_proxy bare domains whitelist (deprecated)", %{conn: conn} do
  85. clear_config([:media_proxy, :whitelist], ["example4.com", "example5.com"])
  86. assert_media_img_src(conn, "example5.com example4.com")
  87. end
  88. end
  89. defp assert_media_img_src(conn, url) do
  90. conn = get(conn, "/api/v1/instance")
  91. [csp] = Conn.get_resp_header(conn, "content-security-policy")
  92. assert csp =~ "media-src 'self' #{url};"
  93. assert csp =~ "img-src 'self' data: blob: #{url};"
  94. end
  95. test "it does not send CSP headers when disabled", %{conn: conn} do
  96. clear_config([:http_security, :enabled], false)
  97. conn = get(conn, "/api/v1/instance")
  98. assert Conn.get_resp_header(conn, "x-xss-protection") == []
  99. assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  100. assert Conn.get_resp_header(conn, "x-frame-options") == []
  101. assert Conn.get_resp_header(conn, "x-content-type-options") == []
  102. assert Conn.get_resp_header(conn, "x-download-options") == []
  103. assert Conn.get_resp_header(conn, "referrer-policy") == []
  104. assert Conn.get_resp_header(conn, "content-security-policy") == []
  105. end
  106. end