f9aff2faba03e93bdcbfa352d4102278a4471ed4
[akkoma] / lib / pleroma / plugs / http_security_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.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())
17 |> maybe_send_sts_header(Config.get([:http_security, :sts]))
18 else
19 conn
20 end
21 end
22
23 defp headers do
24 referrer_policy = Config.get([:http_security, :referrer_policy])
25 report_uri = Config.get([:http_security, :report_uri])
26
27 headers = [
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()}
35 ]
36
37 if report_uri do
38 report_group = %{
39 "group" => "csp-endpoint",
40 "max-age" => 10_886_400,
41 "endpoints" => [
42 %{"url" => report_uri}
43 ]
44 }
45
46 [{"reply-to", Jason.encode!(report_group)} | headers]
47 else
48 headers
49 end
50 end
51
52 @csp_start [
53 "default-src 'none'",
54 "base-uri 'self'",
55 "frame-ancestors 'none'",
56 "style-src 'self' 'unsafe-inline'",
57 "font-src 'self'",
58 "manifest-src 'self'"
59 ]
60 |> Enum.join(";")
61 |> Kernel.<>(";")
62 |> List.wrap()
63
64 defp csp_string do
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])
69
70 connect_src = ["connect-src 'self' ", static_url, ?\s, websocket_url]
71
72 connect_src =
73 if Pleroma.Config.get(:env) == :dev do
74 [connect_src," http://localhost:3035/"]
75 else
76 connect_src
77 end
78
79 script_src =
80 if Pleroma.Config.get(:env) == :dev do
81 "script-src 'self' 'unsafe-eval'"
82 else
83 "script-src 'self'"
84 end
85
86 report = if report_uri, do: ["report-uri ", report_uri, ";report-to csp-endpoint"]
87 insecure = if scheme == "https", do: "upgrade-insecure-requests"
88
89 @csp_start
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()
97 end
98
99 defp add_csp_param(csp_iodata, nil), do: csp_iodata
100
101 defp add_csp_param(csp_iodata, param), do: [[param, ?;] | csp_iodata]
102
103 def warn_if_disabled do
104 unless Config.get([:http_security, :enabled]) do
105 Logger.warn("
106 .i;;;;i.
107 iYcviii;vXY:
108 .YXi .i1c.
109 .YC. . in7.
110 .vc. ...... ;1c.
111 i7, .. .;1;
112 i7, .. ... .Y1i
113 ,7v .6MMM@; .YX,
114 .7;. ..IMMMMMM1 :t7.
115 .;Y. ;$MMMMMM9. :tc.
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:
136 v97,......... .voC.
137 izAotX7777777777777777777777777777777777777777Y7n92:
138 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
139
140 HTTP Security is disabled. Please re-enable it to prevent users from attacking
141 your instance and your users via malicious posts:
142
143 config :pleroma, :http_security, enabled: true
144 ")
145 end
146 end
147
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])
151
152 merge_resp_headers(conn, [
153 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
154 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
155 ])
156 end
157
158 defp maybe_send_sts_header(conn, _), do: conn
159 end