Skip to content

Commit

Permalink
eliminate eval from Builder#initialize
Browse files Browse the repository at this point in the history
which was raised by Rubocop's security filter

related to #1915
  • Loading branch information
flavorjones committed Aug 10, 2019
1 parent 47a7bc7 commit 6777008
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/nokogiri/xml/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,13 @@ def initialize(options = {}, root = nil, &block)
@doc = root.document
@parent = root
else
namespace = self.class.name.split("::")
namespace[-1] = "Document"
@doc = eval(namespace.join("::")).new
@parent = @doc
klassname = "::" + (self.class.name.split("::")[0..-2] + ["Document"]).join("::")
klass = begin
Object.const_get(klassname)
rescue NameError
Nokogiri::XML::Document
end
@parent = @doc = klass.new
end

@context = nil
Expand Down
15 changes: 15 additions & 0 deletions test/xml/test_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ def test_builder_reuses_namespaces
assert_equal envelope.namespace.object_id, package.namespace.object_id
end

def test_builder_uses_proper_document_class
xml_builder = Nokogiri::XML::Builder.new
assert_instance_of Nokogiri::XML::Document, xml_builder.doc

html_builder = Nokogiri::HTML::Builder.new
assert_instance_of Nokogiri::HTML::Document, html_builder.doc

foo_builder = ThisIsATestBuilder.new
assert_instance_of Nokogiri::XML::Document, foo_builder.doc
end

private

def namespaces_defined_on(node)
Expand All @@ -350,3 +361,7 @@ def namespaces_defined_on(node)
end
end
end

class ThisIsATestBuilder < Nokogiri::XML::Builder
# this exists for the test_builder_uses_proper_document_class and should be empty
end

0 comments on commit 6777008

Please sign in to comment.