From 2ef145883348e92c9e6393ece9b6967e3a5a80c7 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 21 Mar 2018 16:33:36 +0000 Subject: Use ASCII-8BIT paths in ActionDispatch::Static The rack gem returns PATH_INFO as an ASCII-8BIT encoded string but it was being converted to US-ASCII by the match? method because it was calling Rack::Utils.escape_path. To prevent incompatibile encoding warnings use ASCII-8BIT strings for the root path and let Ruby handle any filename encoding conversion. Fixes #32294, Closes #32314. --- actionpack/lib/action_dispatch/middleware/static.rb | 6 +++--- actionpack/test/dispatch/static_test.rb | 11 ++++++++++- ...5\343\202\210\343\201\206\343\201\252\343\202\211.html" | 1 + ...43\202\210\343\201\206\343\201\252\343\202\211.html.gz" | Bin 0 -> 67 bytes ...5\343\202\210\343\201\206\343\201\252\343\202\211.html" | 1 + ...43\202\210\343\201\206\343\201\252\343\202\211.html.gz" | Bin 0 -> 67 bytes 6 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 "actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" create mode 100644 "actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" create mode 100644 "actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" create mode 100644 "actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index 23492e14eb..acd999444a 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -16,7 +16,7 @@ module ActionDispatch # does not exist, a 404 "File not Found" response will be returned. class FileHandler def initialize(root, index: "index", headers: {}) - @root = root.chomp("/") + @root = root.chomp("/").b @file_server = ::Rack::File.new(@root, headers) @index = index end @@ -35,7 +35,7 @@ module ActionDispatch paths = [path, "#{path}#{ext}", "#{path}/#{@index}#{ext}"] if match = paths.detect { |p| - path = File.join(@root, p.dup.force_encoding(Encoding::UTF_8)) + path = File.join(@root, p.b) begin File.file?(path) && File.readable?(path) rescue SystemCallError @@ -43,7 +43,7 @@ module ActionDispatch end } - return ::Rack::Utils.escape_path(match) + return ::Rack::Utils.escape_path(match).b end end diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb index 0bdff68692..6b69cd9999 100644 --- a/actionpack/test/dispatch/static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -71,7 +71,16 @@ module StaticTests end def test_served_static_file_with_non_english_filename - assert_html "means hello in Japanese\n", get("/foo/#{Rack::Utils.escape("こんにちは.html")}") + assert_html "means hello in Japanese\n", get("/foo/%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF.html") + end + + def test_served_gzipped_static_file_with_non_english_filename + response = get("/foo/%E3%81%95%E3%82%88%E3%81%86%E3%81%AA%E3%82%89.html", "HTTP_ACCEPT_ENCODING" => "gzip") + + assert_gzip "/foo/さようなら.html", response + assert_equal "text/html", response.headers["Content-Type"] + assert_equal "Accept-Encoding", response.headers["Vary"] + assert_equal "gzip", response.headers["Content-Encoding"] end def test_serves_static_file_with_exclamation_mark_in_filename diff --git "a/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" "b/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" new file mode 100644 index 0000000000..627bb2469f --- /dev/null +++ "b/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" @@ -0,0 +1 @@ +means goodbye in Japanese diff --git "a/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" "b/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" new file mode 100644 index 0000000000..4f484cfe86 Binary files /dev/null and "b/actionpack/test/fixtures/public/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" differ diff --git "a/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" "b/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" new file mode 100644 index 0000000000..627bb2469f --- /dev/null +++ "b/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html" @@ -0,0 +1 @@ +means goodbye in Japanese diff --git "a/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" "b/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" new file mode 100644 index 0000000000..4f484cfe86 Binary files /dev/null and "b/actionpack/test/fixtures/\345\205\254\345\205\261/foo/\343\201\225\343\202\210\343\201\206\343\201\252\343\202\211.html.gz" differ -- cgit v1.2.3