Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent crash for non-string values #65

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 3.1.5
- Fixed an issue where a non-string value existing in the resolve/reverse field could cause the plugin to crash

## 3.1.4
- Replaced Timeout::timeout block with `Resolv::DNS::timeouts=` [#62](https://github.com/logstash-plugins/logstash-filter-dns/pull/62)
- Added restriction for ruby version > 2.0, effectively making Logstash 6.x+ a requirement [#62](https://github.com/logstash-plugins/logstash-filter-dns/pull/62)
Expand Down
10 changes: 10 additions & 0 deletions lib/logstash/filters/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ def resolve(event)
raw = raw.first
end

if !raw.kind_of?(String)
@logger.warn("DNS: skipping resolve, can't deal with non-string values", :field => field, :value => raw)
return
end

begin
return if @failed_cache && @failed_cache[raw] # recently failed resolv, skip
if @hit_cache
Expand Down Expand Up @@ -269,6 +274,11 @@ def reverse(event)
end
raw = raw.first
end

if !raw.kind_of?(String)
@logger.warn("DNS: skipping reverse, can't deal with non-string values", :field => field, :value => raw)
return
end

if ! @ip_validator.match(raw)
@logger.debug("DNS: not an address",
Expand Down
2 changes: 1 addition & 1 deletion logstash-filter-dns.gemspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|

s.name = 'logstash-filter-dns'
s.version = '3.1.4'
s.version = '3.1.5'
s.licenses = ['Apache License (2.0)']
s.summary = "Performs a standard or reverse DNS lookup"
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
Expand Down
36 changes: 36 additions & 0 deletions spec/filters/dns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@
insist { subject.get("foo")[1] } == "carrera.databits.net"
end
end

describe "dns reverse lookup, non-string field" do
let(:plugin) { ::LogStash::Filters::DNS.new("reverse" => "foo") }
let(:event) { ::LogStash::Event.new("foo" => {"ip" => "1.2.3.4"} ) }

before do
plugin.register
allow(plugin.logger).to receive(:warn).with(any_args)
end

it "does not throw an error when filtering" do
expect do
plugin.filter(event)
end.not_to raise_error
end

it "logs an informative warning" do
plugin.filter(event)
expect(plugin.logger).to have_received(:warn).with("DNS: skipping reverse, can't deal with non-string values", :field => "foo", value: {"ip" => "1.2.3.4"})
end
end

describe "dns reverse lookup, not an IP" do
config <<-CONFIG
Expand Down Expand Up @@ -211,6 +232,21 @@
end
end

describe "dns resolve lookup, skip non-string value" do
config <<-CONFIG
filter {
dns {
resolve => "foo"
action => "replace"
}
}
CONFIG

sample("foo" => { "hostname" => "carrera.databits.net" }) do
insist { subject.get("foo") } == { "hostname" => "carrera.databits.net" }
end
end

describe "dns resolve lookup, append" do
config <<-CONFIG
filter {
Expand Down