Permalink
Browse files

Fix ActionDispatch::Static to serve files with unencoded PCHAR

RFC 3986[1] allows sub-delim characters in path segments unencoded,
however Rack::File requires them to be encoded so we use URI's
unescape method to leave them alone and then escape them again.

Also since the path gets passed to Dir[] we need to escape any glob
characters in the path.

[1]: http://www.ietf.org/rfc/rfc3986.txt
  • Loading branch information...
1 parent ce51edb commit ceb288b8ce552a248f141bddbd16426641a4fd0d @pixeltrix pixeltrix committed Feb 17, 2012
View
11 actionpack/lib/action_dispatch/middleware/static.rb
@@ -1,4 +1,5 @@
require 'rack/utils'
+require 'active_support/core_ext/uri'
module ActionDispatch
class FileHandler
@@ -11,7 +12,7 @@ def initialize(root, cache_control)
def match?(path)
path = path.dup
- full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
+ full_path = path.empty? ? @root : File.join(@root, escape_glob_chars(unescape_path(path)))
paths = "#{full_path}#{ext}"
matches = Dir[paths]
@@ -32,6 +33,14 @@ def ext
"{,#{ext},/index#{ext}}"
end
end
+
+ def unescape_path(path)
+ URI.parser.unescape(path)
+ end
+
+ def escape_glob_chars(path)
+ path.gsub(/(\*|\?|\[|\]|\{|\})/, "\\\\\\1")
+ end
end
class Static
View
28 actionpack/test/dispatch/static_test.rb
@@ -35,8 +35,32 @@ def test_served_static_file_with_non_english_filename
assert_html "means hello in Japanese\n", get("/foo/#{Rack::Utils.escape("こんにちは.html")}")
end
- def test_serves_static_file_with_plus_in_filename
- assert_html "foo+bar\n", get('/foo/foo%2Bbar.html')
+ def test_serves_static_file_with_encoded_pchar
+ assert_html "/foo/foo!bar.html", get("/foo/foo%21bar.html")
+ assert_html "/foo/foo$bar.html", get("/foo/foo%24bar.html")
+ assert_html "/foo/foo&bar.html", get("/foo/foo%26bar.html")
+ assert_html "/foo/foo'bar.html", get("/foo/foo%27bar.html")
+ assert_html "/foo/foo(bar).html", get("/foo/foo%28bar%29.html")
+ assert_html "/foo/foo*bar.html", get("/foo/foo%2Abar.html")
+ assert_html "/foo/foo+bar.html", get("/foo/foo%2Bbar.html")
+ assert_html "/foo/foo,bar.html", get("/foo/foo%2Cbar.html")
+ assert_html "/foo/foo;bar.html", get("/foo/foo%3Bbar.html")
+ assert_html "/foo/foo:bar.html", get("/foo/foo%3Abar.html")
+ assert_html "/foo/foo@bar.html", get("/foo/foo%40bar.html")
+ end
+
+ def test_serves_static_file_with_unencoded_pchar
+ assert_html "/foo/foo!bar.html", get("/foo/foo!bar.html")
+ assert_html "/foo/foo$bar.html", get("/foo/foo$bar.html")
+ assert_html "/foo/foo&bar.html", get("/foo/foo&bar.html")
+ assert_html "/foo/foo'bar.html", get("/foo/foo'bar.html")
+ assert_html "/foo/foo(bar).html", get("/foo/foo(bar).html")
+ assert_html "/foo/foo*bar.html", get("/foo/foo*bar.html")
+ assert_html "/foo/foo+bar.html", get("/foo/foo+bar.html")
+ assert_html "/foo/foo,bar.html", get("/foo/foo,bar.html")
+ assert_html "/foo/foo;bar.html", get("/foo/foo;bar.html")
+ assert_html "/foo/foo:bar.html", get("/foo/foo:bar.html")
+ assert_html "/foo/foo@bar.html", get("/foo/foo@bar.html")
end
private
View
1 actionpack/test/fixtures/public/foo/foo!bar.html
@@ -0,0 +1 @@
+/foo/foo!bar.html
View
1 actionpack/test/fixtures/public/foo/foo$bar.html
@@ -0,0 +1 @@
+/foo/foo$bar.html
View
1 actionpack/test/fixtures/public/foo/foo&bar.html
@@ -0,0 +1 @@
+/foo/foo&bar.html
View
1 actionpack/test/fixtures/public/foo/foo'bar.html
@@ -0,0 +1 @@
+/foo/foo'bar.html
View
1 actionpack/test/fixtures/public/foo/foo(bar).html
@@ -0,0 +1 @@
+/foo/foo(bar).html
View
1 actionpack/test/fixtures/public/foo/foo*bar.html
@@ -0,0 +1 @@
+/foo/foo*bar.html
View
2 actionpack/test/fixtures/public/foo/foo+bar.html
@@ -1 +1 @@
-foo+bar
+/foo/foo+bar.html
View
1 actionpack/test/fixtures/public/foo/foo,bar.html
@@ -0,0 +1 @@
+/foo/foo,bar.html
View
1 actionpack/test/fixtures/public/foo/foo:bar.html
@@ -0,0 +1 @@
+/foo/foo:bar.html
View
1 actionpack/test/fixtures/public/foo/foo;bar.html
@@ -0,0 +1 @@
+/foo/foo;bar.html
View
1 actionpack/test/fixtures/public/foo/foo=bar.html
@@ -0,0 +1 @@
+/foo/foo=bar.html
View
1 actionpack/test/fixtures/public/foo/foo@bar.html
@@ -0,0 +1 @@
+/foo/foo@bar.html

3 comments on commit ceb288b

@korzhyk

have a problem with file names on windows, becouse \ / : * ? " < > | are not allowed symbols

@pixeltrix
Ruby on Rails member

I guess the only way round this would be to create the files in the test and then don't do the tests which can't be checked on Windows.

@pixeltrix
Ruby on Rails member

@korzhyk fixed in 1ef12fd

Please sign in to comment.