From 95abfdebe8f7271163b3a2b0909377f4c993716f Mon Sep 17 00:00:00 2001 From: Wiibaa Date: Fri, 11 Apr 2014 07:31:27 +0200 Subject: [PATCH] reject invalid UNIX timestamp --- lib/logstash/filters/date.rb | 18 ++++++++++++------ spec/filters/date.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/logstash/filters/date.rb b/lib/logstash/filters/date.rb index 937127a646b..4faf64c6be7 100644 --- a/lib/logstash/filters/date.rb +++ b/lib/logstash/filters/date.rb @@ -133,14 +133,20 @@ def setupMatcher(field, locale, value) end parser = lambda { |date| joda_parser.parseMillis(date) } when "UNIX" # unix epoch - joda_instant = org.joda.time.Instant.java_class.constructor(Java::long).method(:new_instance) - #parser = lambda { |date| joda_instant.call((date.to_f * 1000).to_i).to_java.toDateTime } - parser = lambda { |date| (date.to_f * 1000).to_i } + parser = lambda do |date| + if /\d+/ === date || date.is_a?(Numeric) + (date.to_f * 1000).to_i + else + raise "Invalid UNIX epoch value '#{date}'" + end + end when "UNIX_MS" # unix epoch in ms - joda_instant = org.joda.time.Instant.java_class.constructor(Java::long).method(:new_instance) parser = lambda do |date| - #return joda_instant.call(date.to_i).to_java.toDateTime - return date.to_i + if /\d+/ === date || date.is_a?(Numeric) + date.to_i + else + raise "Invalid UNIX epoch value '#{date}'" + end end when "TAI64N" # TAI64 with nanoseconds, -10000 accounts for leap seconds joda_instant = org.joda.time.Instant.java_class.constructor(Java::long).method(:new_instance) diff --git a/spec/filters/date.rb b/spec/filters/date.rb index 2419f4399ea..6b5633c4655 100644 --- a/spec/filters/date.rb +++ b/spec/filters/date.rb @@ -112,6 +112,12 @@ insist { subject["@timestamp"].time } == Time.iso8601(output).utc end end # times.each + + #Invalid value should not be evaluated to zero (String#to_i madness) + sample("mydate" => "%{bad_value}") do + insist { subject["mydate"] } == "%{bad_value}" + insist { subject["@timestamp"] } != Time.iso8601("1970-01-01T00:00:00.000Z").utc + end end describe "parsing microsecond-precise times with UNIX (#213)" do @@ -128,6 +134,18 @@ # Joda time only supports milliseconds :\ insist { subject.timestamp.time } == Time.iso8601("2012-10-16T12:15:44.123-07:00").utc end + + #Support float values + sample("mydate" => 1350414944.123456) do + insist { subject["mydate"] } == 1350414944.123456 + insist { subject.timestamp } == Time.iso8601("2012-10-16T12:15:44.123-07:00").utc + end + + #Invalid value should not be evaluated to zero (String#to_i madness) + sample("mydate" => "%{bad_value}") do + insist { subject["mydate"] } == "%{bad_value}" + insist { subject["@timestamp"] } != Time.iso8601("1970-01-01T00:00:00.000Z").utc + end end describe "parsing with UNIX_MS" do