Class Asciidoctor::PathResolver
In: lib/asciidoctor/path_resolver.rb
Parent: Object

Public: Handles all operations for resolving, cleaning and joining paths. This class includes operations for handling both web paths (request URIs) and system paths.

The main emphasis of the class is on creating clean and secure paths. Clean paths are void of duplicate parent and current directory references in the path name. Secure paths are paths which are restricted from accessing directories outside of a jail root, if specified.

Since joining two paths can result in an insecure path, this class also handles the task of joining a parent (start) and child (target) path.

This class makes no use of path utilities from the Ruby libraries. Instead, it handles all aspects of path manipulation. The main benefit of internalizing these operations is that the class is able to handle both posix and windows paths independent of the operating system on which it runs. This makes the class both deterministic and easier to test.

Examples

    resolver = PathResolver.new

    # Web Paths

    resolver.web_path('images')
    => 'images'

    resolver.web_path('./images')
    => './images'

    resolver.web_path('/images')
    => '/images'

    resolver.web_path('./images/../assets/images')
    => './assets/images'

    resolver.web_path('/../images')
    => '/images'

    resolver.web_path('images', 'assets')
    => 'assets/images'

    resolver.web_path('tiger.png', '../assets/images')
    => '../assets/images/tiger.png'

    # System Paths

    resolver.working_dir
    => '/path/to/docs'

    resolver.system_path('images')
    => '/path/to/docs/images'

    resolver.system_path('../images')
    => '/path/to/images'

    resolver.system_path('/etc/images')
    => '/etc/images'

    resolver.system_path('images', '/etc')
    => '/etc/images'

    resolver.system_path('', '/etc/images')
    => '/etc/images'

    resolver.system_path(nil, nil, '/path/to/docs')
    => '/path/to/docs'

    resolver.system_path('..', nil, '/path/to/docs')
    => '/path/to/docs'

    resolver.system_path('../../../css', nil, '/path/to/docs')
    => '/path/to/docs/css'

    resolver.system_path('../../../css', '../../..', '/path/to/docs')
    => '/path/to/docs/css'

    resolver.system_path('..', 'C:\\data\\docs\\assets', 'C:\\data\\docs')
    => 'C:/data/docs'

    resolver.system_path('..\\..\\css', 'C:\\data\\docs\\assets', 'C:\\data\\docs')
    => 'C:/data/docs/css'

    begin
      resolver.system_path('../../../css', '../../..', '/path/to/docs', :recover => false)
    rescue SecurityError => e
      puts e.message
    end
    => 'path ../../../../../../css refers to location outside jail: /path/to/docs (disallowed in safe mode)'

    resolver.system_path('/path/to/docs/images', nil, '/path/to/docs')
    => '/path/to/docs/images'

    begin
      resolver.system_path('images', '/etc', '/path/to/docs')
    rescue SecurityError => e
      puts e.message
    end
    => Start path /etc is outside of jail: /path/to/docs'

Methods

Constants

DOT = '.'
DOT_DOT = '..'
DOT_SLASH = './'
SLASH = '/'
BACKSLASH = '\\'
DOUBLE_SLASH = '//'
WindowsRootRx = /^[a-zA-Z]:(?:\\|\/)/

Attributes

file_separator  [RW] 
working_dir  [RW] 

Public Class methods

Public: Construct a new instance of PathResolver, optionally specifying the file separator (to override the system default) and the working directory (to override the present working directory). The working directory will be expanded to an absolute path inside the constructor.

file_separator - the String file separator to use for path operations

                 (optional, default: File::SEPARATOR)

working_dir - the String working directory (optional, default: Dir.pwd)

Public Instance methods

Public: Expand the path by resolving any parent references (..) and cleaning self references (.).

The result will be relative if the path is relative and absolute if the path is absolute. The file separator used in the expanded path is the one specified when the class was constructed.

path - the String path to expand

returns a String path with any parent or self references resolved.

Public: Check if the specified path is an absolute root path This operation correctly handles both posix and windows paths.

path - the String path to check

returns a Boolean indicating whether the path is an absolute root path

Public: Determine if the path is a UNC (root) path

path - the String path to check

returns a Boolean indicating whether the path is a UNC path

Public: Determine if the path is an absolute (root) web path

path - the String path to check

returns a Boolean indicating whether the path is an absolute (root) web path

Public: Join the segments using the posix file separator (since Ruby knows how to work with paths specified this way, regardless of OS). Use the root, if specified, to construct an absolute path. Otherwise join the segments as a relative path.

segments - a String Array of path segments root - a String path root (optional, default: nil)

returns a String path formed by joining the segments using the posix file separator and prepending the root, if specified

Public: Partition the path into path segments and remove any empty segments or segments that are self references (.). The path is converted to a posix path before being partitioned.

path - the String path to partition web_path - a Boolean indicating whether the path should be handled

           as a web path (optional, default: false)

Returns a 3-item Array containing the Array of String path segments, the path root (e.g., ’/’, ’./’, ‘c:/’) if the path is absolute and the posix version of the path.

Public: Normalize path by converting any backslashes to forward slashes

path - the String path to normalize

returns a String path with any backslashes replaced with forward slashes

Public: Calculate the relative path to this absolute filename from the specified base directory

If either the filename or the base_directory are not absolute paths, no work is done.

filename - An absolute file name as a String base_directory - An absolute base directory as a String

Return the relative path String of the filename calculated from the base directory

Public: Resolve a system path from the target and start paths. If a jail path is specified, enforce that the resolved directory is contained within the jail path. If a jail path is not provided, the resolved path may be any location on the system. If the resolved path is absolute, use it as is. If the resolved path is relative, resolve it relative to the working_dir specified in the constructor.

target - the String target path start - the String start (i.e., parent) path jail - the String jail path to confine the resolved path opts - an optional Hash of options to control processing (default: {}):

         * :recover is used to control whether the processor should auto-recover
             when an illegal path is encountered
         * :target_name is used in messages to refer to the path being resolved

returns a String path that joins the target path with the start path with any parent references resolved and self references removed and enforces that the resolved path be contained within the jail, if provided

Public: Resolve a web path from the target and start paths. The main function of this operation is to resolve any parent references and remove any self references.

The target is assumed to be a path, not a qualified URI. That check should happen before this method is invoked.

target - the String target path start - the String start (i.e., parent) path

returns a String path that joins the target path with the start path with any parent references resolved and self references removed

[Validate]