1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.HTTPSecurityPlug do
9 def init(opts), do: opts
11 def call(conn, _options) do
12 if Config.get([:http_security, :enabled]) do
14 |> merge_resp_headers(headers())
15 |> maybe_send_sts_header(Config.get([:http_security, :sts]))
22 referrer_policy = Config.get([:http_security, :referrer_policy])
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() <> ";"}
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()
40 connect_src = "connect-src 'self' #{static_url} #{websocket_url}"
43 if Mix.env() == :dev do
44 connect_src <> " http://localhost:3035/"
50 if Mix.env() == :dev do
51 "script-src 'self' 'unsafe-eval'"
59 "frame-ancestors 'none'",
60 "img-src 'self' data: https:",
61 "media-src 'self' https:",
62 "style-src 'self' 'unsafe-inline'",
64 "manifest-src 'self'",
67 if scheme == "https" do
68 "upgrade-insecure-requests"
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])
78 merge_resp_headers(conn, [
79 {"strict-transport-security", "max-age=#{max_age_sts}; includeSubDomains"},
80 {"expect-ct", "enforce, max-age=#{max_age_ct}"}
84 defp maybe_send_sts_header(conn, _), do: conn