1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.HTTPSecurityPlug do
11 def init(opts), do: opts
13 def call(conn, _options) do
14 if Config.get([:http_security, :enabled]) do
16 |> merge_resp_headers(headers(conn))
17 |> maybe_send_sts_header(Config.get([:http_security, :sts]))
23 def primary_frontend do
24 with %{"name" => frontend} <- Config.get([:frontends, :primary]),
25 available <- Config.get([:frontends, :available]),
26 %{} = primary_frontend <- Map.get(available, frontend) do
27 {:ok, primary_frontend}
31 def custom_http_frontend_headers do
32 with {:ok, %{"custom-http-headers" => custom_headers}} <- primary_frontend() do
39 @spec headers(Plug.Conn.t()) :: [{String.t(), String.t()}]
41 referrer_policy = Config.get([:http_security, :referrer_policy])
42 report_uri = Config.get([:http_security, :report_uri])
43 custom_http_frontend_headers = custom_http_frontend_headers()
46 {"x-xss-protection", "0"},
47 {"x-permitted-cross-domain-policies", "none"},
48 {"x-frame-options", "DENY"},
49 {"x-content-type-options", "nosniff"},
50 {"referrer-policy", referrer_policy},
51 {"content-security-policy", csp_string(conn)},
52 {"permissions-policy", "interest-cohort=()"}
56 if custom_http_frontend_headers do
57 custom_http_frontend_headers ++ headers
64 "group" => "csp-endpoint",
65 "max-age" => 10_886_400,
67 %{"url" => report_uri}
71 [{"report-to", Jason.encode!(report_group)} | headers]
80 "frame-ancestors 'none'",
84 @csp_start [Enum.join(static_csp_rules, ";") <> ";"]
86 defp csp_string(conn) do
87 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
88 static_url = Pleroma.Web.Endpoint.static_url()
89 websocket_url = Pleroma.Web.Endpoint.websocket_url()
90 report_uri = Config.get([:http_security, :report_uri])
91 %{assigns: %{csp_nonce: nonce}} = conn
92 nonce_tag = "nonce-" <> nonce
93 img_src = "img-src 'self' data: blob:"
94 media_src = "media-src 'self'"
96 # Strict multimedia CSP enforcement only when MediaProxy is enabled
97 {img_src, media_src} =
98 if Config.get([:media_proxy, :enabled]) &&
99 !Config.get([:media_proxy, :proxy_opts, :redirect_on_failure]) do
100 sources = build_csp_multimedia_source_list()
101 {[img_src, sources], [media_src, sources]}
103 {[img_src, " https:"], [media_src, " https:"]}
107 if Config.get([:media_proxy, :enabled]) do
108 sources = build_csp_multimedia_source_list()
109 ["connect-src 'self' ", static_url, ?\s, websocket_url, ?\s, sources]
111 ["connect-src 'self' ", static_url, ?\s, websocket_url]
114 style_src = "style-src 'self' '#{nonce_tag}'"
115 font_src = "font-src 'self'"
117 script_src = "script-src 'self' '#{nonce_tag}'"
119 script_src = if Mix.env() == :dev, do: [script_src, " 'unsafe-eval'"], else: script_src
121 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
122 insecure = if scheme == "https", do: "upgrade-insecure-requests"
125 |> add_csp_param(img_src)
126 |> add_csp_param(media_src)
127 |> add_csp_param(connect_src)
128 |> add_csp_param(script_src)
129 |> add_csp_param(font_src)
130 |> add_csp_param(style_src)
131 |> add_csp_param(insecure)
132 |> add_csp_param(report)
133 |> :erlang.iolist_to_binary()
136 defp build_csp_from_whitelist([], acc), do: acc
138 defp build_csp_from_whitelist([last], acc) do
139 [build_csp_param_from_whitelist(last) | acc]
142 defp build_csp_from_whitelist([head | tail], acc) do
143 build_csp_from_whitelist(tail, [[?\s, build_csp_param_from_whitelist(head)] | acc])
146 # TODO: use `build_csp_param/1` after removing support bare domains for media proxy whitelist
147 defp build_csp_param_from_whitelist("http" <> _ = url) do
151 defp build_csp_param_from_whitelist(url), do: url
153 defp build_csp_multimedia_source_list do
154 media_proxy_whitelist =
155 [:media_proxy, :whitelist]
157 |> build_csp_from_whitelist([])
159 captcha_method = Config.get([Pleroma.Captcha, :method])
160 captcha_endpoint = Config.get([captcha_method, :endpoint])
164 [:media_proxy, :base_url],
165 [Pleroma.Upload, :base_url],
166 [Pleroma.Uploaders.S3, :public_endpoint]
168 |> Enum.map(&Config.get/1)
170 [captcha_endpoint | base_endpoints]
171 |> Enum.map(&build_csp_param/1)
172 |> Enum.reduce([], &add_source(&2, &1))
173 |> add_source(media_proxy_whitelist)
176 defp add_source(iodata, nil), do: iodata
177 defp add_source(iodata, []), do: iodata
178 defp add_source(iodata, source), do: [[?\s, source] | iodata]
180 defp add_csp_param(csp_iodata, nil), do: csp_iodata
182 defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
184 defp build_csp_param(nil), do: nil
186 defp build_csp_param(url) when is_binary(url) do
187 %{host: host, scheme: scheme} = URI.parse(url)
190 [scheme, "://", host]
194 def warn_if_disabled do
195 unless Config.get([:http_security, :enabled]) do
207 vY. .. .nMMM@MMU. ;1v.
208 i7i ... .#MM@M@C. .....:71i
209 it: .... $MMM@9;.,i;;;i,;tti
210 :t7. ..... 0MMMWv.,iii:::,,;St.
211 .nC. ..... IMMMQ..,::::::,.,czX.
212 .ct: ....... .ZMMMI..,:::::::,,:76Y.
213 c2: ......,i..Y$M@t..:::::::,,..inZY
214 vov ......:ii..c$MBc..,,,,,,,,,,..iI9i
215 i9Y ......iii:..7@MA,..,,,,,,,,,....;AA:
216 iIS. ......:ii::..;@MI....,............;Ez.
217 .I9. ......:i::::...8M1..................C0z.
218 .z9; ......:i::::,.. .i:...................zWX.
219 vbv ......,i::::,,. ................. :AQY
220 c6Y. .,...,::::,,..:t0@@QY. ................ :8bi
221 :6S. ..,,...,:::,,,..EMMMMMMI. ............... .;bZ,
222 :6o, .,,,,..:::,,,..i#MMMMMM#v................. YW2.
223 .n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn
224 7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi
225 7C...::::::::::::,,,,.. .................... vSi.
226 ;1;...,,::::::,......... .................. Yz:
228 izAotX7777777777777777777777777777777777777777Y7n92:
229 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
231 HTTP Security is disabled. Please re-enable it to prevent users from attacking
232 your instance and your users via malicious posts:
234 config :pleroma, :http_security, enabled: true
239 defp maybe_send_sts_header(conn, true) do
240 max_age_sts = Config.get([:http_security, :sts_max_age])
242 merge_resp_headers(conn, [
243 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains; preload"}
247 defp maybe_send_sts_header(conn, _), do: conn