Skip to content

Commit

Permalink
Merge pull request #1176 from sonots/prevent_overwriting_reserved_pla…
Browse files Browse the repository at this point in the history
…ceholders

Fix to prevent overwriting reserved placeholder keys such as tag
  • Loading branch information
repeatedly authored Aug 21, 2016
2 parents 7194cb9 + 28470d0 commit 89b32d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/fluent/plugin/filter_record_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ def prepare_placeholders(placeholder_values)
end
elsif value.kind_of?(Hash) # record, etc
value.each do |k, v|
placeholders.store("${#{k}}", v) # foo
unless placeholder_values.has_key?(k) # prevent overwriting reserved keys such as tag
placeholders.store("${#{k}}", v) # foo
end
placeholders.store(%Q[${#{key}["#{k}"]}], v) # record["foo"]
end
else # string, interger, float, and others?
Expand Down
28 changes: 22 additions & 6 deletions test/plugin/test_filter_record_transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ def filter(config, msgs = [''])
d = create_driver(config)
yield d if block_given?
d.run {
msgs.each do |msg|
record = {
'eventType0' => 'bar',
'message' => msg,
}
record = record.merge(msg) if msg.is_a?(Hash)
records = msgs.map do |msg|
next msg if msg.is_a?(Hash)
{ 'eventType0' => 'bar', 'message' => msg }
end
records.each do |record|
d.feed(@tag, @time, record)
end
}
Expand Down Expand Up @@ -273,6 +272,23 @@ def filter(config, msgs = [''])
end
end

test "Prevent overwriting reserved keys such as tag with enable_ruby #{enable_ruby}" do
config = %[
enable_ruby #{enable_ruby}
<record>
new_tag ${tag}
new_record_tag ${record["tag"]}
</record>
]
records = [{'tag' => 'tag', 'time' => 'time'}]
filtered = filter(config, records)
filtered.each_with_index do |(_t, r), i|
assert_not_equal('tag', r['new_tag'])
assert_equal(@tag, r['new_tag'])
assert_equal('tag', r['new_record_tag'])
end
end

test "hash values with placeholders with enable_ruby #{enable_ruby}" do
config = %[
enable_ruby #{enable_ruby}
Expand Down

0 comments on commit 89b32d8

Please sign in to comment.