Fix typo in CSP Report-To header name
[akkoma] / test / pleroma / web / plugs / http_security_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
6 use Pleroma.Web.ConnCase
7
8 alias Plug.Conn
9
10 describe "http security enabled" do
11 setup do: clear_config([:http_security, :enabled], true)
12
13 test "it sends CSP headers when enabled", %{conn: conn} do
14 conn = get(conn, "/api/v1/instance")
15
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
25 test "it sends STS headers when enabled", %{conn: conn} do
26 clear_config([:http_security, :sts], true)
27
28 conn = get(conn, "/api/v1/instance")
29
30 refute Conn.get_resp_header(conn, "strict-transport-security") == []
31 refute Conn.get_resp_header(conn, "expect-ct") == []
32 end
33
34 test "it does not send STS headers when disabled", %{conn: conn} do
35 clear_config([:http_security, :sts], false)
36
37 conn = get(conn, "/api/v1/instance")
38
39 assert Conn.get_resp_header(conn, "strict-transport-security") == []
40 assert Conn.get_resp_header(conn, "expect-ct") == []
41 end
42
43 test "referrer-policy header reflects configured value", %{conn: conn} do
44 resp = get(conn, "/api/v1/instance")
45
46 assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
47
48 clear_config([:http_security, :referrer_policy], "no-referrer")
49
50 resp = get(conn, "/api/v1/instance")
51
52 assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
53 end
54
55 test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
56 conn = get(conn, "/api/v1/instance")
57
58 [csp] = Conn.get_resp_header(conn, "content-security-policy")
59
60 assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
61
62 [report_to] = Conn.get_resp_header(conn, "report-to")
63
64 assert report_to ==
65 "{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
66 end
67
68 test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
69 conn = get(conn, "/api/v1/instance")
70
71 [csp] = Conn.get_resp_header(conn, "content-security-policy")
72 assert csp =~ "media-src 'self' https:;"
73 assert csp =~ "img-src 'self' data: blob: https:;"
74 end
75
76 test "it sets the Service-Worker-Allowed header", %{conn: conn} do
77 clear_config([:http_security, :enabled], true)
78 clear_config([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
79
80 clear_config([:frontends, :available], %{
81 "fedi-fe" => %{
82 "name" => "fedi-fe",
83 "custom-http-headers" => [{"service-worker-allowed", "/"}]
84 }
85 })
86
87 conn = get(conn, "/api/v1/instance")
88 assert Conn.get_resp_header(conn, "service-worker-allowed") == ["/"]
89 end
90 end
91
92 describe "img-src and media-src" do
93 setup do
94 clear_config([:http_security, :enabled], true)
95 clear_config([:media_proxy, :enabled], true)
96 clear_config([:media_proxy, :proxy_opts, :redirect_on_failure], false)
97 end
98
99 test "media_proxy with base_url", %{conn: conn} do
100 url = "https://example.com"
101 clear_config([:media_proxy, :base_url], url)
102 assert_media_img_src(conn, url)
103 end
104
105 test "upload with base url", %{conn: conn} do
106 url = "https://example2.com"
107 clear_config([Pleroma.Upload, :base_url], url)
108 assert_media_img_src(conn, url)
109 end
110
111 test "with S3 public endpoint", %{conn: conn} do
112 url = "https://example3.com"
113 clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
114 assert_media_img_src(conn, url)
115 end
116
117 test "with captcha endpoint", %{conn: conn} do
118 clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
119 assert_media_img_src(conn, "https://captcha.com")
120 end
121
122 test "with media_proxy whitelist", %{conn: conn} do
123 clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
124 assert_media_img_src(conn, "https://example7.com https://example6.com")
125 end
126
127 # TODO: delete after removing support bare domains for media proxy whitelist
128 test "with media_proxy bare domains whitelist (deprecated)", %{conn: conn} do
129 clear_config([:media_proxy, :whitelist], ["example4.com", "example5.com"])
130 assert_media_img_src(conn, "example5.com example4.com")
131 end
132 end
133
134 defp assert_media_img_src(conn, url) do
135 conn = get(conn, "/api/v1/instance")
136 [csp] = Conn.get_resp_header(conn, "content-security-policy")
137 assert csp =~ "media-src 'self' #{url};"
138 assert csp =~ "img-src 'self' data: blob: #{url};"
139 end
140
141 test "it does not send CSP headers when disabled", %{conn: conn} do
142 clear_config([:http_security, :enabled], false)
143
144 conn = get(conn, "/api/v1/instance")
145
146 assert Conn.get_resp_header(conn, "x-xss-protection") == []
147 assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
148 assert Conn.get_resp_header(conn, "x-frame-options") == []
149 assert Conn.get_resp_header(conn, "x-content-type-options") == []
150 assert Conn.get_resp_header(conn, "x-download-options") == []
151 assert Conn.get_resp_header(conn, "referrer-policy") == []
152 assert Conn.get_resp_header(conn, "content-security-policy") == []
153 end
154 end