filter/source/config/cache/typedetection.cxx |   26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

New commits:
commit 93357349ff1998b41ea1ebedf09dc1cc5da316f7
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Thu Nov 30 23:42:54 2023 +0300
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Fri Dec 1 07:23:32 2023 +0100

    Related: tdf#96401 Check ZIP magic number, to avoid false detections
    
    Change-Id: Ice077f35293a3d0f37c74911d5a6db7ccab5fb31
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160168
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/filter/source/config/cache/typedetection.cxx 
b/filter/source/config/cache/typedetection.cxx
index c411249877af..065fe483a3dd 100644
--- a/filter/source/config/cache/typedetection.cxx
+++ b/filter/source/config/cache/typedetection.cxx
@@ -42,6 +42,7 @@
 #include <comphelper/fileurl.hxx>
 #include <comphelper/lok.hxx>
 #include <comphelper/sequence.hxx>
+#include <comphelper/scopeguard.hxx>
 #include <utility>
 
 #define DEBUG_TYPE_DETECTION 0
@@ -841,6 +842,31 @@ void TypeDetection::impl_getAllFormatTypes(
 static bool isBrokenZIP(const css::uno::Reference<css::io::XInputStream>& 
xStream,
                         const 
css::uno::Reference<css::uno::XComponentContext>& xContext)
 {
+    try
+    {
+        // Only consider seekable streams starting with "PK", to avoid false 
detections
+        css::uno::Reference<css::io::XSeekable> xSeek(xStream, 
css::uno::UNO_QUERY_THROW);
+        comphelper::ScopeGuard restorePos(
+            [xSeek, nPos = xSeek->getPosition()]
+            {
+                try
+                {
+                    xSeek->seek(nPos);
+                }
+                catch (const css::uno::Exception&)
+                {
+                }
+            });
+        css::uno::Sequence<sal_Int8> magic(2);
+        xStream->readBytes(magic, 2);
+        if (magic.getLength() < 2 || magic[0] != 'P' || magic[1] != 'K')
+            return false;
+    }
+    catch (const css::uno::Exception&)
+    {
+        return false;
+    }
+
     std::vector<css::uno::Any> aArguments{
         css::uno::Any(xStream),
         css::uno::Any(css::beans::NamedValue("AllowRemoveOnInsert", 
css::uno::Any(false))),

Reply via email to