microblogpub federation fixes (#288)
authorfloatingghost <hannah@coffee-and-dreams.uk>
Fri, 18 Nov 2022 11:14:35 +0000 (11:14 +0000)
committerfloatingghost <hannah@coffee-and-dreams.uk>
Fri, 18 Nov 2022 11:14:35 +0000 (11:14 +0000)
Co-authored-by: FloatingGhost <hannah@coffee-and-dreams.uk>
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/288

lib/pleroma/http/adapter_helper.ex
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
test/fixtures/microblogpub/user_with_invalid_also_known_as.json [new file with mode: 0644]
test/pleroma/object/fetcher_test.exs
test/pleroma/user_test.exs

index 4949dd7277ba6c00197ea60570cbbe8db30eec81..e837ac8d44713d6ce551611f1637e8e8b2db7560 100644 (file)
@@ -99,6 +99,7 @@ defmodule Pleroma.HTTP.AdapterHelper do
           | {:error, atom()}
           | nil
   def parse_proxy(nil), do: nil
+  def parse_proxy(""), do: nil
 
   def parse_proxy(proxy) when is_binary(proxy) do
     with %URI{} = uri <- URI.parse(proxy),
index 700cab2b591ea00af408092ba7e45fa060c8ac39..eb907a2d86340d43caef83fa7a6e8a7f9182de6c 100644 (file)
@@ -1910,7 +1910,8 @@ defmodule Pleroma.User do
       {%User{} = user, _} ->
         {:ok, user}
 
-      _ ->
+      e ->
+        Logger.error("Could not fetch user, #{inspect(e)}")
         {:error, :not_found}
     end
   end
index dcdc7085fdaebc45eb36f4d279100ff33cce59a3..254d91a7e9f9911af91c8ad02fe472d968be2f96 100644 (file)
@@ -1530,6 +1530,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     # we request WebFinger here
     nickname = additional[:nickname_from_acct] || generate_nickname(data)
 
+    # also_known_as must be a URL
+    also_known_as =
+      data
+      |> Map.get("alsoKnownAs", [])
+      |> Enum.filter(fn url ->
+        case URI.parse(url) do
+          %URI{scheme: "http"} -> true
+          %URI{scheme: "https"} -> true
+          _ -> false
+        end
+      end)
+
     %{
       ap_id: data["id"],
       uri: get_actor_url(data["url"]),
@@ -1547,7 +1559,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       featured_address: featured_address,
       bio: data["summary"] || "",
       actor_type: actor_type,
-      also_known_as: Map.get(data, "alsoKnownAs", []),
+      also_known_as: also_known_as,
       public_key: public_key,
       inbox: data["inbox"],
       shared_inbox: shared_inbox,
diff --git a/test/fixtures/microblogpub/user_with_invalid_also_known_as.json b/test/fixtures/microblogpub/user_with_invalid_also_known_as.json
new file mode 100644 (file)
index 0000000..a030762
--- /dev/null
@@ -0,0 +1,57 @@
+{
+  "@context": [
+    "https://www.w3.org/ns/activitystreams",
+    "https://w3id.org/security/v1",
+    {
+      "Hashtag": "as:Hashtag",
+      "sensitive": "as:sensitive",
+      "manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
+      "alsoKnownAs": {
+        "@id": "as:alsoKnownAs",
+        "@type": "@id"
+      },
+      "movedTo": {
+        "@id": "as:movedTo",
+        "@type": "@id"
+      },
+      "toot": "http://joinmastodon.org/ns#",
+      "featured": {
+        "@id": "toot:featured",
+        "@type": "@id"
+      },
+      "Emoji": "toot:Emoji",
+      "blurhash": "toot:blurhash",
+      "votersCount": "toot:votersCount",
+      "schema": "http://schema.org#",
+      "PropertyValue": "schema:PropertyValue",
+      "value": "schema:value",
+      "ostatus": "http://ostatus.org#",
+      "conversation": "ostatus:conversation"
+    }
+  ],
+  "type": "Person",
+  "id": "https://mbp.example.com",
+  "following": "https://mbp.example.com/following",
+  "followers": "https://mbp.example.com/followers",
+  "featured": "https://mbp.example.com/featured",
+  "inbox": "https://mbp.example.com/inbox",
+  "outbox": "https://mbp.example.com/outbox",
+  "preferredUsername": "MBP",
+  "name": "MBP",
+  "summary": "wowee",
+  "endpoints": {
+    "sharedInbox": "https://mbp.example.com/inbox"
+  },
+  "url": "https://mbp.example.com/",
+  "manuallyApprovesFollowers": false,
+  "attachment": [],
+  "icon": {
+    "mediaType": "image/jpeg",
+    "type": "Image",
+    "url": "https://beta.4201337.xyz/static/denise.jpg"
+  },
+  "tag": [],
+  "alsoKnownAs": [
+    "example@elsewhere.com"
+  ]
+}
index cd5437617b6aa5229df14e747e16bcc43ee0a2a1..71306cdfe4f822c3bf2f42ef51e1ff194d2cc4f7 100644 (file)
@@ -166,7 +166,7 @@ defmodule Pleroma.Object.FetcherTest do
       Instances.set_consistently_unreachable(id)
       refute Instances.reachable?(id)
 
-      {:ok, object} =
+      {:ok, _object} =
         Fetcher.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
 
       assert Instances.reachable?(id)
index 195df2a03047231e9a95325af57ebe9306825565..44763daf70bcf544c273dea4866693b3444b6495 100644 (file)
@@ -968,6 +968,25 @@ defmodule Pleroma.UserTest do
 
       assert user.last_refreshed_at == orig_user.last_refreshed_at
     end
+
+    test "it doesn't fail on invalid alsoKnownAs entries" do
+      Tesla.Mock.mock(fn
+        %{url: "https://mbp.example.com/"} ->
+          %Tesla.Env{
+            status: 200,
+            body:
+              "test/fixtures/microblogpub/user_with_invalid_also_known_as.json"
+              |> File.read!(),
+            headers: [{"content-type", "application/activity+json"}]
+          }
+
+        _ ->
+          %Tesla.Env{status: 404}
+      end)
+
+      assert {:ok, %User{also_known_as: []}} =
+               User.get_or_fetch_by_ap_id("https://mbp.example.com/")
+    end
   end
 
   test "returns an ap_id for a user" do