From 1147186b4c31919d391386a724512bc739be5fdc Mon Sep 17 00:00:00 2001 From: Dustin DeYoung Date: Sun, 26 May 2013 10:03:23 -0400 Subject: [PATCH 1/5] Page create with a relative path forces an absolute path. --- lib/gollum/app.rb | 9 ++++--- lib/gollum/helpers.rb | 11 +++++++-- test/test_app.rb | 53 +++++++++++++++++++++++++++------------- test/test_app_helpers.rb | 12 +++++++++ 4 files changed, 62 insertions(+), 23 deletions(-) diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 1a043a56..a8b9abab 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -101,7 +101,7 @@ module Precious # name, path, version def wiki_page(name, path = nil, version = nil, exact = true) wiki = wiki_new - + path = name if path.nil? name = extract_name(name) || wiki.index_page path = extract_path(path) @@ -125,7 +125,7 @@ module Precious wikip = wiki_page(params[:splat].first) @name = wikip.name @path = wikip.path - + wiki = wikip.wiki if page = wikip.page if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent) @@ -222,6 +222,7 @@ module Precious # not /docs/Home because write_page will append /docs @path = @path.sub(page_dir, '/') if @path.start_with? page_dir end + @path = clean_path(@path) page = wikip.page if page @@ -393,14 +394,14 @@ module Precious @page = page @name = name @content = page.formatted_data - + # Extensions and layout data @editable = true @toc_content = wiki.universal_toc ? @page.toc_data : nil @mathjax = wiki.mathjax @h1_title = wiki.h1_title @bar_side = wiki.bar_side - + mustache :page elsif file = wiki.file(fullpath) content_type file.mime_type diff --git a/lib/gollum/helpers.rb b/lib/gollum/helpers.rb index e9145a0a..69d60f6a 100644 --- a/lib/gollum/helpers.rb +++ b/lib/gollum/helpers.rb @@ -15,8 +15,8 @@ module Precious if file_path[-1, 1] == "/" return nil end - - # File.basename is too eager to please and will return the last + + # File.basename is too eager to please and will return the last # component of the path even if it ends with a directory separator. ::File.basename(file_path) end @@ -25,6 +25,13 @@ module Precious [nil,''].include?(param) ? nil : CGI.unescape(param) end + # Ensure path begins with a single leading slash + def clean_path(path) + if path + (path[0] != '/' ? path.insert(0, '/') : path).gsub('//','/') + end + end + # Remove all slashes from the start of string. # Remove all double slashes def clean_url url diff --git a/test/test_app.rb b/test/test_app.rb index 6ee2bf03..17b43c16 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -5,14 +5,14 @@ context "Frontend" do include Rack::Test::Methods setup do - @path = cloned_testpath("examples/revert.git") - @wiki = Gollum::Wiki.new(@path) - Precious::App.set(:gollum_path, @path) + @wikipath = cloned_testpath("examples/revert.git") + @wiki = Gollum::Wiki.new(@wikipath) + Precious::App.set(:gollum_path, @wikipath) Precious::App.set(:wiki_options, {}) end teardown do - FileUtils.rm_rf(@path) + FileUtils.rm_rf(@wikipath) end test "urls transform unicode" do @@ -279,7 +279,7 @@ context "Frontend" do :format => 'markdown', :message => 'foo' assert_equal "http://example.org/foo/home", last_response.headers['Location'] - + follow_redirect! assert last_response.ok? end @@ -300,6 +300,25 @@ context "Frontend" do assert last_response.ok? end + test "create sets the correct path for a relative path subdirectory" do + dir = "foodir" + name = "#{dir}/bar" + get "/create/#{name}" + assert_match(/\/#{dir}/, last_response.body) + assert_not_match(/(? "foo"}) + dir = "bardir" + name = "#{dir}/baz" + get "/create/foo/#{name}" + assert_match(/\/#{dir}/, last_response.body) + assert_not_match(/(? nil}) + end + test "edit returns nil for non-existant page" do # post '/edit' fails. post '/edit/' works. page = 'not-real-page' @@ -372,9 +391,9 @@ context "Frontend" do end test "previews content on the first page of an empty wiki" do - @path = cloned_testpath("examples/empty.git") - @wiki = Gollum::Wiki.new(@path) - Precious::App.set(:gollum_path, @path) + @wikipath = cloned_testpath("examples/empty.git") + @wiki = Gollum::Wiki.new(@wikipath) + Precious::App.set(:gollum_path, @wikipath) Precious::App.set(:wiki_options, {}) post "/preview", :content => 'abc', :format => 'markdown' @@ -442,20 +461,20 @@ context "Frontend" do Precious::App.set(:wiki_options, { :base_path => nil }) end =end - + test "author details in session are used" do page1 = @wiki.page('A') - + gollum_author = { :name => 'ghi', :email => 'jkl' } session = { 'gollum.author' => gollum_author } - + post "/edit/A", { :content => 'abc', :page => 'A', :format => page1.format, :message => 'def' }, { 'rack.session' => session } follow_redirect! assert last_response.ok? - + @wiki.clear_cache page2 = @wiki.page(page1.name) - + author = page2.version.author assert_equal 'ghi', author.name assert_equal 'jkl', author.email @@ -494,14 +513,14 @@ context "Frontend with lotr" do include Rack::Test::Methods setup do - @path = cloned_testpath("examples/lotr.git") - @wiki = Gollum::Wiki.new(@path) - Precious::App.set(:gollum_path, @path) + @wikipath = cloned_testpath("examples/lotr.git") + @wiki = Gollum::Wiki.new(@wikipath) + Precious::App.set(:gollum_path, @wikipath) Precious::App.set(:wiki_options, {}) end teardown do - FileUtils.rm_rf(@path) + FileUtils.rm_rf(@wikipath) end # Here's the dir structure of lotr.git diff --git a/test/test_app_helpers.rb b/test/test_app_helpers.rb index e2946e8e..549afff2 100644 --- a/test/test_app_helpers.rb +++ b/test/test_app_helpers.rb @@ -9,5 +9,17 @@ context "Precious::Helpers" do assert_equal 'Mordor', extract_path('Mordor/Sauron') assert_equal 'Mordor/Sauron', extract_path('Mordor/Sauron/Evil') end + + test "clean path without leading slash" do + assert_equal '/Mordor', clean_path('Mordor') + end + + test "clean path with leading slash" do + assert_equal '/Mordor', clean_path('/Mordor') + end + + test "clean path with double leading slash" do + assert_equal '/Mordor', clean_path('//Mordor') + end end From eab612bdd0b61f67eb6f2296a9b87c01c4c58eaa Mon Sep 17 00:00:00 2001 From: Dustin DeYoung Date: Sun, 26 May 2013 11:03:40 -0400 Subject: [PATCH 2/5] Remove temporary change in app test. --- test/test_app.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/test_app.rb b/test/test_app.rb index 17b43c16..d764cb66 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -5,14 +5,14 @@ context "Frontend" do include Rack::Test::Methods setup do - @wikipath = cloned_testpath("examples/revert.git") - @wiki = Gollum::Wiki.new(@wikipath) - Precious::App.set(:gollum_path, @wikipath) + @path = cloned_testpath("examples/revert.git") + @wiki = Gollum::Wiki.new(@path) + Precious::App.set(:gollum_path, @path) Precious::App.set(:wiki_options, {}) end teardown do - FileUtils.rm_rf(@wikipath) + FileUtils.rm_rf(@path) end test "urls transform unicode" do @@ -391,9 +391,9 @@ context "Frontend" do end test "previews content on the first page of an empty wiki" do - @wikipath = cloned_testpath("examples/empty.git") - @wiki = Gollum::Wiki.new(@wikipath) - Precious::App.set(:gollum_path, @wikipath) + @path = cloned_testpath("examples/empty.git") + @wiki = Gollum::Wiki.new(@path) + Precious::App.set(:gollum_path, @path) Precious::App.set(:wiki_options, {}) post "/preview", :content => 'abc', :format => 'markdown' @@ -513,14 +513,14 @@ context "Frontend with lotr" do include Rack::Test::Methods setup do - @wikipath = cloned_testpath("examples/lotr.git") - @wiki = Gollum::Wiki.new(@wikipath) - Precious::App.set(:gollum_path, @wikipath) + @path = cloned_testpath("examples/lotr.git") + @wiki = Gollum::Wiki.new(@path) + Precious::App.set(:gollum_path, @path) Precious::App.set(:wiki_options, {}) end teardown do - FileUtils.rm_rf(@wikipath) + FileUtils.rm_rf(@path) end # Here's the dir structure of lotr.git From 999bbf3d508286a9aaeffa56653d2abb0bbf405c Mon Sep 17 00:00:00 2001 From: Dustin DeYoung Date: Sun, 26 May 2013 11:10:50 -0400 Subject: [PATCH 3/5] Regex is 1.8.7 compatible for subpage create tests --- test/test_app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_app.rb b/test/test_app.rb index d764cb66..2a44fee8 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -305,7 +305,7 @@ context "Frontend" do name = "#{dir}/bar" get "/create/#{name}" assert_match(/\/#{dir}/, last_response.body) - assert_not_match(/(? nil}) end From f63180d8d80d6d42471fd276be89d6e3dbfb18c7 Mon Sep 17 00:00:00 2001 From: Dustin DeYoung Date: Sun, 26 May 2013 11:16:43 -0400 Subject: [PATCH 4/5] Correct 1.8.7 negative match assertion. --- test/test_app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_app.rb b/test/test_app.rb index 2a44fee8..c7fb3aad 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -305,7 +305,7 @@ context "Frontend" do name = "#{dir}/bar" get "/create/#{name}" assert_match(/\/#{dir}/, last_response.body) - assert_not_match(/[^\/]#{dir}/, last_response.body) + assert_no_match(/[^\/]#{dir}/, last_response.body) end test "create sets the correct path for a relative path subdirectory with the page file directory set" do @@ -314,7 +314,7 @@ context "Frontend" do name = "#{dir}/baz" get "/create/foo/#{name}" assert_match(/\/#{dir}/, last_response.body) - assert_not_match(/[^\/]#{dir}/, last_response.body) + assert_no_match(/[^\/]#{dir}/, last_response.body) # reset page_file_dir Precious::App.set(:wiki_options, {:page_file_dir => nil}) end From e2c0dcc0da6582d63c335c4d77a983e83fbb8c10 Mon Sep 17 00:00:00 2001 From: Dustin DeYoung Date: Sun, 26 May 2013 16:14:56 -0400 Subject: [PATCH 5/5] Ruby 1.8.7 compatible double slash gsub --- lib/gollum/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gollum/helpers.rb b/lib/gollum/helpers.rb index 69d60f6a..1354f862 100644 --- a/lib/gollum/helpers.rb +++ b/lib/gollum/helpers.rb @@ -28,7 +28,7 @@ module Precious # Ensure path begins with a single leading slash def clean_path(path) if path - (path[0] != '/' ? path.insert(0, '/') : path).gsub('//','/') + (path[0] != '/' ? path.insert(0, '/') : path).gsub(/\/{2,}/,'/') end end