http_security_plug.ex 2.23 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

kaniini's avatar
kaniini committed
5
defmodule Pleroma.Plugs.HTTPSecurityPlug do
kaniini's avatar
kaniini committed
6
  alias Pleroma.Config
kaniini's avatar
kaniini committed
7
8
9
10
  import Plug.Conn

  def init(opts), do: opts

Maksim's avatar
Maksim committed
11
  def call(conn, _options) do
kaniini's avatar
kaniini committed
12
    if Config.get([:http_security, :enabled]) do
Maksim's avatar
Maksim committed
13
14
15
      conn
      |> merge_resp_headers(headers())
      |> maybe_send_sts_header(Config.get([:http_security, :sts]))
kaniini's avatar
kaniini committed
16
17
18
    else
      conn
    end
kaniini's avatar
kaniini committed
19
20
21
  end

  defp headers do
22
23
    referrer_policy = Config.get([:http_security, :referrer_policy])

kaniini's avatar
kaniini committed
24
25
26
27
28
    [
      {"x-xss-protection", "1; mode=block"},
      {"x-permitted-cross-domain-policies", "none"},
      {"x-frame-options", "DENY"},
      {"x-content-type-options", "nosniff"},
29
      {"referrer-policy", referrer_policy},
kaniini's avatar
kaniini committed
30
31
32
33
34
35
      {"x-download-options", "noopen"},
      {"content-security-policy", csp_string() <> ";"}
    ]
  end

  defp csp_string do
36
    scheme = Config.get([Pleroma.Web.Endpoint, :url])[:scheme]
37
38
39
40
    static_url = Pleroma.Web.Endpoint.static_url()
    websocket_url = String.replace(static_url, "http", "ws")

    connect_src = "connect-src 'self' #{static_url} #{websocket_url}"
41
42
43

    connect_src =
      if Mix.env() == :dev do
44
        connect_src <> " http://localhost:3035/"
45
      else
46
        connect_src
47
48
49
50
51
52
53
54
      end

    script_src =
      if Mix.env() == :dev do
        "script-src 'self' 'unsafe-eval'"
      else
        "script-src 'self'"
      end
55

kaniini's avatar
kaniini committed
56
57
58
59
60
61
62
63
    [
      "default-src 'none'",
      "base-uri 'self'",
      "frame-ancestors 'none'",
      "img-src 'self' data: https:",
      "media-src 'self' https:",
      "style-src 'self' 'unsafe-inline'",
      "font-src 'self'",
64
      "manifest-src 'self'",
65
66
      connect_src,
      script_src,
67
      if scheme == "https" do
68
69
        "upgrade-insecure-requests"
      end
kaniini's avatar
kaniini committed
70
71
72
    ]
    |> Enum.join("; ")
  end
kaniini's avatar
kaniini committed
73
74

  defp maybe_send_sts_header(conn, true) do
kaniini's avatar
kaniini committed
75
76
    max_age_sts = Config.get([:http_security, :sts_max_age])
    max_age_ct = Config.get([:http_security, :ct_max_age])
kaniini's avatar
kaniini committed
77
78

    merge_resp_headers(conn, [
79
80
      {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
      {"expect-ct", "enforce, max-age=#{max_age_ct}"}
kaniini's avatar
kaniini committed
81
82
83
84
    ])
  end

  defp maybe_send_sts_header(conn, _), do: conn
kaniini's avatar
kaniini committed
85
end