This is an automated email from the ASF dual-hosted git repository.

tballison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/main by this push:
     new 533addc12a TIKA-4962: text/html magic requires the html tag to end 
(#3312)
533addc12a is described below

commit 533addc12a062cbd61d4a15df255150f86af17f4
Author: Tim Allison <[email protected]>
AuthorDate: Fri Oct 9 06:49:39 2026 -0400

    TIKA-4962: text/html magic requires the html tag to end (#3312)
---
 CHANGES.txt                                        |  4 ++++
 .../org/apache/tika/mime/tika-mimetypes.xml        |  6 ++---
 .../org/apache/tika/mime/MimeDetectionTest.java    | 28 ++++++++++++++++++++++
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 7f7b5de69f..e2144fe4c0 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,9 @@
 Release 4.2.0 - unreleased
 
+   * The text/html magic now requires the html tag to end, so an XML file with 
an
+     element such as <htmlPath> in its first 8K is no longer detected as HTML 
and sent
+     to the HTML parser (TIKA-4962).
+
    * OutlookExtractor now falls back to the "Date:" transport header for
      dcterms:created/modified when a .msg has no MAPI date property; the
      fallback previously could never match (TIKA-4959).
diff --git 
a/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml 
b/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml
index e39ed9036a..88387956e5 100644
--- a/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml
+++ b/tika-core/src/main/resources/org/apache/tika/mime/tika-mimetypes.xml
@@ -7893,8 +7893,8 @@
       <match value="&lt;head" type="string" offset="0:64"/>
       <match value="&lt;TITLE" type="string" offset="0:64"/>
       <match value="&lt;title" type="string" offset="0:64"/>
-      <match value="&lt;HTML" type="string" offset="0:64"/>
-      <match value="&lt;html" type="string" offset="0:128"/>
+      <!-- the tag must end: <htmlPath> in an XML file is not HTML -->
+      <match value="(?i)&lt;html[\\s>]" type="regex" offset="0:128"/>
     </magic>
     <magic priority="20">
       <!-- Lower priority match for <html anywhere near the top of the file -->
@@ -7904,7 +7904,7 @@
            MimeTypes#getMinLength() bytes. It should also only start after
            the higher priority "start of file" one above
        -->
-      <match value="&lt;html" type="string" offset="128:8192"/>
+      <match value="(?i)&lt;html[\\s>]" type="regex" offset="128:8192"/>
     </magic>
     <glob pattern="*.html"/>
     <glob pattern="*.htm"/>
diff --git 
a/tika-core/src/test/java/org/apache/tika/mime/MimeDetectionTest.java 
b/tika-core/src/test/java/org/apache/tika/mime/MimeDetectionTest.java
index d9540190e4..01dab1f671 100644
--- a/tika-core/src/test/java/org/apache/tika/mime/MimeDetectionTest.java
+++ b/tika-core/src/test/java/org/apache/tika/mime/MimeDetectionTest.java
@@ -93,6 +93,34 @@ public class MimeDetectionTest {
         testFile("application/vnd.android.axml", "test-android-binary.xml");
     }
 
+    /** The html magic needs the tag to end: an XML element such as htmlPath 
is not html. */
+    @Test
+    public void testHtmlMagicNeedsTagEnd() throws Exception {
+        String pad = " ".repeat(200);
+        String[] notHtml = {
+                "<htmlPath><![CDATA[TmplData\\tmpl.html]]></htmlPath>",
+                "<flashTemplate><info>" + pad + 
"<htmlPath>x</htmlPath></info></flashTemplate>",
+                "<root>" + pad + "<htmlContent>x</htmlContent></root>"
+        };
+        for (String xml : notHtml) {
+            assertDetected(MediaType.TEXT_PLAIN, xml.getBytes(UTF_8));
+            Metadata metadata = new Metadata();
+            metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, 
"flashTemplate.xml");
+            try (TikaInputStream tis = 
TikaInputStream.get(xml.getBytes(UTF_8))) {
+                assertEquals(MediaType.APPLICATION_XML, MIME_TYPES.detect(tis, 
metadata, new ParseContext()), xml);
+            }
+        }
+        String[] html = {
+                "<html><body>x</body></html>",
+                "<HTML>\n<body>x</body></HTML>",
+                "<html\nlang=\"en\"><body>x</body></html>",
+                "garbage\n".repeat(30) + "<html><body>x</body></html>"
+        };
+        for (String h : html) {
+            assertDetected(MediaType.TEXT_HTML, h.getBytes(UTF_8));
+        }
+    }
+
     /** An OS/2 bitmap array's first entry header (size 40, next at 46) is not 
an icon. */
     @Test
     public void testOs2BitmapArrayIsNotAnIcon() throws Exception {

Reply via email to