66using System . IO ;
77using System . Linq ;
88using System . Security . Cryptography ;
9+ using System . Security . Cryptography . X509Certificates ;
910using Microsoft . Build . Framework ;
1011
1112namespace NuGet . Services . Build
@@ -21,6 +22,8 @@ public class FindDuplicateFiles : Microsoft.Build.Utilities.Task
2122 [ Output ]
2223 public ITaskItem [ ] DuplicateFiles { get ; set ; }
2324
25+ public string SkipAuthenticodeSubjects { get ; set ; }
26+
2427 public override bool Execute ( )
2528 {
2629 var infos = GetUniqueTaskItemInfo ( ) ;
@@ -123,6 +126,57 @@ private Dictionary<string, List<TaskItemInfo>> FindDuplicates(List<TaskItemInfo>
123126 BreakTiesWithLeadingHash ( filePathToDuplicates , buffer , fileSizePair ) ;
124127 }
125128
129+ if ( ! string . IsNullOrWhiteSpace ( SkipAuthenticodeSubjects ) )
130+ {
131+ Log . LogMessage ( "Skipping files with the following Authenticode subjects: {0}" , SkipAuthenticodeSubjects ) ;
132+
133+ var skipSubjects = SkipAuthenticodeSubjects
134+ . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries )
135+ . Select ( s => s . Trim ( ) )
136+ . ToHashSet ( StringComparer . Ordinal ) ;
137+
138+ foreach ( var pair in filePathToDuplicates . ToList ( ) )
139+ {
140+ var path = pair . Key ;
141+ try
142+ {
143+ var cert = X509Certificate . CreateFromSignedFile ( path ) ;
144+ var subject = cert . Subject ;
145+
146+ if ( skipSubjects . Contains ( subject ) )
147+ {
148+ Log . LogMessage (
149+ "Skipping file '{0}' with Authenticode subject '{1}'." ,
150+ path ,
151+ subject ) ;
152+
153+ filePathToDuplicates . Remove ( path ) ;
154+ }
155+ else
156+ {
157+ Log . LogMessage (
158+ "Not skipping file '{0}' with Authenticode subject '{1}'." ,
159+ path ,
160+ subject ) ;
161+ }
162+ }
163+ catch ( Exception ex )
164+ {
165+ if ( ex is CryptographicException )
166+ {
167+ // The file is not signed, proceed as normal.
168+ continue ;
169+ }
170+
171+ throw ;
172+ }
173+ }
174+ }
175+ else
176+ {
177+ Log . LogMessage ( "No Authenticode subjects to skip were provided." ) ;
178+ }
179+
126180 return filePathToDuplicates ;
127181 }
128182
0 commit comments