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'",
65 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
66 static_url = Pleroma.Web.Endpoint.static_url()
67 websocket_url = Pleroma.Web.Endpoint.websocket_url()
68 report_uri = Config.get([:http_security, :report_uri])
70 connect_src = ["connect-src 'self' ", static_url, ?\s, websocket_url]
73 if Pleroma.Config.get(:env) == :dev do
74 [connect_src," http://localhost:3035/"]
80 if Pleroma.Config.get(:env) == :dev do
81 "script-src 'self' 'unsafe-eval'"
86 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
87 insecure = if scheme == "https", do: "upgrade-insecure-requests"
90 |> add_csp_param("img-src 'self' data: blob: https:")
91 |> add_csp_param("media-src 'self' https:")
92 |> add_csp_param(connect_src)
93 |> add_csp_param(script_src)
94 |> add_csp_param(insecure)
95 |> add_csp_param(report)
96 |> :erlang.iolist_to_binary()
99 defp add_csp_param(csp_iodata, nil), do: csp_iodata
101 defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
103 def warn_if_disabled do
104 unless Config.get([:http_security, :enabled]) do
116 vY. .. .nMMM@MMU. ;1v.
117 i7i ... .#MM@M@C. .....:71i
118 it: .... $MMM@9;.,i;;;i,;tti
119 :t7. ..... 0MMMWv.,iii:::,,;St.
120 .nC. ..... IMMMQ..,::::::,.,czX.
121 .ct: ....... .ZMMMI..,:::::::,,:76Y.
122 c2: ......,i..Y$M@t..:::::::,,..inZY
123 vov ......:ii..c$MBc..,,,,,,,,,,..iI9i
124 i9Y ......iii:..7@MA,..,,,,,,,,,....;AA:
125 iIS. ......:ii::..;@MI....,............;Ez.
126 .I9. ......:i::::...8M1..................C0z.
127 .z9; ......:i::::,.. .i:...................zWX.
128 vbv ......,i::::,,. ................. :AQY
129 c6Y. .,...,::::,,..:t0@@QY. ................ :8bi
130 :6S. ..,,...,:::,,,..EMMMMMMI. ............... .;bZ,
131 :6o, .,,,,..:::,,,..i#MMMMMM#v................. YW2.
132 .n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn
133 7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi
134 7C...::::::::::::,,,,.. .................... vSi.
135 ;1;...,,::::::,......... .................. Yz:
137 izAotX7777777777777777777777777777777777777777Y7n92:
138 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
140 HTTP Security is disabled. Please re-enable it to prevent users from attacking
141 your instance and your users via malicious posts:
143 config :pleroma, :http_security, enabled: true
148 defp maybe_send_sts_header(conn, true) do
149 max_age_sts = Config.get([:http_security, :sts_max_age])
150 max_age_ct = Config.get([:http_security, :ct_max_age])
152 merge_resp_headers(conn, [
153 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
154 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
158 defp maybe_send_sts_header(conn, _), do: conn