HttpSecurityPlug: Don't allow unsafe-eval by default
This commit is contained in:
parent
07b7a8d697
commit
1c699144d2
5 changed files with 204 additions and 64 deletions
|
|
@ -3,14 +3,52 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
||||
use Pleroma.Web.ConnCase
|
||||
use Pleroma.Web.ConnCase, async: true
|
||||
|
||||
alias Plug.Conn
|
||||
|
||||
describe "http security enabled" do
|
||||
setup do: clear_config([:http_security, :enabled], true)
|
||||
import Mox
|
||||
|
||||
test "it sends CSP headers when enabled", %{conn: conn} do
|
||||
setup do
|
||||
base_config = Pleroma.Config.get([:http_security])
|
||||
%{base_config: base_config}
|
||||
end
|
||||
|
||||
defp mock_config(config, additional \\ %{}) do
|
||||
Pleroma.UnstubbedConfigMock
|
||||
|> stub(:get, fn
|
||||
[:http_security, key] -> config[key]
|
||||
key -> additional[key]
|
||||
end)
|
||||
end
|
||||
|
||||
describe "http security enabled" do
|
||||
setup %{base_config: base_config} do
|
||||
%{base_config: Keyword.put(base_config, :enabled, true)}
|
||||
end
|
||||
|
||||
test "it does not contain unsafe-eval", %{conn: conn, base_config: base_config} do
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
[header] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
refute header =~ ~r/unsafe-eval/
|
||||
end
|
||||
|
||||
test "with allow_unsafe_eval set, it does contain it", %{conn: conn, base_config: base_config} do
|
||||
base_config =
|
||||
base_config
|
||||
|> Keyword.put(:allow_unsafe_eval, true)
|
||||
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
[header] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
assert header =~ ~r/unsafe-eval/
|
||||
end
|
||||
|
||||
test "it sends CSP headers when enabled", %{conn: conn, base_config: base_config} do
|
||||
mock_config(base_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
refute Conn.get_resp_header(conn, "x-xss-protection") == []
|
||||
|
|
@ -22,8 +60,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
refute Conn.get_resp_header(conn, "content-security-policy") == []
|
||||
end
|
||||
|
||||
test "it sends STS headers when enabled", %{conn: conn} do
|
||||
clear_config([:http_security, :sts], true)
|
||||
test "it sends STS headers when enabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:sts, true)
|
||||
|> mock_config()
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
@ -31,8 +71,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
refute Conn.get_resp_header(conn, "expect-ct") == []
|
||||
end
|
||||
|
||||
test "it does not send STS headers when disabled", %{conn: conn} do
|
||||
clear_config([:http_security, :sts], false)
|
||||
test "it does not send STS headers when disabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:sts, false)
|
||||
|> mock_config()
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
@ -40,19 +82,30 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert Conn.get_resp_header(conn, "expect-ct") == []
|
||||
end
|
||||
|
||||
test "referrer-policy header reflects configured value", %{conn: conn} do
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
test "referrer-policy header reflects configured value", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
assert Conn.get_resp_header(resp, "referrer-policy") == ["same-origin"]
|
||||
|
||||
clear_config([:http_security, :referrer_policy], "no-referrer")
|
||||
base_config
|
||||
|> Keyword.put(:referrer_policy, "no-referrer")
|
||||
|> mock_config
|
||||
|
||||
resp = get(conn, "/api/v1/instance")
|
||||
|
||||
assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
|
||||
end
|
||||
|
||||
test "it sends `report-to` & `report-uri` CSP response headers", %{conn: conn} do
|
||||
test "it sends `report-to` & `report-uri` CSP response headers", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
|
|
@ -65,7 +118,11 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
"{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
|
||||
end
|
||||
|
||||
test "default values for img-src and media-src with disabled media proxy", %{conn: conn} do
|
||||
test "default values for img-src and media-src with disabled media proxy", %{
|
||||
conn: conn,
|
||||
base_config: base_config
|
||||
} do
|
||||
mock_config(base_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
[csp] = Conn.get_resp_header(conn, "content-security-policy")
|
||||
|
|
@ -73,60 +130,129 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert csp =~ "img-src 'self' data: blob: https:;"
|
||||
end
|
||||
|
||||
test "it sets the Service-Worker-Allowed header", %{conn: conn} do
|
||||
clear_config([:http_security, :enabled], true)
|
||||
clear_config([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
|
||||
test "it sets the Service-Worker-Allowed header", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:enabled, true)
|
||||
|
||||
clear_config([:frontends, :available], %{
|
||||
"fedi-fe" => %{
|
||||
"name" => "fedi-fe",
|
||||
"custom-http-headers" => [{"service-worker-allowed", "/"}]
|
||||
}
|
||||
})
|
||||
additional_config =
|
||||
%{}
|
||||
|> Map.put([:frontends, :primary], %{"name" => "fedi-fe", "ref" => "develop"})
|
||||
|> Map.put(
|
||||
[:frontends, :available],
|
||||
%{
|
||||
"fedi-fe" => %{
|
||||
"name" => "fedi-fe",
|
||||
"custom-http-headers" => [{"service-worker-allowed", "/"}]
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
assert Conn.get_resp_header(conn, "service-worker-allowed") == ["/"]
|
||||
end
|
||||
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)
|
||||
setup %{base_config: base_config} do
|
||||
base_config =
|
||||
base_config
|
||||
|> Keyword.put(:enabled, true)
|
||||
|
||||
additional_config =
|
||||
%{}
|
||||
|> Map.put([:media_proxy, :enabled], true)
|
||||
|> Map.put([:media_proxy, :proxy_opts, :redirect_on_failure], false)
|
||||
|> Map.put([:media_proxy, :whitelist], [])
|
||||
|
||||
%{base_config: base_config, additional_config: additional_config}
|
||||
end
|
||||
|
||||
test "media_proxy with base_url", %{conn: conn} do
|
||||
test "media_proxy with base_url", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example.com"
|
||||
clear_config([:media_proxy, :base_url], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :base_url], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "upload with base url", %{conn: conn} do
|
||||
test "upload with base url", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example2.com"
|
||||
clear_config([Pleroma.Upload, :base_url], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Upload, :base_url], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "with S3 public endpoint", %{conn: conn} do
|
||||
test "with S3 public endpoint", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
url = "https://example3.com"
|
||||
clear_config([Pleroma.Uploaders.S3, :public_endpoint], url)
|
||||
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Uploaders.S3, :public_endpoint], url)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, url)
|
||||
end
|
||||
|
||||
test "with captcha endpoint", %{conn: conn} do
|
||||
clear_config([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
|
||||
test "with captcha endpoint", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([Pleroma.Captcha.Mock, :endpoint], "https://captcha.com")
|
||||
|> Map.put([Pleroma.Captcha, :method], Pleroma.Captcha.Mock)
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "https://captcha.com")
|
||||
end
|
||||
|
||||
test "with media_proxy whitelist", %{conn: conn} do
|
||||
clear_config([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
||||
test "with media_proxy whitelist", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :whitelist], ["https://example6.com", "https://example7.com"])
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "https://example7.com https://example6.com")
|
||||
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"])
|
||||
test "with media_proxy bare domains whitelist (deprecated)", %{
|
||||
conn: conn,
|
||||
base_config: base_config,
|
||||
additional_config: additional_config
|
||||
} do
|
||||
additional_config =
|
||||
additional_config
|
||||
|> Map.put([:media_proxy, :whitelist], ["example4.com", "example5.com"])
|
||||
|
||||
mock_config(base_config, additional_config)
|
||||
assert_media_img_src(conn, "example5.com example4.com")
|
||||
end
|
||||
end
|
||||
|
|
@ -138,8 +264,10 @@ defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
|
|||
assert csp =~ "img-src 'self' data: blob: #{url};"
|
||||
end
|
||||
|
||||
test "it does not send CSP headers when disabled", %{conn: conn} do
|
||||
clear_config([:http_security, :enabled], false)
|
||||
test "it does not send CSP headers when disabled", %{conn: conn, base_config: base_config} do
|
||||
base_config
|
||||
|> Keyword.put(:enabled, false)
|
||||
|> mock_config
|
||||
|
||||
conn = get(conn, "/api/v1/instance")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue