LibreOffice Module unotools (master) 1
securityoptions.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
24#include <com/sun/star/uno/Any.hxx>
25#include <com/sun/star/uno/Sequence.hxx>
26
27#include <com/sun/star/beans/PropertyValue.hpp>
30#include <tools/urlobj.hxx>
31
33#include <officecfg/Office/Common.hxx>
34
35// namespaces
36
37using namespace ::com::sun::star::uno;
38
39constexpr OUStringLiteral PROPERTYNAME_MACRO_TRUSTEDAUTHORS = u"TrustedAuthors";
40constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME = u"SubjectName";
41constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER = u"SerialNumber";
42constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA = u"RawData";
43
44
46{
47
48bool IsReadOnly( EOption eOption )
49{
50 bool bReadonly;
51 switch(eOption)
52 {
54 bReadonly = officecfg::Office::Common::Security::Scripting::SecureURL::isReadOnly();
55 break;
57 bReadonly = officecfg::Office::Common::Security::Scripting::WarnSaveOrSendDoc::isReadOnly();
58 break;
60 bReadonly = officecfg::Office::Common::Security::Scripting::WarnSignDoc::isReadOnly();
61 break;
63 bReadonly = officecfg::Office::Common::Security::Scripting::WarnPrintDoc::isReadOnly();
64 break;
66 bReadonly = officecfg::Office::Common::Security::Scripting::WarnCreatePDF::isReadOnly();
67 break;
69 bReadonly = officecfg::Office::Common::Security::Scripting::RemovePersonalInfoOnSaving::isReadOnly();
70 break;
72 bReadonly = officecfg::Office::Common::Security::Scripting::RecommendPasswordProtection::isReadOnly();
73 break;
75 bReadonly = officecfg::Office::Common::Security::Scripting::MacroSecurityLevel::isReadOnly();
76 break;
78 bReadonly = false; // TODO? officecfg::Office::Common::Security::Scripting::TrustedAuthors::isReadOnly();
79 break;
81 bReadonly = officecfg::Office::Common::Security::Scripting::HyperlinksWithCtrlClick::isReadOnly();
82 break;
84 bReadonly = officecfg::Office::Common::Security::Scripting::BlockUntrustedRefererLinks::isReadOnly();
85 break;
86
87 default:
88 assert(false);
89 bReadonly = true;
90 }
91
92 return bReadonly;
93}
94
95std::vector< OUString > GetSecureURLs()
96{
98 return {};
99 std::vector<OUString> aRet = comphelper::sequenceToContainer<std::vector<OUString>>(
101 SvtPathOptions aOpt;
102 std::transform(aRet.begin(), aRet.end(), aRet.begin(),
103 [&aOpt](const OUString& rUrl) -> OUString { return aOpt.SubstituteVariable( rUrl ); });
104 return aRet;
105}
106
107void SetSecureURLs( std::vector< OUString >&& urlList )
108{
109// DBG_ASSERT(!officecfg::SecureURL::isReadOnly(), "SvtSecurityOptions_Impl::SetSecureURLs()\nYou tried to write on a readonly value!\n");
110// if (officecfg::SecureURL::isReadOnly())
111// return;
112 std::vector< OUString > lURLs( std::move(urlList) );
113 SvtPathOptions aOpt;
114 std::transform(lURLs.begin(), lURLs.end(), lURLs.begin(),
115 [&aOpt](const OUString& rUrl) -> OUString { return aOpt.UseVariable( rUrl ); });
116 std::shared_ptr<comphelper::ConfigurationChanges> xChanges = comphelper::ConfigurationChanges::create();
117 officecfg::Office::Common::Security::Scripting::SecureURL::set(comphelper::containerToSequence(lURLs), xChanges);
118 xChanges->commit();
119}
120
122 OUString const & uri, OUString const & referer)
123{
124 switch (INetURLObject(uri).GetProtocol()) {
125 case INetProtocol::Macro:
126 if (uri.startsWithIgnoreAsciiCase("macro:///")) {
127 // Denotes an App-BASIC macro (see SfxMacroLoader::loadMacro), which
128 // is considered safe:
129 return true;
130 }
131 [[fallthrough]];
132 case INetProtocol::Slot:
133 return referer.equalsIgnoreAsciiCase("private:user")
134 || isTrustedLocationUri(referer);
135 default:
136 return true;
137 }
138}
139
140bool isUntrustedReferer(OUString const & referer)
141{
143 && !(referer.isEmpty() || referer.startsWithIgnoreAsciiCase("private:")
144 || isTrustedLocationUri(referer));
145}
146
147bool isTrustedLocationUri(OUString const & uri)
148{
149 for (const auto & url : GetSecureURLs())
150 {
152 {
153 return true;
154 }
155 }
156 return false;
157}
158
159bool isTrustedLocationUriForUpdatingLinks(OUString const & uri)
160{
161 return GetMacroSecurityLevel() == 0 || uri.isEmpty()
162 || uri.startsWithIgnoreAsciiCase("private:")
163 || isTrustedLocationUri(uri);
164}
165
167{
169}
170
171void SetMacroSecurityLevel( sal_Int32 _nLevel )
172{
173 if (utl::ConfigManager::IsFuzzing() || officecfg::Office::Common::Security::Scripting::MacroSecurityLevel::isReadOnly())
174 return;
175
176 if( _nLevel > 3 || _nLevel < 0 )
177 _nLevel = 3;
178
179 std::shared_ptr<comphelper::ConfigurationChanges> xChanges = comphelper::ConfigurationChanges::create();
180 officecfg::Office::Common::Security::Scripting::MacroSecurityLevel::set(_nLevel, xChanges);
181 xChanges->commit();
182}
183
185{
187}
188
189std::vector< SvtSecurityOptions::Certificate > GetTrustedAuthors()
190{
191 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Security/Scripting");
193 sal_Int32 c1 = lAuthors.getLength();
194 if( !c1 )
195 return {};
196
197 sal_Int32 c2 = c1 * 3; // 3 Properties inside Struct TrustedAuthor
198 Sequence< OUString > lAllAuthors( c2 );
199 auto plAllAuthors = lAllAuthors.getArray();
200 sal_Int32 i2 = 0;
201 OUString aSep( "/" );
202 for( const auto& rAuthor : lAuthors )
203 {
204 plAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME;
205 ++i2;
206 plAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER;
207 ++i2;
208 plAllAuthors[ i2 ] = PROPERTYNAME_MACRO_TRUSTEDAUTHORS + aSep + rAuthor + aSep + PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA;
209 ++i2;
210 }
211
212 Sequence< Any > lValues = utl::ConfigItem::GetProperties( xHierarchyAccess, lAllAuthors, /*bAllLocales*/false );
213 if( lValues.getLength() != c2 )
214 return {};
215
216 std::vector< SvtSecurityOptions::Certificate > aTrustedAuthors;
218 i2 = 0;
219 for( sal_Int32 i1 = 0; i1 < c1; ++i1 )
220 {
221 lValues[ i2 ] >>= aCert.SubjectName;
222 ++i2;
223 lValues[ i2 ] >>= aCert.SerialNumber;
224 ++i2;
225 lValues[ i2 ] >>= aCert.RawData;
226 ++i2;
227 // Filter out TrustedAuthor entries with empty RawData, which
228 // would cause an unexpected std::bad_alloc in
229 // SecurityEnvironment_NssImpl::createCertificateFromAscii and
230 // have been observed in the wild (fdo#55019):
231 if( !aCert.RawData.isEmpty() )
232 {
233 aTrustedAuthors.push_back( aCert );
234 }
235 }
236 return aTrustedAuthors;
237}
238
239void SetTrustedAuthors( const std::vector< Certificate >& rAuthors )
240{
241// DBG_ASSERT(!m_bROTrustedAuthors, "SvtSecurityOptions_Impl::SetTrustedAuthors()\nYou tried to write on a readonly value!\n");
242// if( m_bROTrustedAuthors )
243// return;
244
245 Reference<css::container::XHierarchicalNameAccess> xHierarchyAccess = utl::ConfigManager::acquireTree(u"Office.Common/Security/Scripting");
246 sal_Int32 nCnt = rAuthors.size();
247 for( sal_Int32 i = 0; i < nCnt; ++i )
248 {
249 OUString aPrefix(
251 + OUString::number(i) + "/");
252 Sequence< css::beans::PropertyValue > lPropertyValues{
254 rAuthors[i].SubjectName),
256 rAuthors[i].SerialNumber),
258 rAuthors[i].RawData)
259 };
260
261 utl::ConfigItem::SetSetProperties( xHierarchyAccess, PROPERTYNAME_MACRO_TRUSTEDAUTHORS, lPropertyValues );
262 }
263}
264
265bool IsOptionSet( EOption eOption )
266{
268 return false;
269 bool bSet = false;
270 switch(eOption)
271 {
274 break;
277 break;
280 break;
283 break;
286 break;
289 break;
292 break;
295 break;
296
297 default:
298 assert(false);
299 }
300
301 return bSet;
302}
303
304void SetOption( EOption eOption, bool bValue )
305{
306 std::shared_ptr<comphelper::ConfigurationChanges> xChanges = comphelper::ConfigurationChanges::create();
307 switch(eOption)
308 {
310 officecfg::Office::Common::Security::Scripting::WarnSaveOrSendDoc::set(bValue, xChanges);
311 break;
313 officecfg::Office::Common::Security::Scripting::WarnSignDoc::set(bValue, xChanges);
314 break;
316 officecfg::Office::Common::Security::Scripting::WarnPrintDoc::set(bValue, xChanges);
317 break;
319 officecfg::Office::Common::Security::Scripting::WarnCreatePDF::set(bValue, xChanges);
320 break;
322 officecfg::Office::Common::Security::Scripting::RemovePersonalInfoOnSaving::set(bValue, xChanges);
323 break;
325 officecfg::Office::Common::Security::Scripting::RecommendPasswordProtection::set(bValue, xChanges);
326 break;
328 officecfg::Office::Common::Security::Scripting::HyperlinksWithCtrlClick::set(bValue, xChanges);
329 break;
331 officecfg::Office::Common::Security::Scripting::BlockUntrustedRefererLinks::set(bValue, xChanges);
332 break;
333
334 default:
335 assert(false);
336 }
337 xChanges->commit();
338}
339
340} // namespace SvtSecurityOptions
341
342
343// map personal info strings to 1, 2, ... to remove personal info
344size_t SvtSecurityMapPersonalInfo::GetInfoID( const OUString sPersonalInfo )
345{
346 auto aIter = aInfoIDs.find( sPersonalInfo );
347 if ( aIter == aInfoIDs.end() )
348 {
349 size_t nNewID = aInfoIDs.size() + 1;
350 aInfoIDs[sPersonalInfo] = nNewID;
351 return nNewID;
352 }
353
354 return aIter->second;
355}
356
357/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unordered_map< OUString, size_t > aInfoIDs
size_t GetInfoID(const OUString sPersonalInfo)
static std::shared_ptr< ConfigurationChanges > create()
css::uno::Sequence< css::uno::Any > GetProperties(const css::uno::Sequence< OUString > &rNames)
bool SetSetProperties(const OUString &rNode, const css::uno::Sequence< css::beans::PropertyValue > &rValues)
css::uno::Sequence< OUString > GetNodeNames(const OUString &rNode)
Definition: configitem.cxx:685
static SAL_DLLPRIVATE css::uno::Reference< css::container::XHierarchicalNameAccess > acquireTree(utl::ConfigItem const &item)
Definition: configmgr.cxx:117
static bool IsFuzzing()
Definition: configmgr.cxx:181
float u
void SetMacroSecurityLevel(sal_Int32 _nLevel)
bool isTrustedLocationUri(OUString const &uri)
Check whether the given uri is a trusted location.
bool IsReadOnly(EOption eOption)
void SetSecureURLs(std::vector< OUString > &&urlList)
void SetTrustedAuthors(const std::vector< Certificate > &rAuthors)
sal_Int32 GetMacroSecurityLevel()
bool isSecureMacroUri(OUString const &uri, OUString const &referer)
Check whether the given uri is either no dangerous macro-execution URI at all or else the given refer...
bool isTrustedLocationUriForUpdatingLinks(OUString const &uri)
std::vector< SvtSecurityOptions::Certificate > GetTrustedAuthors()
bool isUntrustedReferer(OUString const &referer)
Check whether the given referer URI is untrusted, and links originating from it should not be accesse...
void SetOption(EOption eOption, bool bValue)
std::vector< OUString > GetSecureURLs()
bool IsOptionSet(EOption eOption)
OUString get(TranslateId sContextAndId, const std::locale &loc)
Definition: resmgr.cxx:211
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
int i
UNOTOOLS_DLLPUBLIC bool IsSubPath(OUString const &parent, OUString const &child)
Definition: ucbhelper.cxx:326
constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_RAWDATA
constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_SERIALNUMBER
constexpr OUStringLiteral PROPERTYNAME_TRUSTEDAUTHOR_SUBJECTNAME
constexpr OUStringLiteral PROPERTYNAME_MACRO_TRUSTEDAUTHORS