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())
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
40 referrer_policy = Config.get([:http_security, :referrer_policy])
41 report_uri = Config.get([:http_security, :report_uri])
42 custom_http_frontend_headers = custom_http_frontend_headers()
45 {"x-xss-protection", "1; mode=block"},
46 {"x-permitted-cross-domain-policies", "none"},
47 {"x-frame-options", "DENY"},
48 {"x-content-type-options", "nosniff"},
49 {"referrer-policy", referrer_policy},
50 {"x-download-options", "noopen"},
51 {"content-security-policy", csp_string()},
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 [{"reply-to", Jason.encode!(report_group)} | headers]
80 "frame-ancestors 'none'",
81 "style-src 'self' 'unsafe-inline'",
86 @csp_start [Enum.join(static_csp_rules, ";") <> ";"]
89 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
90 static_url = Pleroma.Web.Endpoint.static_url()
91 websocket_url = Pleroma.Web.Endpoint.websocket_url()
92 report_uri = Config.get([:http_security, :report_uri])
94 img_src = "img-src 'self' data: blob:"
95 media_src = "media-src 'self'"
97 # Strict multimedia CSP enforcement only when MediaProxy is enabled
98 {img_src, media_src} =
99 if Config.get([:media_proxy, :enabled]) &&
100 !Config.get([:media_proxy, :proxy_opts, :redirect_on_failure]) do
101 sources = build_csp_multimedia_source_list()
102 {[img_src, sources], [media_src, sources]}
104 {[img_src, " https:"], [media_src, " https:"]}
107 connect_src = ["connect-src 'self' blob: ", static_url, ?\s, websocket_url]
110 if Config.get(:env) == :dev do
111 [connect_src, " http://localhost:3035/"]
117 if Config.get(:env) == :dev do
118 "script-src 'self' 'unsafe-eval'"
123 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
124 insecure = if scheme == "https", do: "upgrade-insecure-requests"
127 |> add_csp_param(img_src)
128 |> add_csp_param(media_src)
129 |> add_csp_param(connect_src)
130 |> add_csp_param(script_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])
241 max_age_ct = Config.get([:http_security, :ct_max_age])
243 merge_resp_headers(conn, [
244 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
245 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
249 defp maybe_send_sts_header(conn, _), do: conn