1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-23 00:14:48 +00:00

Support CSP report_uri, report_to and the Report-To and Reporting-Endpoints headers

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-11-04 10:55:26 +01:00
parent e97206077c
commit 57fac37347
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
3 changed files with 50 additions and 5 deletions

View file

@ -88,6 +88,8 @@ config :mobilizon, Mobilizon.Web.Gettext, allowed_locales: ["fr", "en", "es", "r
config :junit_formatter, report_dir: "."
config :mobilizon, :http_security, report_uri: "https://endpoint.com"
if System.get_env("DOCKER", "false") == "false" && File.exists?("./config/test.secret.exs") do
import_config "test.secret.exs"
end

View file

@ -10,6 +10,7 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
alias Mobilizon.Config
alias Mobilizon.Service.{FrontEndAnalytics, GlobalSearch, Pictures}
alias Mobilizon.Web.Endpoint
import Plug.Conn
require Logger
@ -33,13 +34,32 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
referrer_policy =
Keyword.get(options, :referrer_policy, Config.get([:http_security, :referrer_policy]))
[
report_uri = Config.get([:http_security, :report_uri])
headers = [
{"x-xss-protection", "0"},
{"x-frame-options", "DENY"},
{"x-content-type-options", "nosniff"},
{"referrer-policy", referrer_policy},
{"content-security-policy", csp_string(options)}
]
if report_uri do
report_group = %{
"group" => "csp-endpoint",
"max-age" => 10_886_400,
"endpoints" => [
%{"url" => report_uri}
]
}
[
{"report-to", Jason.encode!(report_group)},
{"reporting-endpoints", "csp-endpoint=\"#{report_uri}\""} | headers
]
else
headers
end
end
@static_csp_rules [
@ -61,9 +81,10 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
@spec csp_string(Keyword.t()) :: String.t()
defp csp_string(options) do
scheme = Keyword.get(options, :scheme, Config.get([Pleroma.Web.Endpoint, :url])[:scheme])
static_url = Mobilizon.Web.Endpoint.static_url()
websocket_url = Mobilizon.Web.Endpoint.websocket_url()
scheme = Keyword.get(options, :scheme, Config.get([Endpoint, :url])[:scheme])
static_url = Endpoint.static_url()
websocket_url = Endpoint.websocket_url()
report_uri = Config.get([:http_security, :report_uri])
img_src = [@img_src] ++ [get_csp_config(:img_src, options)]
@ -106,6 +127,7 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
frame_ancestors = [frame_ancestors] ++ [get_csp_config(:frame_ancestors, options)]
report = if report_uri, do: ["report-uri ", report_uri, " ; report-to csp-endpoint"]
insecure = if scheme == "https", do: "upgrade-insecure-requests"
@csp_start
@ -118,6 +140,7 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
|> add_csp_param(frame_src)
|> add_csp_param(frame_ancestors)
|> add_csp_param(insecure)
|> add_csp_param(report)
|> to_string()
end

View file

@ -49,6 +49,26 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlugTest do
assert Conn.get_resp_header(resp, "referrer-policy") == ["no-referrer"]
end
test "it sends `report-to`, `reporting-endpoints` & `report-uri` CSP response headers", %{
conn: conn
} do
conn = post(conn, "/api")
[csp] = Conn.get_resp_header(conn, "content-security-policy")
assert csp =~ ~r|report-uri https://endpoint.com ; report-to csp-endpoint;|
[report_to] = Conn.get_resp_header(conn, "report-to")
assert report_to ==
"{\"endpoints\":[{\"url\":\"https://endpoint.com\"}],\"group\":\"csp-endpoint\",\"max-age\":10886400}"
[reporting_endpoints] = Conn.get_resp_header(conn, "reporting-endpoints")
assert reporting_endpoints ==
"csp-endpoint=\"https://endpoint.com\""
end
test "default values for content-security-policy are always included", %{conn: conn} do
conn = post(conn, "/api")
@ -73,7 +93,7 @@ defmodule Mobilizon.Web.Plugs.HTTPSecurityPlugTest do
[csp] = Conn.get_resp_header(conn, "content-security-policy")
assert csp =~
~r/script-src 'self' 'unsafe-eval' 'sha256-[\w+\/=]*' 'sha256-[\w+\/=]*' example.com matomo.example.com ;/
~r/script-src 'self' 'unsafe-eval' 'sha256-[\w+\/=]*' 'sha256-[\w+\/=]*' example.com matomo.example.com\s+;/
end
end