diff --git a/lib/rex/file.rb b/lib/rex/file.rb index 2c04765bb5..51337657da 100644 --- a/lib/rex/file.rb +++ b/lib/rex/file.rb @@ -35,14 +35,14 @@ module FileUtils # Filter out double slashes s = s.gsub(/\\\\/, '\\') while s.index('\\\\') + # Keep the trailing slash if exists + trailing_s = ('\\' if s =~ /\\$/) || '' + # Check the items (fie/dir) individually s = s.split(/\\/) # Parse the path prefix prefix = (s[0] || '').gsub(/[\*<>\?\/]/, '') - if prefix =~ /^\w:$/ and s.length == 1 - prefix += '\\' - end # Delete the original prefix. We want the new one later. s.delete_at(0) @@ -55,6 +55,9 @@ module FileUtils # And then safely join the items s *= '\\' + + # Add the trailing slash back if exists + s << trailing_s end # diff --git a/spec/lib/rex/file_utils_spec.rb b/spec/lib/rex/file_utils_spec.rb index 7778a13b3b..e160cf95a5 100644 --- a/spec/lib/rex/file_utils_spec.rb +++ b/spec/lib/rex/file_utils_spec.rb @@ -17,6 +17,11 @@ describe Rex::FileUtils do described_class.normalize_win_path('\\temp').should eq("\\temp") end + it "should keep the trailing slash if exists" do + described_class.normalize_win_path('/', 'test', 'me\\').should eq("\\test\\me\\") + described_class.normalize_win_path('\\temp\\').should eq("\\temp\\") + end + it "should convert a path without reserved characters" do described_class.normalize_win_path('C:\\', 'Windows:').should eq("C:\\Windows") described_class.normalize_win_path('C:\\Windows???\\test').should eq("C:\\Windows\\test") @@ -26,6 +31,7 @@ describe Rex::FileUtils do described_class.normalize_win_path('C:\\\\\\', 'Windows').should eq("C:\\Windows") described_class.normalize_win_path('C:\\\\\\Hello World\\\\whatever.txt').should eq("C:\\Hello World\\whatever.txt") described_class.normalize_win_path('C:\\\\').should eq("C:\\") + described_class.normalize_win_path('\\test\\\\test\\\\').should eq("\\test\\test\\") end end