-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSourceList.cpp
More file actions
1009 lines (877 loc) · 42.9 KB
/
SourceList.cpp
File metadata and controls
1009 lines (877 loc) · 42.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "SourceList.h"
#include "SourcePolicy.h"
#include "Microsoft/PreIndexedPackageSourceFactory.h"
#include "Rest/RestSourceFactory.h"
#include <winget/AdminSettings.h>
#include <winget/Certificates.h>
#include <CertificateResources.h>
using namespace AppInstaller::Settings;
using namespace std::string_view_literals;
namespace AppInstaller::Repository
{
namespace
{
constexpr std::string_view s_SourcesYaml_Sources = "Sources"sv;
constexpr std::string_view s_SourcesYaml_Source_Name = "Name"sv;
constexpr std::string_view s_SourcesYaml_Source_Type = "Type"sv;
constexpr std::string_view s_SourcesYaml_Source_Arg = "Arg"sv;
constexpr std::string_view s_SourcesYaml_Source_Data = "Data"sv;
constexpr std::string_view s_SourcesYaml_Source_Identifier = "Identifier"sv;
constexpr std::string_view s_SourcesYaml_Source_IsTombstone = "IsTombstone"sv;
constexpr std::string_view s_SourcesYaml_Source_IsOverride = "IsOverride"sv;
constexpr std::string_view s_SourcesYaml_Source_Explicit = "Explicit"sv;
constexpr std::string_view s_SourcesYaml_Source_TrustLevel = "TrustLevel"sv;
constexpr std::string_view s_SourcesYaml_Source_Priority = "Priority"sv;
constexpr std::string_view s_MetadataYaml_Sources = "Sources"sv;
constexpr std::string_view s_MetadataYaml_Source_Name = "Name"sv;
constexpr std::string_view s_MetadataYaml_Source_LastUpdate = "LastUpdate"sv;
constexpr std::string_view s_MetadataYaml_Source_DoNotUpdateBefore = "DoNotUpdateBefore"sv;
constexpr std::string_view s_MetadataYaml_Source_AcceptedAgreementsIdentifier = "AcceptedAgreementsIdentifier"sv;
constexpr std::string_view s_MetadataYaml_Source_AcceptedAgreementFields = "AcceptedAgreementFields"sv;
constexpr std::string_view s_Source_WingetCommunityDefault_Name = "winget"sv;
constexpr std::string_view s_Source_WingetCommunityDefault_Arg = "https://cdn.winget.microsoft.com/cache"sv;
constexpr std::string_view s_Source_WingetCommunityDefault_Data = "Microsoft.Winget.Source_8wekyb3d8bbwe"sv;
constexpr std::string_view s_Source_WingetCommunityDefault_Identifier = "Microsoft.Winget.Source_8wekyb3d8bbwe"sv;
constexpr std::string_view s_Source_MSStoreDefault_Name = "msstore"sv;
constexpr std::string_view s_Source_MSStoreDefault_Arg = "https://storeedgefd.dsx.mp.microsoft.com/v9.0"sv;
constexpr std::string_view s_Source_MSStoreDefault_Identifier = "StoreEdgeFD"sv;
constexpr std::string_view s_Source_DesktopFrameworks_Name = "microsoft.builtin.desktop.frameworks"sv;
constexpr std::string_view s_Source_DesktopFrameworks_Arg = "https://cdn.winget.microsoft.com/platform"sv;
constexpr std::string_view s_Source_DesktopFrameworks_Data = "Microsoft.Winget.Platform.Source_8wekyb3d8bbwe"sv;
constexpr std::string_view s_Source_DesktopFrameworks_Identifier = "Microsoft.Winget.Platform.Source_8wekyb3d8bbwe"sv;
constexpr std::string_view s_Source_WingetCommunityFont_Name = "winget-font"sv;
constexpr std::string_view s_Source_WingetCommunityFont_Arg = "https://cdn.winget.microsoft.com/fonts"sv;
constexpr std::string_view s_Source_WingetCommunityFont_Data = "Microsoft.Winget.Fonts.Source_8wekyb3d8bbwe"sv;
constexpr std::string_view s_Source_WingetCommunityFont_Identifier = "Microsoft.Winget.Fonts.Source_8wekyb3d8bbwe"sv;
// Attempts to read a single scalar value from the node.
template<typename Value>
bool TryReadScalar(std::string_view settingName, const std::string& settingValue, const YAML::Node& sourceNode, std::string_view name, Value& value, bool required = true)
{
YAML::Node valueNode = sourceNode[std::string{ name }];
if (!valueNode || !valueNode.IsScalar())
{
if (required)
{
AICLI_LOG(Repo, Error, << "Setting '" << settingName << "' did not contain the expected format (" << name << " is invalid within a source):\n" << settingValue);
}
return false;
}
value = valueNode.as<Value>();
return true;
}
// Attempts to read the source details from the given stream.
// Results are all or nothing; if any failures occur, no details are returned.
bool TryReadSourceDetails(
std::string_view settingName,
std::istream& stream,
std::string_view rootName,
std::function<bool(SourceDetailsInternal&, const std::string&, const YAML::Node&)> parse,
std::vector<SourceDetailsInternal>& sourceDetails)
{
std::vector<SourceDetailsInternal> result;
std::string settingValue = Utility::ReadEntireStream(stream);
YAML::Node document;
try
{
document = YAML::Load(settingValue);
}
catch (const std::exception& e)
{
AICLI_LOG(YAML, Error, << "Setting '" << settingName << "' contained invalid YAML (" << e.what() << "):\n" << settingValue);
return false;
}
try
{
YAML::Node sources = document[rootName];
if (!sources)
{
AICLI_LOG(Repo, Error, << "Setting '" << settingName << "' did not contain the expected format (missing " << rootName << "):\n" << settingValue);
return false;
}
if (sources.IsNull())
{
// An empty sources is an acceptable thing.
return true;
}
if (!sources.IsSequence())
{
AICLI_LOG(Repo, Error, << "Setting '" << settingName << "' did not contain the expected format (" << rootName << " was not a sequence):\n" << settingValue);
return false;
}
for (const auto& source : sources.Sequence())
{
SourceDetailsInternal details;
if (!parse(details, settingValue, source))
{
return false;
}
result.emplace_back(std::move(details));
}
}
catch (const std::exception& e)
{
AICLI_LOG(YAML, Error, << "Setting '" << settingName << "' contained unexpected YAML (" << e.what() << "):\n" << settingValue);
return false;
}
sourceDetails = std::move(result);
return true;
}
// Gets the source details from a particular setting, or an empty optional if no setting exists.
std::optional<std::vector<SourceDetailsInternal>> TryGetSourcesFromSetting(
Settings::Stream& setting,
std::string_view rootName,
std::function<bool(SourceDetailsInternal&, const std::string&, const YAML::Node&)> parse)
{
auto sourcesStream = setting.Get();
if (!sourcesStream)
{
// Note that this case is different than the one in which all sources have been removed.
return {};
}
else
{
std::vector<SourceDetailsInternal> result;
if (!TryReadSourceDetails(setting.GetName(), *sourcesStream, rootName, parse, result))
{
AICLI_LOG(YAML, Error, << "Ignoring corrupted source data.");
}
return result;
}
}
// Gets the source details from a particular setting.
std::vector<SourceDetailsInternal> GetSourcesFromSetting(
Settings::Stream& setting,
std::string_view rootName,
std::function<bool(SourceDetailsInternal&, const std::string&, const YAML::Node&)> parse)
{
return TryGetSourcesFromSetting(setting, rootName, parse).value_or(std::vector<SourceDetailsInternal>{});
}
// Sets the sources for a particular setting, from a particular origin.
[[nodiscard]] bool SetSourcesToSettingWithFilter(Settings::Stream& setting, SourceOrigin origin, const std::vector<SourceDetailsInternal>& sources)
{
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << s_SourcesYaml_Sources;
out << YAML::BeginSeq;
for (const auto& details : sources)
{
if (details.Origin == origin)
{
out << YAML::BeginMap;
out << YAML::Key << s_SourcesYaml_Source_Name << YAML::Value << details.Name;
out << YAML::Key << s_SourcesYaml_Source_Type << YAML::Value << details.Type;
out << YAML::Key << s_SourcesYaml_Source_Arg << YAML::Value << details.Arg;
out << YAML::Key << s_SourcesYaml_Source_Data << YAML::Value << details.Data;
out << YAML::Key << s_SourcesYaml_Source_Identifier << YAML::Value << details.Identifier;
out << YAML::Key << s_SourcesYaml_Source_IsTombstone << YAML::Value << details.IsTombstone;
out << YAML::Key << s_SourcesYaml_Source_IsOverride << YAML::Value << details.IsOverride;
out << YAML::Key << s_SourcesYaml_Source_Explicit << YAML::Value << details.Explicit;
out << YAML::Key << s_SourcesYaml_Source_TrustLevel << YAML::Value << static_cast<int64_t>(details.TrustLevel);
out << YAML::Key << s_SourcesYaml_Source_Priority << YAML::Value << details.Priority;
out << YAML::EndMap;
}
}
out << YAML::EndSeq;
out << YAML::EndMap;
return setting.Set(out.str());
}
// Assumes that names match already
bool DoSourceDetailsInternalMatch(const SourceDetailsInternal& left, const SourceDetailsInternal& right)
{
return left.Arg == right.Arg &&
left.Identifier == right.Identifier &&
Utility::CaseInsensitiveEquals(left.Type, right.Type);
}
bool ShouldBeHidden(const SourceDetailsInternal& details)
{
return details.IsTombstone || details.Origin == SourceOrigin::Metadata || !details.IsVisible;
}
}
void SourceDetailsInternal::CopyMetadataFieldsTo(SourceDetailsInternal& target)
{
if (LastUpdateTime > target.LastUpdateTime)
{
target.LastUpdateTime = LastUpdateTime;
}
if (DoNotUpdateBefore > target.DoNotUpdateBefore)
{
target.DoNotUpdateBefore = DoNotUpdateBefore;
}
target.AcceptedAgreementFields = AcceptedAgreementFields;
target.AcceptedAgreementsIdentifier = AcceptedAgreementsIdentifier;
}
void SourceDetailsInternal::CopyMetadataFieldsFrom(const SourceDetails& source)
{
LastUpdateTime = source.LastUpdateTime;
DoNotUpdateBefore = source.DoNotUpdateBefore;
}
void SourceDetailsInternal::CopyOverrideFieldsFrom(const SourceDetails& overrideSource)
{
// These are the supported Override fields.
Explicit = overrideSource.Explicit;
Priority = overrideSource.Priority;
}
bool SourceDetailsInternal::operator<(const SourceDetailsInternal& other) const
{
// Higher values come first in ordering and must be "less than" for standard sorting
return Priority > other.Priority;
}
std::string_view GetWellKnownSourceName(WellKnownSource source)
{
switch (source)
{
case WellKnownSource::WinGet:
return s_Source_WingetCommunityDefault_Name;
case WellKnownSource::MicrosoftStore:
return s_Source_MSStoreDefault_Name;
case WellKnownSource::DesktopFrameworks:
return s_Source_DesktopFrameworks_Name;
case WellKnownSource::WinGetFont:
return s_Source_WingetCommunityFont_Name;
}
return {};
}
std::string_view GetWellKnownSourceArg(WellKnownSource source)
{
switch (source)
{
case WellKnownSource::WinGet:
return s_Source_WingetCommunityDefault_Arg;
case WellKnownSource::MicrosoftStore:
return s_Source_MSStoreDefault_Arg;
case WellKnownSource::DesktopFrameworks:
return s_Source_DesktopFrameworks_Arg;
case WellKnownSource::WinGetFont:
return s_Source_WingetCommunityFont_Arg;
}
return {};
}
std::string_view GetWellKnownSourceIdentifier(WellKnownSource source)
{
switch (source)
{
case WellKnownSource::WinGet:
return s_Source_WingetCommunityDefault_Identifier;
case WellKnownSource::MicrosoftStore:
return s_Source_MSStoreDefault_Identifier;
case WellKnownSource::DesktopFrameworks:
return s_Source_DesktopFrameworks_Identifier;
case WellKnownSource::WinGetFont:
return s_Source_WingetCommunityFont_Identifier;
}
return {};
}
std::optional<WellKnownSource> CheckForWellKnownSourceMatch(std::string_view name, std::string_view arg, std::string_view type)
{
if (name == s_Source_WingetCommunityDefault_Name && arg == s_Source_WingetCommunityDefault_Arg && type == Microsoft::PreIndexedPackageSourceFactory::Type())
{
return WellKnownSource::WinGet;
}
if (name == s_Source_MSStoreDefault_Name && arg == s_Source_MSStoreDefault_Arg && type == Rest::RestSourceFactory::Type())
{
return WellKnownSource::MicrosoftStore;
}
if (name == s_Source_DesktopFrameworks_Name && arg == s_Source_DesktopFrameworks_Arg && type == Microsoft::PreIndexedPackageSourceFactory::Type())
{
return WellKnownSource::DesktopFrameworks;
}
if (name == s_Source_WingetCommunityFont_Name && arg == s_Source_WingetCommunityFont_Arg && type == Rest::RestSourceFactory::Type())
{
return WellKnownSource::WinGetFont;
}
return {};
}
SourceDetailsInternal GetWellKnownSourceDetailsInternal(WellKnownSource source)
{
switch (source)
{
case WellKnownSource::WinGet:
{
SourceDetailsInternal details;
details.Origin = SourceOrigin::Default;
details.Name = s_Source_WingetCommunityDefault_Name;
details.Type = Microsoft::PreIndexedPackageSourceFactory::Type();
details.Arg = s_Source_WingetCommunityDefault_Arg;
details.Data = s_Source_WingetCommunityDefault_Data;
details.Identifier = s_Source_WingetCommunityDefault_Identifier;
details.TrustLevel = SourceTrustLevel::Trusted | SourceTrustLevel::StoreOrigin;
return details;
}
case WellKnownSource::MicrosoftStore:
{
SourceDetailsInternal details;
details.Origin = SourceOrigin::Default;
details.Name = s_Source_MSStoreDefault_Name;
details.Type = Rest::RestSourceFactory::Type();
details.Arg = s_Source_MSStoreDefault_Arg;
details.Identifier = s_Source_MSStoreDefault_Identifier;
details.TrustLevel = SourceTrustLevel::Trusted;
details.SupportInstalledSearchCorrelation = false;
if (!Settings::IsAdminSettingEnabled(Settings::BoolAdminSetting::BypassCertificatePinningForMicrosoftStore))
{
using namespace AppInstaller::Certificates;
PinningChain chain;
auto chainElement = chain.Root();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_ROOT_1, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::PublicKey);
chainElement = chainElement.Next();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_INTERMEDIATE_1, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
chainElement = chainElement.Next();
chainElement->LoadCertificate(IDX_CERTIFICATE_STORE_LEAF_1, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
PinningChain chain2;
auto chainElement2 = chain2.Root();
chainElement2->LoadCertificate(IDX_CERTIFICATE_STORE_ROOT_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::PublicKey);
chainElement2 = chainElement2.Next();
chainElement2->LoadCertificate(IDX_CERTIFICATE_STORE_INTERMEDIATE_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
chainElement2 = chainElement2.Next();
chainElement2->LoadCertificate(IDX_CERTIFICATE_STORE_LEAF_2, CERTIFICATE_RESOURCE_TYPE).SetPinning(PinningVerificationType::Subject | PinningVerificationType::Issuer);
// See https://aka.ms/AzureTLSCAs (internal) for the source of these CAs
PinningChain chain3;
chain3.PartialChain().Root()->
LoadCertificate(IDX_CERTIFICATE_MS_TLS_ECC_ROOT_G2, CERTIFICATE_RESOURCE_TYPE).
SetPinning(PinningVerificationType::PublicKey | PinningVerificationType::AnyIssuer | PinningVerificationType::RequireNonLeaf);
PinningChain chain4;
chain4.PartialChain().Root()->
LoadCertificate(IDX_CERTIFICATE_MS_TLS_RSA_ROOT_G2, CERTIFICATE_RESOURCE_TYPE).
SetPinning(PinningVerificationType::PublicKey | PinningVerificationType::AnyIssuer | PinningVerificationType::RequireNonLeaf);
details.CertificatePinningConfiguration = PinningConfiguration("Microsoft Store Source");
details.CertificatePinningConfiguration.AddChain(std::move(chain));
details.CertificatePinningConfiguration.AddChain(std::move(chain2));
details.CertificatePinningConfiguration.AddChain(std::move(chain3));
details.CertificatePinningConfiguration.AddChain(std::move(chain4));
}
return details;
}
case WellKnownSource::DesktopFrameworks:
{
SourceDetailsInternal details;
details.Origin = SourceOrigin::Default;
details.Name = s_Source_DesktopFrameworks_Name;
details.Type = Microsoft::PreIndexedPackageSourceFactory::Type();
details.Arg = s_Source_DesktopFrameworks_Arg;
details.Data = s_Source_DesktopFrameworks_Data;
details.Identifier = s_Source_DesktopFrameworks_Identifier;
details.TrustLevel = SourceTrustLevel::Trusted | SourceTrustLevel::StoreOrigin;
details.IsVisible = false;
return details;
}
case WellKnownSource::WinGetFont:
{
SourceDetailsInternal details;
details.Origin = SourceOrigin::Default;
details.Name = s_Source_WingetCommunityFont_Name;
details.Type = Microsoft::PreIndexedPackageSourceFactory::Type();
details.Arg = s_Source_WingetCommunityFont_Arg;
details.Data = s_Source_WingetCommunityFont_Data;
details.Identifier = s_Source_WingetCommunityFont_Identifier;
details.TrustLevel = SourceTrustLevel::Trusted | SourceTrustLevel::StoreOrigin;
details.Explicit = true;
return details;
}
}
THROW_HR(E_UNEXPECTED);
}
SourceList::SourceList() : m_userSourcesStream(Stream::UserSources), m_metadataStream(Stream::SourcesMetadata)
{
OverwriteSourceList();
OverwriteMetadata();
}
std::vector<std::reference_wrapper<SourceDetailsInternal>> SourceList::GetCurrentSourceRefs()
{
std::vector<std::reference_wrapper<SourceDetailsInternal>> result;
for (auto& s : m_sourceList)
{
if (!ShouldBeHidden(s))
{
result.emplace_back(std::ref(s));
}
else
{
AICLI_LOG(Repo, Verbose, << "GetCurrentSourceRefs: Source named '" << s.Name << "' from origin " << ToString(s.Origin) << " is hidden and is dropped.");
}
}
return result;
}
auto SourceList::FindSource(std::string_view name, bool includeHidden)
{
return std::find_if(m_sourceList.begin(), m_sourceList.end(),
[name, includeHidden](const SourceDetailsInternal& sd)
{
return Utility::ICUCaseInsensitiveEquals(sd.Name, name) &&
(includeHidden || !ShouldBeHidden(sd));
});
}
bool SourceList::TryFindSourceByOrigin(std::string_view name, SourceOrigin origin, SourceDetailsInternal& targetSourceOut, bool includeHidden)
{
auto defaultSources = GetSourcesByOrigin(origin);
auto iter = std::find_if(defaultSources.begin(), defaultSources.end(),
[name, includeHidden](const SourceDetailsInternal& sd)
{
return Utility::ICUCaseInsensitiveEquals(sd.Name, name) &&
(includeHidden || !ShouldBeHidden(sd));
});
if (iter == defaultSources.end())
{
return false;
}
targetSourceOut = (*iter);
return true;
}
SourceDetailsInternal* SourceList::GetCurrentSource(std::string_view name)
{
auto itr = FindSource(name);
return itr == m_sourceList.end() ? nullptr : &(*itr);
}
SourceDetailsInternal* SourceList::GetSource(std::string_view name)
{
auto itr = FindSource(name, true);
return itr == m_sourceList.end() ? nullptr : &(*itr);
}
void SourceList::AddSource(const SourceDetailsInternal& details)
{
bool sourcesSet = false;
for (size_t i = 0; !sourcesSet && i < 10; ++i)
{
auto itr = FindSource(details.Name, true);
THROW_HR_IF(APPINSTALLER_CLI_ERROR_SOURCE_NAME_ALREADY_EXISTS,
itr != m_sourceList.end() && itr->Origin != SourceOrigin::Metadata && !itr->IsTombstone);
// Erase the source's entry if applicable
if (itr != m_sourceList.end())
{
m_sourceList.erase(itr);
}
m_sourceList.emplace_back(details);
sourcesSet = SetSourcesByOrigin(SourceOrigin::User, m_sourceList);
if (!sourcesSet)
{
OverwriteSourceList();
OverwriteMetadata();
}
}
THROW_HR_IF_MSG(E_UNEXPECTED, !sourcesSet, "Too many attempts at SetSourcesByOrigin");
SaveMetadataInternal(details);
}
void SourceList::RemoveSource(const SourceDetailsInternal& detailsRef)
{
// Copy the incoming details because we might destroy the referenced structure
// when reloading the source details from settings.
SourceDetailsInternal details = detailsRef;
bool sourcesSet = false;
for (size_t i = 0; !sourcesSet && i < 10; ++i)
{
switch (details.Origin)
{
case SourceOrigin::Default:
{
auto target = FindSource(details.Name, true);
if (target == m_sourceList.end())
{
THROW_HR_MSG(E_UNEXPECTED, "Default source not in SourceList");
}
if (!target->IsTombstone)
{
SourceDetailsInternal tombstone;
tombstone.Name = details.Name;
tombstone.IsTombstone = true;
tombstone.Origin = SourceOrigin::User;
m_sourceList.emplace_back(std::move(tombstone));
}
}
break;
case SourceOrigin::User:
{
auto target = FindSource(details.Name);
if (target == m_sourceList.end())
{
// Assumed that an update to the sources removed it first
return;
}
// If this is an override of a default source, turn this into a tombstone instead of removing it.
if (target->IsOverride)
{
target->IsOverride = false;
target->IsTombstone = true;
break;
}
m_sourceList.erase(target);
}
break;
case SourceOrigin::GroupPolicy:
// This should have already been blocked higher up.
AICLI_LOG(Repo, Error, << "Attempting to remove Group Policy source: " << details.Name);
THROW_HR(E_UNEXPECTED);
default:
THROW_HR(E_UNEXPECTED);
}
sourcesSet = SetSourcesByOrigin(SourceOrigin::User, m_sourceList);
if (!sourcesSet)
{
OverwriteSourceList();
OverwriteMetadata();
}
}
THROW_HR_IF_MSG(E_UNEXPECTED, !sourcesSet, "Too many attempts at SetSourcesByOrigin");
SaveMetadataInternal(details, true);
}
void SourceList::EditSource(const SourceDetailsInternal& detailsRef)
{
// Copy the incoming details because we might destroy the referenced structure
// when reloading the source details from settings.
SourceDetailsInternal details = detailsRef;
bool sourcesSet = false;
for (size_t i = 0; !sourcesSet && i < 10; ++i)
{
switch (details.Origin)
{
case SourceOrigin::Default:
{
auto target = FindSource(details.Name, true);
if (target == m_sourceList.end())
{
THROW_HR_MSG(E_UNEXPECTED, "Default source not in SourceList");
}
if (!target->IsTombstone)
{
// Copy the original and then apply the override fields.
SourceDetailsInternal override = *target;
override.Origin = SourceOrigin::User;
override.IsOverride = true;
override.CopyOverrideFieldsFrom(details);
m_sourceList.emplace_back(std::move(override));
}
}
break;
case SourceOrigin::User:
{
auto target = FindSource(details.Name);
if (target == m_sourceList.end())
{
// Assumed that an update to the sources removed it first
return;
}
// Editing a User Source is just replacing the fields that can be edited.
target->CopyOverrideFieldsFrom(details);
}
break;
case SourceOrigin::GroupPolicy:
// This should have already been blocked higher up.
AICLI_LOG(Repo, Error, << "Attempting to edit a Group Policy source: " << details.Name);
THROW_HR(E_UNEXPECTED);
default:
THROW_HR(E_UNEXPECTED);
}
sourcesSet = SetSourcesByOrigin(SourceOrigin::User, m_sourceList);
if (!sourcesSet)
{
OverwriteSourceList();
OverwriteMetadata();
}
}
THROW_HR_IF_MSG(E_UNEXPECTED, !sourcesSet, "Too many attempts at SetSourcesByOrigin");
SaveMetadataInternal(details, true);
}
void SourceList::SaveMetadata(const SourceDetailsInternal& details)
{
SaveMetadataInternal(details);
}
bool SourceList::CheckSourceAgreements(std::string_view sourceName, std::string_view agreementsIdentifier, ImplicitAgreementFieldEnum agreementFields)
{
if (agreementFields == ImplicitAgreementFieldEnum::None && agreementsIdentifier.empty())
{
// No agreements to be accepted.
return true;
}
auto detailsInternal = GetCurrentSource(sourceName);
if (!detailsInternal)
{
// Source not found.
return false;
}
return static_cast<int>(agreementFields) == detailsInternal->AcceptedAgreementFields &&
agreementsIdentifier == detailsInternal->AcceptedAgreementsIdentifier;
}
void SourceList::SaveAcceptedSourceAgreements(std::string_view sourceName, std::string_view agreementsIdentifier, ImplicitAgreementFieldEnum agreementFields)
{
if (agreementFields == ImplicitAgreementFieldEnum::None && agreementsIdentifier.empty())
{
// No agreements to be accepted.
return;
}
auto detailsInternal = GetCurrentSource(sourceName);
if (!detailsInternal)
{
// No source to update.
return;
}
detailsInternal->AcceptedAgreementFields = static_cast<int>(agreementFields);
detailsInternal->AcceptedAgreementsIdentifier = agreementsIdentifier;
SaveMetadataInternal(*detailsInternal);
}
void SourceList::RemoveSettingsStreams()
{
Stream{ Stream::UserSources }.Remove();
Stream{ Stream::SourcesMetadata }.Remove();
}
void SourceList::OverwriteSourceList()
{
m_sourceList.clear();
for (SourceOrigin origin : { SourceOrigin::GroupPolicy, SourceOrigin::User, SourceOrigin::Default })
{
auto forOrigin = GetSourcesByOrigin(origin);
for (auto&& source : forOrigin)
{
auto foundSource = GetSource(source.Name);
if (!foundSource)
{
// Name not already defined, add it
m_sourceList.emplace_back(std::move(source));
}
else
{
AICLI_LOG(Repo, Info, << "Source named '" << foundSource->Name << "' is already defined at origin " << ToString(foundSource->Origin) <<
". The source from origin " << ToString(origin) << " is dropped.");
}
}
}
if (ExperimentalFeature::IsEnabled(ExperimentalFeature::Feature::SourcePriority))
{
std::stable_sort(m_sourceList.begin(), m_sourceList.end());
}
}
void SourceList::OverwriteMetadata()
{
auto metadata = GetMetadata();
for (auto& metaSource : metadata)
{
auto source = GetSource(metaSource.Name);
if (source)
{
metaSource.CopyMetadataFieldsTo(*source);
}
else
{
m_sourceList.emplace_back(std::move(metaSource));
}
}
}
// Gets the sources from a particular origin.
std::vector<SourceDetailsInternal> SourceList::GetSourcesByOrigin(SourceOrigin origin)
{
std::vector<SourceDetailsInternal> result;
switch (origin)
{
case SourceOrigin::Default:
{
if (IsWellKnownSourceEnabled(WellKnownSource::MicrosoftStore))
{
result.emplace_back(GetWellKnownSourceDetailsInternal(WellKnownSource::MicrosoftStore));
}
if (IsWellKnownSourceEnabled(WellKnownSource::WinGet))
{
result.emplace_back(GetWellKnownSourceDetailsInternal(WellKnownSource::WinGet));
}
if (IsWellKnownSourceEnabled(WellKnownSource::WinGetFont))
{
result.emplace_back(GetWellKnownSourceDetailsInternal(WellKnownSource::WinGetFont));
}
// Since the source is not visible outside, this is added just to have the source in the internal
// list for tracking updates. Thus there is no need to check a policy.
result.emplace_back(GetWellKnownSourceDetailsInternal(WellKnownSource::DesktopFrameworks));
}
break;
case SourceOrigin::User:
{
std::vector<SourceDetailsInternal> userSources = GetSourcesFromSetting(
m_userSourcesStream,
s_SourcesYaml_Sources,
[&](SourceDetailsInternal& details, const std::string& settingValue, const YAML::Node& source)
{
std::string_view name = m_userSourcesStream.GetName();
if (!TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Name, details.Name)) { return false; }
if (!TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Type, details.Type)) { return false; }
if (!TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Arg, details.Arg)) { return false; }
if (!TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Data, details.Data)) { return false; }
if (!TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_IsTombstone, details.IsTombstone)) { return false; }
TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Explicit, details.Explicit, false);
TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Identifier, details.Identifier, false);
TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_IsOverride, details.IsOverride, false);
TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_Priority, details.Priority, false);
int64_t trustLevelValue;
if (TryReadScalar(name, settingValue, source, s_SourcesYaml_Source_TrustLevel, trustLevelValue, false))
{
details.TrustLevel = static_cast<Repository::SourceTrustLevel>(trustLevelValue);
}
return true;
});
for (auto& source : userSources)
{
// Check source against list of allowed sources and drop tombstones for required sources
if (!IsUserSourceAllowedByPolicy(source.Name, source.Type, source.Arg, source.IsTombstone))
{
AICLI_LOG(Repo, Warning, << "User source " << source.Name << " dropped because of group policy");
continue;
}
// If this is an override source, we need to get the target of the override and apply the override data on top of it.
if (source.IsOverride)
{
SourceDetailsInternal override;
if (!TryFindSourceByOrigin(source.Name, SourceOrigin::Default, override))
{
// The default source may be disabled, in which case it may not be returned in the list of default sources.
AICLI_LOG(Repo, Warning, << "User source " << source.Name << " is an override for a nonexistent Default Source.");
continue;
}
override.CopyOverrideFieldsFrom(source);
override.Origin = SourceOrigin::User;
override.IsOverride = true;
result.emplace_back(std::move(override));
AICLI_LOG(Repo, Info, << "User source " << source.Name << " is overriding the Default source of the same name.");
continue;
}
result.emplace_back(std::move(source));
}
}
break;
case SourceOrigin::GroupPolicy:
{
if (GroupPolicies().GetState(TogglePolicy::Policy::AdditionalSources) == PolicyState::Enabled)
{
AICLI_LOG(Repo, Verbose, << "Additional sources GP is enabled...");
auto additionalSourcesOpt = GroupPolicies().GetValueRef<ValuePolicy::AdditionalSources>();
if (additionalSourcesOpt.has_value())
{
const auto& additionalSources = additionalSourcesOpt->get();
for (const auto& additionalSource : additionalSources)
{
AICLI_LOG(Repo, Verbose, << "... with configured source " << additionalSource.Name);
SourceDetailsInternal details;
details.Name = additionalSource.Name;
details.Type = additionalSource.Type;
details.Arg = additionalSource.Arg;
details.Data = additionalSource.Data;
details.Identifier = additionalSource.Identifier;
details.Origin = SourceOrigin::GroupPolicy;
details.Explicit = additionalSource.Explicit;
#ifndef AICLI_DISABLE_TEST_HOOKS
details.CertificatePinningConfiguration = additionalSource.PinningConfiguration;
#endif
try
{
details.TrustLevel = Repository::ConvertToSourceTrustLevelFlag(additionalSource.TrustLevel);
}
catch (...)
{
details.TrustLevel = Repository::SourceTrustLevel::None;
AICLI_LOG(Repo, Verbose, << "Invalid source trust level from policy. Trust level set to None.");
}
result.emplace_back(std::move(details));
}
}
else
{
AICLI_LOG(Repo, Verbose, << "... but has no values.");
}
}
else
{
AICLI_LOG(Repo, Verbose, << "Additional sources GP is not enabled.");
}
}
break;
default:
THROW_HR(E_UNEXPECTED);
}
for (auto& source : result)
{
source.Origin = origin;
}
return result;
}
bool SourceList::SetSourcesByOrigin(SourceOrigin origin, const std::vector<SourceDetailsInternal>& sources)
{
switch (origin)
{
case SourceOrigin::User:
return SetSourcesToSettingWithFilter(m_userSourcesStream, SourceOrigin::User, sources);
}
THROW_HR(E_UNEXPECTED);
}
std::vector<SourceDetailsInternal> SourceList::GetMetadata()
{
return GetSourcesFromSetting(
m_metadataStream,
s_MetadataYaml_Sources,
[&](SourceDetailsInternal& details, const std::string& settingValue, const YAML::Node& source)
{
details.Origin = SourceOrigin::Metadata;
std::string_view name = m_metadataStream.GetName();
if (!TryReadScalar(name, settingValue, source, s_MetadataYaml_Source_Name, details.Name)) { return false; }
int64_t lastUpdateInEpoch{};
if (!TryReadScalar(name, settingValue, source, s_MetadataYaml_Source_LastUpdate, lastUpdateInEpoch)) { return false; }
details.LastUpdateTime = Utility::ConvertUnixEpochToSystemClock(lastUpdateInEpoch);
int64_t doNotUpdateBeforeInEpoch{};
if (TryReadScalar(name, settingValue, source, s_MetadataYaml_Source_DoNotUpdateBefore, doNotUpdateBeforeInEpoch, false))
{
details.DoNotUpdateBefore = Utility::ConvertUnixEpochToSystemClock(doNotUpdateBeforeInEpoch);
}
TryReadScalar(name, settingValue, source, s_MetadataYaml_Source_AcceptedAgreementsIdentifier, details.AcceptedAgreementsIdentifier, false);
TryReadScalar(name, settingValue, source, s_MetadataYaml_Source_AcceptedAgreementFields, details.AcceptedAgreementFields, false);
return true;
});
}
bool SourceList::SetMetadata(const std::vector<SourceDetailsInternal>& sources)
{
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << s_MetadataYaml_Sources;
out << YAML::BeginSeq;
for (const auto& details : sources)
{
out << YAML::BeginMap;
out << YAML::Key << s_MetadataYaml_Source_Name << YAML::Value << details.Name;
out << YAML::Key << s_MetadataYaml_Source_LastUpdate << YAML::Value << Utility::ConvertSystemClockToUnixEpoch(details.LastUpdateTime);
out << YAML::Key << s_MetadataYaml_Source_DoNotUpdateBefore << YAML::Value << Utility::ConvertSystemClockToUnixEpoch(details.DoNotUpdateBefore);
out << YAML::Key << s_MetadataYaml_Source_AcceptedAgreementsIdentifier << YAML::Value << details.AcceptedAgreementsIdentifier;
out << YAML::Key << s_MetadataYaml_Source_AcceptedAgreementFields << YAML::Value << details.AcceptedAgreementFields;
out << YAML::EndMap;
}
out << YAML::EndSeq;
out << YAML::EndMap;
return m_metadataStream.Set(out.str());
}
void SourceList::SaveMetadataInternal(const SourceDetailsInternal& detailsRef, bool remove)
{
// Copy the incoming details because we might overwrite the metadata
// when reloading the source details from settings.
SourceDetailsInternal details = detailsRef;
bool metadataSet = false;
for (size_t i = 0; !metadataSet && i < 10; ++i)
{
metadataSet = SetMetadata(m_sourceList);
if (!metadataSet)
{
OverwriteMetadata();
auto target = FindSource(details.Name, true);
if (target == m_sourceList.end())
{
// Didn't find the metadata, so we consider this a success
return;
}
if (remove)
{
// The remove will have removed the source but not the metadata.
// Remove it again here.
m_sourceList.erase(target);
}
else
{