From 596f5d1af340a78ec4d1b2666e5f281564f21479 Mon Sep 17 00:00:00 2001 From: "Heng, K. (Stephen)" Date: Thu, 18 Aug 2016 12:02:00 +0800 Subject: [PATCH 1/5] Proposed fix for #5192 Strip drive name only when necessary. --- lib/jekyll.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index a9fe696f..ffe9e4ac 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -160,11 +160,11 @@ module Jekyll questionable_path.insert(0, "/") if questionable_path.start_with?("~") clean_path = File.expand_path(questionable_path, "/") - clean_path.sub!(%r!\A\w:/!, "/") - if clean_path.start_with?(base_directory.sub(%r!\A\w:/!, "/")) + if clean_path.start_with?(base_directory) clean_path else + clean_path.sub!(%r!\A\w:/!, "/") File.join(base_directory, clean_path) end end From 7892c5e1f392061c3d6862a83cefb6da8f134b6d Mon Sep 17 00:00:00 2001 From: "Heng, K. (Stephen)" Date: Thu, 25 Aug 2016 18:35:09 +0800 Subject: [PATCH 2/5] Fix issue #5276, where path strips root destination dir if filename matches --- lib/jekyll.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/jekyll.rb b/lib/jekyll.rb index ffe9e4ac..f3c38f53 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -161,7 +161,9 @@ module Jekyll questionable_path.insert(0, "/") if questionable_path.start_with?("~") clean_path = File.expand_path(questionable_path, "/") - if clean_path.start_with?(base_directory) + return clean_path if clean_path.eql?(base_directory) + + if clean_path.start_with?(base_directory.sub(%r!\z!, "/")) clean_path else clean_path.sub!(%r!\A\w:/!, "/") From 0d8796fbe60dbca7e7af8d82bf94a822a8ffce90 Mon Sep 17 00:00:00 2001 From: "Heng, K. (Stephen)" Date: Fri, 26 Aug 2016 11:20:03 +0800 Subject: [PATCH 3/5] Test case for issue #5276, where Jekyll.sanitized_path strips base path incorrectly if file path has matching prefix --- test/test_path_sanitization.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_path_sanitization.rb b/test/test_path_sanitization.rb index df7ef172..a0e2f8d6 100644 --- a/test/test_path_sanitization.rb +++ b/test/test_path_sanitization.rb @@ -28,4 +28,22 @@ class TestPathSanitization < JekyllUnitTest assert_equal source_dir("files", "hi.txt"), Jekyll.sanitized_path(source_dir, "f./../../../../../../files/hi.txt") end + + context "on Windows with file path has matching prefix" do + setup do + @base_path = "D:/site" + @file_path = "D:/sitemap.xml" + allow(Dir).to receive(:pwd).and_return("D:/") + end + + should "not strip base path" do + assert_equal "D:/site/sitemap.xml", + Jekyll.sanitized_path(@base_path, @file_path) + end + end + + should "not strip base path if file path has matching prefix" do + assert_equal "/site/sitemap.xml", + Jekyll.sanitized_path("/site", "sitemap.xml") + end end From 23d79299480fe8dd6c609aebcfd0e2ba8c193039 Mon Sep 17 00:00:00 2001 From: "Heng, K. (Stephen)" Date: Fri, 26 Aug 2016 11:43:32 +0800 Subject: [PATCH 4/5] Test case for issue #5192, where Jekyll.sanitized_path strips drive name on Windows incorrectly --- test/test_path_sanitization.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_path_sanitization.rb b/test/test_path_sanitization.rb index a0e2f8d6..a94fc0c5 100644 --- a/test/test_path_sanitization.rb +++ b/test/test_path_sanitization.rb @@ -29,6 +29,19 @@ class TestPathSanitization < JekyllUnitTest Jekyll.sanitized_path(source_dir, "f./../../../../../../files/hi.txt") end + context "on Windows with absolute path" do + setup do + @base_path = "D:/demo" + @file_path = "D:/demo/_site" + allow(Dir).to receive(:pwd).and_return("D:/") + end + + should "strip just the clean path drive name" do + assert_equal "D:/demo/_site", + Jekyll.sanitized_path(@base_path, @file_path) + end + end + context "on Windows with file path has matching prefix" do setup do @base_path = "D:/site" From bacb300876b758b0ff1a27ed62e456757eea372e Mon Sep 17 00:00:00 2001 From: Heng Kwokfu Date: Sat, 24 Sep 2016 21:55:02 +0800 Subject: [PATCH 5/5] Skip Windows tests in non-Windows environment. --- test/test_path_sanitization.rb | 42 ++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/test/test_path_sanitization.rb b/test/test_path_sanitization.rb index a94fc0c5..31a4b92e 100644 --- a/test/test_path_sanitization.rb +++ b/test/test_path_sanitization.rb @@ -29,29 +29,31 @@ class TestPathSanitization < JekyllUnitTest Jekyll.sanitized_path(source_dir, "f./../../../../../../files/hi.txt") end - context "on Windows with absolute path" do - setup do - @base_path = "D:/demo" - @file_path = "D:/demo/_site" - allow(Dir).to receive(:pwd).and_return("D:/") + if Jekyll::Utils::Platforms.really_windows? + context "on Windows with absolute path" do + setup do + @base_path = "D:/demo" + @file_path = "D:/demo/_site" + allow(Dir).to receive(:pwd).and_return("D:/") + end + + should "strip just the clean path drive name" do + assert_equal "D:/demo/_site", + Jekyll.sanitized_path(@base_path, @file_path) + end end - should "strip just the clean path drive name" do - assert_equal "D:/demo/_site", - Jekyll.sanitized_path(@base_path, @file_path) - end - end + context "on Windows with file path has matching prefix" do + setup do + @base_path = "D:/site" + @file_path = "D:/sitemap.xml" + allow(Dir).to receive(:pwd).and_return("D:/") + end - context "on Windows with file path has matching prefix" do - setup do - @base_path = "D:/site" - @file_path = "D:/sitemap.xml" - allow(Dir).to receive(:pwd).and_return("D:/") - end - - should "not strip base path" do - assert_equal "D:/site/sitemap.xml", - Jekyll.sanitized_path(@base_path, @file_path) + should "not strip base path" do + assert_equal "D:/site/sitemap.xml", + Jekyll.sanitized_path(@base_path, @file_path) + end end end