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