From 1277279527c9e57327a8aa615f8782dc11c9758d Mon Sep 17 00:00:00 2001 From: junhyeong9812 Date: Fri, 19 Jun 2026 07:32:03 +0900 Subject: [PATCH 1/2] 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 --- .../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 3c78313bd0d..31671eabe73 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 @@ 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 = ""; 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 9714eef1c64..dcdcf3e49d0 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 @@ class XmlValidationModeDetectorTests { "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 00000000000..2a34756ee05 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInMultiLineCommentBody.xml @@ -0,0 +1,10 @@ + + + + + From 4074155d76aa021455f54a2bc73376e7a7d5c6e1 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 25 Jun 2026 13:23:49 +0200 Subject: [PATCH 2/2] Polish contribution See gh-36948 --- .../util/xml/XmlValidationModeDetectorTests.java | 6 ++++-- .../xml/dtdWithDoctypeInMultiLineCommentBody.xml | 8 ++++++++ ...xsdWithMultipleDoctypesInMultiLineCommentBody.xml | 12 ++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 spring-core/src/test/resources/org/springframework/util/xml/dtdWithDoctypeInMultiLineCommentBody.xml create mode 100644 spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleDoctypesInMultiLineCommentBody.xml 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 dcdcf3e49d0..15a97691c1e 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 @@ -44,7 +44,8 @@ class XmlValidationModeDetectorTests { "dtdWithTrailingComment.xml", "dtdWithTrailingCommentAcrossMultipleLines.xml", "dtdWithCommentOnNextLine.xml", - "dtdWithMultipleComments.xml" + "dtdWithMultipleComments.xml", + "dtdWithDoctypeInMultiLineCommentBody.xml" }) void dtdDetection(String fileName) throws Exception { assertValidationMode(fileName, VALIDATION_DTD); @@ -56,7 +57,8 @@ class XmlValidationModeDetectorTests { "xsdWithMultipleComments.xml", "xsdWithDoctypeInComment.xml", "xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml", - "xsdWithDoctypeInMultiLineCommentBody.xml" + "xsdWithDoctypeInMultiLineCommentBody.xml", + "xsdWithMultipleDoctypesInMultiLineCommentBody.xml" }) void xsdDetection(String fileName) throws Exception { assertValidationMode(fileName, VALIDATION_XSD); diff --git a/spring-core/src/test/resources/org/springframework/util/xml/dtdWithDoctypeInMultiLineCommentBody.xml b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithDoctypeInMultiLineCommentBody.xml new file mode 100644 index 00000000000..9256a3103b0 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithDoctypeInMultiLineCommentBody.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleDoctypesInMultiLineCommentBody.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleDoctypesInMultiLineCommentBody.xml new file mode 100644 index 00000000000..87a39cdaaf8 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleDoctypesInMultiLineCommentBody.xml @@ -0,0 +1,12 @@ + + + + +