Escape raw input if there is a Liquid syntax error

This commit is contained in:
Holger Just 2011-12-17 21:48:06 +01:00
parent d2ccdc88fa
commit 4656cf1c57
2 changed files with 22 additions and 5 deletions

View File

@ -446,20 +446,20 @@ module ApplicationHelper
case args.size case args.size
when 1 when 1
obj = options[:object] obj = options[:object]
text = args.shift input_text = args.shift
when 2 when 2
obj = args.shift obj = args.shift
attr = args.shift attr = args.shift
text = obj.send(attr).to_s input_text = obj.send(attr).to_s
else else
raise ArgumentError, 'invalid arguments to textilizable' raise ArgumentError, 'invalid arguments to textilizable'
end end
return '' if text.blank? return '' if input_text.blank?
project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
only_path = options.delete(:only_path) == false ? false : true only_path = options.delete(:only_path) == false ? false : true
begin begin
text = ChiliProject::Liquid::Legacy.run_macros(text) text = ChiliProject::Liquid::Legacy.run_macros(input_text)
liquid_template = ChiliProject::Liquid::Template.parse(text) liquid_template = ChiliProject::Liquid::Template.parse(text)
liquid_variables = get_view_instance_variables_for_liquid liquid_variables = get_view_instance_variables_for_liquid
liquid_variables.merge!({'current_user' => User.current}) liquid_variables.merge!({'current_user' => User.current})
@ -478,8 +478,15 @@ module ApplicationHelper
end end
Rails.logger.debug msg Rails.logger.debug msg
end end
rescue Liquid::SyntaxError rescue Liquid::SyntaxError => exception
if Rails.logger && Rails.logger.debug?
msg = "[Liquid Syntax Error] #{exception.message}\n:\n#{exception.backtrace.join("\n")}"
msg << "\n\n"
Rails.logger.debug msg
end
# Skip Liquid if there is a syntax error # Skip Liquid if there is a syntax error
text = h(input_text)
end end
@parsed_headings = [] @parsed_headings = []

View File

@ -208,4 +208,14 @@ class ChiliProject::LiquidTest < ActionView::TestCase
end end
end end
end end
context "invalid input" do
should "be escaped" do
text = "{% --- something invalid %}\n"
text << '<script>alert("Hello")</script>'
formatted = textilizable(text)
assert_match '&lt;script&gt;alert(&quot;Hello&quot;)&lt;/script&gt;', formatted
end
end
end end