Removed file as requested
[akkoma] / lib / pleroma / plugs / http_security_plug.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 def init(opts), do: opts
10
11 def call(conn, _options) do
12 if Config.get([:http_security, :enabled]) do
13 conn
14 |> merge_resp_headers(headers())
15 |> maybe_send_sts_header(Config.get([:http_security, :sts]))
16 else
17 conn
18 end
19 end
20
21 defp headers do
22 referrer_policy = Config.get([:http_security, :referrer_policy])
23
24 [
25 {"x-xss-protection", "1; mode=block"},
26 {"x-permitted-cross-domain-policies", "none"},
27 {"x-frame-options", "DENY"},
28 {"x-content-type-options", "nosniff"},
29 {"referrer-policy", referrer_policy},
30 {"x-download-options", "noopen"},
31 {"content-security-policy", csp_string() <> ";"}
32 ]
33 end
34
35 defp csp_string do
36 protocol = Config.get([Pleroma.Web.Endpoint, :protocol])
37
38 [
39 "default-src 'none'",
40 "base-uri 'self'",
41 "frame-ancestors 'none'",
42 "img-src 'self' data: https:",
43 "media-src 'self' https:",
44 "style-src 'self' 'unsafe-inline'",
45 "font-src 'self'",
46 "script-src 'self'",
47 "connect-src 'self' " <> String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"),
48 "manifest-src 'self'",
49 if protocol == "https" do
50 "upgrade-insecure-requests"
51 end
52 ]
53 |> Enum.join("; ")
54 end
55
56 defp maybe_send_sts_header(conn, true) do
57 max_age_sts = Config.get([:http_security, :sts_max_age])
58 max_age_ct = Config.get([:http_security, :ct_max_age])
59
60 merge_resp_headers(conn, [
61 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
62 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
63 ])
64 end
65
66 defp maybe_send_sts_header(conn, _), do: conn
67 end