Ignore DOCTYPE inside a multi-line comment body

XmlValidationModeDetector peeks at the start of an XML document to
choose between DTD- and XSD-based validation, skipping any DOCTYPE that
appears inside an XML comment.

Prior to this commit, consumeCommentTokens() short-circuited a line
with no start or end comment marker by returning it unchanged, even
while already inside a multi-line comment. Such a body line was then
treated as content, so a literal "DOCTYPE" word in the comment body
caused an XSD document to be misdetected as DTD-based.

This commit honors the "in comment" parse state in that early return so
a comment body line is treated as empty content, completing the fix for
gh-27915 which only covered comment markers on the same line.

Closes gh-36948

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
This commit is contained in:
junhyeong9812
2026-06-25 13:07:57 +02:00
committed by Sam Brannen
parent d0331a049a
commit 1277279527
3 changed files with 15 additions and 2 deletions
@@ -151,7 +151,9 @@ public class XmlValidationModeDetector {
private String consumeCommentTokens(String line) {
int indexOfStartComment = line.indexOf(START_COMMENT);
if (indexOfStartComment == -1 && !line.contains(END_COMMENT)) {
return line;
// If we are inside a multi-line comment, the entire line is comment
// data and must not be treated as content.
return (this.inComment ? "" : line);
}
String result = "";
@@ -55,7 +55,8 @@ class XmlValidationModeDetectorTests {
"xsdWithNoComments.xml",
"xsdWithMultipleComments.xml",
"xsdWithDoctypeInComment.xml",
"xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml"
"xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml",
"xsdWithDoctypeInMultiLineCommentBody.xml"
})
void xsdDetection(String fileName) throws Exception {
assertValidationMode(fileName, VALIDATION_XSD);
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
See the DOCTYPE notes for legacy configs
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>