"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/paths_test.rb" (1 Jun 2019, 17294 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require_relative 'test_helper'
    3 
    4 context 'Path Resolver' do
    5   context 'Web Paths' do
    6     def setup
    7       @resolver = Asciidoctor::PathResolver.new
    8     end
    9 
   10     test 'target with absolute path' do
   11       assert_equal '/images', @resolver.web_path('/images')
   12       assert_equal '/images', @resolver.web_path('/images', '')
   13       assert_equal '/images', @resolver.web_path('/images', nil)
   14     end
   15 
   16     test 'target with relative path' do
   17       assert_equal 'images', @resolver.web_path('images')
   18       assert_equal 'images', @resolver.web_path('images', '')
   19       assert_equal 'images', @resolver.web_path('images', nil)
   20     end
   21 
   22     test 'target with hidden relative path' do
   23       assert_equal '.images', @resolver.web_path('.images')
   24       assert_equal '.images', @resolver.web_path('.images', '')
   25       assert_equal '.images', @resolver.web_path('.images', nil)
   26     end
   27 
   28     test 'target with path relative to current directory' do
   29       assert_equal './images', @resolver.web_path('./images')
   30       assert_equal './images', @resolver.web_path('./images', '')
   31       assert_equal './images', @resolver.web_path('./images', nil)
   32     end
   33 
   34     test 'target with absolute path ignores start path' do
   35       assert_equal '/images', @resolver.web_path('/images', 'foo')
   36       assert_equal '/images', @resolver.web_path('/images', '/foo')
   37       assert_equal '/images', @resolver.web_path('/images', './foo')
   38     end
   39 
   40     test 'target with relative path appended to start path' do
   41       assert_equal 'assets/images', @resolver.web_path('images', 'assets')
   42       assert_equal '/assets/images', @resolver.web_path('images', '/assets')
   43       #assert_equal '/assets/images/tiger.png', @resolver.web_path('tiger.png', '/assets//images')
   44       assert_equal './assets/images', @resolver.web_path('images', './assets')
   45       assert_equal '/theme.css', @resolver.web_path('theme.css', '/')
   46       assert_equal '/css/theme.css', @resolver.web_path('theme.css', '/css/')
   47     end
   48 
   49     test 'target with path relative to current directory appended to start path' do
   50       assert_equal 'assets/images', @resolver.web_path('./images', 'assets')
   51       assert_equal '/assets/images', @resolver.web_path('./images', '/assets')
   52       assert_equal './assets/images', @resolver.web_path('./images', './assets')
   53     end
   54 
   55     test 'target with relative path appended to url start path' do
   56       assert_equal 'http://www.example.com/assets/images', @resolver.web_path('images', 'http://www.example.com/assets')
   57     end
   58 
   59     # enable if we want to allow web_path to detect and preserve a target URI
   60     #test 'target with file url appended to relative path' do
   61     #  assert_equal 'file:///home/username/styles/asciidoctor.css', @resolver.web_path('file:///home/username/styles/asciidoctor.css', '.')
   62     #end
   63 
   64     # enable if we want to allow web_path to detect and preserve a target URI
   65     #test 'target with http url appended to relative path' do
   66     #  assert_equal 'http://example.com/asciidoctor.css', @resolver.web_path('http://example.com/asciidoctor.css', '.')
   67     #end
   68 
   69     test 'normalize target' do
   70       assert_equal '../images', @resolver.web_path('../images/../images')
   71     end
   72 
   73     test 'append target to start path and normalize' do
   74       assert_equal '../images', @resolver.web_path('../images/../images', '../images')
   75       assert_equal '../../images', @resolver.web_path('../images', '..')
   76     end
   77 
   78     test 'normalize parent directory that follows root' do
   79       assert_equal '/tiger.png', @resolver.web_path('/../tiger.png')
   80       assert_equal '/tiger.png', @resolver.web_path('/../../tiger.png')
   81     end
   82 
   83     test 'uses start when target is empty' do
   84       assert_equal 'assets/images', @resolver.web_path('', 'assets/images')
   85       assert_equal 'assets/images', @resolver.web_path(nil, 'assets/images')
   86     end
   87 
   88     test 'posixifies windows paths' do
   89       @resolver.file_separator = '\\'
   90       assert_equal '/images', @resolver.web_path('\\images')
   91       assert_equal '../images', @resolver.web_path('..\\images')
   92       assert_equal '/images', @resolver.web_path('\\..\\images')
   93       assert_equal 'assets/images', @resolver.web_path('assets\\images')
   94       assert_equal '../assets/images', @resolver.web_path('assets\\images', '..\\images\\..')
   95     end
   96 
   97     test 'URL encode spaces in path' do
   98       assert_equal 'assets%20and%20stuff/lots%20of%20images', @resolver.web_path('lots of images', 'assets and stuff')
   99     end
  100   end
  101 
  102   context 'System Paths' do
  103     JAIL = '/home/doctor/docs'
  104     default_logger = Asciidoctor::LoggerManager.logger
  105 
  106     def setup
  107       @resolver = Asciidoctor::PathResolver.new
  108       @logger = (Asciidoctor::LoggerManager.logger = Asciidoctor::MemoryLogger.new)
  109     end
  110 
  111     teardown do
  112       Asciidoctor::LoggerManager.logger = default_logger
  113     end
  114 
  115     test 'raises security error if jail is not an absolute path' do
  116       begin
  117         @resolver.system_path('images/tiger.png', '/etc', 'foo')
  118         flunk 'Expecting SecurityError to be raised'
  119       rescue SecurityError
  120       end
  121     end
  122 
  123     #test 'raises security error if jail is not a canoncial path' do
  124     #  begin
  125     #    @resolver.system_path('images/tiger.png', '/etc', %(#{JAIL}/../foo))
  126     #    flunk 'Expecting SecurityError to be raised'
  127     #  rescue SecurityError
  128     #  end
  129     #end
  130 
  131     test 'prevents access to paths outside of jail' do
  132       result = @resolver.system_path '../../../../../css', %(#{JAIL}/assets/stylesheets), JAIL
  133       assert_equal %(#{JAIL}/css), result
  134       assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
  135 
  136       @logger.clear
  137       result = @resolver.system_path '/../../../../../css', %(#{JAIL}/assets/stylesheets), JAIL
  138       assert_equal %(#{JAIL}/css), result
  139       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  140 
  141       @logger.clear
  142       result = @resolver.system_path '../../../css', '../../..', JAIL
  143       assert_equal %(#{JAIL}/css), result
  144       assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
  145     end
  146 
  147     test 'throws exception for illegal path access if recover is false' do
  148       begin
  149         @resolver.system_path('../../../../../css', "#{JAIL}/assets/stylesheets", JAIL, recover: false)
  150         flunk 'Expecting SecurityError to be raised'
  151       rescue SecurityError
  152       end
  153     end
  154 
  155     test 'resolves start path if target is empty' do
  156       assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('', "#{JAIL}/assets/stylesheets", JAIL)
  157       assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path(nil, "#{JAIL}/assets/stylesheets", JAIL)
  158     end
  159 
  160     test 'expands parent references in start path if target is empty' do
  161       assert_equal "#{JAIL}/stylesheets", @resolver.system_path('', "#{JAIL}/assets/../stylesheets", JAIL)
  162     end
  163 
  164     test 'expands parent references in start path if target is not empty' do
  165       assert_equal "#{JAIL}/stylesheets/site.css", @resolver.system_path('site.css', "#{JAIL}/assets/../stylesheets", JAIL)
  166     end
  167 
  168     test 'resolves start path if target is dot' do
  169       assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('.', "#{JAIL}/assets/stylesheets", JAIL)
  170       assert_equal "#{JAIL}/assets/stylesheets", @resolver.system_path('./', "#{JAIL}/assets/stylesheets", JAIL)
  171     end
  172 
  173     test 'treats absolute target outside of jail as relative when jail is specified' do
  174       result = @resolver.system_path '/', "#{JAIL}/assets/stylesheets", JAIL
  175       assert_equal JAIL, result
  176       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  177 
  178       @logger.clear
  179       result = @resolver.system_path '/foo', "#{JAIL}/assets/stylesheets", JAIL
  180       assert_equal "#{JAIL}/foo", result
  181       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  182 
  183       @logger.clear
  184       result = @resolver.system_path '/../foo', "#{JAIL}/assets/stylesheets", JAIL
  185       assert_equal "#{JAIL}/foo", result
  186       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  187 
  188       @logger.clear
  189       @resolver.file_separator = '\\'
  190       result = @resolver.system_path 'baz.adoc', 'C:/foo', 'C:/bar'
  191       assert_equal 'C:/bar/baz.adoc', result
  192       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  193     end
  194 
  195     test 'allows use of absolute target or start if resolved path is sub-path of jail' do
  196       assert_equal "#{JAIL}/my/path", @resolver.system_path("#{JAIL}/my/path", '', JAIL)
  197       assert_equal "#{JAIL}/my/path", @resolver.system_path("#{JAIL}/my/path", nil, JAIL)
  198       assert_equal "#{JAIL}/my/path", @resolver.system_path('', "#{JAIL}/my/path", JAIL)
  199       assert_equal "#{JAIL}/my/path", @resolver.system_path(nil, "#{JAIL}/my/path", JAIL)
  200       assert_equal "#{JAIL}/my/path", @resolver.system_path('path', "#{JAIL}/my", JAIL)
  201       assert_equal '/foo/bar/baz.adoc', @resolver.system_path('/foo/bar/baz.adoc', nil, '/')
  202       assert_equal '/foo/bar/baz.adoc', @resolver.system_path('baz.adoc', '/foo/bar', '/')
  203       assert_equal '/foo/bar/baz.adoc', @resolver.system_path('baz.adoc', 'foo/bar', '/')
  204     end
  205 
  206     test 'uses jail path if start path is empty' do
  207       assert_equal "#{JAIL}/images/tiger.png", @resolver.system_path('images/tiger.png', '', JAIL)
  208       assert_equal "#{JAIL}/images/tiger.png", @resolver.system_path('images/tiger.png', nil, JAIL)
  209     end
  210 
  211     test 'warns if start is not contained within jail' do
  212       result = @resolver.system_path 'images/tiger.png', '/etc', JAIL
  213       assert_equal %(#{JAIL}/images/tiger.png), result
  214       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  215 
  216       @logger.clear
  217       result = @resolver.system_path '.', '/etc', JAIL
  218       assert_equal JAIL, result
  219       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  220 
  221       @logger.clear
  222       @resolver.file_separator = '\\'
  223       result = @resolver.system_path '.', 'C:/foo', 'C:/bar'
  224       assert_equal 'C:/bar', result
  225       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  226     end
  227 
  228     test 'allows start path to be parent of jail if resolved target is inside jail' do
  229       assert_equal "#{JAIL}/foo/path", @resolver.system_path('foo/path', JAIL, "#{JAIL}/foo")
  230       @resolver.file_separator = '\\'
  231       assert_equal "C:/dev/project/README.adoc", @resolver.system_path('project/README.adoc', 'C:/dev', 'C:/dev/project')
  232     end
  233 
  234     test 'relocates target to jail if resolved value fails outside of jail' do
  235       result = @resolver.system_path 'bar/baz.adoc', JAIL, "#{JAIL}/foo"
  236       assert_equal %(#{JAIL}/foo/bar/baz.adoc), result
  237       assert_message @logger, :WARN, 'path is outside of jail; recovering automatically'
  238 
  239       @logger.clear
  240       @resolver.file_separator = '\\'
  241       result = @resolver.system_path 'bar/baz.adoc', 'D:/', 'C:/foo'
  242       assert_equal 'C:/foo/bar/baz.adoc', result
  243       assert_message @logger, :WARN, '~outside of jail root'
  244     end
  245 
  246     test 'raises security error if start is not contained within jail and recover is disabled' do
  247       begin
  248         @resolver.system_path('images/tiger.png', '/etc', JAIL, recover: false)
  249         flunk 'Expecting SecurityError to be raised'
  250       rescue SecurityError
  251       end
  252 
  253       begin
  254         @resolver.system_path('.', '/etc', JAIL, recover: false)
  255         flunk 'Expecting SecurityError to be raised'
  256       rescue SecurityError
  257       end
  258     end
  259 
  260     test 'expands parent references in absolute path if jail is not specified' do
  261       assert_equal '/etc/stylesheet.css', @resolver.system_path('/usr/share/../../etc/stylesheet.css')
  262     end
  263 
  264     test 'resolves absolute directory if jail is not specified' do
  265       assert_equal '/usr/share/stylesheet.css', @resolver.system_path('/usr/share/stylesheet.css', '/home/dallen/docs/assets/stylesheets')
  266     end
  267 
  268     test 'resolves ancestor directory of start if jail is not specified' do
  269       assert_equal '/usr/share/stylesheet.css', @resolver.system_path('../../../../../usr/share/stylesheet.css', '/home/dallen/docs/assets/stylesheets')
  270     end
  271 
  272     test 'resolves absolute path if start is absolute and target is relative' do
  273       assert_equal '/usr/share/assets/stylesheet.css', @resolver.system_path('assets/stylesheet.css', '/usr/share')
  274     end
  275 
  276     test 'File.dirname preserves UNC path root on Windows' do
  277       assert_equal File.dirname('\\\\server\\docs\\file.html'), '\\\\server\\docs'
  278     end if windows?
  279 
  280     test 'File.dirname preserves posix-style UNC path root on Windows' do
  281       assert_equal File.dirname('//server/docs/file.html'), '//server/docs'
  282     end if windows?
  283 
  284     test 'resolves UNC path if start is absolute and target is relative' do
  285       assert_equal '//QA/c$/users/asciidoctor/assets/stylesheet.css', @resolver.system_path('assets/stylesheet.css', '//QA/c$/users/asciidoctor')
  286     end
  287 
  288     test 'resolves UNC path if target is UNC path' do
  289       @resolver.file_separator = '\\'
  290       assert_equal '//server/docs/output.html', @resolver.system_path('\\\\server\\docs\\output.html')
  291     end
  292 
  293     test 'resolves UNC path if target is posix-style UNC path' do
  294       assert_equal '//server/docs/output.html', @resolver.system_path('//server/docs/output.html')
  295     end
  296 
  297     test 'resolves relative target relative to current directory if start is empty' do
  298       pwd = File.expand_path(Dir.pwd)
  299       assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png', '')
  300       assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png', nil)
  301       assert_equal "#{pwd}/images/tiger.png", @resolver.system_path('images/tiger.png')
  302     end
  303 
  304     test 'resolves relative hidden target relative to current directory if start is empty' do
  305       pwd = File.expand_path(Dir.pwd)
  306       assert_equal "#{pwd}/.images/tiger.png", @resolver.system_path('.images/tiger.png', '')
  307       assert_equal "#{pwd}/.images/tiger.png", @resolver.system_path('.images/tiger.png', nil)
  308     end
  309 
  310     test 'resolves and normalizes start when target is empty' do
  311       pwd = File.expand_path Dir.pwd
  312       assert_equal '/home/doctor/docs', (@resolver.system_path '', '/home/doctor/docs')
  313       assert_equal '/home/doctor/docs', (@resolver.system_path '', '/home/doctor/./docs')
  314       assert_equal '/home/doctor/docs', (@resolver.system_path nil, '/home/doctor/docs')
  315       assert_equal '/home/doctor/docs', (@resolver.system_path nil, '/home/doctor/./docs')
  316       assert_equal %(#{pwd}/assets/images), (@resolver.system_path nil, 'assets/images')
  317       @resolver.system_path '', '../assets/images', JAIL
  318       assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
  319     end
  320 
  321     test 'posixifies windows paths' do
  322       @resolver.file_separator = '\\'
  323       assert_equal "#{JAIL}/assets/css", @resolver.system_path('..\\css', 'assets\\stylesheets', JAIL)
  324     end
  325 
  326     test 'resolves windows paths when file separator is backlash' do
  327       @resolver.file_separator = '\\'
  328 
  329       assert_equal 'C:/data/docs', (@resolver.system_path '..', 'C:\\data\\docs\\assets', 'C:\\data\\docs')
  330 
  331       result = @resolver.system_path '..\\..', 'C:\\data\\docs\\assets', 'C:\\data\\docs'
  332       assert_equal 'C:/data/docs', result
  333       assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
  334 
  335       @logger.clear
  336       result = @resolver.system_path '..\\..\\css', 'C:\\data\\docs\\assets', 'C:\\data\\docs'
  337       assert_equal 'C:/data/docs/css', result
  338       assert_message @logger, :WARN, 'path has illegal reference to ancestor of jail; recovering automatically'
  339     end
  340 
  341     test 'should calculate relative path' do
  342       filename = @resolver.system_path('part1/chapter1/section1.adoc', nil, JAIL)
  343       assert_equal "#{JAIL}/part1/chapter1/section1.adoc", filename
  344       assert_equal 'part1/chapter1/section1.adoc', @resolver.relative_path(filename, JAIL)
  345     end
  346 
  347     test 'should resolve relative path to filename outside of base directory' do
  348       filename = '/home/shared/partials'
  349       base_dir = '/home/user/docs'
  350       result = @resolver.relative_path filename, base_dir
  351       assert_equal '../../shared/partials', result
  352     end
  353 
  354     test 'should return original path if relative path cannot be computed' do
  355       filename = 'D:/path/to/include/file.txt'
  356       base_dir = 'C:/docs'
  357       result = @resolver.relative_path filename, base_dir
  358       assert_equal 'D:/path/to/include/file.txt', result
  359     end if windows?
  360 
  361     test 'should resolve relative path relative to base dir in unsafe mode' do
  362       base_dir = fixture_path 'base'
  363       doc = empty_document base_dir: base_dir, safe: Asciidoctor::SafeMode::UNSAFE
  364       expected = ::File.join base_dir, 'images', 'tiger.png'
  365       actual = doc.normalize_system_path 'tiger.png', 'images'
  366       assert_equal expected, actual
  367     end
  368 
  369     test 'should resolve absolute path as absolute in unsafe mode' do
  370       base_dir = fixture_path 'base'
  371       doc = empty_document base_dir: base_dir, safe: Asciidoctor::SafeMode::UNSAFE
  372       actual = doc.normalize_system_path 'tiger.png', '/etc/images'
  373       assert_equal '/etc/images/tiger.png', actual
  374     end
  375   end
  376 end