[Libreoffice-commits] core.git: officecfg/registry sfx2/Library_sfx.mk sfx2/source

Samuel Mehrbrodt (via logerrit) logerrit at kemper.freedesktop.org
Thu Apr 1 05:46:09 UTC 2021


 officecfg/registry/schema/org/openoffice/Office/Common.xcs |   16 ++++++
 sfx2/Library_sfx.mk                                        |    3 +
 sfx2/source/inc/eventsupplier.hxx                          |    4 +
 sfx2/source/notify/eventsupplier.cxx                       |   34 +++++++++++++
 4 files changed, 57 insertions(+)

New commits:
commit 0a893a15b02a3662e3c68776be09534c9f955e4f
Author:     Samuel Mehrbrodt <samuel.mehrbrodt at allotropia.de>
AuthorDate: Tue Mar 16 16:27:44 2021 +0100
Commit:     Samuel Mehrbrodt <samuel.mehrbrodt at allotropia.de>
CommitDate: Thu Apr 1 07:45:21 2021 +0200

    Add mechanism to selectively enable macros for document events
    
    Change-Id: I56703b2c0ee009a645458c78c026c546b2e7e321
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112584
    Tested-by: Jenkins
    Reviewed-by: Samuel Mehrbrodt <samuel.mehrbrodt at allotropia.de>

diff --git a/officecfg/registry/schema/org/openoffice/Office/Common.xcs b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
index 91bf8142c72f..486c9aafb47f 100644
--- a/officecfg/registry/schema/org/openoffice/Office/Common.xcs
+++ b/officecfg/registry/schema/org/openoffice/Office/Common.xcs
@@ -2783,6 +2783,22 @@
           </info>
           <value>true</value>
         </prop>
+        <prop oor:name="AllowedDocumentEventURLs" oor:type="oor:string-list">
+          <info>
+            <desc>List of script URLS which are allowed to be called by document events.
+            Look into content.xml of the odf file to find the URL.
+            You can either write the full URL, a part of it (starting from the beginning),
+            or use regular expressions.
+            Examples:
+              * vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=user
+              * vnd.sun.star.script:Standard.Module1
+              * vnd.sun.star.script:YourScript.*location=share
+              * .*location=application.*
+
+            When this list is empty, all document event URLs are allowed.
+            </desc>
+          </info>
+        </prop>
         <set oor:name="TrustedAuthors" oor:node-type="TrustedAuthor">
           <info>
             <desc>List with trusted authors.</desc>
diff --git a/sfx2/Library_sfx.mk b/sfx2/Library_sfx.mk
index b5126ca87893..34ea74dfe38c 100644
--- a/sfx2/Library_sfx.mk
+++ b/sfx2/Library_sfx.mk
@@ -69,6 +69,9 @@ $(eval $(call gb_Library_use_libraries,sfx,\
 
 $(eval $(call gb_Library_use_externals,sfx,\
     boost_headers \
+    icu_headers \
+    icui18n \
+    icuuc \
     libxml2 \
     orcus \
     orcus-parser\
diff --git a/sfx2/source/inc/eventsupplier.hxx b/sfx2/source/inc/eventsupplier.hxx
index b6d63a09ea59..3aa73a7ffc8d 100644
--- a/sfx2/source/inc/eventsupplier.hxx
+++ b/sfx2/source/inc/eventsupplier.hxx
@@ -78,6 +78,10 @@ public:
                                     ::comphelper::NamedValueCollection& o_normalizedDescriptor,
                                     SfxObjectShell* i_document );
     static void Execute( css::uno::Any const & aEventData, const css::document::DocumentEvent& aTrigger, SfxObjectShell* pDoc );
+
+private:
+    /// Check if script URL whitelist exists, and if so, if current script url is part of it
+    static bool isScriptURLAllowed(const OUString& aScriptURL);
 };
 
 #endif
diff --git a/sfx2/source/notify/eventsupplier.cxx b/sfx2/source/notify/eventsupplier.cxx
index 57c8ebe31751..091ed963de90 100644
--- a/sfx2/source/notify/eventsupplier.cxx
+++ b/sfx2/source/notify/eventsupplier.cxx
@@ -21,6 +21,7 @@
 
 #include <com/sun/star/util/URL.hpp>
 #include <com/sun/star/frame/Desktop.hpp>
+#include <com/sun/star/uno/Sequence.hxx>
 #include <com/sun/star/util/URLTransformer.hpp>
 #include <com/sun/star/util/XURLTransformer.hpp>
 #include <tools/urlobj.hxx>
@@ -33,6 +34,7 @@
 #include <comphelper/processfactory.hxx>
 #include <comphelper/namedvaluecollection.hxx>
 #include <comphelper/sequence.hxx>
+#include <officecfg/Office/Common.hxx>
 #include <eventsupplier.hxx>
 
 #include <sfx2/app.hxx>
@@ -41,6 +43,10 @@
 #include <sfx2/frame.hxx>
 #include <macroloader.hxx>
 
+#include <unicode/errorcode.h>
+#include <unicode/regex.h>
+#include <unicode/unistr.h>
+
 using namespace css;
 using namespace ::com::sun::star;
 
@@ -147,6 +153,31 @@ sal_Bool SAL_CALL SfxEvents_Impl::hasElements()
     return maEventNames.hasElements();
 }
 
+bool SfxEvents_Impl::isScriptURLAllowed(const OUString& aScriptURL)
+{
+    std::optional<css::uno::Sequence<OUString>> allowedEvents(
+        officecfg::Office::Common::Security::Scripting::AllowedDocumentEventURLs::get());
+    // When AllowedDocumentEventURLs is empty, all event URLs are allowed
+    if (!allowedEvents)
+        return true;
+
+    icu::ErrorCode status;
+    const uint32_t rMatcherFlags = UREGEX_CASE_INSENSITIVE;
+    icu::UnicodeString usInput(aScriptURL.getStr());
+    const css::uno::Sequence<OUString>& rAllowedEvents = *allowedEvents;
+    for (auto const& allowedEvent : rAllowedEvents)
+    {
+        icu::UnicodeString usRegex(allowedEvent.getStr());
+        icu::RegexMatcher rmatch1(usRegex, usInput, rMatcherFlags, status);
+        if (aScriptURL.startsWith(allowedEvent) || rmatch1.matches(status))
+        {
+            return true;
+        }
+    }
+
+    return false;
+}
+
 void SfxEvents_Impl::Execute( uno::Any const & aEventData, const document::DocumentEvent& aTrigger, SfxObjectShell* pDoc )
 {
     uno::Sequence < beans::PropertyValue > aProperties;
@@ -185,6 +216,9 @@ void SfxEvents_Impl::Execute( uno::Any const & aEventData, const document::Docum
     if (aScript.isEmpty())
         return;
 
+    if (!isScriptURLAllowed(aScript))
+        return;
+
     if (!pDoc)
         pDoc = SfxObjectShell::Current();
 


More information about the Libreoffice-commits mailing list