From 4ba924f28f0dc79efbdc58b2cba57c08176924b5 Mon Sep 17 00:00:00 2001 From: Michael Crumm Date: Fri, 6 Sep 2024 12:31:41 -0700 Subject: [PATCH 1/2] Send download encodes reserved chars, closes #5926 --- lib/phoenix/controller.ex | 2 +- test/phoenix/controller/controller_test.exs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/phoenix/controller.ex b/lib/phoenix/controller.ex index 95cb139e5e..74f1d5bd46 100644 --- a/lib/phoenix/controller.ex +++ b/lib/phoenix/controller.ex @@ -1274,7 +1274,7 @@ defmodule Phoenix.Controller do end defp encode_filename(filename, false), do: filename - defp encode_filename(filename, true), do: URI.encode(filename) + defp encode_filename(filename, true), do: URI.encode(filename, &URI.char_unreserved?/1) defp get_disposition_type(:attachment), do: "attachment" defp get_disposition_type(:inline), do: "inline" diff --git a/test/phoenix/controller/controller_test.exs b/test/phoenix/controller/controller_test.exs index 279715fa72..34098c7445 100644 --- a/test/phoenix/controller/controller_test.exs +++ b/test/phoenix/controller/controller_test.exs @@ -537,6 +537,17 @@ defmodule Phoenix.Controller.ControllerTest do "world" end + test "issue #5926, sends file for download for filename with unreserved characters" do + conn = send_download(conn(:get, "/"), {:file, @hello_txt}, filename: "hello, world.json") + assert conn.status == 200 + assert get_resp_header(conn, "content-disposition") == + ["attachment; filename=\"hello%2C%20world.json\"; filename*=utf-8''hello%2C%20world.json"] + assert get_resp_header(conn, "content-type") == + ["application/json"] + assert conn.resp_body == + "world" + end + test "sends file supports UTF-8" do conn = send_download(conn(:get, "/"), {:file, @hello_txt}, filename: "测 试.txt") assert conn.status == 200 From 62b935b0958d4d5c6d4bbb92d819dc6e323b7e93 Mon Sep 17 00:00:00 2001 From: Michael Crumm Date: Tue, 10 Sep 2024 10:10:28 -0700 Subject: [PATCH 2/2] Update test/phoenix/controller/controller_test.exs --- test/phoenix/controller/controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/phoenix/controller/controller_test.exs b/test/phoenix/controller/controller_test.exs index 34098c7445..4b2057f09c 100644 --- a/test/phoenix/controller/controller_test.exs +++ b/test/phoenix/controller/controller_test.exs @@ -537,7 +537,7 @@ defmodule Phoenix.Controller.ControllerTest do "world" end - test "issue #5926, sends file for download for filename with unreserved characters" do + test "sends file for download for filename with unreserved characters" do conn = send_download(conn(:get, "/"), {:file, @hello_txt}, filename: "hello, world.json") assert conn.status == 200 assert get_resp_header(conn, "content-disposition") ==