20#include <config_features.h>
26#include <com/sun/star/document/MacroExecMode.hpp>
27#include <com/sun/star/task/ErrorCodeRequest.hpp>
28#include <com/sun/star/task/DocumentMacroConfirmationRequest.hpp>
29#include <com/sun/star/security/DocumentDigitalSignatures.hpp>
30#include <com/sun/star/script/XLibraryContainer.hpp>
31#include <com/sun/star/document/XEmbeddedScripts.hpp>
35#include <osl/file.hxx>
43#include <officecfg/Office/Common.hxx>
44#include <systools/win32/comtools.hxx>
52 using ::com::sun::star::uno::Reference;
53 using ::com::sun::star::task::XInteractionHandler;
54 using ::com::sun::star::uno::Any;
55 using ::com::sun::star::uno::Sequence;
56 using ::com::sun::star::task::DocumentMacroConfirmationRequest;
57 using ::com::sun::star::uno::Exception;
58 using ::com::sun::star::security::DocumentDigitalSignatures;
59 using ::com::sun::star::security::XDocumentDigitalSignatures;
60 using ::com::sun::star::embed::XStorage;
61 using ::com::sun::star::document::XEmbeddedScripts;
62 using ::com::sun::star::script::XLibraryContainer;
63 using ::com::sun::star::container::XNameAccess;
64 using ::com::sun::star::uno::UNO_QUERY_THROW;
66 namespace MacroExecMode = ::com::sun::star::document::MacroExecMode;
85 bool lcl_showMacroWarning(
const Reference< XInteractionHandler >& rxHandler,
86 const OUString& rDocumentLocation )
88 DocumentMacroConfirmationRequest aRequest;
89 aRequest.DocumentURL = rDocumentLocation;
102 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::ALWAYS_EXECUTE_NO_WARN );
108 m_xData->m_rDocumentAccess.setCurrentMacroExecMode( MacroExecMode::NEVER_EXECUTE );
114 sal_uInt16 nMacroExecutionMode =
m_xData->m_rDocumentAccess.getCurrentMacroExecMode();
123 enum AutoConfirmation
129 AutoConfirmation eAutoConfirm( eNoAutoConfirm );
131 if ( ( nMacroExecutionMode == MacroExecMode::USE_CONFIG )
132 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION )
133 || ( nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION )
137 if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_REJECT_CONFIRMATION)
138 eAutoConfirm = eAutoConfirmReject;
139 else if (nMacroExecutionMode == MacroExecMode::USE_CONFIG_APPROVE_CONFIRMATION)
140 eAutoConfirm = eAutoConfirmApprove;
145 nMacroExecutionMode = MacroExecMode::FROM_LIST_NO_WARN;
148 nMacroExecutionMode = MacroExecMode::FROM_LIST_AND_SIGNED_WARN;
151 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE;
154 nMacroExecutionMode = MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
157 OSL_FAIL(
"DocumentMacroMode::adjustMacroMode: unexpected macro security level!" );
158 nMacroExecutionMode = MacroExecMode::NEVER_EXECUTE;
162 if ( nMacroExecutionMode == MacroExecMode::NEVER_EXECUTE )
165 if ( nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE_NO_WARN )
168 const OUString sURL(
m_xData->m_rDocumentAccess.getDocumentLocation());
173 Reference< XDocumentDigitalSignatures > xSignatures(DocumentDigitalSignatures::createDefault(::comphelper::getProcessComponentContext()));
180 if ( !aLocation.isEmpty() && xSignatures->isLocationTrusted( aLocation ) )
186 if ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
192 if ( nMacroExecutionMode != MacroExecMode::FROM_LIST )
195 const bool bAllowUIToAddAuthor = nMacroExecutionMode != MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN
196 && (nMacroExecutionMode == MacroExecMode::ALWAYS_EXECUTE
198 const bool bHasTrustedMacroSignature =
m_xData->m_rDocumentAccess.hasTrustedScriptingSignature(bAllowUIToAddAuthor);
205 else if (
m_xData->m_rDocumentAccess.macroCallsSeenWhileLoading() &&
206 bHasTrustedMacroSignature &&
207 !bHasValidContentSignature)
210 m_xData->m_bHasUnsignedContentError =
true;
213 else if ( bHasTrustedMacroSignature )
227 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
228 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
236 if ( ( nMacroExecutionMode == MacroExecMode::FROM_LIST_NO_WARN )
237 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_WARN )
238 || ( nMacroExecutionMode == MacroExecMode::FROM_LIST_AND_SIGNED_NO_WARN )
249 osl::FileBase::getSystemPathFromFileURL(sURL, sFilePath);
250 sal::systools::COMReference<IZoneIdentifier> pZoneId;
251 pZoneId.CoCreateInstance(CLSID_PersistentZoneIdentifier);
252 sal::systools::COMReference<IPersistFile> pPersist(pZoneId, sal::systools::COM_QUERY_THROW);
254 if (!SUCCEEDED(pPersist->Load(o3tl::toW(sFilePath.getStr()), STGM_READ)) ||
255 !SUCCEEDED(pZoneId->GetId(&dwZone)))
259 dwZone = URLZONE_LOCAL_MACHINE;
265 case URLZONE_LOCAL_MACHINE:
266 nAction = officecfg::Office::Common::Security::Scripting::WindowsSecurityZone::ZoneLocal::get();
268 case URLZONE_INTRANET:
269 nAction = officecfg::Office::Common::Security::Scripting::WindowsSecurityZone::ZoneIntranet::get();
271 case URLZONE_TRUSTED:
272 nAction = officecfg::Office::Common::Security::Scripting::WindowsSecurityZone::ZoneTrusted::get();
274 case URLZONE_INTERNET:
275 nAction = officecfg::Office::Common::Security::Scripting::WindowsSecurityZone::ZoneInternet::get();
277 case URLZONE_UNTRUSTED:
278 nAction = officecfg::Office::Common::Security::Scripting::WindowsSecurityZone::ZoneUntrusted::get();
298 bool bSecure =
false;
300 if ( eAutoConfirm == eNoAutoConfirm )
302 OUString sReferrer(sURL);
304 OUString aSystemFileURL;
305 if ( osl::FileBase::getSystemPathFromFileURL( sReferrer, aSystemFileURL ) == osl::FileBase::E_None )
306 sReferrer = aSystemFileURL;
308 bSecure = lcl_showMacroWarning( rxInteraction, sReferrer );
311 bSecure = ( eAutoConfirm == eAutoConfirmApprove );
319 return m_xData->m_rDocumentAccess.getCurrentMacroExecMode() == MacroExecMode::NEVER_EXECUTE;
325 bool bHasMacroLib =
false;
328 if ( xContainer.is() )
334 if ( !xContainer->hasElements() )
335 bHasMacroLib =
false;
338 static constexpr OUStringLiteral aStdLibName(
u"Standard" );
339 static constexpr OUStringLiteral aVBAProject(
u"VBAProject" );
340 const Sequence< OUString > aElements = xContainer->getElementNames();
341 for(
const OUString& aElement : aElements )
343 if( aElement == aStdLibName || aElement == aVBAProject )
345 Reference < XNameAccess > xLib;
346 Any aAny = xContainer->getByName( aElement );
348 if ( xLib.is() && xLib->hasElements() )
367 bool bHasMacroLib =
false;
368#if HAVE_FEATURE_SCRIPTING
371 Reference< XEmbeddedScripts > xScripts(
m_xData->m_rDocumentAccess.getEmbeddedDocumentScripts() );
372 Reference< XLibraryContainer > xContainer;
374 xContainer.set( xScripts->getBasicLibraries(), UNO_QUERY_THROW );
388 return m_xData->m_bHasUnsignedContentError;
394 bool bHasMacros =
false;
395 if ( rxStorage.is() )
399 static constexpr OUStringLiteral s_sBasicStorageName(
u"Basic" );
400 static constexpr OUStringLiteral s_sScriptsStorageName(
u"Scripts" );
402 bHasMacros =( ( rxStorage->hasByName( s_sBasicStorageName )
403 && rxStorage->isStorageElement( s_sBasicStorageName )
405 || ( rxStorage->hasByName( s_sScriptsStorageName )
406 && rxStorage->isStorageElement( s_sScriptsStorageName )
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool removeSegment(sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true)
static bool CallApproveHandler(const css::uno::Reference< css::task::XInteractionHandler > &xHandler, const css::uno::Any &rRequest, bool bAllowAbort)
bool hasUnsignedContentError() const
bool hasMacroLibrary() const
determines whether the document actually has a macros library
bool allowMacroExecution()
allows macro execution in the document
std::shared_ptr< DocumentMacroMode_Data > m_xData
bool disallowMacroExecution()
disallows macro execution in the document
bool adjustMacroMode(const css::uno::Reference< css::task::XInteractionHandler > &_rxInteraction, bool bHasValidContentSignature=false)
checks whether the document allows executing contained macros.
DocumentMacroMode(IMacroDocumentAccess &_rDocumentAccess)
creates an instance
static bool containerHasBasicMacros(const css::uno::Reference< css::script::XLibraryContainer > &xContainer)
bool checkMacrosOnLoading(const css::uno::Reference< css::task::XInteractionHandler > &_rxInteraction, bool bHasValidContentSignature=false)
checks the macro execution mode while loading the document.
bool isMacroExecutionDisallowed() const
determines whether macro execution is disallowed
static bool storageHasMacros(const css::uno::Reference< css::embed::XStorage > &_rxStorage)
determines whether the given document storage has sub storages containing scripts or macros.
provides access to several settings of a document, which are needed by ->DocumentMacroMode to properl...
#define DBG_UNHANDLED_EXCEPTION(...)
bool IsReadOnly(EOption eOption)
sal_Int32 GetMacroSecurityLevel()
std::shared_ptr< T > make_shared(Args &&... args)
bool m_bHasUnsignedContentError
IMacroDocumentAccess & m_rDocumentAccess
DocumentMacroMode_Data(IMacroDocumentAccess &rDocumentAccess)