2018-12-23 12:11:29 -08:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 21:08:45 -08:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 12:11:29 -08:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-12 07:08:02 -08:00
|
|
|
defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
2018-11-10 23:26:31 -08:00
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Plug.Conn
|
|
|
|
|
2019-08-19 08:34:29 -07:00
|
|
|
clear_config([:http_securiy, :enabled])
|
|
|
|
clear_config([:http_security, :sts])
|
2020-02-13 10:55:47 -08:00
|
|
|
clear_config([:http_security, :referrer_policy])
|
2019-08-19 08:34:29 -07:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
describe "http security enabled" do
|
|
|
|
setup do
|
|
|
|
Config.put([:http_security, :enabled], true)
|
|
|
|
end
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
test "it sends CSP headers when enabled", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07: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, "x-download-options") == []
|
|
|
|
refute Conn.get_resp_header(conn, "referrer-policy") == []
|
|
|
|
refute Conn.get_resp_header(conn, "content-security-policy") == []
|
|
|
|
end
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
test "it sends STS headers when enabled", %{conn: conn} do
|
|
|
|
Config.put([:http_security, :sts], true)
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
conn = get(conn, "/api/v1/instance")
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
refute Conn.get_resp_header(conn, "strict-transport-security") == []
|
|
|
|
refute Conn.get_resp_header(conn, "expect-ct") == []
|
|
|
|
end
|
2018-11-10 23:26:31 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
test "it does not send STS headers when disabled", %{conn: conn} do
|
|
|
|
Config.put([:http_security, :sts], false)
|
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
|
|
|
|
assert Conn.get_resp_header(conn, "strict-transport-security") == []
|
|
|
|
assert Conn.get_resp_header(conn, "expect-ct") == []
|
|
|
|
end
|
|
|
|
|
|
|
|
test "referrer-policy header reflects configured value", %{conn: conn} do
|
|
|
|
conn = get(conn, "/api/v1/instance")
|
|
|
|
|
|
|
|
assert Conn.get_resp_header(conn, "referrer-policy") == ["same-origin"]
|
2018-11-12 07:14:46 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
Config.put([:http_security, :referrer_policy], "no-referrer")
|
2018-11-16 07:45:21 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> get("/api/v1/instance")
|
2018-11-12 07:14:46 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
assert Conn.get_resp_header(conn, "referrer-policy") == ["no-referrer"]
|
|
|
|
end
|
2018-11-12 07:14:46 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
test "it sends `report-to` & `report-uri` CSP response headers" do
|
|
|
|
conn =
|
|
|
|
build_conn()
|
|
|
|
|> get("/api/v1/instance")
|
2018-11-12 07:14:46 -08:00
|
|
|
|
2019-05-15 22:49:40 -07:00
|
|
|
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
2018-11-12 07:14:46 -08:00
|
|
|
|
2020-05-29 02:46:31 -07:00
|
|
|
assert csp =~ ~r|report-uri https://endpoint.com;report-to csp-endpoint;|
|
2019-05-15 22:49:40 -07:00
|
|
|
|
|
|
|
[reply_to] = Conn.get_resp_header(conn, "reply-to")
|
|
|
|
|
|
|
|
assert reply_to ==
|
|
|
|
"{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not send CSP headers when disabled", %{conn: conn} do
|
|
|
|
Config.put([:http_security, :enabled], false)
|
|
|
|
|
|
|
|
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, "x-download-options") == []
|
|
|
|
assert Conn.get_resp_header(conn, "referrer-policy") == []
|
|
|
|
assert Conn.get_resp_header(conn, "content-security-policy") == []
|
2018-11-12 07:14:46 -08:00
|
|
|
end
|
2018-11-10 23:26:31 -08:00
|
|
|
end
|