logo

pleroma

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

web_finger_test.exs (7091B)


  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.WebFingerTest do
  5. use Pleroma.DataCase, async: true
  6. alias Pleroma.Web.WebFinger
  7. import Pleroma.Factory
  8. import Tesla.Mock
  9. setup do
  10. mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  11. :ok
  12. end
  13. describe "host meta" do
  14. test "returns a link to the xml lrdd" do
  15. host_info = WebFinger.host_meta()
  16. assert String.contains?(host_info, Pleroma.Web.Endpoint.url())
  17. end
  18. end
  19. describe "incoming webfinger request" do
  20. test "works for fqns" do
  21. user = insert(:user)
  22. {:ok, result} =
  23. WebFinger.webfinger("#{user.nickname}@#{Pleroma.Web.Endpoint.host()}", "XML")
  24. assert is_binary(result)
  25. end
  26. test "works for ap_ids" do
  27. user = insert(:user)
  28. {:ok, result} = WebFinger.webfinger(user.ap_id, "XML")
  29. assert is_binary(result)
  30. end
  31. end
  32. describe "fingering" do
  33. test "returns error for nonsensical input" do
  34. assert {:error, _} = WebFinger.finger("bliblablu")
  35. assert {:error, _} = WebFinger.finger("pleroma.social")
  36. end
  37. test "returns error when there is no content-type header" do
  38. Tesla.Mock.mock(fn
  39. %{url: "https://social.heldscal.la/.well-known/host-meta"} ->
  40. {:ok,
  41. %Tesla.Env{
  42. status: 200,
  43. body: File.read!("test/fixtures/tesla_mock/social.heldscal.la_host_meta")
  44. }}
  45. %{
  46. url:
  47. "https://social.heldscal.la/.well-known/webfinger?resource=acct:invalid_content@social.heldscal.la"
  48. } ->
  49. {:ok, %Tesla.Env{status: 200, body: ""}}
  50. end)
  51. user = "invalid_content@social.heldscal.la"
  52. assert {:error, {:content_type, nil}} = WebFinger.finger(user)
  53. end
  54. test "returns error when fails parse xml or json" do
  55. user = "invalid_content@social.heldscal.la"
  56. assert {:error, %Jason.DecodeError{}} = WebFinger.finger(user)
  57. end
  58. test "returns the ActivityPub actor URI for an ActivityPub user" do
  59. user = "framasoft@framatube.org"
  60. {:ok, _data} = WebFinger.finger(user)
  61. end
  62. test "it work for AP-only user" do
  63. user = "kpherox@mstdn.jp"
  64. {:ok, data} = WebFinger.finger(user)
  65. assert data["magic_key"] == nil
  66. assert data["salmon"] == nil
  67. assert data["topic"] == nil
  68. assert data["subject"] == "acct:kPherox@mstdn.jp"
  69. assert data["ap_id"] == "https://mstdn.jp/users/kPherox"
  70. assert data["subscribe_address"] == "https://mstdn.jp/authorize_interaction?acct={uri}"
  71. end
  72. test "it gets the xrd endpoint" do
  73. {:ok, template} = WebFinger.find_lrdd_template("social.heldscal.la")
  74. assert template == "https://social.heldscal.la/.well-known/webfinger?resource={uri}"
  75. end
  76. test "it gets the xrd endpoint for hubzilla" do
  77. {:ok, template} = WebFinger.find_lrdd_template("macgirvin.com")
  78. assert template == "https://macgirvin.com/xrd/?uri={uri}"
  79. end
  80. test "it gets the xrd endpoint for statusnet" do
  81. {:ok, template} = WebFinger.find_lrdd_template("status.alpicola.com")
  82. assert template == "https://status.alpicola.com/main/xrd?uri={uri}"
  83. end
  84. test "it works with idna domains as nickname" do
  85. nickname = "lain@" <> to_string(:idna.encode("zetsubou.みんな"))
  86. {:ok, _data} = WebFinger.finger(nickname)
  87. end
  88. test "it works with idna domains as link" do
  89. ap_id = "https://" <> to_string(:idna.encode("zetsubou.みんな")) <> "/users/lain"
  90. {:ok, _data} = WebFinger.finger(ap_id)
  91. end
  92. test "respects json content-type" do
  93. Tesla.Mock.mock(fn
  94. %{
  95. url:
  96. "https://mastodon.social/.well-known/webfinger?resource=acct:emelie@mastodon.social"
  97. } ->
  98. {:ok,
  99. %Tesla.Env{
  100. status: 200,
  101. body: File.read!("test/fixtures/tesla_mock/webfinger_emelie.json"),
  102. headers: [{"content-type", "application/jrd+json"}]
  103. }}
  104. %{url: "https://mastodon.social/.well-known/host-meta"} ->
  105. {:ok,
  106. %Tesla.Env{
  107. status: 200,
  108. body: File.read!("test/fixtures/tesla_mock/mastodon.social_host_meta")
  109. }}
  110. end)
  111. {:ok, _data} = WebFinger.finger("emelie@mastodon.social")
  112. end
  113. test "respects xml content-type" do
  114. Tesla.Mock.mock(fn
  115. %{
  116. url: "https://pawoo.net/.well-known/webfinger?resource=acct:pekorino@pawoo.net"
  117. } ->
  118. {:ok,
  119. %Tesla.Env{
  120. status: 200,
  121. body: File.read!("test/fixtures/tesla_mock/https___pawoo.net_users_pekorino.xml"),
  122. headers: [{"content-type", "application/xrd+xml"}]
  123. }}
  124. %{url: "https://pawoo.net/.well-known/host-meta"} ->
  125. {:ok,
  126. %Tesla.Env{
  127. status: 200,
  128. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  129. }}
  130. end)
  131. {:ok, _data} = WebFinger.finger("pekorino@pawoo.net")
  132. end
  133. test "refuses to process XML remote entities" do
  134. Tesla.Mock.mock(fn
  135. %{
  136. url: "https://pawoo.net/.well-known/webfinger?resource=acct:pekorino@pawoo.net"
  137. } ->
  138. {:ok,
  139. %Tesla.Env{
  140. status: 200,
  141. body: File.read!("test/fixtures/xml_external_entities.xml"),
  142. headers: [{"content-type", "application/xrd+xml"}]
  143. }}
  144. %{url: "https://pawoo.net/.well-known/host-meta"} ->
  145. {:ok,
  146. %Tesla.Env{
  147. status: 200,
  148. body: File.read!("test/fixtures/tesla_mock/pawoo.net_host_meta")
  149. }}
  150. end)
  151. assert :error = WebFinger.finger("pekorino@pawoo.net")
  152. end
  153. test "prevents spoofing" do
  154. Tesla.Mock.mock(fn
  155. %{
  156. url: "https://gleasonator.com/.well-known/webfinger?resource=acct:alex@gleasonator.com"
  157. } ->
  158. {:ok,
  159. %Tesla.Env{
  160. status: 200,
  161. body: File.read!("test/fixtures/tesla_mock/webfinger_spoof.json"),
  162. headers: [{"content-type", "application/jrd+json"}]
  163. }}
  164. %{url: "https://gleasonator.com/.well-known/host-meta"} ->
  165. {:ok,
  166. %Tesla.Env{
  167. status: 200,
  168. body: File.read!("test/fixtures/tesla_mock/gleasonator.com_host_meta")
  169. }}
  170. end)
  171. {:error, _data} = WebFinger.finger("alex@gleasonator.com")
  172. end
  173. end
  174. test "prevents forgeries" do
  175. Tesla.Mock.mock(fn
  176. %{url: "https://fba.ryona.agency/.well-known/webfinger?resource=acct:graf@fba.ryona.agency"} ->
  177. fake_webfinger =
  178. File.read!("test/fixtures/webfinger/graf-imposter-webfinger.json") |> Jason.decode!()
  179. Tesla.Mock.json(fake_webfinger)
  180. %{url: "https://fba.ryona.agency/.well-known/host-meta"} ->
  181. {:ok, %Tesla.Env{status: 404}}
  182. end)
  183. assert {:error, _} = WebFinger.finger("graf@fba.ryona.agency")
  184. end
  185. end