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.

92 lines
3.2KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 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. clear_config([:http_securiy, :enabled])
  9. clear_config([:http_security, :sts])
  10. describe "http security enabled" do
  11. setup do
  12. Config.put([:http_security, :enabled], true)
  13. end
  14. test "it sends CSP headers when enabled", %{conn: conn} do
  15. conn = get(conn, "/api/v1/instance")
  16. refute Conn.get_resp_header(conn, "x-xss-protection") == []
  17. refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  18. refute Conn.get_resp_header(conn, "x-frame-options") == []
  19. refute Conn.get_resp_header(conn, "x-content-type-options") == []
  20. refute Conn.get_resp_header(conn, "x-download-options") == []
  21. refute Conn.get_resp_header(conn, "referrer-policy") == []
  22. refute Conn.get_resp_header(conn, "content-security-policy") == []
  23. end
  24. test "it sends STS headers when enabled", %{conn: conn} do
  25. Config.put([:http_security, :sts], true)
  26. conn = get(conn, "/api/v1/instance")
  27. refute Conn.get_resp_header(conn, "strict-transport-security") == []
  28. refute Conn.get_resp_header(conn, "expect-ct") == []
  29. end
  30. test "it does not send STS headers when disabled", %{conn: conn} do
  31. Config.put([:http_security, :sts], false)
  32. conn = get(conn, "/api/v1/instance")
  33. assert Conn.get_resp_header(conn, "strict-transport-security") == []
  34. assert Conn.get_resp_header(conn, "expect-ct") == []
  35. end
  36. test "referrer-policy header reflects configured value", %{conn: conn} do
  37. conn = get(conn, "/api/v1/instance")
  38. assert Conn.get_resp_header(conn, "referrer-policy") == ["same-origin"]
  39. Config.put([:http_security, :referrer_policy], "no-referrer")
  40. conn =
  41. build_conn()
  42. |> get("/api/v1/instance")
  43. assert Conn.get_resp_header(conn, "referrer-policy") == ["no-referrer"]
  44. end
  45. test "it sends `report-to` & `report-uri` CSP response headers" do
  46. conn =
  47. build_conn()
  48. |> get("/api/v1/instance")
  49. [csp] = Conn.get_resp_header(conn, "content-security-policy")
  50. assert csp =~ ~r|report-uri https://endpoint.com; report-to csp-endpoint;|
  51. [reply_to] = Conn.get_resp_header(conn, "reply-to")
  52. assert reply_to ==
  53. "{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
  54. end
  55. end
  56. test "it does not send CSP headers when disabled", %{conn: conn} do
  57. Config.put([:http_security, :enabled], false)
  58. conn = get(conn, "/api/v1/instance")
  59. assert Conn.get_resp_header(conn, "x-xss-protection") == []
  60. assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  61. assert Conn.get_resp_header(conn, "x-frame-options") == []
  62. assert Conn.get_resp_header(conn, "x-content-type-options") == []
  63. assert Conn.get_resp_header(conn, "x-download-options") == []
  64. assert Conn.get_resp_header(conn, "referrer-policy") == []
  65. assert Conn.get_resp_header(conn, "content-security-policy") == []
  66. end
  67. end