Class: Natsuzora::TemplateLoader::IncludePathResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/natsuzora/template_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(include_root) ⇒ IncludePathResolver

Returns a new instance of IncludePathResolver.



6
7
8
9
# File 'lib/natsuzora/template_loader.rb', line 6

def initialize(include_root)
  @include_root = File.expand_path(include_root)
  @include_root_realpath = nil
end

Instance Method Details

#ensure_within_root!(path) ⇒ Object

Raises:



17
18
19
20
21
22
23
# File 'lib/natsuzora/template_loader.rb', line 17

def ensure_within_root!(path)
  candidate = canonicalize_candidate(path)
  root = canonical_root
  return if within_root?(candidate, root)

  raise IncludeError, "Path traversal detected: #{path}"
end

#resolve_template_path(name) ⇒ Object



11
12
13
14
15
# File 'lib/natsuzora/template_loader.rb', line 11

def resolve_template_path(name)
  segments = name.split('/').reject(&:empty?)
  segments[-1] = "_#{segments[-1]}"
  "#{File.join(@include_root, *segments)}.ntzr"
end