From e9709aec0a5f76164c09beb6fa0a77b3c004d4b8 Mon Sep 17 00:00:00 2001 From: Dominik Sander Date: Thu, 31 Aug 2017 21:11:26 +0200 Subject: [PATCH] Prevent PeakDetectorAgent from storing invalid data in it's memory The Agent expects the data in its memory groups to be castable to a float. By attempting the type cast when receiving events we prevent the memory from being corrupted with invalid data which lead exceptions while trying to access it. #2101 --- app/models/agents/peak_detector_agent.rb | 2 +- spec/models/agents/peak_detector_agent_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/agents/peak_detector_agent.rb b/app/models/agents/peak_detector_agent.rb index 126f08d2..2a59eef2 100644 --- a/app/models/agents/peak_detector_agent.rb +++ b/app/models/agents/peak_detector_agent.rb @@ -138,7 +138,7 @@ module Agents def remember(group, event) memory['data'] ||= {} memory['data'][group] ||= [] - memory['data'][group] << [ Utils.value_at(event.payload, interpolated['value_path']), event.created_at.to_i ] + memory['data'][group] << [ Utils.value_at(event.payload, interpolated['value_path']).to_f, event.created_at.to_i ] cleanup group end diff --git a/spec/models/agents/peak_detector_agent_spec.rb b/spec/models/agents/peak_detector_agent_spec.rb index 75f4aecf..da83c40c 100644 --- a/spec/models/agents/peak_detector_agent_spec.rb +++ b/spec/models/agents/peak_detector_agent_spec.rb @@ -78,6 +78,13 @@ describe Agents::PeakDetectorAgent do :pattern => { 'filter' => "something" }) expect(@agent.memory['peaks']['something'].length).to eq(1) end + + it 'raised an exception if the extracted data can not be casted to a float' do + event = Event.new(payload: {count: ["not working"]}) + expect { + @agent.receive([event]) + }.to raise_error(NoMethodError, /undefined method `to_f'/) + end end describe "validation" do