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 headers ++ [{"reply-to", Jason.encode!(report_group)}]
53 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
54 static_url = Pleroma.Web.Endpoint.static_url()
55 websocket_url = Pleroma.Web.Endpoint.websocket_url()
56 report_uri = Config.get([:http_security, :report_uri])
58 connect_src = "connect-src 'self' #{static_url} #{websocket_url}"
61 if Pleroma.Config.get(:env) == :dev do
62 connect_src <> " http://localhost:3035/"
68 if Pleroma.Config.get(:env) == :dev do
69 "script-src 'self' 'unsafe-eval'"
77 "frame-ancestors 'none'",
78 "img-src 'self' data: blob: https:",
79 "media-src 'self' https:",
80 "style-src 'self' 'unsafe-inline'",
82 "manifest-src 'self'",
87 report = if report_uri, do: ["report-uri #{report_uri}; report-to csp-endpoint"], else: []
89 insecure = if scheme == "https", do: ["upgrade-insecure-requests"], else: []
91 (main_part ++ report ++ insecure)
95 def warn_if_disabled do
96 unless Config.get([:http_security, :enabled]) do
108 vY. .. .nMMM@MMU. ;1v.
109 i7i ... .#MM@M@C. .....:71i
110 it: .... $MMM@9;.,i;;;i,;tti
111 :t7. ..... 0MMMWv.,iii:::,,;St.
112 .nC. ..... IMMMQ..,::::::,.,czX.
113 .ct: ....... .ZMMMI..,:::::::,,:76Y.
114 c2: ......,i..Y$M@t..:::::::,,..inZY
115 vov ......:ii..c$MBc..,,,,,,,,,,..iI9i
116 i9Y ......iii:..7@MA,..,,,,,,,,,....;AA:
117 iIS. ......:ii::..;@MI....,............;Ez.
118 .I9. ......:i::::...8M1..................C0z.
119 .z9; ......:i::::,.. .i:...................zWX.
120 vbv ......,i::::,,. ................. :AQY
121 c6Y. .,...,::::,,..:t0@@QY. ................ :8bi
122 :6S. ..,,...,:::,,,..EMMMMMMI. ............... .;bZ,
123 :6o, .,,,,..:::,,,..i#MMMMMM#v................. YW2.
124 .n8i ..,,,,,,,::,,,,.. tMMMMM@C:.................. .1Wn
125 7Uc. .:::,,,,,::,,,,.. i1t;,..................... .UEi
126 7C...::::::::::::,,,,.. .................... vSi.
127 ;1;...,,::::::,......... .................. Yz:
129 izAotX7777777777777777777777777777777777777777Y7n92:
130 .;CoIIIIIUAA666666699999ZZZZZZZZZZZZZZZZZZZZ6ov.
132 HTTP Security is disabled. Please re-enable it to prevent users from attacking
133 your instance and your users via malicious posts:
135 config :pleroma, :http_security, enabled: true
140 defp maybe_send_sts_header(conn, true) do
141 max_age_sts = Config.get([:http_security, :sts_max_age])
142 max_age_ct = Config.get([:http_security, :ct_max_age])
144 merge_resp_headers(conn, [
145 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
146 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
150 defp maybe_send_sts_header(conn, _), do: conn