8000 Add position check for XML declaration (#162) · ruby/rexml@ebc3e85 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebc3e85

Browse files
authored
Add position check for XML declaration (#162)
## Why? XML declaration must be the first item. https://www.w3.org/TR/2006/REC-xml11-20060816/#document ``` [1] document ::= ( prolog element Misc* ) - ( Char* RestrictedChar Char* ) ``` https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-prolog ``` [22] prolog ::= XMLDecl Misc* (doctypedecl Misc*)? ``` https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-XMLDecl ``` [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' ``` See: #161 (comment)
1 parent eb45c8d commit ebc3e85

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rexml/parsers/baseparser.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,10 @@ def process_instruction(start_position)
644644
@source.position = start_position
645645
raise REXML::ParseException.new(message, @source)
646646
end
647-
if @document_status.nil? and match_data[1] == "xml"
647+
if match_data[1] == "xml"
648+
if @document_status
649+
raise ParseException.new("Malformed XML: XML declaration is not at the start", @source)
650+
end
648651
content = match_data[2]
649652
version = VERSION.match(content)
650653
version = version[1] unless version.nil?

test/parse/test_processing_instruction.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ def test_garbage_text
3939
pi.content,
4040
])
4141
end
42+
43+
def test_xml_declaration_not_at_document_start
44+
exception = assert_raise(REXML::ParseException) do
45+
parser = REXML::Parsers::BaseParser.new('<a><?xml version="1.0" ?></a>')
46+
while parser.has_next?
47+
parser.pull
48+
end
49+
end
50+
51+
assert_equal(<<~DETAIL.chomp, exception.to_s)
52+
Malformed XML: XML declaration is not at the start
53+
Line: 1
54+
Position: 25
55+
Last 80 unconsumed characters:
56+
57+
DETAIL
58+
end
4259
end
4360

4461
def test_after_root

0 commit comments

Comments
 (0)
0