2021-01-25 15:55:52 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Mobilizon.Web.Plugs.HTTPSecurityPlug do
|
|
|
|
@moduledoc """
|
|
|
|
A plug to setup some HTTP security-related headers, like CSP
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Config
|
2022-11-04 09:26:45 +01:00
|
|
|
alias Mobilizon.Service.{FrontEndAnalytics, GlobalSearch, Pictures}
|
2022-11-04 10:55:26 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2021-01-25 15:55:52 +01:00
|
|
|
import Plug.Conn
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec init(any()) :: any()
|
2021-01-25 15:55:52 +01:00
|
|
|
def init(opts), do: opts
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
|
2021-06-28 11:57:11 +02:00
|
|
|
def call(conn, options \\ []) do
|
2021-01-25 15:55:52 +01:00
|
|
|
if Config.get([:http_security, :enabled]) do
|
|
|
|
conn
|
2021-06-28 11:57:11 +02:00
|
|
|
|> merge_resp_headers(headers(options))
|
2021-09-10 11:27:59 +02:00
|
|
|
|> maybe_send_sts_header(Config.get([:http_security, :sts], false))
|
2021-01-25 15:55:52 +01:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec headers(Keyword.t()) :: list({String.t(), String.t()})
|
2021-06-28 11:57:11 +02:00
|
|
|
defp headers(options) do
|
|
|
|
referrer_policy =
|
|
|
|
Keyword.get(options, :referrer_policy, Config.get([:http_security, :referrer_policy]))
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2022-11-04 10:55:26 +01:00
|
|
|
report_uri = Config.get([:http_security, :report_uri])
|
|
|
|
|
|
|
|
headers = [
|
2021-04-13 19:30:25 +02:00
|
|
|
{"x-xss-protection", "0"},
|
|
|
|
{"x-frame-options", "DENY"},
|
|
|
|
{"x-content-type-options", "nosniff"},
|
2021-01-25 15:55:52 +01:00
|
|
|
{"referrer-policy", referrer_policy},
|
2021-06-28 11:57:11 +02:00
|
|
|
{"content-security-policy", csp_string(options)}
|
2021-01-25 15:55:52 +01:00
|
|
|
]
|
2022-11-04 10:55:26 +01:00
|
|
|
|
|
|
|
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
|
2021-01-25 15:55:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@static_csp_rules [
|
|
|
|
"default-src 'none'",
|
|
|
|
"base-uri 'self'",
|
|
|
|
"manifest-src 'self'"
|
|
|
|
]
|
|
|
|
|
|
|
|
@csp_start [Enum.join(@static_csp_rules, ";") <> ";"]
|
|
|
|
|
2021-04-13 19:30:25 +02:00
|
|
|
@img_src "img-src 'self' data: blob: "
|
|
|
|
@media_src "media-src 'self' "
|
|
|
|
# Connect-src is available for any origin (*) because of webfinger query to redirect to content
|
|
|
|
@connect_src "connect-src 'self' * blob: "
|
|
|
|
# unsafe-eval is because of JS issues with regenerator-runtime
|
2021-07-01 18:30:34 +02:00
|
|
|
@script_src "script-src 'self' 'unsafe-eval' "
|
2021-04-13 19:30:25 +02:00
|
|
|
@style_src "style-src 'self' "
|
|
|
|
@font_src "font-src 'self' "
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec csp_string(Keyword.t()) :: String.t()
|
2021-06-28 11:57:11 +02:00
|
|
|
defp csp_string(options) do
|
2022-11-04 10:55:26 +01:00
|
|
|
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])
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
img_src = [@img_src] ++ [get_csp_config(:img_src, options)]
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
media_src = [@media_src] ++ [get_csp_config(:media_src, options)]
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2021-04-13 19:30:25 +02:00
|
|
|
connect_src = [
|
|
|
|
@connect_src,
|
|
|
|
static_url,
|
|
|
|
?\s,
|
|
|
|
websocket_url,
|
|
|
|
?\s,
|
2021-06-28 11:57:11 +02:00
|
|
|
get_csp_config(:connect_src, options)
|
2021-04-13 19:30:25 +02:00
|
|
|
]
|
2021-01-25 15:55:52 +01:00
|
|
|
|
|
|
|
script_src =
|
|
|
|
if Config.get(:env) == :dev do
|
2021-07-01 18:30:34 +02:00
|
|
|
[@script_src, "'unsafe-inline' "]
|
2021-01-25 15:55:52 +01:00
|
|
|
else
|
2021-04-13 19:30:25 +02:00
|
|
|
[
|
|
|
|
@script_src,
|
2022-11-02 11:36:32 +01:00
|
|
|
"'sha256-4RS22DYeB7U14dra4KcQYxmwt5HkOInieXK1NUMBmQI=' ",
|
|
|
|
"'sha256-zJdRXhLWm9NGI6BFr+sNmHBBrjAdJdFr7MpUq0EwK58=' "
|
2021-04-13 19:30:25 +02:00
|
|
|
]
|
2021-01-25 15:55:52 +01:00
|
|
|
end
|
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
script_src = [script_src] ++ [get_csp_config(:script_src, options)]
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2021-06-28 11:57:11 +02:00
|
|
|
style_src =
|
|
|
|
if Config.get(:env) == :dev, do: [@style_src | "'unsafe-inline' "], else: @style_src
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
style_src = [style_src] ++ [get_csp_config(:style_src, options)]
|
2021-06-28 11:57:11 +02:00
|
|
|
|
2021-09-27 09:41:36 +02:00
|
|
|
font_src = [@font_src] ++ [get_csp_config(:font_src, options)]
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2022-11-04 16:10:24 +01:00
|
|
|
frame_src = build_csp_field(:frame_src, options)
|
|
|
|
frame_ancestors = build_csp_field(:frame_ancestors, options)
|
2021-01-25 15:55:52 +01:00
|
|
|
|
2022-11-04 10:55:26 +01:00
|
|
|
report = if report_uri, do: ["report-uri ", report_uri, " ; report-to csp-endpoint"]
|
2021-01-25 15:55:52 +01:00
|
|
|
insecure = if scheme == "https", do: "upgrade-insecure-requests"
|
|
|
|
|
|
|
|
@csp_start
|
|
|
|
|> add_csp_param(script_src)
|
|
|
|
|> add_csp_param(style_src)
|
|
|
|
|> add_csp_param(connect_src)
|
|
|
|
|> add_csp_param(img_src)
|
|
|
|
|> add_csp_param(media_src)
|
|
|
|
|> add_csp_param(font_src)
|
|
|
|
|> add_csp_param(frame_src)
|
|
|
|
|> add_csp_param(frame_ancestors)
|
|
|
|
|> add_csp_param(insecure)
|
2022-11-04 10:55:26 +01:00
|
|
|
|> add_csp_param(report)
|
2021-04-13 19:30:25 +02:00
|
|
|
|> to_string()
|
2021-01-25 15:55:52 +01:00
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec add_csp_param(iodata(), iodata() | nil) :: list()
|
2021-01-25 15:55:52 +01:00
|
|
|
defp add_csp_param(csp_iodata, nil), do: csp_iodata
|
|
|
|
defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec maybe_send_sts_header(Plug.Conn.t(), boolean()) :: Plug.Conn.t()
|
2021-01-25 15:55:52 +01:00
|
|
|
defp maybe_send_sts_header(conn, true) do
|
|
|
|
max_age_sts = Config.get([:http_security, :sts_max_age])
|
|
|
|
|
|
|
|
merge_resp_headers(conn, [
|
|
|
|
{"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"}
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
defp maybe_send_sts_header(conn, false), do: conn
|
2021-04-13 19:30:25 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec get_csp_config(atom(), Keyword.t()) :: iodata()
|
2021-06-28 11:57:11 +02:00
|
|
|
defp get_csp_config(type, options) do
|
2022-04-06 16:53:20 +02:00
|
|
|
config_policy = Keyword.get(options, type, Config.get([:http_security, :csp_policy, type]))
|
2022-11-04 16:10:24 +01:00
|
|
|
front_end_analytics_policy = Keyword.get(FrontEndAnalytics.csp(), type, [])
|
|
|
|
global_search_policy = Keyword.get(GlobalSearch.service().csp(), type, [])
|
|
|
|
pictures_policy = Keyword.get(Pictures.service().csp(), type, [])
|
2022-04-06 16:53:20 +02:00
|
|
|
|
2022-11-04 11:50:15 +01:00
|
|
|
resource_providers = Config.get([Mobilizon.Service.ResourceProviders, :csp_policy, type], [])
|
|
|
|
|
2022-11-04 09:26:45 +01:00
|
|
|
Enum.join(
|
2022-11-04 11:50:15 +01:00
|
|
|
config_policy ++
|
|
|
|
front_end_analytics_policy ++
|
|
|
|
global_search_policy ++ pictures_policy ++ resource_providers,
|
2022-11-04 09:26:45 +01:00
|
|
|
" "
|
|
|
|
)
|
2021-06-28 11:57:11 +02:00
|
|
|
end
|
2022-11-04 16:10:24 +01:00
|
|
|
|
|
|
|
defp build_csp_field(type, options) do
|
|
|
|
csp_config = get_csp_config(type, options)
|
|
|
|
|
|
|
|
csp_config =
|
|
|
|
if Config.get(:env) == :dev do
|
|
|
|
[csp_config] ++ ["'self'"]
|
|
|
|
else
|
|
|
|
if csp_config == "" do
|
|
|
|
["'none'"]
|
|
|
|
else
|
|
|
|
[csp_config]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Enum.join([type |> to_string() |> String.replace("_", "-")] ++ csp_config, " ")
|
|
|
|
end
|
2021-01-25 15:55:52 +01:00
|
|
|
end
|