diff --git a/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex b/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex index 26d3dc592..4649db2a1 100644 --- a/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex @@ -101,10 +101,19 @@ defp get_extension_if_safe(response) do end end + defp get_int_header(headers, header_name, default \\ nil) do + with rawval when rawval != :undefined <- :proplists.get_value(header_name, headers), + {int, ""} <- Integer.parse(rawval) do + int + else + _ -> default + end + end + defp is_remote_size_within_limit?(url) do with {:ok, %{status: status, headers: headers} = _response} when status in 200..299 <- Pleroma.HTTP.request(:head, url, nil, [], []) do - content_length = :proplists.get_value("content-length", headers, nil) + content_length = get_int_header(headers, "content-length") size_limit = Config.get([:mrf_steal_emoji, :size_limit], @size_limit) accept_unknown = @@ -172,7 +181,7 @@ def filter(message), do: {:ok, message} description: <<_::272, _::_*256>>, key: :hosts | :rejected_shortcodes | :size_limit, suggestions: [any(), ...], - type: {:list, :string} | {:list, :string} | :integer + type: {:list, :string} | {:list, :string} | :integer | :boolean }, ... ], @@ -209,6 +218,12 @@ def config_description do type: :integer, description: "File size limit (in bytes), checked before an emoji is saved to the disk", suggestions: ["100000"] + }, + %{ + key: :download_unknown_size, + type: :boolean, + description: "Whether to download emoji if size can't be determined ahead of time", + suggestions: [false, true] } ] } diff --git a/test/pleroma/signature_test.exs b/test/pleroma/signature_test.exs index b7c9bbb62..768c78f21 100644 --- a/test/pleroma/signature_test.exs +++ b/test/pleroma/signature_test.exs @@ -110,7 +110,7 @@ test "it returns signature headers" do headers = %{ host: "test.test", - "content-length": 100 + "content-length": "100" } assert_signature_equal( @@ -127,7 +127,7 @@ test "it returns error" do assert Signature.sign( user, - %{host: "test.test", "content-length": 100} + %{host: "test.test", "content-length": "100"} ) == {:error, []} end end diff --git a/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs index 932251389..45fe183a4 100644 --- a/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/steal_emoji_policy_test.exs @@ -202,7 +202,7 @@ test "reject unknown size", %{message: message} do test "reject too large content-size before download", %{message: message} do clear_config([:mrf_steal_emoji, :download_unknown_size], false) - mock_tesla("https://example.org/emoji/firedfox.png", 200, [{"content-length", 2 ** 30}]) + mock_tesla("https://example.org/emoji/firedfox.png", 200, [{"content-length", "#{2 ** 30}"}]) refute "firedfox" in installed() @@ -216,7 +216,7 @@ test "reject too large content-size before download", %{message: message} do test "accepts content-size below limit", %{message: message} do clear_config([:mrf_steal_emoji, :download_unknown_size], false) - mock_tesla("https://example.org/emoji/firedfox.png", 200, [{"content-length", 2}]) + mock_tesla("https://example.org/emoji/firedfox.png", 200, [{"content-length", "2"}]) refute "firedfox" in installed()