make bulk user creation from admin works as a transaction
[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 scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
37 static_url = Pleroma.Web.Endpoint.static_url()
38 websocket_url = Pleroma.Web.Endpoint.websocket_url()
39
40 connect_src = "connect-src 'self' #{static_url} #{websocket_url}"
41
42 connect_src =
43 if Mix.env() == :dev do
44 connect_src <> " http://localhost:3035/"
45 else
46 connect_src
47 end
48
49 script_src =
50 if Mix.env() == :dev do
51 "script-src 'self' 'unsafe-eval'"
52 else
53 "script-src 'self'"
54 end
55
56 [
57 "default-src 'none'",
58 "base-uri 'self'",
59 "frame-ancestors 'none'",
60 "img-src 'self' data: https:",
61 "media-src 'self' https:",
62 "style-src 'self' 'unsafe-inline'",
63 "font-src 'self'",
64 "manifest-src 'self'",
65 connect_src,
66 script_src,
67 if scheme == "https" do
68 "upgrade-insecure-requests"
69 end
70 ]
71 |> Enum.join("; ")
72 end
73
74 defp maybe_send_sts_header(conn, true) do
75 max_age_sts = Config.get([:http_security, :sts_max_age])
76 max_age_ct = Config.get([:http_security, :ct_max_age])
77
78 merge_resp_headers(conn, [
79 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
80 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
81 ])
82 end
83
84 defp maybe_send_sts_header(conn, _), do: conn
85 end