1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
6 use Pleroma.Web.ConnCase
10 setup do: clear_config([:http_securiy, :enabled])
11 setup do: clear_config([:http_security, :sts])
12 setup do: clear_config([:http_security, :referrer_policy])
14 describe "http security enabled" do
16 Config.put([:http_security, :enabled], true)
19 test "it sends CSP headers when enabled", %{conn: conn} do
20 conn = get(conn, "/api/v1/instance")
22 refute Conn.get_resp_header(conn, "x-xss-protection") == []
23 refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
24 refute Conn.get_resp_header(conn, "x-frame-options") == []
25 refute Conn.get_resp_header(conn, "x-content-type-options") == []
26 refute Conn.get_resp_header(conn, "x-download-options") == []
27 refute Conn.get_resp_header(conn, "referrer-policy") == []
28 refute Conn.get_resp_header(conn, "content-security-policy") == []
31 test "it sends STS headers when enabled", %{conn: conn} do
32 Config.put([:http_security, :sts], true)
34 conn = get(conn, "/api/v1/instance")
36 refute Conn.get_resp_header(conn, "strict-transport-security") == []
37 refute Conn.get_resp_header(conn, "expect-ct") == []
40 test "it does not send STS headers when disabled", %{conn: conn} do
41 Config.put([:http_security, :sts], false)
43 conn = get(conn, "/api/v1/instance")
45 assert Conn.get_resp_header(conn, "strict-transport-security") == []
46 assert Conn.get_resp_header(conn, "expect-ct") == []
49 test "referrer-policy header reflects configured value", %{conn: conn} do
50 conn = get(conn, "/api/v1/instance")
52 assert Conn.get_resp_header(conn, "referrer-policy") == ["same-origin"]
54 Config.put([:http_security, :referrer_policy], "no-referrer")
58 |> get("/api/v1/instance")
60 assert Conn.get_resp_header(conn, "referrer-policy") == ["no-referrer"]
63 test "it sends `report-to` & `report-uri` CSP response headers" do
66 |> get("/api/v1/instance")
68 [csp] = Conn.get_resp_header(conn, "content-security-policy")
70 assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
72 [reply_to] = Conn.get_resp_header(conn, "reply-to")
75 "{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
79 test "it does not send CSP headers when disabled", %{conn: conn} do
80 Config.put([:http_security, :enabled], false)
82 conn = get(conn, "/api/v1/instance")
84 assert Conn.get_resp_header(conn, "x-xss-protection") == []
85 assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
86 assert Conn.get_resp_header(conn, "x-frame-options") == []
87 assert Conn.get_resp_header(conn, "x-content-type-options") == []
88 assert Conn.get_resp_header(conn, "x-download-options") == []
89 assert Conn.get_resp_header(conn, "referrer-policy") == []
90 assert Conn.get_resp_header(conn, "content-security-policy") == []