Skip to content

Commit

Permalink
reject invalid UNIX timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
wiibaa authored and Suyog Rao committed Jul 30, 2014
1 parent f8e0099 commit 95abfde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 12 additions & 6 deletions lib/logstash/filters/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions spec/filters/date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 95abfde

Please sign in to comment.