logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

http_signature_plug_test.exs (5367B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.Plugs.HTTPSignaturePlugTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. alias Pleroma.StaticStubbedConfigMock, as: ConfigMock
  7. alias Pleroma.StubbedHTTPSignaturesMock, as: HTTPSignaturesMock
  8. alias Pleroma.Web.Plugs.HTTPSignaturePlug
  9. import Mox
  10. import Phoenix.Controller, only: [put_format: 2]
  11. import Plug.Conn
  12. test "it calls HTTPSignatures to check validity if the actor signed it" do
  13. params = %{"actor" => "http://mastodon.example.org/users/admin"}
  14. conn = build_conn(:get, "/doesntmattter", params)
  15. HTTPSignaturesMock
  16. |> expect(:validate_conn, fn _ -> true end)
  17. conn =
  18. conn
  19. |> put_req_header(
  20. "signature",
  21. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  22. )
  23. |> put_format("activity+json")
  24. |> HTTPSignaturePlug.call(%{})
  25. assert conn.assigns.valid_signature == true
  26. assert conn.halted == false
  27. end
  28. describe "requires a signature when `authorized_fetch_mode` is enabled" do
  29. setup do
  30. params = %{"actor" => "http://mastodon.example.org/users/admin"}
  31. conn = build_conn(:get, "/doesntmattter", params) |> put_format("activity+json")
  32. [conn: conn]
  33. end
  34. test "when signature header is present", %{conn: orig_conn} do
  35. ConfigMock
  36. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  37. |> expect(:get, fn [:activitypub, :authorized_fetch_mode_exceptions], [] -> [] end)
  38. HTTPSignaturesMock
  39. |> expect(:validate_conn, 2, fn _ -> false end)
  40. conn =
  41. orig_conn
  42. |> put_req_header(
  43. "signature",
  44. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  45. )
  46. |> HTTPSignaturePlug.call(%{})
  47. assert conn.assigns.valid_signature == false
  48. assert conn.halted == true
  49. assert conn.status == 401
  50. assert conn.state == :sent
  51. assert conn.resp_body == "Request not signed"
  52. ConfigMock
  53. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  54. HTTPSignaturesMock
  55. |> expect(:validate_conn, fn _ -> true end)
  56. conn =
  57. orig_conn
  58. |> put_req_header(
  59. "signature",
  60. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  61. )
  62. |> HTTPSignaturePlug.call(%{})
  63. assert conn.assigns.valid_signature == true
  64. assert conn.halted == false
  65. end
  66. test "halts the connection when `signature` header is not present", %{conn: conn} do
  67. ConfigMock
  68. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  69. |> expect(:get, fn [:activitypub, :authorized_fetch_mode_exceptions], [] -> [] end)
  70. conn = HTTPSignaturePlug.call(conn, %{})
  71. assert conn.assigns[:valid_signature] == nil
  72. assert conn.halted == true
  73. assert conn.status == 401
  74. assert conn.state == :sent
  75. assert conn.resp_body == "Request not signed"
  76. end
  77. test "exempts specific IPs from `authorized_fetch_mode_exceptions`", %{conn: conn} do
  78. ConfigMock
  79. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  80. |> expect(:get, fn [:activitypub, :authorized_fetch_mode_exceptions], [] ->
  81. ["192.168.0.0/24"]
  82. end)
  83. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  84. HTTPSignaturesMock
  85. |> expect(:validate_conn, 2, fn _ -> false end)
  86. conn =
  87. conn
  88. |> Map.put(:remote_ip, {192, 168, 0, 1})
  89. |> put_req_header(
  90. "signature",
  91. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  92. )
  93. |> HTTPSignaturePlug.call(%{})
  94. assert conn.remote_ip == {192, 168, 0, 1}
  95. assert conn.halted == false
  96. end
  97. end
  98. test "rejects requests from `rejected_instances` when `authorized_fetch_mode` is enabled" do
  99. ConfigMock
  100. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  101. |> expect(:get, fn [:instance, :rejected_instances] ->
  102. [{"mastodon.example.org", "no reason"}]
  103. end)
  104. HTTPSignaturesMock
  105. |> expect(:validate_conn, fn _ -> true end)
  106. conn =
  107. build_conn(:get, "/doesntmattter", %{"actor" => "http://mastodon.example.org/users/admin"})
  108. |> put_req_header(
  109. "signature",
  110. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  111. )
  112. |> put_format("activity+json")
  113. |> HTTPSignaturePlug.call(%{})
  114. assert conn.assigns.valid_signature == true
  115. assert conn.halted == true
  116. ConfigMock
  117. |> expect(:get, fn [:activitypub, :authorized_fetch_mode], false -> true end)
  118. |> expect(:get, fn [:instance, :rejected_instances] ->
  119. [{"mastodon.example.org", "no reason"}]
  120. end)
  121. HTTPSignaturesMock
  122. |> expect(:validate_conn, fn _ -> true end)
  123. conn =
  124. build_conn(:get, "/doesntmattter", %{"actor" => "http://allowed.example.org/users/admin"})
  125. |> put_req_header(
  126. "signature",
  127. "keyId=\"http://allowed.example.org/users/admin#main-key"
  128. )
  129. |> put_format("activity+json")
  130. |> HTTPSignaturePlug.call(%{})
  131. assert conn.assigns.valid_signature == true
  132. assert conn.halted == false
  133. end
  134. end