Skip to content

Commit e559b24

Browse files
committed
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
1 parent 857a2ce commit e559b24

File tree

5 files changed

+64
-16
lines changed

5 files changed

+64
-16
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Fri Feb 22 18:36:51 2013 Aaron Patterson <aaron@tenderlovemaking.com>
2+
3+
* lib/rexml/document.rb (REXML::Document.entity_expansion_text_limit):
4+
new attribute to read/write entity expansion text limit. the default
5+
limit is 10Kb.
6+
7+
* lib/rexml/text.rb (REXML::Text.unnormalize): check above attribute.
8+
19
Fri Feb 22 14:48:15 2013 NARUSE, Yui <naruse@ruby-lang.org>
210

311
* vm.c (vm_exec): get rid of a SEGV when calling rb_iter_break() from

lib/rexml/document.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ def Document::entity_expansion_limit
217217
return @@entity_expansion_limit
218218
end
219219

220+
@@entity_expansion_text_limit = 10_240
221+
222+
# Set the entity expansion limit. By default the limit is set to 10240.
223+
def Document::entity_expansion_text_limit=( val )
224+
@@entity_expansion_text_limit = val
225+
end
226+
227+
# Get the entity expansion limit. By default the limit is set to 10000.
228+
def Document::entity_expansion_text_limit
229+
return @@entity_expansion_text_limit
230+
end
231+
220232
attr_reader :entity_expansion_count
221233

222234
def record_entity_expansion

lib/rexml/text.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -380,25 +380,35 @@ def Text::normalize( input, doctype=nil, entity_filter=nil )
380380

381381
# Unescapes all possible entities
382382
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
383+
sum = 0
383384
string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
384-
ref = $&
385-
if ref[1] == ?#
386-
if ref[2] == ?x
387-
[ref[3...-1].to_i(16)].pack('U*')
388-
else
389-
[ref[2...-1].to_i].pack('U*')
390-
end
391-
elsif ref == '&amp;'
392-
'&'
393-
elsif filter and filter.include?( ref[1...-1] )
394-
ref
395-
elsif doctype
396-
doctype.entity( ref[1...-1] ) or ref
385+
s = Text.expand($&, doctype, filter)
386+
if sum + s.bytesize > Document.entity_expansion_text_limit
387+
raise "entity expansion has grown too large"
397388
else
398-
entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
399-
entity_value ? entity_value.value : ref
389+
sum += s.bytesize
400390
end
391+
s
401392
}
402393
end
394+
395+
def Text.expand(ref, doctype, filter)
396+
if ref[1] == ?#
397+
if ref[2] == ?x
398+
[ref[3...-1].to_i(16)].pack('U*')
399+
else
400+
[ref[2...-1].to_i].pack('U*')
401+
end
402+
elsif ref == '&amp;'
403+
'&'
404+
elsif filter and filter.include?( ref[1...-1] )
405+
ref
406+
elsif doctype
407+
doctype.entity( ref[1...-1] ) or ref
408+
else
409+
entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ]
410+
entity_value ? entity_value.value : ref
411+
end
412+
end
403413
end
404414
end

test/rexml/test_entity.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ def test_replace_entities
104104
assert_equal source, out
105105
end
106106

107+
def test_entity_string_limit
108+
template = '<!DOCTYPE bomb [ <!ENTITY a "^" > ]> <bomb>$</bomb>'
109+
len = 5120 # 5k per entity
110+
template.sub!(/\^/, "B" * len)
111+
112+
# 10k is OK
113+
entities = '&a;' * 2 # 5k entity * 2 = 10k
114+
xmldoc = REXML::Document.new(template.sub(/\$/, entities))
115+
assert_equal(len * 2, xmldoc.root.text.bytesize)
116+
117+
# above 10k explodes
118+
entities = '&a;' * 3 # 5k entity * 2 = 15k
119+
xmldoc = REXML::Document.new(template.sub(/\$/, entities))
120+
assert_raises(RuntimeError) do
121+
xmldoc.root.text
122+
end
123+
end
124+
107125
def test_raw
108126
source = '<!DOCTYPE foo [
109127
<!ENTITY ent "replace">

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define RUBY_VERSION "1.9.3"
2-
#define RUBY_PATCHLEVEL 391
2+
#define RUBY_PATCHLEVEL 392
33

44
#define RUBY_RELEASE_DATE "2013-02-22"
55
#define RUBY_RELEASE_YEAR 2013

0 commit comments

Comments
 (0)