7aa3618
diff -pruN ruby-1.8.6-p287.orig/lib/rexml/document.rb ruby-1.8.6-p287/lib/rexml/document.rb
7aa3618
--- ruby-1.8.6-p287.orig/lib/rexml/document.rb	2007-11-04 13:50:15.000000000 +0900
7aa3618
+++ ruby-1.8.6-p287/lib/rexml/document.rb	2008-10-08 22:25:14.000000000 +0900
7aa3618
@@ -32,6 +32,7 @@ module REXML
7aa3618
 	  # @param context if supplied, contains the context of the document;
7aa3618
 	  # this should be a Hash.
7aa3618
 		def initialize( source = nil, context = {} )
7aa3618
+      @entity_expansion_count = 0
7aa3618
 			super()
7aa3618
 			@context = context
7aa3618
 			return if source.nil?
7aa3618
@@ -200,6 +201,27 @@ module REXML
7aa3618
 			Parsers::StreamParser.new( source, listener ).parse
7aa3618
 		end
0a0cf58
7aa3618
+    @@entity_expansion_limit = 10_000
7aa3618
+
7aa3618
+    # Set the entity expansion limit. By defualt the limit is set to 10000.
7aa3618
+    def Document::entity_expansion_limit=( val )
7aa3618
+      @@entity_expansion_limit = val
7aa3618
+    end
7aa3618
+
7aa3618
+    # Get the entity expansion limit. By defualt the limit is set to 10000.
7aa3618
+    def Document::entity_expansion_limit
7aa3618
+      return @@entity_expansion_limit
7aa3618
+    end
7aa3618
+
7aa3618
+    attr_reader :entity_expansion_count
0a0cf58
+
7aa3618
+    def record_entity_expansion
7aa3618
+      @entity_expansion_count += 1
7aa3618
+      if @entity_expansion_count > @@entity_expansion_limit
7aa3618
+        raise "number of entity expansions exceeded, processing aborted."
7aa3618
+      end
7aa3618
+    end
7aa3618
+
7aa3618
 		private
7aa3618
 		def build( source )
7aa3618
       Parsers::TreeParser.new( source, self ).parse
7aa3618
diff -pruN ruby-1.8.6-p287.orig/lib/rexml/entity.rb ruby-1.8.6-p287/lib/rexml/entity.rb
7aa3618
--- ruby-1.8.6-p287.orig/lib/rexml/entity.rb	2007-07-28 11:46:08.000000000 +0900
7aa3618
+++ ruby-1.8.6-p287/lib/rexml/entity.rb	2008-10-08 22:25:14.000000000 +0900
7aa3618
@@ -73,6 +73,7 @@ module REXML
7aa3618
 		# all entities -- both %ent; and &ent; entities.  This differs from
7aa3618
 		# +value()+ in that +value+ only replaces %ent; entities.
7aa3618
 		def unnormalized
0a0cf58
+      document.record_entity_expansion unless document.nil?
7aa3618
 			v = value()
7aa3618
 			return nil if v.nil?
7aa3618
 			@unnormalized = Text::unnormalize(v, parent)
7aa3618
diff -pruN ruby-1.8.6-p287.orig/test/rexml/test_document.rb ruby-1.8.6-p287/test/rexml/test_document.rb
7aa3618
--- ruby-1.8.6-p287.orig/test/rexml/test_document.rb	1970-01-01 09:00:00.000000000 +0900
7aa3618
+++ ruby-1.8.6-p287/test/rexml/test_document.rb	2008-10-08 22:25:14.000000000 +0900
7aa3618
@@ -0,0 +1,42 @@
7aa3618
+require "rexml/document"
7aa3618
+require "test/unit"
7aa3618
+
7aa3618
+class REXML::TestDocument < Test::Unit::TestCase
7aa3618
+  def test_new
7aa3618
+    doc = REXML::Document.new(<
7aa3618
+
7aa3618
+<message>Hello world!</message>
7aa3618
+EOF
7aa3618
+    assert_equal("Hello world!", doc.root.children.first.value)
7aa3618
+  end
7aa3618
+
7aa3618
+  XML_WITH_NESTED_ENTITY = <
7aa3618
+
7aa3618
+
7aa3618
+  
7aa3618
+  
7aa3618
+  
7aa3618
+  
7aa3618
+  
7aa3618
+  
7aa3618
+  
7aa3618
+]>
7aa3618
+<member>
7aa3618
+&a;
7aa3618
+</member>
7aa3618
+EOF
7aa3618
+
7aa3618
+  def test_entity_expansion_limit
7aa3618
+    doc = REXML::Document.new(XML_WITH_NESTED_ENTITY)
7aa3618
+    assert_raise(RuntimeError) do
7aa3618
+      doc.root.children.first.value
7aa3618
+    end
7aa3618
+    REXML::Document.entity_expansion_limit = 100
7aa3618
+    assert_equal(100, REXML::Document.entity_expansion_limit)
7aa3618
+    doc = REXML::Document.new(XML_WITH_NESTED_ENTITY)
7aa3618
+    assert_raise(RuntimeError) do
7aa3618
+      doc.root.children.first.value
7aa3618
+    end
7aa3618
+    assert_equal(101, doc.entity_expansion_count)
7aa3618
+  end
7aa3618
+end