patch URLFilters to prevent `//` (#6058)

Merge pull request 6058
This commit is contained in:
ashmaroli 2017-06-15 00:36:55 +05:30 committed by jekyllbot
parent cee3cc506e
commit 2a4d33e615
2 changed files with 47 additions and 4 deletions

View File

@ -11,7 +11,6 @@ module Jekyll
def absolute_url(input) def absolute_url(input)
return if input.nil? return if input.nil?
return input if Addressable::URI.parse(input).absolute? return input if Addressable::URI.parse(input).absolute?
site = @context.registers[:site]
return relative_url(input).to_s if site.config["url"].nil? return relative_url(input).to_s if site.config["url"].nil?
Addressable::URI.parse(site.config["url"] + relative_url(input)).normalize.to_s Addressable::URI.parse(site.config["url"] + relative_url(input)).normalize.to_s
end end
@ -23,14 +22,22 @@ module Jekyll
# Returns a URL relative to the domain root as a String. # Returns a URL relative to the domain root as a String.
def relative_url(input) def relative_url(input)
return if input.nil? return if input.nil?
site = @context.registers[:site] return ensure_leading_slash(input.to_s) if sanitized_baseurl.nil?
return ensure_leading_slash(input.to_s) if site.config["baseurl"].nil?
Addressable::URI.parse( Addressable::URI.parse(
ensure_leading_slash(site.config["baseurl"]) + ensure_leading_slash(input.to_s) ensure_leading_slash(sanitized_baseurl) + ensure_leading_slash(input.to_s)
).normalize.to_s ).normalize.to_s
end end
private private
def site
@context.registers[:site]
end
def sanitized_baseurl
site.config["baseurl"].chomp("/")
end
def ensure_leading_slash(input) def ensure_leading_slash(input)
return input if input.nil? || input.empty? || input.start_with?("/") return input if input.nil? || input.empty? || input.start_with?("/")
"/#{input}" "/#{input}"

View File

@ -394,6 +394,15 @@ class TestFilters < JekyllUnitTest
assert_equal "http://example.com/", filter.absolute_url(page_url) assert_equal "http://example.com/", filter.absolute_url(page_url)
end end
should "not append a forward slash if both input and baseurl are simply '/'" do
page_url = "/"
filter = make_filter_mock({
"url" => "http://example.com",
"baseurl" => "/",
})
assert_equal "http://example.com/", filter.absolute_url(page_url)
end
should "normalize international URLs" do should "normalize international URLs" do
page_url = "" page_url = ""
filter = make_filter_mock({ filter = make_filter_mock({
@ -448,6 +457,33 @@ class TestFilters < JekyllUnitTest
}) })
assert_equal "/base", filter.relative_url(page_url) assert_equal "/base", filter.relative_url(page_url)
end end
should "not prepend a forward slash if baseurl ends with a single '/'" do
page_url = "/css/main.css"
filter = make_filter_mock({
"url" => "http://example.com",
"baseurl" => "/base/",
})
assert_equal "/base/css/main.css", filter.relative_url(page_url)
end
should "not return valid URI if baseurl ends with multiple '/'" do
page_url = "/css/main.css"
filter = make_filter_mock({
"url" => "http://example.com",
"baseurl" => "/base//",
})
refute_equal "/base/css/main.css", filter.relative_url(page_url)
end
should "not prepend a forward slash if both input and baseurl are simply '/'" do
page_url = "/"
filter = make_filter_mock({
"url" => "http://example.com",
"baseurl" => "/",
})
assert_equal "/", filter.relative_url(page_url)
end
end end
context "jsonify filter" do context "jsonify filter" do