logo

pleroma

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

signature.ex (4521B)


  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.Signature do
  5. @behaviour HTTPSignatures.Adapter
  6. alias Pleroma.EctoType.ActivityPub.ObjectValidators
  7. alias Pleroma.Keys
  8. alias Pleroma.User
  9. alias Pleroma.Web.ActivityPub.ActivityPub
  10. import Plug.Conn, only: [put_req_header: 3]
  11. @http_signatures_impl Application.compile_env(
  12. :pleroma,
  13. [__MODULE__, :http_signatures_impl],
  14. HTTPSignatures
  15. )
  16. @known_suffixes ["/publickey", "/main-key"]
  17. def key_id_to_actor_id(key_id) do
  18. uri =
  19. key_id
  20. |> URI.parse()
  21. |> Map.put(:fragment, nil)
  22. |> remove_suffix(@known_suffixes)
  23. maybe_ap_id = URI.to_string(uri)
  24. case ObjectValidators.ObjectID.cast(maybe_ap_id) do
  25. {:ok, ap_id} ->
  26. {:ok, ap_id}
  27. _ ->
  28. case Pleroma.Web.WebFinger.finger(maybe_ap_id) do
  29. {:ok, %{"ap_id" => ap_id}} -> {:ok, ap_id}
  30. _ -> {:error, maybe_ap_id}
  31. end
  32. end
  33. end
  34. defp remove_suffix(uri, [test | rest]) do
  35. if not is_nil(uri.path) and String.ends_with?(uri.path, test) do
  36. Map.put(uri, :path, String.replace(uri.path, test, ""))
  37. else
  38. remove_suffix(uri, rest)
  39. end
  40. end
  41. defp remove_suffix(uri, []), do: uri
  42. def fetch_public_key(conn) do
  43. with {:ok, actor_id} <- get_actor_id(conn),
  44. {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
  45. {:ok, public_key}
  46. else
  47. e ->
  48. {:error, e}
  49. end
  50. end
  51. def refetch_public_key(conn) do
  52. with {:ok, actor_id} <- get_actor_id(conn),
  53. {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id),
  54. {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
  55. {:ok, public_key}
  56. else
  57. e ->
  58. {:error, e}
  59. end
  60. end
  61. def get_actor_id(conn) do
  62. with %{"keyId" => kid} <- HTTPSignatures.signature_for_conn(conn),
  63. {:ok, actor_id} <- key_id_to_actor_id(kid) do
  64. {:ok, actor_id}
  65. else
  66. e ->
  67. {:error, e}
  68. end
  69. end
  70. def sign(%User{keys: keys} = user, headers) do
  71. with {:ok, private_key, _} <- Keys.keys_from_pem(keys) do
  72. HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
  73. end
  74. end
  75. def signed_date, do: signed_date(NaiveDateTime.utc_now())
  76. def signed_date(%NaiveDateTime{} = date) do
  77. Timex.format!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
  78. end
  79. @spec validate_signature(Plug.Conn.t(), String.t()) :: boolean()
  80. def validate_signature(%Plug.Conn{} = conn, request_target) do
  81. # Newer drafts for HTTP signatures now use @request-target instead of the
  82. # old (request-target). We'll now support both for incoming signatures.
  83. conn =
  84. conn
  85. |> put_req_header("(request-target)", request_target)
  86. |> put_req_header("@request-target", request_target)
  87. @http_signatures_impl.validate_conn(conn)
  88. end
  89. @spec validate_signature(Plug.Conn.t()) :: boolean()
  90. def validate_signature(%Plug.Conn{} = conn) do
  91. # This (request-target) is non-standard, but many implementations do it
  92. # this way due to a misinterpretation of
  93. # https://datatracker.ietf.org/doc/html/draft-cavage-http-signatures-06
  94. # "path" was interpreted as not having the query, though later examples
  95. # show that it must be the absolute path + query. This behavior is kept to
  96. # make sure most software (Pleroma itself, Mastodon, and probably others)
  97. # do not break.
  98. request_target = Enum.join([String.downcase(conn.method), conn.request_path], " ")
  99. # This is the proper way to build the @request-target, as expected by
  100. # many HTTP signature libraries, clarified in the following draft:
  101. # https://www.ietf.org/archive/id/draft-ietf-httpbis-message-signatures-11.html#section-2.2.6
  102. # It is the same as before, but containing the query part as well.
  103. proper_target = Enum.join([request_target, "?", conn.query_string], "")
  104. cond do
  105. # Normal, non-standard behavior but expected by Pleroma and more.
  106. validate_signature(conn, request_target) ->
  107. true
  108. # Has query string and the previous one failed: let's try the standard.
  109. conn.query_string != "" ->
  110. validate_signature(conn, proper_target)
  111. # If there's no query string and signature fails, it's rotten.
  112. true ->
  113. false
  114. end
  115. end
  116. end