summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-02-22 09:37:09 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-02-22 09:37:09 +0000
commite559b24b526bd4d2559283ed2640681595b5ff3d (patch)
treebc159ccce6d776fa04a3ecbfa7c115872e327bc1
parent857a2ceef5960dc5c526172690eab7a40a532e2c (diff)
merge revision(s) 39384:
* lib/rexml/document.rb (REXML::Document.entity_expansion_text_limit): new attribute to read/write entity expansion text limit. the default limit is 10Kb. * lib/rexml/text.rb (REXML::Text.unnormalize): check above attribute. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@39385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog8
-rw-r--r--lib/rexml/document.rb12
-rw-r--r--lib/rexml/text.rb40
-rw-r--r--test/rexml/test_entity.rb18
-rw-r--r--version.h2
5 files changed, 64 insertions, 16 deletions
diff --git a/ChangeLog b/ChangeLog
index e9c80a655e..4354671c1f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+Fri Feb 22 18:36:51 2013 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * lib/rexml/document.rb (REXML::Document.entity_expansion_text_limit):
+ new attribute to read/write entity expansion text limit. the default
+ limit is 10Kb.
+
+ * lib/rexml/text.rb (REXML::Text.unnormalize): check above attribute.
+
Fri Feb 22 14:48:15 2013 NARUSE, Yui <naruse@ruby-lang.org>
* vm.c (vm_exec): get rid of a SEGV when calling rb_iter_break() from
diff --git a/lib/rexml/document.rb b/lib/rexml/document.rb
index 96db53bdb0..f15c1bd762 100644
--- a/lib/rexml/document.rb
+++ b/lib/rexml/document.rb
@@ -217,6 +217,18 @@ module REXML
return @@entity_expansion_limit
end
+ @@entity_expansion_text_limit = 10_240
+
+ # Set the entity expansion limit. By default the limit is set to 10240.
+ def Document::entity_expansion_text_limit=( val )
+ @@entity_expansion_text_limit = val
+ end
+
+ # Get the entity expansion limit. By default the limit is set to 10000.
+ def Document::entity_expansion_text_limit
+ return @@entity_expansion_text_limit
+ end
+
attr_reader :entity_expansion_count
def record_entity_expansion
diff --git a/lib/rexml/text.rb b/lib/rexml/text.rb
index 6623c0c03b..878d13b8e8 100644
--- a/lib/rexml/text.rb
+++ b/lib/rexml/text.rb
@@ -380,25 +380,35 @@ module REXML
# Unescapes all possible entities
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
+ sum = 0
string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
- ref = $&
- if ref[1] == ?#
- if ref[2] == ?x
- [ref[3...-1].to_i(16)].pack('U*')
- else
- [ref[2...-1].to_i].pack('U*')
- end
- elsif ref == '&amp;'
- '&'
- elsif filter and filter.include?( ref[1...-1] )
- ref
- elsif doctype
- doctype.entity( ref[1...-1] ) or ref
+ s = Text.expand($&, doctype, filter)
+ if sum + s.bytesize > Document.entity_expansion_text_limit
+ raise "entity expansion has grown too large"
else
- entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
- entity_value ? entity_value.value : ref
+ sum += s.bytesize
end
+ s
}
end
+
+ def Text.expand(ref, doctype, filter)
+ if ref[1] == ?#
+ if ref[2] == ?x
+ [ref[3...-1].to_i(16)].pack('U*')
+ else
+ [ref[2...-1].to_i].pack('U*')
+ end
+ elsif ref == '&amp;'
+ '&'
+ elsif filter and filter.include?( ref[1...-1] )
+ ref
+ elsif doctype
+ doctype.entity( ref[1...-1] ) or ref
+ else
+ entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
+ entity_value ? entity_value.value : ref
+ end
+ end
end
end
diff --git a/test/rexml/test_entity.rb b/test/rexml/test_entity.rb
index e6d6f29007..5900fac7a8 100644
--- a/test/rexml/test_entity.rb
+++ b/test/rexml/test_entity.rb
@@ -104,6 +104,24 @@ class EntityTester < Test::Unit::TestCase
assert_equal source, out
end
+ def test_entity_string_limit
+ template = '<!DOCTYPE bomb [ <!ENTITY a "^" > ]> <bomb>$</bomb>'
+ len = 5120 # 5k per entity
+ template.sub!(/\^/, "B" * len)
+
+ # 10k is OK
+ entities = '&a;' * 2 # 5k entity * 2 = 10k
+ xmldoc = REXML::Document.new(template.sub(/\$/, entities))
+ assert_equal(len * 2, xmldoc.root.text.bytesize)
+
+ # above 10k explodes
+ entities = '&a;' * 3 # 5k entity * 2 = 15k
+ xmldoc = REXML::Document.new(template.sub(/\$/, entities))
+ assert_raises(RuntimeError) do
+ xmldoc.root.text
+ end
+ end
+
def test_raw
source = '<!DOCTYPE foo [
<!ENTITY ent "replace">
diff --git a/version.h b/version.h
index 6cfc67a7ed..9ce7e38533 100644
--- a/version.h
+++ b/version.h
@@ -1,5 +1,5 @@
#define RUBY_VERSION "1.9.3"
-#define RUBY_PATCHLEVEL 391
+#define RUBY_PATCHLEVEL 392
#define RUBY_RELEASE_DATE "2013-02-22"
#define RUBY_RELEASE_YEAR 2013