logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

http_signature_plug_test.exs (2887B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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
  6. alias Pleroma.Web.Plugs.HTTPSignaturePlug
  7. import Plug.Conn
  8. import Phoenix.Controller, only: [put_format: 2]
  9. import Mock
  10. test "it call HTTPSignatures to check validity if the actor sighed it" do
  11. params = %{"actor" => "http://mastodon.example.org/users/admin"}
  12. conn = build_conn(:get, "/doesntmattter", params)
  13. with_mock HTTPSignatures, validate_conn: fn _ -> true end do
  14. conn =
  15. conn
  16. |> put_req_header(
  17. "signature",
  18. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  19. )
  20. |> put_format("activity+json")
  21. |> HTTPSignaturePlug.call(%{})
  22. assert conn.assigns.valid_signature == true
  23. assert conn.halted == false
  24. assert called(HTTPSignatures.validate_conn(:_))
  25. end
  26. end
  27. describe "requires a signature when `authorized_fetch_mode` is enabled" do
  28. setup do
  29. Pleroma.Config.put([:activitypub, :authorized_fetch_mode], true)
  30. on_exit(fn ->
  31. Pleroma.Config.put([:activitypub, :authorized_fetch_mode], false)
  32. end)
  33. params = %{"actor" => "http://mastodon.example.org/users/admin"}
  34. conn = build_conn(:get, "/doesntmattter", params) |> put_format("activity+json")
  35. [conn: conn]
  36. end
  37. test "when signature header is present", %{conn: conn} do
  38. with_mock HTTPSignatures, validate_conn: fn _ -> false end do
  39. conn =
  40. conn
  41. |> put_req_header(
  42. "signature",
  43. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  44. )
  45. |> HTTPSignaturePlug.call(%{})
  46. assert conn.assigns.valid_signature == false
  47. assert conn.halted == true
  48. assert conn.status == 401
  49. assert conn.state == :sent
  50. assert conn.resp_body == "Request not signed"
  51. assert called(HTTPSignatures.validate_conn(:_))
  52. end
  53. with_mock HTTPSignatures, validate_conn: fn _ -> true end do
  54. conn =
  55. conn
  56. |> put_req_header(
  57. "signature",
  58. "keyId=\"http://mastodon.example.org/users/admin#main-key"
  59. )
  60. |> HTTPSignaturePlug.call(%{})
  61. assert conn.assigns.valid_signature == true
  62. assert conn.halted == false
  63. assert called(HTTPSignatures.validate_conn(:_))
  64. end
  65. end
  66. test "halts the connection when `signature` header is not present", %{conn: conn} do
  67. conn = HTTPSignaturePlug.call(conn, %{})
  68. assert conn.assigns[:valid_signature] == nil
  69. assert conn.halted == true
  70. assert conn.status == 401
  71. assert conn.state == :sent
  72. assert conn.resp_body == "Request not signed"
  73. end
  74. end
  75. end