-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSourcePolicy.cpp
More file actions
234 lines (202 loc) · 10.9 KB
/
SourcePolicy.cpp
File metadata and controls
234 lines (202 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "SourcePolicy.h"
#include "Microsoft/PreIndexedPackageSourceFactory.h"
#include "Rest/RestSourceFactory.h"
using namespace AppInstaller::Settings;
namespace AppInstaller::Repository
{
namespace
{
// Checks whether a default source is enabled with the current settings.
// onlyExplicit determines whether we consider the not-configured state to be enabled or not.
bool IsDefaultSourceEnabled(WellKnownSource sourceToLog, ExperimentalFeature::Feature feature, bool onlyExplicit, TogglePolicy::Policy policy)
{
if (!ExperimentalFeature::IsEnabled(feature))
{
// No need to log here
return false;
}
if (onlyExplicit)
{
// No need to log here
return GroupPolicies().GetState(policy) == PolicyState::Enabled;
}
if (!GroupPolicies().IsEnabled(policy))
{
AICLI_LOG(Repo, Info, << "The default source " << GetWellKnownSourceName(sourceToLog) << " is disabled due to Group Policy");
return false;
}
return true;
}
template<ValuePolicy P>
std::optional<SourceFromPolicy> FindSourceInPolicy(std::string_view name, std::string_view type, std::string_view arg)
{
auto sourcesOpt = GroupPolicies().GetValueRef<P>();
if (!sourcesOpt.has_value())
{
return std::nullopt;
}
const auto& sources = sourcesOpt->get();
auto source = std::find_if(
sources.begin(),
sources.end(),
[&](const SourceFromPolicy& policySource)
{
return Utility::ICUCaseInsensitiveEquals(name, policySource.Name) && Utility::ICUCaseInsensitiveEquals(type, policySource.Type) && arg == policySource.Arg;
});
if (source == sources.end())
{
return std::nullopt;
}
return *source;
}
template<ValuePolicy P>
bool IsSourceInPolicy(std::string_view name, std::string_view type, std::string_view arg)
{
return FindSourceInPolicy<P>(name, type, arg).has_value();
}
}
// Checks whether the Group Policy allows this user source.
// If it does it returns None, otherwise it returns which policy is blocking it.
// Note that this applies to user sources that are being added as well as user sources
// that already existed when the Group Policy came into effect.
TogglePolicy::Policy GetPolicyBlockingUserSource(std::string_view name, std::string_view type, std::string_view arg, bool isTombstone)
{
// Reasons for not allowing:
// 1. The source is a tombstone for default source that is explicitly enabled
// 2. The source is a default source that is disabled
// 3. The source has the same name as a default source that is explicitly enabled (to prevent shadowing)
// 4. Allowed sources are disabled, blocking all user sources
// 5. There is an explicit list of allowed sources and this source is not in it
//
// We don't need to check sources added by policy as those have higher priority.
//
// Use the name and arg to match sources as we don't have the identifier before adding.
// Case 1:
// The source is a tombstone and we need the policy to be explicitly enabled.
if (isTombstone)
{
if (name == GetWellKnownSourceName(WellKnownSource::WinGet) && IsWellKnownSourceEnabled(WellKnownSource::WinGet, true))
{
return TogglePolicy::Policy::DefaultSource;
}
if (name == GetWellKnownSourceName(WellKnownSource::MicrosoftStore) && IsWellKnownSourceEnabled(WellKnownSource::MicrosoftStore, true))
{
return TogglePolicy::Policy::MSStoreSource;
}
if (name == GetWellKnownSourceName(WellKnownSource::WinGetFont) && IsWellKnownSourceEnabled(WellKnownSource::WinGetFont, true))
{
return TogglePolicy::Policy::FontSource;
}
// Any other tombstone is allowed
return TogglePolicy::Policy::None;
}
// Case 2:
// - The source is not a tombstone and we don't need the policy to be explicitly enabled.
// - Check only against the source argument and type as the user source may have a different name.
// - Do a case-insensitive check as the domain portion of the URL is case-insensitive,
// and we don't need case sensitivity for the rest as we control the domain.
if (Utility::CaseInsensitiveEquals(arg, GetWellKnownSourceArg(WellKnownSource::WinGet)) &&
Utility::CaseInsensitiveEquals(type, Microsoft::PreIndexedPackageSourceFactory::Type()))
{
return IsWellKnownSourceEnabled(WellKnownSource::WinGet) ? TogglePolicy::Policy::None : TogglePolicy::Policy::DefaultSource;
}
if (Utility::CaseInsensitiveEquals(arg, GetWellKnownSourceArg(WellKnownSource::MicrosoftStore)) &&
Utility::CaseInsensitiveEquals(type, Rest::RestSourceFactory::Type()))
{
return IsWellKnownSourceEnabled(WellKnownSource::MicrosoftStore) ? TogglePolicy::Policy::None : TogglePolicy::Policy::MSStoreSource;
}
if (Utility::CaseInsensitiveEquals(arg, GetWellKnownSourceArg(WellKnownSource::WinGetFont)) &&
Utility::CaseInsensitiveEquals(type, Microsoft::PreIndexedPackageSourceFactory::Type()))
{
return IsWellKnownSourceEnabled(WellKnownSource::WinGetFont) ? TogglePolicy::Policy::None : TogglePolicy::Policy::FontSource;
}
// Case 3:
// If the source has the same name as a default source, it is shadowing with a different argument
// (as it didn't match above). We only care if Group Policy requires the default source.
if (name == GetWellKnownSourceName(WellKnownSource::WinGet) && IsWellKnownSourceEnabled(WellKnownSource::WinGet, true))
{
AICLI_LOG(Repo, Warning, << "User source is not allowed as it shadows the default source. Name [" << name << "]. Arg [" << arg << "] Type [" << type << ']');
return TogglePolicy::Policy::DefaultSource;
}
if (name == GetWellKnownSourceName(WellKnownSource::MicrosoftStore) && IsWellKnownSourceEnabled(WellKnownSource::MicrosoftStore, true))
{
AICLI_LOG(Repo, Warning, << "User source is not allowed as it shadows a default MS Store source. Name [" << name << "]. Arg [" << arg << "] Type [" << type << ']');
return TogglePolicy::Policy::MSStoreSource;
}
if (name == GetWellKnownSourceName(WellKnownSource::WinGetFont) && IsWellKnownSourceEnabled(WellKnownSource::WinGetFont, true))
{
AICLI_LOG(Repo, Warning, << "User source is not allowed as it shadows a default font source. Name [" << name << "]. Arg [" << arg << "] Type [" << type << ']');
return TogglePolicy::Policy::FontSource;
}
// Case 4:
// The guard in the source add command should already block adding.
// This check drops existing user sources.
auto allowedSourcesPolicy = GroupPolicies().GetState(TogglePolicy::Policy::AllowedSources);
if (allowedSourcesPolicy == PolicyState::Disabled)
{
AICLI_LOG(Repo, Warning, << "User sources are disabled by Group Policy");
return TogglePolicy::Policy::AllowedSources;
}
// Case 5:
if (allowedSourcesPolicy == PolicyState::Enabled)
{
if (!IsSourceInPolicy<ValuePolicy::AllowedSources>(name, type, arg))
{
AICLI_LOG(Repo, Warning, << "Source is not in the Group Policy allowed list. Name [" << name << "]. Arg [" << arg << "] Type [" << type << ']');
return TogglePolicy::Policy::AllowedSources;
}
}
return TogglePolicy::Policy::None;
}
bool IsUserSourceAllowedByPolicy(std::string_view name, std::string_view type, std::string_view arg, bool isTombstone)
{
return GetPolicyBlockingUserSource(name, type, arg, isTombstone) == TogglePolicy::Policy::None;
}
bool IsWellKnownSourceEnabled(WellKnownSource source, bool onlyExplicit)
{
switch (source)
{
case AppInstaller::Repository::WellKnownSource::WinGet:
return IsDefaultSourceEnabled(source, ExperimentalFeature::Feature::None, onlyExplicit, TogglePolicy::Policy::DefaultSource);
case AppInstaller::Repository::WellKnownSource::MicrosoftStore:
return IsDefaultSourceEnabled(source, ExperimentalFeature::Feature::None, onlyExplicit, TogglePolicy::Policy::MSStoreSource);
case AppInstaller::Repository::WellKnownSource::WinGetFont:
return IsDefaultSourceEnabled(source, ExperimentalFeature::Feature::None, onlyExplicit, TogglePolicy::Policy::FontSource);
case AppInstaller::Repository::WellKnownSource::DesktopFrameworks:
// No corresponding policy available for this source.
return true;
}
return false;
}
void EnsureSourceIsRemovable(const SourceDetailsInternal& source)
{
// Block removing sources added by Group Policy
if (source.Origin == SourceOrigin::GroupPolicy)
{
AICLI_LOG(Repo, Error, << "Cannot remove source added by Group Policy");
throw GroupPolicyException(TogglePolicy::Policy::AdditionalSources);
}
// Block removing default sources required by Group Policy.
if (source.Origin == SourceOrigin::Default)
{
if (GroupPolicies().GetState(TogglePolicy::Policy::DefaultSource) == PolicyState::Enabled &&
source.Identifier == GetWellKnownSourceIdentifier(WellKnownSource::WinGet))
{
throw GroupPolicyException(TogglePolicy::Policy::DefaultSource);
}
if (GroupPolicies().GetState(TogglePolicy::Policy::MSStoreSource) == PolicyState::Enabled &&
source.Identifier == GetWellKnownSourceIdentifier(WellKnownSource::MicrosoftStore))
{
throw GroupPolicyException(TogglePolicy::Policy::MSStoreSource);
}
if (GroupPolicies().GetState(TogglePolicy::Policy::FontSource) == PolicyState::Enabled &&
source.Identifier == GetWellKnownSourceIdentifier(WellKnownSource::WinGetFont))
{
throw GroupPolicyException(TogglePolicy::Policy::FontSource);
}
}
}
}