Skip to content

Commit

Permalink
ensure EntityReferences ignore malformed children
Browse files Browse the repository at this point in the history
libxml2 will cause EntityReferences to have a malformed child node for
predefined entities. because any use of that child is likely to cause a
segfault, we shall pretend that it doesn't exist.

[fixes sparklemotion#1238]
  • Loading branch information
flavorjones authored and stevecrozz committed Oct 5, 2018
1 parent c216273 commit 84105bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ lib/nokogiri/xml/dtd.rb
lib/nokogiri/xml/element_content.rb
lib/nokogiri/xml/element_decl.rb
lib/nokogiri/xml/entity_decl.rb
lib/nokogiri/xml/entity_reference.rb
lib/nokogiri/xml/namespace.rb
lib/nokogiri/xml/node.rb
lib/nokogiri/xml/node/save_options.rb
Expand Down Expand Up @@ -297,8 +298,8 @@ test/files/xinclude.xml
test/helper.rb
test/html/sax/test_parser.rb
test/html/sax/test_parser_context.rb
test/html/sax/test_push_parser.rb
test/html/sax/test_parser_text.rb
test/html/sax/test_push_parser.rb
test/html/test_builder.rb
test/html/test_document.rb
test/html/test_document_encoding.rb
Expand All @@ -325,8 +326,8 @@ test/xml/node/test_save_options.rb
test/xml/node/test_subclass.rb
test/xml/sax/test_parser.rb
test/xml/sax/test_parser_context.rb
test/xml/sax/test_push_parser.rb
test/xml/sax/test_parser_text.rb
test/xml/sax/test_push_parser.rb
test/xml/test_attr.rb
test/xml/test_attribute_decl.rb
test/xml/test_builder.rb
Expand Down
1 change: 1 addition & 0 deletions lib/nokogiri/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'nokogiri/xml/reader'
require 'nokogiri/xml/notation'
require 'nokogiri/xml/entity_decl'
require 'nokogiri/xml/entity_reference'
require 'nokogiri/xml/schema'
require 'nokogiri/xml/relax_ng'

Expand Down
18 changes: 18 additions & 0 deletions lib/nokogiri/xml/entity_reference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Nokogiri
module XML
class EntityReference < Nokogiri::XML::Node
def children
# libxml2 will create a malformed child node for predefined
# entities. because any use of that child is likely to cause a
# segfault, we shall pretend that it doesn't exist.
#
# see https://github.com/sparklemotion/nokogiri/issues/1238 for details
NodeSet.new(document)
end

def inspect_attributes
[:name]
end
end
end
end
11 changes: 11 additions & 0 deletions test/xml/test_entity_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def test_newline_node
doc.xpath('/item').first.add_child(lf_node)
assert_match(/&#xa;/, doc.to_xml)
end

def test_children_should_always_be_empty
# https://github.com/sparklemotion/nokogiri/issues/1238
#
# libxml2 will create a malformed child node for predefined
# entities. because any use of that child is likely to cause a
# segfault, we shall pretend that it doesn't exist.
entity = Nokogiri::XML::EntityReference.new(@xml, "amp")
assert_equal 0, entity.children.length
entity.inspect # should not segfault
end
end

module Common
Expand Down

0 comments on commit 84105bf

Please sign in to comment.