summaryrefslogtreecommitdiff
path: root/test/plugs/http_security_plug_test.exs
blob: 169c3b3a84d98949736a27bfaa55852a4f83bd21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
defmodule Pleroma.Web.Plugs.HTTPSecurityPlugTest do
  use Pleroma.Web.ConnCase
  alias Pleroma.Config
  alias Plug.Conn

  test "it sends CSP headers when enabled", %{conn: conn} do
    Config.put([:http_security, :enabled], true)

    conn =
      conn
      |> get("/api/v1/instance")

    refute Conn.get_resp_header(conn, "x-xss-protection") == []
    refute Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
    refute Conn.get_resp_header(conn, "x-frame-options") == []
    refute Conn.get_resp_header(conn, "x-content-type-options") == []
    refute Conn.get_resp_header(conn, "x-download-options") == []
    refute Conn.get_resp_header(conn, "referrer-policy") == []
    refute Conn.get_resp_header(conn, "content-security-policy") == []
  end

  test "it does not send CSP headers when disabled", %{conn: conn} do
    Config.put([:http_security, :enabled], false)

    conn =
      conn
      |> get("/api/v1/instance")

    assert Conn.get_resp_header(conn, "x-xss-protection") == []
    assert Conn.get_resp_header(conn, "x-permitted-cross-domain-policies") == []
    assert Conn.get_resp_header(conn, "x-frame-options") == []
    assert Conn.get_resp_header(conn, "x-content-type-options") == []
    assert Conn.get_resp_header(conn, "x-download-options") == []
    assert Conn.get_resp_header(conn, "referrer-policy") == []
    assert Conn.get_resp_header(conn, "content-security-policy") == []
  end

  test "it sends STS headers when enabled", %{conn: conn} do
    Config.put([:http_security, :enabled], true)
    Config.put([:http_security, :sts], true)

    conn =
      conn
      |> get("/api/v1/instance")

    refute Conn.get_resp_header(conn, "strict-transport-security") == []
    refute Conn.get_resp_header(conn, "expect-ct") == []
  end

  test "it does not send STS headers when disabled", %{conn: conn} do
    Config.put([:http_security, :enabled], true)
    Config.put([:http_security, :sts], false)

    conn =
      conn
      |> get("/api/v1/instance")

    assert Conn.get_resp_header(conn, "strict-transport-security") == []
    assert Conn.get_resp_header(conn, "expect-ct") == []
  end

  test "referrer-policy header reflects configured value", %{conn: conn} do
    Config.put([:http_security, :enabled], true)

    conn =
      conn
      |> get("/api/v1/instance")

    assert Conn.get_resp_header(conn, "referrer-policy") == ["same-origin"]

    Config.put([:http_security, :referrer_policy], "no-referrer")

    conn =
      build_conn()
      |> get("/api/v1/instance")

    assert Conn.get_resp_header(conn, "referrer-policy") == ["no-referrer"]
  end
end