@@ -40,6 +40,7 @@ internal class NuGetPackageDownloader : INuGetPackageDownloader
40
40
41
41
private readonly bool _verifySignatures ;
42
42
private readonly VerbosityOptions _verbosityOptions ;
43
+ private readonly string _currentWorkingDirectory ;
43
44
44
45
public NuGetPackageDownloader (
45
46
DirectoryPath packageInstallDir ,
@@ -51,8 +52,10 @@ public NuGetPackageDownloader(
51
52
Func < IEnumerable < Task > > timer = null ,
52
53
bool verifySignatures = false ,
53
54
bool shouldUsePackageSourceMapping = false ,
54
- VerbosityOptions verbosityOptions = VerbosityOptions . normal )
55
+ VerbosityOptions verbosityOptions = VerbosityOptions . normal ,
56
+ string currentWorkingDirectory = null )
55
57
{
58
+ _currentWorkingDirectory = currentWorkingDirectory ;
56
59
_packageInstallDir = packageInstallDir ;
57
60
_reporter = reporter ?? Reporter . Output ;
58
61
_verboseLogger = verboseLogger ?? new NuGetConsoleLogger ( ) ;
@@ -127,22 +130,22 @@ public async Task<string> DownloadPackageAsync(PackageId packageId,
127
130
packageVersion . ToNormalizedString ( ) ) ) ;
128
131
}
129
132
130
- VerifySigning ( nupkgPath ) ;
131
-
133
+ await VerifySigning ( nupkgPath , repository ) ;
134
+
132
135
return nupkgPath ;
133
136
}
134
137
135
- private bool verbosityGreaterThanMinimal ( )
136
- {
137
- return _verbosityOptions != VerbosityOptions . quiet && _verbosityOptions != VerbosityOptions . q
138
- && _verbosityOptions != VerbosityOptions . minimal && _verbosityOptions != VerbosityOptions . m ;
139
- }
138
+ private bool VerbosityGreaterThanMinimal ( ) =>
139
+ _verbosityOptions != VerbosityOptions . quiet && _verbosityOptions != VerbosityOptions . q &&
140
+ _verbosityOptions != VerbosityOptions . minimal && _verbosityOptions != VerbosityOptions . m ;
140
141
141
- private void VerifySigning ( string nupkgPath )
142
+ private bool DiagnosticVerbosity ( ) => _verbosityOptions == VerbosityOptions . diag || _verbosityOptions == VerbosityOptions . diagnostic ;
143
+
144
+ private async Task VerifySigning ( string nupkgPath , SourceRepository repository )
142
145
{
143
146
if ( ! _verifySignatures && ! _validationMessagesDisplayed )
144
147
{
145
- if ( verbosityGreaterThanMinimal ( ) )
148
+ if ( VerbosityGreaterThanMinimal ( ) )
146
149
{
147
150
_reporter . WriteLine ( LocalizableStrings . NuGetPackageSignatureVerificationSkipped ) ;
148
151
}
@@ -154,15 +157,28 @@ private void VerifySigning(string nupkgPath)
154
157
return ;
155
158
}
156
159
157
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
160
+ if ( repository is not null &&
161
+ await repository . GetResourceAsync < RepositorySignatureResource > ( ) . ConfigureAwait ( false ) is RepositorySignatureResource resource &&
162
+ resource . AllRepositorySigned )
158
163
{
159
- if ( ! _firstPartyNuGetPackageSigningVerifier . Verify ( new FilePath ( nupkgPath ) ,
160
- out string commandOutput ) )
164
+ string commandOutput ;
165
+ // The difference between _firstPartyNuGetPackageSigningVerifier.Verify and FirstPartyNuGetPackageSigningVerifier.NuGetVerify is that while NuGetVerify
166
+ // just ensures that the package is signed properly, Verify additionally requires that the package be from Microsoft. NuGetVerify does not require that
167
+ // the package be from Microsoft.
168
+ if ( ( ! _shouldUsePackageSourceMapping && ! _firstPartyNuGetPackageSigningVerifier . Verify ( new FilePath ( nupkgPath ) , out commandOutput ) ) ||
169
+ ( _shouldUsePackageSourceMapping && ! FirstPartyNuGetPackageSigningVerifier . NuGetVerify ( new FilePath ( nupkgPath ) , out commandOutput , _currentWorkingDirectory ) ) )
161
170
{
162
- throw new NuGetPackageInstallerException ( LocalizableStrings . FailedToValidatePackageSigning +
163
- Environment . NewLine +
164
- commandOutput ) ;
171
+ throw new NuGetPackageInstallerException ( string . Format ( LocalizableStrings . FailedToValidatePackageSigning , commandOutput ) ) ;
165
172
}
173
+
174
+ if ( DiagnosticVerbosity ( ) )
175
+ {
176
+ _reporter . WriteLine ( LocalizableStrings . VerifyingNuGetPackageSignature , Path . GetFileNameWithoutExtension ( nupkgPath ) ) ;
177
+ }
178
+ }
179
+ else if ( DiagnosticVerbosity ( ) )
180
+ {
181
+ _reporter . WriteLine ( LocalizableStrings . NuGetPackageShouldNotBeSigned , Path . GetFileNameWithoutExtension ( nupkgPath ) ) ;
166
182
}
167
183
}
168
184
@@ -334,7 +350,7 @@ private IEnumerable<PackageSource> LoadOverrideSources(PackageSourceLocation pac
334
350
private List < PackageSource > LoadDefaultSources ( PackageId packageId , PackageSourceLocation packageSourceLocation = null , PackageSourceMapping packageSourceMapping = null )
335
351
{
336
352
List < PackageSource > defaultSources = new ( ) ;
337
- string currentDirectory = Directory . GetCurrentDirectory ( ) ;
353
+ string currentDirectory = _currentWorkingDirectory ?? Directory . GetCurrentDirectory ( ) ;
338
354
ISettings settings ;
339
355
if ( packageSourceLocation ? . NugetConfig != null )
340
356
{
0 commit comments