Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "malformed comment" check for top-level comments #145

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,14 @@ def pull_event
return process_instruction(start_position)
elsif @source.match("<!", true)
if @source.match("--", true)
return [ :comment, @source.match(/(.*?)-->/um, true)[1] ]
md = @source.match(/(.*?)-->/um, true)
if md.nil?
raise REXML::ParseException.new("Unclosed comment", @source)
end
if md[1] =~ /--|-\z/
makenowjust marked this conversation as resolved.
Show resolved Hide resolved
raise REXML::ParseException.new("Malformed comment", @source)
end
return [ :comment, md[1] ]
elsif @source.match("DOCTYPE", true)
base_error_message = "Malformed DOCTYPE"
unless @source.match(/\s+/um, true)
Expand Down
96 changes: 96 additions & 0 deletions test/parse/test_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require "test/unit"
require "rexml/document"

module REXMLTests
class TestParseComment < Test::Unit::TestCase
def parse(xml)
REXML::Document.new(xml)
end

class TestInvalid < self
def test_toplevel_unclosed_comment
exception = assert_raise(REXML::ParseException) do
parse("<!--")
end
assert_equal(<<~DETAIL, exception.to_s)
Unclosed comment
Line: 1
Position: 4
Last 80 unconsumed characters:
DETAIL
end

def test_toplevel_malformed_comment_inner
exception = assert_raise(REXML::ParseException) do
parse("<!-- -- -->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 11
Last 80 unconsumed characters:
DETAIL
end

def test_toplevel_malformed_comment_end
exception = assert_raise(REXML::ParseException) do
parse("<!-- --->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 9
Last 80 unconsumed characters:
DETAIL
end

def test_doctype_malformed_comment_inner
exception = assert_raise(REXML::ParseException) do
parse("<!DOCTYPE foo [<!-- -- -->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 26
Last 80 unconsumed characters:
DETAIL
end

def test_doctype_malformed_comment_end
exception = assert_raise(REXML::ParseException) do
parse("<!DOCTYPE foo [<!-- --->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 24
Last 80 unconsumed characters:
DETAIL
end

def test_after_doctype_malformed_comment_inner
exception = assert_raise(REXML::ParseException) do
parse("<a><!-- -- -->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 14
Last 80 unconsumed characters:
DETAIL
end

def test_after_doctype_malformed_comment_end
exception = assert_raise(REXML::ParseException) do
parse("<a><!-- --->")
end
assert_equal(<<~DETAIL, exception.to_s)
Malformed comment
Line: 1
Position: 12
Last 80 unconsumed characters:
DETAIL
end
end
end
end