[前][次][番号順一覧][スレッド一覧]

ruby-changes:10199

From: shyouhei <ko1@a...>
Date: Fri, 23 Jan 2009 11:49:09 +0900 (JST)
Subject: [ruby-changes:10199] Ruby:r21742 (ruby_1_8_6): merge revision(s) 19320,19322:

shyouhei	2009-01-23 11:48:45 +0900 (Fri, 23 Jan 2009)

  New Revision: 21742

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21742

  Log:
    merge revision(s) 19320,19322:
    * lib/rexml/document.rb: limit entity expansion.  Thanks, Luka
      Treiber, Mitja Kolsek, and Michael Koziarski.  backported from
      trunk r19033, r19317, r19318.
    * lib/rexml/entity.rb: ditto.
    * test/rexml/test_document.rb: ditto.
    * NEWS: added an entry for REXML.
    * lib/rexml/document.rb: fixed typo.

  Added directories:
    branches/ruby_1_8_6/test/rexml/
  Modified files:
    branches/ruby_1_8_6/ChangeLog
    branches/ruby_1_8_6/lib/rexml/document.rb
    branches/ruby_1_8_6/lib/rexml/entity.rb
    branches/ruby_1_8_6/version.h

Index: ruby_1_8_6/ChangeLog
===================================================================
--- ruby_1_8_6/ChangeLog	(revision 21741)
+++ ruby_1_8_6/ChangeLog	(revision 21742)
@@ -1,3 +1,19 @@
+Fri Jan 23 11:42:21 2009  Shugo Maeda  <shugo@r...>
+
+	* NEWS: added an entry for REXML.
+
+	* lib/rexml/document.rb: fixed typo.
+
+Fri Jan 23 11:42:21 2009  Shugo Maeda  <shugo@r...>
+
+	* lib/rexml/document.rb: limit entity expansion.  Thanks, Luka
+	  Treiber, Mitja Kolsek, and Michael Koziarski.  backported from
+	  trunk r19033, r19317, r19318.
+
+	* lib/rexml/entity.rb: ditto.
+
+	* test/rexml/test_document.rb: ditto.
+
 Thu Jan 22 15:11:09 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* marshal.c (marshal_load): arg.data is no longer a VALUE but a
Index: ruby_1_8_6/version.h
===================================================================
--- ruby_1_8_6/version.h	(revision 21741)
+++ ruby_1_8_6/version.h	(revision 21742)
@@ -1,15 +1,15 @@
 #define RUBY_VERSION "1.8.6"
-#define RUBY_RELEASE_DATE "2009-01-22"
+#define RUBY_RELEASE_DATE "2009-01-23"
 #define RUBY_VERSION_CODE 186
-#define RUBY_RELEASE_CODE 20090122
-#define RUBY_PATCHLEVEL 306
+#define RUBY_RELEASE_CODE 20090123
+#define RUBY_PATCHLEVEL 307
 
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 8
 #define RUBY_VERSION_TEENY 6
 #define RUBY_RELEASE_YEAR 2009
 #define RUBY_RELEASE_MONTH 1
-#define RUBY_RELEASE_DAY 22
+#define RUBY_RELEASE_DAY 23
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: ruby_1_8_6/lib/rexml/document.rb
===================================================================
--- ruby_1_8_6/lib/rexml/document.rb	(revision 21741)
+++ ruby_1_8_6/lib/rexml/document.rb	(revision 21742)
@@ -32,6 +32,7 @@
 	  # @param context if supplied, contains the context of the document;
 	  # this should be a Hash.
 		def initialize( source = nil, context = {} )
+      @entity_expansion_count = 0
 			super()
 			@context = context
 			return if source.nil?
@@ -200,6 +201,27 @@
 			Parsers::StreamParser.new( source, listener ).parse
 		end
 
+    @@entity_expansion_limit = 10_000
+
+    # Set the entity expansion limit. By default the limit is set to 10000.
+    def Document::entity_expansion_limit=( val )
+      @@entity_expansion_limit = val
+    end
+
+    # Get the entity expansion limit. By default the limit is set to 10000.
+    def Document::entity_expansion_limit
+      return @@entity_expansion_limit
+    end
+
+    attr_reader :entity_expansion_count
+    
+    def record_entity_expansion
+      @entity_expansion_count += 1
+      if @entity_expansion_count > @@entity_expansion_limit
+        raise "number of entity expansions exceeded, processing aborted."
+      end
+    end
+
 		private
 		def build( source )
       Parsers::TreeParser.new( source, self ).parse
Index: ruby_1_8_6/lib/rexml/entity.rb
===================================================================
--- ruby_1_8_6/lib/rexml/entity.rb	(revision 21741)
+++ ruby_1_8_6/lib/rexml/entity.rb	(revision 21742)
@@ -73,6 +73,7 @@
 		# all entities -- both %ent; and &ent; entities.  This differs from
 		# +value()+ in that +value+ only replaces %ent; entities.
 		def unnormalized
+      document.record_entity_expansion
 			v = value()
 			return nil if v.nil?
 			@unnormalized = Text::unnormalize(v, parent)
Index: ruby_1_8_6/test/rexml/test_document.rb
===================================================================
--- ruby_1_8_6/test/rexml/test_document.rb	(revision 0)
+++ ruby_1_8_6/test/rexml/test_document.rb	(revision 21742)
@@ -0,0 +1,65 @@
+require "rexml/document"
+require "test/unit"
+
+class REXML::TestDocument < Test::Unit::TestCase
+  def test_new
+    doc = REXML::Document.new(<<EOF)
+<?xml version="1.0" encoding="UTF-8"?>
+<message>Hello world!</message>
+EOF
+    assert_equal("Hello world!", doc.root.children.first.value)
+  end
+
+  XML_WITH_NESTED_ENTITY = <<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE member [
+  <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
+  <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
+  <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
+  <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
+  <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
+  <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
+  <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
+]>
+<member>
+&a;
+</member>
+EOF
+
+  XML_WITH_4_ENTITY_EXPANSION = <<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE member [
+  <!ENTITY a "a">
+  <!ENTITY a2 "&a; &a;">
+]>
+<member>
+&a;
+&a2;
+</member>
+EOF
+
+  def test_entity_expansion_limit
+    doc = REXML::Document.new(XML_WITH_NESTED_ENTITY)
+    assert_raise(RuntimeError) do
+      doc.root.children.first.value
+    end
+    REXML::Document.entity_expansion_limit = 100
+    assert_equal(100, REXML::Document.entity_expansion_limit)
+    doc = REXML::Document.new(XML_WITH_NESTED_ENTITY)
+    assert_raise(RuntimeError) do
+      doc.root.children.first.value
+    end
+    assert_equal(101, doc.entity_expansion_count)
+
+    REXML::Document.entity_expansion_limit = 4
+    doc = REXML::Document.new(XML_WITH_4_ENTITY_EXPANSION)
+    assert_equal("\na\na a\n", doc.root.children.first.value)
+    REXML::Document.entity_expansion_limit = 3
+    doc = REXML::Document.new(XML_WITH_4_ENTITY_EXPANSION)
+    assert_raise(RuntimeError) do
+      doc.root.children.first.value
+    end
+  ensure
+    REXML::Document.entity_expansion_limit = 10000
+  end
+end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]