Class: Coradoc::IncludeResolver::Filesystem

Inherits:
Coradoc::IncludeResolver show all
Defined in:
lib/coradoc/include_resolver/filesystem.rb

Overview

Default include resolver: reads files from the local filesystem, rooted at base_dir. Path-traversal protection is ON by default to match asciidoctor’s :safe mode.

Pass allow_unsafe: true to opt out (matches :unsafe mode).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Coradoc::IncludeResolver

coerce

Constructor Details

#initialize(base_dir:, allow_unsafe: false, max_bytes: nil) ⇒ Filesystem

Returns a new instance of Filesystem.

Parameters:

  • base_dir (String)

    absolute path to the directory includes are resolved against. Usually the directory of the including document. Relative paths inside the resolver are expanded against this.

  • allow_unsafe (Boolean) (defaults to: false)

    when false (default), refuses any resolved path that escapes base_dir via .. or that is an absolute path outside base_dir.

  • max_bytes (Integer, nil) (defaults to: nil)

    if set, refuses files larger than this. Defense against accidental megabyte-include loops.



24
25
26
27
28
# File 'lib/coradoc/include_resolver/filesystem.rb', line 24

def initialize(base_dir:, allow_unsafe: false, max_bytes: nil)
  @base_dir = File.expand_path(base_dir)
  @allow_unsafe = allow_unsafe
  @max_bytes = max_bytes
end

Instance Attribute Details

#allow_unsafeObject (readonly)

Returns the value of attribute allow_unsafe.



13
14
15
# File 'lib/coradoc/include_resolver/filesystem.rb', line 13

def allow_unsafe
  @allow_unsafe
end

#base_dirObject (readonly)

Returns the value of attribute base_dir.



13
14
15
# File 'lib/coradoc/include_resolver/filesystem.rb', line 13

def base_dir
  @base_dir
end

#max_bytesObject (readonly)

Returns the value of attribute max_bytes.



13
14
15
# File 'lib/coradoc/include_resolver/filesystem.rb', line 13

def max_bytes
  @max_bytes
end

Instance Method Details

#call(target:, base_dir:, options:, context:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/coradoc/include_resolver/filesystem.rb', line 30

def call(target:, base_dir:, options:, context:)
  full = File.expand_path(target, base_dir)
  enforce_safety!(full, base_dir) unless allow_unsafe
  raise Coradoc::IncludeNotFoundError, target unless File.file?(full)

  enforce_size!(full, target)

  encoding = options&.file_encoding || 'utf-8'
  read_with_encoding(full, encoding)
end