From 72aa952b89fc4418504372e5c6c6dd8b3b52b5f5 Mon Sep 17 00:00:00 2001 From: junhyeong9812 Date: Fri, 19 Jun 2026 07:32:03 +0900 Subject: [PATCH] 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. 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. Honor 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. Signed-off-by: junhyeong9812 --- .../util/xml/XmlValidationModeDetector.java | 4 +++- .../util/xml/XmlValidationModeDetectorTests.java | 3 ++- .../util/xml/xsdWithDoctypeInMultiLineCommentBody.xml | 10 ++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInMultiLineCommentBody.xml diff --git a/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java b/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java index 3c78313bd0d5..31671eabe73b 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java +++ b/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java @@ -151,7 +151,9 @@ private boolean hasOpeningTag(String content) { 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 = ""; diff --git a/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java b/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java index 9714eef1c649..dcdcf3e49d0e 100644 --- a/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java +++ b/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java @@ -55,7 +55,8 @@ void dtdDetection(String fileName) throws Exception { "xsdWithNoComments.xml", "xsdWithMultipleComments.xml", "xsdWithDoctypeInComment.xml", - "xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml" + "xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml", + "xsdWithDoctypeInMultiLineCommentBody.xml" }) void xsdDetection(String fileName) throws Exception { assertValidationMode(fileName, VALIDATION_XSD); diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInMultiLineCommentBody.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInMultiLineCommentBody.xml new file mode 100644 index 000000000000..2a34756ee05d --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInMultiLineCommentBody.xml @@ -0,0 +1,10 @@ + + + + +