strip \r and \r from content-disposition filenames
authorFloatingGhost <hannah@coffee-and-dreams.uk>
Thu, 10 Nov 2022 11:54:12 +0000 (11:54 +0000)
committerFloatingGhost <hannah@coffee-and-dreams.uk>
Thu, 10 Nov 2022 11:54:12 +0000 (11:54 +0000)
lib/pleroma/web/plugs/uploaded_media.ex
test/pleroma/web/plugs/uploaded_media_plug_test.exs

index 7b87d8f178defd9515ed9421aa68e0b753730633..72f20e8de16a332c5efda0ae8c9cbe59340c49a8 100644 (file)
@@ -35,7 +35,7 @@ defmodule Pleroma.Web.Plugs.UploadedMedia do
     conn =
       case fetch_query_params(conn) do
         %{query_params: %{"name" => name}} = conn ->
-          name = String.replace(name, "\"", "\\\"")
+          name = escape_header_value(name)
 
           put_resp_header(conn, "content-disposition", "filename=\"#{name}\"")
 
@@ -98,4 +98,11 @@ defmodule Pleroma.Web.Plugs.UploadedMedia do
     |> send_resp(:internal_server_error, dgettext("errors", "Internal Error"))
     |> halt()
   end
+
+  defp escape_header_value(value) do
+    value
+    |> String.replace("\"", "\\\"")
+    |> String.replace("\\r", "")
+    |> String.replace("\\n", "")
+  end
 end
index 75f3132828b07aaedb703c8b93d1af3ac115c030..c71a7e7898edc7436463980c4b4477835c6af9af 100644 (file)
@@ -40,4 +40,15 @@ defmodule Pleroma.Web.Plugs.UploadedMediaPlugTest do
              &(&1 == {"content-disposition", "filename=\"\\\"cofe\\\".gif\""})
            )
   end
+
+  test "removes control characters from the Content-Disposition header", %{
+    attachment_url: attachment_url
+  } do
+    conn = get(build_conn(), attachment_url <> "?name=\"cofe\".gif\\r\\n")
+
+    assert Enum.any?(
+             conn.resp_headers,
+             &(&1 == {"content-disposition", "filename=\"\\\"cofe\\\".gif\""})
+           )
+  end
 end