88using Microsoft . Extensions . Options ;
99using NuGet . Jobs . Validation ;
1010using NuGet . Jobs . Validation . Storage ;
11+ using NuGet . Services . Validation . Orchestrator ;
1112using NuGet . Services . Validation . Orchestrator . PackageSigning . ScanAndSign ;
1213using NuGet . Services . Validation . Orchestrator . Telemetry ;
14+ using NuGetGallery ;
1315
1416namespace NuGet . Services . Validation . PackageSigning . ProcessSignature
1517{
@@ -23,6 +25,7 @@ public class PackageSignatureValidator : BaseSignatureProcessor, IValidator
2325 private readonly IValidatorStateService _validatorStateService ;
2426 private readonly IProcessSignatureEnqueuer _signatureVerificationEnqueuer ;
2527 private readonly ISimpleCloudBlobProvider _blobProvider ;
28+ private readonly ICorePackageService _packages ;
2629 private readonly ScanAndSignConfiguration _config ;
2730 private readonly ITelemetryService _telemetryService ;
2831 private readonly ILogger < PackageSignatureValidator > _logger ;
@@ -31,6 +34,7 @@ public PackageSignatureValidator(
3134 IValidatorStateService validatorStateService ,
3235 IProcessSignatureEnqueuer signatureVerificationEnqueuer ,
3336 ISimpleCloudBlobProvider blobProvider ,
37+ ICorePackageService packages ,
3438 IOptionsSnapshot < ScanAndSignConfiguration > configAccessor ,
3539 ITelemetryService telemetryService ,
3640 ILogger < PackageSignatureValidator > logger )
@@ -39,6 +43,7 @@ public PackageSignatureValidator(
3943 _validatorStateService = validatorStateService ?? throw new ArgumentNullException ( nameof ( validatorStateService ) ) ;
4044 _signatureVerificationEnqueuer = signatureVerificationEnqueuer ?? throw new ArgumentNullException ( nameof ( signatureVerificationEnqueuer ) ) ;
4145 _blobProvider = blobProvider ?? throw new ArgumentNullException ( nameof ( blobProvider ) ) ;
46+ _packages = packages ?? throw new ArgumentNullException ( nameof ( packages ) ) ;
4247 _telemetryService = telemetryService ?? throw new ArgumentNullException ( nameof ( telemetryService ) ) ;
4348 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
4449
@@ -60,45 +65,57 @@ public override async Task<IValidationResult> GetResultAsync(IValidationRequest
6065 {
6166 var result = await base . GetResultAsync ( request ) ;
6267
63- return Validate ( result ) ;
68+ return Validate ( request , result ) ;
6469 }
6570
6671 public override async Task < IValidationResult > StartAsync ( IValidationRequest request )
6772 {
6873 var result = await base . StartAsync ( request ) ;
6974
70- return Validate ( result ) ;
75+ return Validate ( request , result ) ;
7176 }
7277
73- private IValidationResult Validate ( IValidationResult result )
78+ private IValidationResult Validate ( IValidationRequest request , IValidationResult result )
7479 {
7580 /// The package signature validator runs after the <see cref="PackageSignatureProcessor" />.
7681 /// All signature validation issues should be caught and handled by the processor.
7782 if ( result . Status == ValidationStatus . Failed || result . NupkgUrl != null )
7883 {
79- if ( _config . RepositorySigningEnabled )
84+ if ( ! _config . RepositorySigningEnabled )
8085 {
81- _logger . LogCritical (
82- "Unexpected validation result in package signature validator. This may be caused by an invalid repository " +
83- "signature. Throwing an exception to force this validation to dead-letter. " +
86+ _logger . LogInformation (
87+ "Ignoring invalid validation result in package signature validator as repository signing is disabled. " +
8488 "Status = {ValidationStatus}, Nupkg URL = {NupkgUrl}, validation issues = {Issues}" ,
8589 result . Status ,
8690 result . NupkgUrl ,
8791 result . Issues . Select ( i => i . IssueCode ) ) ;
8892
89- throw new InvalidOperationException ( "Package signature validator has an unexpected validation result" ) ;
93+ return ValidationResult . Succeeded ;
9094 }
91- else
95+
96+ // TODO: Remove this.
97+ // See: https://github.com/NuGet/Engineering/issues/1592
98+ if ( HasOwnerWithInvalidUsername ( request ) )
9299 {
93- _logger . LogInformation (
94- "Ignoring invalid validation result in package signature validator as repository signing is disabled . " +
100+ _logger . LogWarning (
101+ "Ignoring invalid validation result in package signature validator as the package has an owner with an invalid username . " +
95102 "Status = {ValidationStatus}, Nupkg URL = {NupkgUrl}, validation issues = {Issues}" ,
96103 result . Status ,
97104 result . NupkgUrl ,
98105 result . Issues . Select ( i => i . IssueCode ) ) ;
99106
100107 return ValidationResult . Succeeded ;
101108 }
109+
110+ _logger . LogCritical (
111+ "Unexpected validation result in package signature validator. This may be caused by an invalid repository " +
112+ "signature. Throwing an exception to force this validation to dead-letter. " +
113+ "Status = {ValidationStatus}, Nupkg URL = {NupkgUrl}, validation issues = {Issues}" ,
114+ result . Status ,
115+ result . NupkgUrl ,
116+ result . Issues . Select ( i => i . IssueCode ) ) ;
117+
118+ throw new InvalidOperationException ( "Package signature validator has an unexpected validation result" ) ;
102119 }
103120
104121 /// Suppress all validation issues. The <see cref="PackageSignatureProcessor"/> should
@@ -116,5 +133,32 @@ private IValidationResult Validate(IValidationResult result)
116133
117134 return result ;
118135 }
136+
137+ private bool HasOwnerWithInvalidUsername ( IValidationRequest request )
138+ {
139+ var registration = _packages . FindPackageRegistrationById ( request . PackageId ) ;
140+
141+ if ( registration == null )
142+ {
143+ _logger . LogError ( "Attempted to validate package that has no package registration" ) ;
144+
145+ throw new InvalidOperationException ( $ "Registration for package id { request . PackageId } does not exist") ;
146+ }
147+
148+ var owners = registration . Owners . Select ( o => o . Username ) . ToList ( ) ;
149+
150+ if ( owners . Any ( UsernameHelper . IsInvalid ) )
151+ {
152+ _logger . LogWarning (
153+ "Package {PackageId} {PackageVersion} has an owner with an invalid username. {Owners}" ,
154+ request . PackageId ,
155+ request . PackageVersion ,
156+ owners ) ;
157+
158+ return true ;
159+ }
160+
161+ return false ;
162+ }
119163 }
120164}
0 commit comments