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 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 [reply_to] = Conn.get_resp_header(conn, "reply-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:;"
77 describe "img-src and media-src" 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)
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)
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)
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)
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")
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")
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")
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};"
126 test "it does not send CSP headers when disabled", %{conn: conn} do
127 clear_config([:http_security, :enabled], false)
129 conn = get(conn, "/api/v1/instance")
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") == []