From ba5db5ad5b3fececafee06b82286b87ebc9b2dfb Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Tue, 23 Jul 2013 14:23:33 +0200 Subject: [PATCH] refactor Include tag file validation Split validation into a separate method, and give a more descriptive error on symlinks. --- lib/jekyll/tags/include.rb | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index ce1350b1..b094b7df 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -51,6 +51,21 @@ eos def render(context) includes_dir = File.join(context.registers[:site].source, '_includes') + error = self.validate_file(includes_dir) + unless error.nil? + return error + end + + source = File.read(File.join(includes_dir, @file)) + partial = Liquid::Template.parse(source) + + context.stack do + context['include'] = parse_params(context) if @params + partial.render(context) + end + end + + def validate_file(includes_dir) if File.symlink?(includes_dir) return "Includes directory '#{includes_dir}' cannot be a symlink" end @@ -59,20 +74,14 @@ eos return "Include file '#{@file}' contains invalid characters or sequences" end - Dir.chdir(includes_dir) do - choices = Dir['**/*'].reject { |x| File.symlink?(x) } - if choices.include?(@file) - source = File.read(@file) - partial = Liquid::Template.parse(source) - - context.stack do - context['include'] = parse_params(context) if @params - partial.render(context) - end - else - "Included file '#{@file}' not found in _includes directory" - end + file = File.join(includes_dir, @file) + if !File.exists?(file) + return "Included file #{@file} not found in _includes directory" + elsif File.symlink?(file) + return "Included file #{@file} is a symlink" end + + nil end end end