AttachmentValidator: Handle empty mediatypes
authorlain <lain@soykaf.club>
Mon, 18 May 2020 18:17:28 +0000 (20:17 +0200)
committerlain <lain@soykaf.club>
Mon, 18 May 2020 18:17:28 +0000 (20:17 +0200)
lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex
test/web/activity_pub/object_validator_test.exs
test/web/activity_pub/transmogrifier/chat_message_test.exs

index 16ed4905182fba55d54ac94d700118db052e154e..c4b502cb971ac75ce0a45aa517e7a8da1904e4f8 100644 (file)
@@ -12,7 +12,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
   @primary_key false
   embedded_schema do
     field(:type, :string)
-    field(:mediaType, :string)
+    field(:mediaType, :string, default: "application/octet-stream")
     field(:name, :string)
 
     embeds_many(:url, UrlObjectValidator)
@@ -41,8 +41,16 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
   end
 
   def fix_media_type(data) do
-    data
-    |> Map.put_new("mediaType", data["mimeType"])
+    data =
+      data
+      |> Map.put_new("mediaType", data["mimeType"])
+
+    if data["mediaType"] == "" do
+      data
+      |> Map.put("mediaType", "application/octet-stream")
+    else
+      data
+    end
   end
 
   def fix_url(data) do
index a79e50a29e812d83246ca9123ff9d50ec8101490..ed6b84e8eb09d4336b41e5e8499eeffe89c82c99 100644 (file)
@@ -15,8 +15,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
   describe "attachments" do
     test "works with honkerific attachments" do
       attachment = %{
-        "mediaType" => "image/jpeg",
-        "name" => "298p3RG7j27tfsZ9RQ.jpg",
+        "mediaType" => "",
+        "name" => "",
         "summary" => "298p3RG7j27tfsZ9RQ.jpg",
         "type" => "Document",
         "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
index 85644d787cbe969607cd36379cc810609345aa63..820090de308a9def09c4a4c23ffe6a8892c64181 100644 (file)
@@ -13,6 +13,43 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
   alias Pleroma.Web.ActivityPub.Transmogrifier
 
   describe "handle_incoming" do
+    test "handles this" do
+      data = %{
+        "@context" => "https://www.w3.org/ns/activitystreams",
+        "actor" => "https://honk.tedunangst.com/u/tedu",
+        "id" => "https://honk.tedunangst.com/u/tedu/honk/x6gt8X8PcyGkQcXxzg1T",
+        "object" => %{
+          "attachment" => [
+            %{
+              "mediaType" => "image/jpeg",
+              "name" => "298p3RG7j27tfsZ9RQ.jpg",
+              "summary" => "298p3RG7j27tfsZ9RQ.jpg",
+              "type" => "Document",
+              "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
+            }
+          ],
+          "attributedTo" => "https://honk.tedunangst.com/u/tedu",
+          "content" => "",
+          "id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
+          "published" => "2020-05-18T01:13:03Z",
+          "to" => [
+            "https://dontbulling.me/users/lain"
+          ],
+          "type" => "ChatMessage"
+        },
+        "published" => "2020-05-18T01:13:03Z",
+        "to" => [
+          "https://dontbulling.me/users/lain"
+        ],
+        "type" => "Create"
+      }
+
+      _user = insert(:user, ap_id: data["actor"])
+      _user = insert(:user, ap_id: hd(data["to"]))
+
+      assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
+    end
+
     test "it rejects messages that don't contain content" do
       data =
         File.read!("test/fixtures/create-chat-message.json")