mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Add taint warnings to the context rather than the template.
This commit is contained in:
@@ -33,6 +33,10 @@ module Liquid
|
||||
@filters = []
|
||||
end
|
||||
|
||||
def warnings
|
||||
@warnings ||= []
|
||||
end
|
||||
|
||||
def strainer
|
||||
@strainer ||= Strainer.create(self, @filters)
|
||||
end
|
||||
|
||||
+10
-6
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user