Add taint warnings to the context rather than the template.

This commit is contained in:
Dylan Thacker-Smith
2015-07-07 15:53:02 -04:00
parent 15974d9168
commit 80b6ac3bc7
3 changed files with 18 additions and 8 deletions
+4
View File
@@ -33,6 +33,10 @@ module Liquid
@filters = []
end
def warnings
@warnings ||= []
end
def strainer
@strainer ||= Strainer.create(self, @filters)
end
+10 -6
View File
@@ -74,7 +74,7 @@ module Liquid
@filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
context.invoke(filter_name, output, *filter_args)
end.tap{ |obj| taint_check(obj) }
end.tap{ |obj| taint_check(context, obj) }
end
private
@@ -106,16 +106,20 @@ module Liquid
parsed_args
end
def taint_check(obj)
if obj.tainted?
def taint_check(context, obj)
if obj.tainted? && Template.taint_mode != :lax
@markup =~ QuotedFragment
name = Regexp.last_match(0)
error = TaintedError.new("variable '#{name}' is tainted and was not escaped")
error.line_number = line_number
error.template_name = context.template_name
case Template.taint_mode
when :warn
@warnings ||= []
@warnings << "variable '#{name}' is tainted and was not escaped"
context.warnings << error
when :error
raise TaintedError, "Error - variable '#{name}' is tainted and was not escaped"
raise error
end
end
end
+4 -2
View File
@@ -124,8 +124,10 @@ class DropsTest < Minitest::Test
def test_rendering_warns_on_tainted_attr
with_taint_mode(:warn) do
tpl = Liquid::Template.parse('{{ product.user_input }}')
tpl.render!('product' => ProductDrop.new)
assert_match /tainted/, tpl.warnings.first
context = Context.new('product' => ProductDrop.new)
tpl.render!(context)
assert_equal [Liquid::TaintedError], context.warnings.map(&:class)
assert_equal "variable 'product.user_input' is tainted and was not escaped", context.warnings.first.to_s(false)
end
end