GeezerVison is showing github.com — Fix a bug that Stream parser doesn't expand the user-defined entity references for "text" by naitoh · Pull Request #200 · ruby/rexml
Conversation
naitoh
commented
Aug 20, 2024
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Why?
Pull parser expands character references and predefined entity references, but doesn't expand user-defined entity references.
Change
- text_stream_unnormalize.rb
$LOAD_PATH.unshift(File.expand_path("lib"))
require 'rexml/document'
require 'rexml/parsers/sax2parser'
require 'rexml/parsers/pullparser'
require 'rexml/parsers/streamparser'
require 'rexml/streamlistener'
xml = <<EOS
<!DOCTYPE foo [
<!ENTITY la "1234">
<!ENTITY lala "--&la;--">
<!ENTITY lalal "&la;&la;">
]><root><la>&la;</la><lala>&lala;</lala><a><P> <I> <B> Text </B> </I></a><b>test™</b></root>
EOS
class StListener
include REXML::StreamListener
def text(text)
puts text
end
end
puts "REXML(DOM)"
REXML::Document.new(xml).elements.each("/root/*") {|element| puts element.text}
puts ""
puts "REXML(Pull)"
parser = REXML::Parsers::PullParser.new(xml)
while parser.has_next?
event = parser.pull
case event.event_type
when :text
puts event[1]
end
end
puts ""
puts "REXML(Stream)"
parser = REXML::Parsers::StreamParser.new(xml, StListener.new).parse
puts ""
puts "REXML(SAX)"
sax = REXML::Parsers::SAX2Parser.new(xml)
sax.listen(:characters) {|x| puts x }
sax.parse
Before (master)
$ ruby text_stream_unnormalize.rb
REXML(DOM)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Pull)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Stream)
&la; #<= This
&lala; #<= This
<P> <I> <B> Text </B> </I>
test™
REXML(SAX)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
After(This PR)
$ ruby text_stream_unnormalize.rb
REXML(DOM)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Pull)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Stream)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(SAX)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…eferences for "text"
## Why?
Pull parser expands character references and predefined entity references, but doesn't expand user-defined entity references.
## Change
- text_stream_unnormalize.rb
```
$LOAD_PATH.unshift(File.expand_path("lib"))
require 'rexml/document'
require 'rexml/parsers/sax2parser'
require 'rexml/parsers/pullparser'
require 'rexml/parsers/streamparser'
require 'rexml/streamlistener'
xml = <<EOS
<!DOCTYPE foo [
<!ENTITY la "1234">
<!ENTITY lala "--&la;--">
<!ENTITY lalal "&la;&la;">
]><root><la>&la;</la><lala>&lala;</lala><a><P> <I> <B> Text </B> </I></a><b>test™</b></root>
EOS
class StListener
include REXML::StreamListener
def text(text)
puts text
end
end
puts "REXML(DOM)"
REXML::Document.new(xml).elements.each("/root/*") {|element| puts element.text}
puts ""
puts "REXML(Pull)"
parser = REXML::Parsers::PullParser.new(xml)
while parser.has_next?
event = parser.pull
case event.event_type
when :text
puts event[1]
end
end
puts ""
puts "REXML(Stream)"
parser = REXML::Parsers::StreamParser.new(xml, StListener.new).parse
puts ""
puts "REXML(SAX)"
sax = REXML::Parsers::SAX2Parser.new(xml)
sax.listen(:characters) {|x| puts x }
sax.parse
```
## Before (master)
```
$ ruby text_stream_unnormalize.rb
REXML(DOM)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Pull)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Stream)
&la; #<= This
&lala; #<= This
<P> <I> <B> Text </B> </I>
test™
REXML(SAX)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
```
After(This PR)
```
$ ruby text_stream_unnormalize.rb
REXML(DOM)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Pull)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(Stream)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
REXML(SAX)
1234
--1234--
<P> <I> <B> Text </B> </I>
test™
```
## Why?
See:
- ruby#187
- ruby#195
## Change
- Supported `REXML::Security.entity_expansion_limit=` in Stream parser
- Supported `REXML::Security.entity_expansion_text_limit=` in Stream parser
## Why?
Because `StreamParser#entity_expansion_count` was added.
Uh oh!
There was an error while loading. Please reload this page.
naitoh reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
kou
Awaiting requested review from kou
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.