summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2014-07-30 21:47:03 -0600
committerDan Allen <dan.j.allen@gmail.com>2014-07-30 21:47:03 -0600
commit05fb22b59b4432971be91eeaed6c56537fcfa94d (patch)
tree9536b8d9de279a505cb192d14d955f88dda2a86e
parenta1a453fd4d19c414104dfef82cfdd090d1dae87e (diff)
parent36a33626251d7ddd05f43f7aa73d31bfe8b2b8c1 (diff)
Merge pull request #1048 from mojavelinux/issue-690
resolve relative paths relative to base_dir in unsafe mode
-rw-r--r--lib/asciidoctor/abstract_node.rb18
-rw-r--r--test/paths_test.rb15
2 files changed, 26 insertions, 7 deletions
diff --git a/lib/asciidoctor/abstract_node.rb b/lib/asciidoctor/abstract_node.rb
index 10dfe419..ed8cc67b 100644
--- a/lib/asciidoctor/abstract_node.rb
+++ b/lib/asciidoctor/abstract_node.rb
@@ -452,14 +452,18 @@ class AbstractNode
# Returns the [String] path resolved from the start and target paths, with any
# parent references resolved and self references removed. If a jail is provided,
# this path will be guaranteed to be contained within the jail.
- def normalize_system_path(target, start = nil, jail = nil, opts = {})
- if start.nil?
- start = @document.base_dir
- end
- if jail.nil? && @document.safe >= SafeMode::SAFE
- jail = @document.base_dir
+ def normalize_system_path target, start = nil, jail = nil, opts = {}
+ if (doc = @document).safe < SafeMode::SAFE
+ if start
+ start = ::File.join doc.base_dir, start unless (@path_resolver ||= PathResolver.new).is_root? start
+ else
+ start = doc.base_dir
+ end
+ else
+ start = doc.base_dir unless start
+ jail = doc.base_dir unless jail
end
- (@path_resolver ||= PathResolver.new).system_path(target, start, jail, opts)
+ (@path_resolver ||= PathResolver.new).system_path target, start, jail, opts
end
# Public: Normalize the asset file or directory to a concrete and rinsed path
diff --git a/test/paths_test.rb b/test/paths_test.rb
index ea7b0a5b..1503d30d 100644
--- a/test/paths_test.rb
+++ b/test/paths_test.rb
@@ -200,6 +200,21 @@ context 'Path Resolver' do
assert_equal "#{JAIL}/part1/chapter1/section1.adoc", filename
assert_equal 'part1/chapter1/section1.adoc', @resolver.relative_path(filename, JAIL)
end
+
+ test 'should resolve relative path relative to base dir in unsafe mode' do
+ base_dir = fixture_path 'base'
+ doc = empty_document :base_dir => base_dir, :safe => Asciidoctor::SafeMode::UNSAFE
+ expected = ::File.join base_dir, 'images', 'tiger.png'
+ actual = doc.normalize_system_path 'tiger.png', 'images'
+ assert_equal expected, actual
+ end
+
+ test 'should resolve absolute path as absolute in unsafe mode' do
+ base_dir = fixture_path 'base'
+ doc = empty_document :base_dir => base_dir, :safe => Asciidoctor::SafeMode::UNSAFE
+ actual = doc.normalize_system_path 'tiger.png', '/etc/images'
+ assert_equal '/etc/images/tiger.png', actual
+ end
end
context 'Helpers' do