Merge remote-tracking branch 'upstream/develop' into aliases
[akkoma] / test / pleroma / web / plugs / http_security_plug_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 [reply_to] = Conn.get_resp_header(conn, "reply-to")
63
64 assert reply_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 end
76
77 describe "img-src and media-src" do
78 setup do
79 clear_config([:http_security, :enabled], true)
80 clear_config([:media_proxy, :enabled], true)
81 clear_config([:media_proxy, :proxy_opts, :redirect_on_failure], false)
82 end
83
84 test "media_proxy with base_url", %{conn: conn} do
85 url = "https://example.com"
86 clear_config([:media_proxy, :base_url], url)
87 assert_media_img_src(conn, url)
88 end
89
90 test "upload with base url", %{conn: conn} do
91 url = "https://example2.com"
92 clear_config([Pleroma.Upload, :base_url], url)
93 assert_media_img_src(conn, url)
94 end
95
96 test "with S3 public endpoint", %{conn: conn} do
97 url = "https://example3.com"
98 clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
99 assert_media_img_src(conn, url)
100 end
101
102 test "with captcha endpoint", %{conn: conn} do
103 clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
104 assert_media_img_src(conn, "https://captcha.com")
105 end
106
107 test "with media_proxy whitelist", %{conn: conn} do
108 clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
109 assert_media_img_src(conn, "https://example7.com https://example6.com")
110 end
111
112 # TODO: delete after removing support bare domains for media proxy whitelist
113 test "with media_proxy bare domains whitelist (deprecated)", %{conn: conn} do
114 clear_config([:media_proxy, :whitelist], ["example4.com", "example5.com"])
115 assert_media_img_src(conn, "example5.com example4.com")
116 end
117 end
118
119 defp assert_media_img_src(conn, url) do
120 conn = get(conn, "/api/v1/instance")
121 [csp] = Conn.get_resp_header(conn, "content-security-policy")
122 assert csp =~ "media-src 'self' #{url};"
123 assert csp =~ "img-src 'self' data: blob: #{url};"
124 end
125
126 test "it does not send CSP headers when disabled", %{conn: conn} do
127 clear_config([:http_security, :enabled], false)
128
129 conn = get(conn, "/api/v1/instance")
130
131 assert Conn.get_resp_header(conn, "x-xss-protection") == []
132 assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
133 assert Conn.get_resp_header(conn, "x-frame-options") == []
134 assert Conn.get_resp_header(conn, "x-content-type-options") == []
135 assert Conn.get_resp_header(conn, "x-download-options") == []
136 assert Conn.get_resp_header(conn, "referrer-policy") == []
137 assert Conn.get_resp_header(conn, "content-security-policy") == []
138 end
139 end