2018-12-23 20:11:29 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:11:29 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-12 15:08:02 +00:00
|
|
|
defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
2023-08-01 10:43:50 +00:00
|
|
|
use Pleroma.Web.ConnCase, async: false
|
2020-07-11 07:36:36 +00:00
|
|
|
|
2018-11-11 07:26:31 +00:00
|
|
|
alias Plug.Conn
|
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
describe "http security enabled" do
|
2020-07-11 07:36:36 +00:00
|
|
|
setup do: clear_config([:http_security, :enabled], true)
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
test "it sends CSP headers when enabled", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
refute Conn.get_resp_header(conn, "x-xss-protection") == []
|
|
|
|
refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
|
|
|
|
refute Conn.get_resp_header(conn, "x-frame-options") == []
|
|
|
|
refute Conn.get_resp_header(conn, "x-content-type-options") == []
|
|
|
|
refute Conn.get_resp_header(conn, "referrer-policy") == []
|
|
|
|
refute Conn.get_resp_header(conn, "content-security-policy") == []
|
|
|
|
end
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
test "it sends STS headers when enabled", %{conn: conn} do
|
2020-07-11 07:36:36 +00:00
|
|
|
clear_config([:http_security, :sts], true)
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
conn = get(conn, "/api/v1/instance")
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
refute Conn.get_resp_header(conn, "strict-transport-security") == []
|
|
|
|
end
|
2018-11-11 07:26:31 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
test "it does not send STS headers when disabled", %{conn: conn} do
|
2020-07-11 07:36:36 +00:00
|
|
|
clear_config([:http_security, :sts], false)
|
2019-05-16 05:49:40 +00:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
|
|
|
|
assert Conn.get_resp_header(conn, "strict-transport-security") == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "referrer-policy header reflects configured value", %{conn: conn} do
|
2020-07-11 07:36:36 +00:00
|
|
|
resp = get(conn, "/api/v1/instance")
|
2019-05-16 05:49:40 +00:00
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
|
2018-11-12 15:14:46 +00:00
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
clear_config([:http_security, :referrer_policy], "no-referrer")
|
2018-11-16 15:45:21 +00:00
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
resp = get(conn, "/api/v1/instance")
|
2018-11-12 15:14:46 +00:00
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
|
2019-05-16 05:49:40 +00:00
|
|
|
end
|
2018-11-12 15:14:46 +00:00
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
2018-11-12 15:14:46 +00:00
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
2018-11-12 15:14:46 +00:00
|
|
|
|
2020-05-27 17:27:30 +00:00
|
|
|
assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
|
2019-05-16 05:49:40 +00:00
|
|
|
|
2022-11-04 08:43:13 +00:00
|
|
|
[report_to] = Conn.get_resp_header(conn, "report-to")
|
2019-05-16 05:49:40 +00:00
|
|
|
|
2022-11-04 08:43:13 +00:00
|
|
|
assert report_to ==
|
2019-05-16 05:49:40 +00:00
|
|
|
"{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
|
|
|
|
end
|
2020-07-11 07:36:36 +00:00
|
|
|
|
|
|
|
test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
|
|
|
|
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
|
|
|
assert csp =~ "media-src 'self' https:;"
|
|
|
|
assert csp =~ "img-src 'self' data: blob: https:;"
|
|
|
|
end
|
2021-01-08 09:06:04 +00:00
|
|
|
|
|
|
|
test "it sets the Service-Worker-Allowed header", %{conn: conn} do
|
|
|
|
clear_config([:http_security, :enabled], true)
|
2021-01-21 11:58:18 +00:00
|
|
|
clear_config([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
|
|
|
|
|
|
|
|
clear_config([:frontends, :available], %{
|
|
|
|
"fedi-fe" => %{
|
|
|
|
"name" => "fedi-fe",
|
|
|
|
"custom-http-headers" => [{"service-worker-allowed", "/"}]
|
|
|
|
}
|
|
|
|
})
|
2021-01-08 09:06:04 +00:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
assert Conn.get_resp_header(conn, "service-worker-allowed") == ["/"]
|
|
|
|
end
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "img-src and media-src" do
|
|
|
|
setup do
|
|
|
|
clear_config([:http_security, :enabled], true)
|
|
|
|
clear_config([:media_proxy, :enabled], true)
|
|
|
|
clear_config([:media_proxy, :proxy_opts, :redirect_on_failure], false)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "media_proxy with base_url", %{conn: conn} do
|
|
|
|
url = "https://example.com"
|
|
|
|
clear_config([:media_proxy, :base_url], url)
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, proxy: url)
|
2022-11-10 17:26:51 +00:00
|
|
|
assert_connect_src(conn, url)
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "upload with base url", %{conn: conn} do
|
|
|
|
url = "https://example2.com"
|
|
|
|
clear_config([Pleroma.Upload, :base_url], url)
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, upload: url)
|
2022-11-10 17:26:51 +00:00
|
|
|
assert_connect_src(conn, url)
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with S3 public endpoint", %{conn: conn} do
|
|
|
|
url = "https://example3.com"
|
|
|
|
clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, s3: url)
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with captcha endpoint", %{conn: conn} do
|
|
|
|
clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, captcha: "https://captcha.com")
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with media_proxy whitelist", %{conn: conn} do
|
|
|
|
clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, proxy_whitelist: "https://example7.com https://example6.com")
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: delete after removing support bare domains for media proxy whitelist
|
|
|
|
test "with media_proxy bare domains whitelist (deprecated)", %{conn: conn} do
|
|
|
|
clear_config([:media_proxy, :whitelist], ["example4.com", "example5.com"])
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, proxy_whitelist: "example5.com example4.com")
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
2023-06-26 13:18:31 +00:00
|
|
|
|
|
|
|
test "with media_proxy blocklist", %{conn: conn} do
|
|
|
|
clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
|
|
|
clear_config([:media_proxy, :blocklist], ["https://example8.com"])
|
2024-04-06 22:06:29 +00:00
|
|
|
assert_media_img_src(conn, proxy_whitelist: "https://example7.com https://example6.com")
|
2023-06-26 13:18:31 +00:00
|
|
|
end
|
2020-07-11 07:36:36 +00:00
|
|
|
end
|
|
|
|
|
2024-04-06 22:06:29 +00:00
|
|
|
defp maybe_concat(nil, b), do: b
|
|
|
|
defp maybe_concat(a, nil), do: a
|
|
|
|
defp maybe_concat(a, b), do: a <> " " <> b
|
|
|
|
|
|
|
|
defp build_src_str(urls) do
|
|
|
|
urls[:proxy_whitelist]
|
|
|
|
|> maybe_concat(urls[:s3])
|
|
|
|
|> maybe_concat(urls[:upload])
|
|
|
|
|> maybe_concat(urls[:proxy])
|
|
|
|
|> maybe_concat(urls[:captcha])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp assert_media_img_src(conn, urls) do
|
|
|
|
urlstr =
|
|
|
|
[upload: "http://localhost", proxy: "http://localhost"]
|
|
|
|
|> Keyword.merge(urls)
|
|
|
|
|> build_src_str()
|
|
|
|
|
2020-07-11 07:36:36 +00:00
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
2024-04-06 22:06:29 +00:00
|
|
|
assert csp =~ "media-src 'self' #{urlstr};"
|
|
|
|
assert csp =~ "img-src 'self' data: blob: #{urlstr};"
|
2019-05-16 05:49:40 +00:00
|
|
|
end
|
|
|
|
|
2022-11-10 17:26:51 +00:00
|
|
|
defp assert_connect_src(conn, url) do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
2023-01-02 15:21:19 +00:00
|
|
|
assert csp =~ ~r/connect-src 'self' [^;]+ #{url}/
|
2022-11-10 17:26:51 +00:00
|
|
|
end
|
|
|
|
|
2019-05-16 05:49:40 +00:00
|
|
|
test "it does not send CSP headers when disabled", %{conn: conn} do
|
2020-07-11 07:36:36 +00:00
|
|
|
clear_config([:http_security, :enabled], false)
|
2019-05-16 05:49:40 +00:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
|
|
|
|
assert Conn.get_resp_header(conn, "x-xss-protection") == []
|
|
|
|
assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
|
|
|
|
assert Conn.get_resp_header(conn, "x-frame-options") == []
|
|
|
|
assert Conn.get_resp_header(conn, "x-content-type-options") == []
|
|
|
|
assert Conn.get_resp_header(conn, "referrer-policy") == []
|
|
|
|
assert Conn.get_resp_header(conn, "content-security-policy") == []
|
2018-11-12 15:14:46 +00:00
|
|
|
end
|
2018-11-11 07:26:31 +00:00
|
|
|
end
|