1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.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]))
24 referrer_policy = Config.get([:http_security, :referrer_policy])
25 report_uri = Config.get([:http_security, :report_uri])
28 {"x-xss-protection", "1; mode=block"},
29 {"x-permitted-cross-domain-policies", "none"},
30 {"x-frame-options", "DENY"},
31 {"x-content-type-options", "nosniff"},
32 {"referrer-policy", referrer_policy},
33 {"x-download-options", "noopen"},
34 {"content-security-policy", csp_string()}
39 "group" => "csp-endpoint",
40 "max-age" => 10_886_400,
42 %{"url" => report_uri}
46 [{"reply-to", Jason.encode!(report_group)} | headers]
55 "frame-ancestors 'none'",
56 "style-src 'self' 'unsafe-inline'",
61 @csp_start [Enum.join(static_csp_rules, ";") <> ";"]
64 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
65 static_url = Pleroma.Web.Endpoint.static_url()
66 websocket_url = Pleroma.Web.Endpoint.websocket_url()
67 report_uri = Config.get([:http_security, :report_uri])
69 img_src = "img-src 'self' data: blob:"
70 media_src = "media-src 'self'"
72 # Strict multimedia CSP enforcement only when MediaProxy is enabled
73 {img_src, media_src} =
74 if Config.get([:media_proxy, :enabled]) &&
75 !Config.get([:media_proxy, :proxy_opts, :redirect_on_failure]) do
76 sources = build_csp_multimedia_source_list()
77 {[img_src, sources], [media_src, sources]}
79 {[img_src, " https:"], [media_src, " https:"]}
82 connect_src = ["connect-src 'self' blob: ", static_url, ?\s, websocket_url]
85 if Config.get(:env) == :dev do
86 [connect_src, " http://localhost:3035/"]
92 if Config.get(:env) == :dev do
93 "script-src 'self' 'unsafe-eval'"
98 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
99 insecure = if scheme == "https", do: "upgrade-insecure-requests"
102 |> add_csp_param(img_src)
103 |> add_csp_param(media_src)
104 |> add_csp_param(connect_src)
105 |> add_csp_param(script_src)
106 |> add_csp_param(insecure)
107 |> add_csp_param(report)
108 |> :erlang.iolist_to_binary()
111 defp build_csp_multimedia_source_list do
112 media_proxy_whitelist =
113 Enum.reduce(Config.get([:media_proxy, :whitelist]), [], fn host, acc ->
114 add_source(acc, host)
117 media_proxy_base_url = build_csp_param(Config.get([:media_proxy, :base_url]))
119 upload_base_url = build_csp_param(Config.get([Pleroma.Upload, :base_url]))
121 s3_endpoint = build_csp_param(Config.get([Pleroma.Uploaders.S3, :public_endpoint]))
123 captcha_method = Config.get([Pleroma.Captcha, :method])
125 captcha_endpoint = build_csp_param(Config.get([captcha_method, :endpoint]))
128 |> add_source(media_proxy_base_url)
129 |> add_source(upload_base_url)
130 |> add_source(s3_endpoint)
131 |> add_source(media_proxy_whitelist)
132 |> add_source(captcha_endpoint)
135 defp add_source(iodata, nil), do: iodata
136 defp add_source(iodata, source), do: [[?\s, source] | iodata]
138 defp add_csp_param(csp_iodata, nil), do: csp_iodata
140 defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
142 defp build_csp_param(nil), do: nil
144 defp build_csp_param(url) when is_binary(url) do
145 %{host: host, scheme: scheme} = URI.parse(url)
148 [scheme, "://", host]
152 def warn_if_disabled do
153 unless Config.get([:http_security, :enabled]) do
165 vY. .. .nMMM@MMU. ;1v.
166 i7i ... .#MM@M@C. .....:71i
167 it: .... $MMM@9;.,i;;;i,;tti
168 :t7. ..... 0MMMWv.,iii:::,,;St.
169 .nC. ..... IMMMQ..,::::::,.,czX.
170 .ct: ....... .ZMMMI..,:::::::,,:76Y.
171 c2: ......,i..Y$M@t..:::::::,,..inZY
172 vov ......:ii..c$MBc..,,,,,,,,,,..iI9i
173 i9Y ......iii:..7@MA,..,,,,,,,,,....;AA:
174 iIS. ......:ii::..;@MI....,............;Ez.
175 .I9. ......:i::::...8M1..................C0z.
176 .z9; ......:i::::,.. .i:...................zWX.
177 vbv ......,i::::,,. ................. :AQY
178 c6Y. .,...,::::,,..:t0@@QY. ................ :8bi
179 :6S. ..,,...,:::,,,..EMMMMMMI. ............... .;bZ,
180 :6o, .,,,,..:::,,,..i#MMMMMM#v................. YW2.
181 .n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn
182 7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi
183 7C...::::::::::::,,,,.. .................... vSi.
184 ;1;...,,::::::,......... .................. Yz:
186 izAotX7777777777777777777777777777777777777777Y7n92:
187 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
189 HTTP Security is disabled. Please re-enable it to prevent users from attacking
190 your instance and your users via malicious posts:
192 config :pleroma, :http_security, enabled: true
197 defp maybe_send_sts_header(conn, true) do
198 max_age_sts = Config.get([:http_security, :sts_max_age])
199 max_age_ct = Config.get([:http_security, :ct_max_age])
201 merge_resp_headers(conn, [
202 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
203 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
207 defp maybe_send_sts_header(conn, _), do: conn