Upload: Restrict description length
authorlain <lain@soykaf.club>
Mon, 6 Jul 2020 09:08:13 +0000 (11:08 +0200)
committerlain <lain@soykaf.club>
Mon, 6 Jul 2020 09:08:13 +0000 (11:08 +0200)
config/config.exs
lib/pleroma/upload.ex
test/upload_test.exs

index 9b550920cb144e469eda74394f67079e133cce37..d28a359b29ebe705898c798c0c906f685c48787d 100644 (file)
@@ -188,6 +188,7 @@ config :pleroma, :instance,
   background_image: "/images/city.jpg",
   instance_thumbnail: "/instance/thumbnail.jpeg",
   limit: 5_000,
+  description_limit: 5_000,
   chat_limit: 5_000,
   remote_limit: 100_000,
   upload_limit: 16_000_000,
index 797555bffa324643d7105450f21cbc1090304c39..0fa6b89dc1c6e53e2dfb62689c049a5a41f19aed 100644 (file)
@@ -63,6 +63,10 @@ defmodule Pleroma.Upload do
     with {:ok, upload} <- prepare_upload(upload, opts),
          upload = %__MODULE__{upload | path: upload.path || "#{upload.id}/#{upload.name}"},
          {:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
+         description = Map.get(opts, :description) || upload.name,
+         {_, true} <-
+           {:description_limit,
+            String.length(description) <= Pleroma.Config.get([:instance, :description_limit])},
          {:ok, url_spec} <- Pleroma.Uploaders.Uploader.put_file(opts.uploader, upload) do
       {:ok,
        %{
@@ -75,9 +79,12 @@ defmodule Pleroma.Upload do
              "href" => url_from_spec(upload, opts.base_url, url_spec)
            }
          ],
-         "name" => Map.get(opts, :description) || upload.name
+         "name" => description
        }}
     else
+      {:description_limit, _} ->
+        {:error, :description_too_long}
+
       {:error, error} ->
         Logger.error(
           "#{__MODULE__} store (using #{inspect(opts.uploader)}) failed: #{inspect(error)}"
index 2abf0edec6b6ee61d7342923eb5749b7bfd57f1a..b06b54487e152dc397e3127930e481cefbaedace 100644 (file)
@@ -107,6 +107,19 @@ defmodule Pleroma.UploadTest do
   describe "Storing a file with the Local uploader" do
     setup [:ensure_local_uploader]
 
+    test "does not allow descriptions longer than the post limit" do
+      clear_config([:instance, :description_limit], 2)
+      File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
+
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image_tmp.jpg"),
+        filename: "image.jpg"
+      }
+
+      {:error, :description_too_long} = Upload.store(file, description: "123")
+    end
+
     test "returns a media url" do
       File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")