Skip to content

Commit

Permalink
Fix path with tilde
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorov committed Oct 30, 2024
1 parent cb98397 commit 92a6031
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/route_downcaser/downcase_route_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def _call(env)

# Downcase path_info if applicable
path_info = downcased_uri(path_info)
path_info = path_info.gsub('%7E', '~') if path_info

# If redirect configured, then return redirect request, if either
# path_info has changed
Expand Down
9 changes: 9 additions & 0 deletions test/integration/route_middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def call(env)
assert_equal "/пиво", URI.decode_www_form_component(response.location)
end

test "Can downcase path with tilde correctly" do
RouteDowncaser.configuration do |config|
config.redirect = true
end
get "/~Test~~test"
assert_equal 301, response.status
assert_equal "/~test~~test", URI.decode_www_form_component(response.location)
end

test "Can downcase longer unicode paths correctly" do
RouteDowncaser.configuration do |config|
config.redirect = true
Expand Down
23 changes: 23 additions & 0 deletions test/route_downcaser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,27 @@ class MultibyteRedirectTests < ActiveSupport::TestCase
assert_instance_of String, headers["Location"], "Headers must be strings"
end
end

class SpecialSymbolsInPathTests < ActiveSupport::TestCase
setup do
@app = MyMockApp.new
RouteDowncaser.configuration do |config|
config.redirect = true
config.exclude_patterns = nil
end
end

test "path with tildes" do
callenv = {"PATH_INFO" => "/~test~~test~", "REQUEST_METHOD" => "GET"}
RouteDowncaser::DowncaseRouteMiddleware.new(@app).call(callenv)
assert_equal("/~test~~test~", @app.env["PATH_INFO"])
end

test "path with escape tilde" do
callenv = {"PATH_INFO" => "/~test%7Etest~~", "REQUEST_METHOD" => "GET"}
status, headers, _ = *RouteDowncaser::DowncaseRouteMiddleware.new(@app).call(callenv)
assert_equal 301, status
assert_equal("/~test~test~~", headers["Location"])
end
end
end

0 comments on commit 92a6031

Please sign in to comment.