2021-01-25 14:55:52 +00: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
|
|
|
|
import Plug.Conn
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
def init(opts), do: opts
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
def call(conn, options \\ []) do
|
2021-01-25 14:55:52 +00:00
|
|
|
if Config.get([:http_security, :enabled]) do
|
|
|
|
conn
|
2021-06-28 09:57:11 +00:00
|
|
|
|> merge_resp_headers(headers(options))
|
2021-01-25 14:55:52 +00:00
|
|
|
|> maybe_send_sts_header(Config.get([:http_security, :sts]))
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
defp headers(options) do
|
|
|
|
referrer_policy =
|
|
|
|
Keyword.get(options, :referrer_policy, Config.get([:http_security, :referrer_policy]))
|
2021-01-25 14:55:52 +00:00
|
|
|
|
|
|
|
[
|
2021-04-13 17:30:25 +00:00
|
|
|
{"x-xss-protection", "0"},
|
|
|
|
{"x-frame-options", "DENY"},
|
|
|
|
{"x-content-type-options", "nosniff"},
|
2021-01-25 14:55:52 +00:00
|
|
|
{"referrer-policy", referrer_policy},
|
2021-06-28 09:57:11 +00:00
|
|
|
{"content-security-policy", csp_string(options)}
|
2021-01-25 14:55:52 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
@static_csp_rules [
|
|
|
|
"default-src 'none'",
|
|
|
|
"base-uri 'self'",
|
|
|
|
"manifest-src 'self'"
|
|
|
|
]
|
|
|
|
|
|
|
|
@csp_start [Enum.join(@static_csp_rules, ";") <> ";"]
|
|
|
|
|
2021-04-13 17:30:25 +00: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 16:30:34 +00:00
|
|
|
@script_src "script-src 'self' 'unsafe-eval' "
|
2021-04-13 17:30:25 +00:00
|
|
|
@style_src "style-src 'self' "
|
|
|
|
@font_src "font-src 'self' "
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
defp csp_string(options) do
|
|
|
|
scheme = Keyword.get(options, :scheme, Config.get([Pleroma.Web.Endpoint, :url])[:scheme])
|
2021-01-25 14:55:52 +00:00
|
|
|
static_url = Mobilizon.Web.Endpoint.static_url()
|
|
|
|
websocket_url = Mobilizon.Web.Endpoint.websocket_url()
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
img_src = [@img_src | get_csp_config(:img_src, options)]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
media_src = [@media_src | get_csp_config(:media_src, options)]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
2021-04-13 17:30:25 +00:00
|
|
|
connect_src = [
|
|
|
|
@connect_src,
|
|
|
|
static_url,
|
|
|
|
?\s,
|
|
|
|
websocket_url,
|
|
|
|
?\s,
|
2021-06-28 09:57:11 +00:00
|
|
|
get_csp_config(:connect_src, options)
|
2021-04-13 17:30:25 +00:00
|
|
|
]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
|
|
|
script_src =
|
|
|
|
if Config.get(:env) == :dev do
|
2021-07-01 16:30:34 +00:00
|
|
|
[@script_src, "'unsafe-inline' "]
|
2021-01-25 14:55:52 +00:00
|
|
|
else
|
2021-04-13 17:30:25 +00:00
|
|
|
[
|
|
|
|
@script_src,
|
|
|
|
"'sha256-4RS22DYeB7U14dra4KcQYxmwt5HkOInieXK1NUMBmQI=' "
|
|
|
|
]
|
2021-01-25 14:55:52 +00:00
|
|
|
end
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
script_src = [script_src | get_csp_config(:script_src, options)]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
style_src =
|
|
|
|
if Config.get(:env) == :dev, do: [@style_src | "'unsafe-inline' "], else: @style_src
|
2021-01-25 14:55:52 +00:00
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
style_src = [style_src | get_csp_config(:style_src, options)]
|
|
|
|
|
|
|
|
font_src = [@font_src | get_csp_config(:font_src, options)]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
|
|
|
frame_src = if Config.get(:env) == :dev, do: "frame-src 'self' ", else: "frame-src 'none' "
|
2021-06-28 09:57:11 +00:00
|
|
|
frame_src = [frame_src | get_csp_config(:frame_src, options)]
|
2021-01-25 14:55:52 +00:00
|
|
|
|
|
|
|
frame_ancestors =
|
|
|
|
if Config.get(:env) == :dev, do: "frame-ancestors 'self' ", else: "frame-ancestors 'none' "
|
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
frame_ancestors = [frame_ancestors | get_csp_config(:frame_ancestors, options)]
|
2021-01-25 14:55:52 +00: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)
|
2021-04-13 17:30:25 +00:00
|
|
|
|> to_string()
|
2021-01-25 14:55:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp add_csp_param(csp_iodata, nil), do: csp_iodata
|
|
|
|
|
|
|
|
defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
defp maybe_send_sts_header(conn, _), do: conn
|
2021-04-13 17:30:25 +00:00
|
|
|
|
2021-06-28 09:57:11 +00:00
|
|
|
defp get_csp_config(type, options) do
|
|
|
|
options
|
|
|
|
|> Keyword.get(type, Config.get([:http_security, :csp_policy, type]))
|
|
|
|
|> Enum.join(" ")
|
|
|
|
end
|
2021-01-25 14:55:52 +00:00
|
|
|
end
|