From 98d77f2552cd02ccd836ce67c72e9868a74b2a2f Mon Sep 17 00:00:00 2001 From: Ry Biesemeyer Date: Thu, 7 Feb 2019 21:55:43 +0000 Subject: [PATCH] backport patch to resolv for underqualified domain names --- lib/logstash/filters/dns.rb | 2 +- .../filters/{ => dns}/resolv_patch.rb | 34 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) rename lib/logstash/filters/{ => dns}/resolv_patch.rb (51%) diff --git a/lib/logstash/filters/dns.rb b/lib/logstash/filters/dns.rb index 5650b72..3a22a09 100644 --- a/lib/logstash/filters/dns.rb +++ b/lib/logstash/filters/dns.rb @@ -4,7 +4,7 @@ require "lru_redux" require "resolv" require "timeout" -require "logstash/filters/resolv_patch" +require "logstash/filters/dns/resolv_patch" java_import 'java.net.IDN' diff --git a/lib/logstash/filters/resolv_patch.rb b/lib/logstash/filters/dns/resolv_patch.rb similarity index 51% rename from lib/logstash/filters/resolv_patch.rb rename to lib/logstash/filters/dns/resolv_patch.rb index 61eb109..9d20751 100644 --- a/lib/logstash/filters/resolv_patch.rb +++ b/lib/logstash/filters/dns/resolv_patch.rb @@ -1,5 +1,7 @@ require "resolv" +jruby_gem_version = Gem::Version.new(JRUBY_VERSION) + # ref: https://github.com/logstash-plugins/logstash-filter-dns/issues/40 # # JRuby 9k versions prior to 9.1.16.0 have a bug which crashes IP address @@ -10,10 +12,7 @@ # # The code below is copied from JRuby 9.1.16.0 resolv.rb: # https://github.com/jruby/jruby/blob/9.1.16.0/lib/ruby/stdlib/resolv.rb#L775-L784 - -JRUBY_GEM_VERSION = Gem::Version.new(JRUBY_VERSION) - -if JRUBY_GEM_VERSION >= Gem::Version.new("9.1.13.0") && JRUBY_GEM_VERSION < Gem::Version.new("9.1.16.0") +if jruby_gem_version >= Gem::Version.new("9.1.13.0") && jruby_gem_version < Gem::Version.new("9.1.16.0") class Resolv class DNS class Requester @@ -32,4 +31,29 @@ def sender(msg, data, host, port=Port) end end end -end \ No newline at end of file +end + +# JRuby 1.x ships with a Ruby stdlib that has a bug in its resolv implementation +# in which it fails to correctly canonicalise unqualified or underqualified +# domains (e.g., domain names with fewer than the configured ndots, which defaults +# to 1). +# +# See: https://bugs.ruby-lang.org/issues/10412 +# +# Conditionally apply the patch to the method definition by wrapping it at runtime. +if jruby_gem_version < Gem::Version.new("9.0") + class Resolv::DNS + class Config + alias generate_candidates_without_toplevel generate_candidates + def generate_candidates_with_toplevel(name) + candidates = generate_candidates_without_toplevel(name) + fname = Name.create("#{name}.") + if !candidates.include?(fname) + candidates << fname + end + candidates + end + alias generate_candidates generate_candidates_with_toplevel + end + end +end