Remove _misskey_reaction matching (#500)
[akkoma] / lib / pleroma / web / plugs / http_security_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Plugs.HTTPSecurityPlug do
6 alias Pleroma.Config
7 import Plug.Conn
8
9 require Logger
10
11 def init(opts), do: opts
12
13 def call(conn, _options) do
14 if Config.get([:http_security, :enabled]) do
15 conn
16 |> merge_resp_headers(headers(conn))
17 |> maybe_send_sts_header(Config.get([:http_security, :sts]))
18 else
19 conn
20 end
21 end
22
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}
28 end
29 end
30
31 def custom_http_frontend_headers do
32 with {:ok, %{"custom-http-headers" => custom_headers}} <- primary_frontend() do
33 custom_headers
34 else
35 _ -> []
36 end
37 end
38
39 @spec headers(Plug.Conn.t()) :: [{String.t(), String.t()}]
40 def headers(conn) do
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()
44
45 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=()"}
53 ]
54
55 headers =
56 if custom_http_frontend_headers do
57 custom_http_frontend_headers ++ headers
58 else
59 headers
60 end
61
62 if report_uri do
63 report_group = %{
64 "group" => "csp-endpoint",
65 "max-age" => 10_886_400,
66 "endpoints" => [
67 %{"url" => report_uri}
68 ]
69 }
70
71 [{"report-to", Jason.encode!(report_group)} | headers]
72 else
73 headers
74 end
75 end
76
77 static_csp_rules = [
78 "default-src 'none'",
79 "base-uri 'none'",
80 "frame-ancestors 'none'",
81 "manifest-src 'self'"
82 ]
83
84 @csp_start [Enum.join(static_csp_rules, ";") <> ";"]
85
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'"
95
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]}
102 else
103 {[img_src, " https:"], [media_src, " https:"]}
104 end
105
106 connect_src =
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]
110 else
111 ["connect-src 'self' ", static_url, ?\s, websocket_url]
112 end
113
114 style_src = "style-src 'self' '#{nonce_tag}'"
115 font_src = "font-src 'self'"
116
117 script_src = "script-src 'self' '#{nonce_tag}'"
118
119 script_src = if Mix.env() == :dev, do: [script_src, " 'unsafe-eval'"], else: script_src
120
121 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
122 insecure = if scheme == "https", do: "upgrade-insecure-requests"
123
124 @csp_start
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()
134 end
135
136 defp build_csp_from_whitelist([], acc), do: acc
137
138 defp build_csp_from_whitelist([last], acc) do
139 [build_csp_param_from_whitelist(last) | acc]
140 end
141
142 defp build_csp_from_whitelist([head | tail], acc) do
143 build_csp_from_whitelist(tail, [[?\s, build_csp_param_from_whitelist(head)] | acc])
144 end
145
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
148 build_csp_param(url)
149 end
150
151 defp build_csp_param_from_whitelist(url), do: url
152
153 defp build_csp_multimedia_source_list do
154 media_proxy_whitelist =
155 [:media_proxy, :whitelist]
156 |> Config.get()
157 |> build_csp_from_whitelist([])
158
159 captcha_method = Config.get([Pleroma.Captcha, :method])
160 captcha_endpoint = Config.get([captcha_method, :endpoint])
161
162 base_endpoints =
163 [
164 [:media_proxy, :base_url],
165 [Pleroma.Upload, :base_url],
166 [Pleroma.Uploaders.S3, :public_endpoint]
167 ]
168 |> Enum.map(&Config.get/1)
169
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)
174 end
175
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]
179
180 defp add_csp_param(csp_iodata, nil), do: csp_iodata
181
182 defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
183
184 defp build_csp_param(nil), do: nil
185
186 defp build_csp_param(url) when is_binary(url) do
187 %{host: host, scheme: scheme} = URI.parse(url)
188
189 if scheme do
190 [scheme, "://", host]
191 end
192 end
193
194 def warn_if_disabled do
195 unless Config.get([:http_security, :enabled]) do
196 Logger.warn("
197 .i;;;;i.
198 iYcviii;vXY:
199 .YXi .i1c.
200 .YC. . in7.
201 .vc. ...... ;1c.
202 i7, .. .;1;
203 i7, .. ... .Y1i
204 ,7v .6MMM@; .YX,
205 .7;. ..IMMMMMM1 :t7.
206 .;Y. ;$MMMMMM9. :tc.
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:
227 v97,......... .voC.
228 izAotX7777777777777777777777777777777777777777Y7n92:
229 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
230
231 HTTP Security is disabled. Please re-enable it to prevent users from attacking
232 your instance and your users via malicious posts:
233
234 config :pleroma, :http_security, enabled: true
235 ")
236 end
237 end
238
239 defp maybe_send_sts_header(conn, true) do
240 max_age_sts = Config.get([:http_security, :sts_max_age])
241
242 merge_resp_headers(conn, [
243 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains; preload"}
244 ])
245 end
246
247 defp maybe_send_sts_header(conn, _), do: conn
248 end