Skip to content

Commit 8554f73

Browse files
committed
add Document#entity_expansion_text_limit=
## Why? See: #192
1 parent 05750a3 commit 8554f73

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

lib/rexml/attribute.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ def to_s
148148
# have been expanded to their values
149149
def value
150150
return @unnormalized if @unnormalized
151-
@unnormalized = Text::unnormalize( @normalized, doctype )
152-
@unnormalized
151+
152+
if @element&.document
153+
@unnormalized = Text::unnormalize(@normalized, doctype, entity_expansion_text_limit: @element.document.entity_expansion_text_limit)
154+
else
155+
@unnormalized = Text::unnormalize(@normalized, doctype)
156+
end
153157
end
154158

155159
# The normalized value of this attribute. That is, the attribute with

lib/rexml/document.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Document < Element
9292
def initialize( source = nil, context = {} )
9393
@entity_expansion_count = 0
9494
@entity_expansion_limit = Security.entity_expansion_limit
95+
@entity_expansion_text_limit = Security.entity_expansion_text_limit
9596
super()
9697
@context = context
9798
return if source.nil?
@@ -432,6 +433,7 @@ def Document::entity_expansion_text_limit
432433
end
433434

434435
attr_reader :entity_expansion_count
436+
attr_accessor :entity_expansion_text_limit
435437

436438
def record_entity_expansion
437439
@entity_expansion_count += 1

lib/rexml/entity.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,14 @@ def Entity::matches? string
7171
# Evaluates to the unnormalized value of this entity; that is, replacing
7272
# &ent; entities.
7373
def unnormalized
74-
document.record_entity_expansion unless document.nil?
7574
return nil if @value.nil?
76-
@unnormalized = Text::unnormalize(@value, parent)
75+
76+
if document.nil?
77+
@unnormalized = Text::unnormalize(@value, parent)
78+
else
79+
document.record_entity_expansion
80+
@unnormalized = Text::unnormalize(@value, parent, entity_expansion_text_limit: document.entity_expansion_text_limit)
81+
end
7782
end
7883

7984
#once :unnormalized

lib/rexml/text.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ def inspect
268268
# u = Text.new( "sean russell", false, nil, true )
269269
# u.value #-> "sean russell"
270270
def value
271-
@unnormalized ||= Text::unnormalize( @string, doctype )
271+
if document.nil?
272+
@unnormalized ||= Text::unnormalize(@string, doctype)
273+
else
274+
@unnormalized ||= Text::unnormalize(@string, doctype, entity_expansion_text_limit: document.entity_expansion_text_limit)
275+
end
272276
end
273277

274278
# Sets the contents of this text node. This expects the text to be
@@ -411,11 +415,11 @@ def Text::normalize( input, doctype=nil, entity_filter=nil )
411415
end
412416

413417
# Unescapes all possible entities
414-
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil )
418+
def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil, entity_expansion_text_limit: Security.entity_expansion_text_limit )
415419
sum = 0
416420
string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) {
417421
s = Text.expand($&, doctype, filter)
418-
if sum + s.bytesize > Security.entity_expansion_text_limit
422+
if sum + s.bytesize > entity_expansion_text_limit
419423
raise "entity expansion has grown too large"
420424
else
421425
sum += s.bytesize

test/test_document.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ def test_new
3131
end
3232

3333
class EntityExpansionLimitTest < Test::Unit::TestCase
34-
def setup
35-
@default_entity_expansion_text_limit = REXML::Security.entity_expansion_text_limit
36-
end
37-
38-
def teardown
39-
REXML::Security.entity_expansion_text_limit = @default_entity_expansion_text_limit
40-
end
41-
4234
class GeneralEntityTest < self
4335
def test_have_value
4436
xml = <<XML
@@ -138,8 +130,8 @@ def test_entity_expansion_text_limit
138130
<member>&a;</member>
139131
XML
140132

141-
REXML::Security.entity_expansion_text_limit = 90
142133
doc = REXML::Document.new(xml)
134+
doc.entity_expansion_text_limit = 90
143135
assert_equal(90, doc.root.children.first.value.bytesize)
144136
end
145137
end

0 commit comments

Comments
 (0)