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.

84 lines
2.7KB

  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.Web.Plugs.HTTPSecurityPlugTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Config
  7. alias Plug.Conn
  8. test "it sends CSP headers when enabled", %{conn: conn} do
  9. Config.put([:http_security, :enabled], true)
  10. conn =
  11. conn
  12. |> get("/api/v1/instance")
  13. refute Conn.get_resp_header(conn, "x-xss-protection") == []
  14. refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  15. refute Conn.get_resp_header(conn, "x-frame-options") == []
  16. refute Conn.get_resp_header(conn, "x-content-type-options") == []
  17. refute Conn.get_resp_header(conn, "x-download-options") == []
  18. refute Conn.get_resp_header(conn, "referrer-policy") == []
  19. refute Conn.get_resp_header(conn, "content-security-policy") == []
  20. end
  21. test "it does not send CSP headers when disabled", %{conn: conn} do
  22. Config.put([:http_security, :enabled], false)
  23. conn =
  24. conn
  25. |> get("/api/v1/instance")
  26. assert Conn.get_resp_header(conn, "x-xss-protection") == []
  27. assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
  28. assert Conn.get_resp_header(conn, "x-frame-options") == []
  29. assert Conn.get_resp_header(conn, "x-content-type-options") == []
  30. assert Conn.get_resp_header(conn, "x-download-options") == []
  31. assert Conn.get_resp_header(conn, "referrer-policy") == []
  32. assert Conn.get_resp_header(conn, "content-security-policy") == []
  33. end
  34. test "it sends STS headers when enabled", %{conn: conn} do
  35. Config.put([:http_security, :enabled], true)
  36. Config.put([:http_security, :sts], true)
  37. conn =
  38. conn
  39. |> get("/api/v1/instance")
  40. refute Conn.get_resp_header(conn, "strict-transport-security") == []
  41. refute Conn.get_resp_header(conn, "expect-ct") == []
  42. end
  43. test "it does not send STS headers when disabled", %{conn: conn} do
  44. Config.put([:http_security, :enabled], true)
  45. Config.put([:http_security, :sts], false)
  46. conn =
  47. conn
  48. |> get("/api/v1/instance")
  49. assert Conn.get_resp_header(conn, "strict-transport-security") == []
  50. assert Conn.get_resp_header(conn, "expect-ct") == []
  51. end
  52. test "referrer-policy header reflects configured value", %{conn: conn} do
  53. Config.put([:http_security, :enabled], true)
  54. conn =
  55. conn
  56. |> get("/api/v1/instance")
  57. assert Conn.get_resp_header(conn, "referrer-policy") == ["same-origin"]
  58. Config.put([:http_security, :referrer_policy], "no-referrer")
  59. conn =
  60. build_conn()
  61. |> get("/api/v1/instance")
  62. assert Conn.get_resp_header(conn, "referrer-policy") == ["no-referrer"]
  63. end
  64. end