1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 describe "http security enabled" do
11 setup do: clear_config([:http_security, :enabled], true)
13 test "it sends CSP headers when enabled", %{conn: conn} do
14 conn = get(conn, "/api/v1/instance")
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") == []
25 test "it sends STS headers when enabled", %{conn: conn} do
26 clear_config([:http_security, :sts], true)
28 conn = get(conn, "/api/v1/instance")
30 refute Conn.get_resp_header(conn, "strict-transport-security") == []
31 refute Conn.get_resp_header(conn, "expect-ct") == []
34 test "it does not send STS headers when disabled", %{conn: conn} do
35 clear_config([:http_security, :sts], false)
37 conn = get(conn, "/api/v1/instance")
39 assert Conn.get_resp_header(conn, "strict-transport-security") == []
40 assert Conn.get_resp_header(conn, "expect-ct") == []
43 test "referrer-policy header reflects configured value", %{conn: conn} do
44 resp = get(conn, "/api/v1/instance")
46 assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
48 clear_config([:http_security, :referrer_policy], "no-referrer")
50 resp = get(conn, "/api/v1/instance")
52 assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
55 test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
56 conn = get(conn, "/api/v1/instance")
58 [csp] = Conn.get_resp_header(conn, "content-security-policy")
60 assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
62 [report_to] = Conn.get_resp_header(conn, "report-to")
65 "{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
68 test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
69 conn = get(conn, "/api/v1/instance")
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:;"
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"})
80 clear_config([:frontends, :available], %{
83 "custom-http-headers" => [{"service-worker-allowed", "/"}]
87 conn = get(conn, "/api/v1/instance")
88 assert Conn.get_resp_header(conn, "service-worker-allowed") == ["/"]
92 describe "img-src and media-src" 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)
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)
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)
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)
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")
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")
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")
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};"
141 test "it does not send CSP headers when disabled", %{conn: conn} do
142 clear_config([:http_security, :enabled], false)
144 conn = get(conn, "/api/v1/instance")
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") == []