Skip to content

Commit

Permalink
Re-sign any assembly with friend reference fixes
Browse files Browse the repository at this point in the history
- Avoid same bug from 1.4.3 by re-signing assemblies that have IVT
attributes removed.
  • Loading branch information
brutaldev committed Apr 19, 2015
1 parent 8a050bd commit 6ed4e9e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ local.properties
*.suo
*.user
*.sln.docstates
*.GhostDoc.xml

# Build results

Expand Down
11 changes: 7 additions & 4 deletions src/Brutal.Dev.StrongNameSigner.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ private static Stats SignAssemblies(Options options)
}

var processedAssemblyPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var signedAssemblyPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

foreach (var filePath in filesToSign)
{
var signedAssembly = SignSingleAssembly(filePath, options.KeyFile, options.OutputDirectory, options.Password);
if (signedAssembly != null)
{
processedAssemblyPaths.Add(signedAssembly.FilePath);
signedAssemblyPaths.Add(signedAssembly.FilePath);
signedFiles++;
}
else
Expand All @@ -112,9 +115,9 @@ private static Stats SignAssemblies(Options options)
}

// Remove all InternalsVisibleTo attributes without public keys from the processed assemblies. Signed assemblies cannot have unsigned friend assemblies.
foreach (var filePath in processedAssemblyPaths)
foreach (var filePath in signedAssemblyPaths)
{
if (RemoveInvalidFriendAssemblyReferences(filePath))
if (RemoveInvalidFriendAssemblyReferences(filePath, options.KeyFile, options.Password))
{
referenceFixes++;
}
Expand Down Expand Up @@ -203,15 +206,15 @@ private static bool FixSingleAssemblyReference(string assemblyPath, string refer
return false;
}

private static bool RemoveInvalidFriendAssemblyReferences(string assemblyPath)
private static bool RemoveInvalidFriendAssemblyReferences(string assemblyPath, string keyFile, string keyFilePassword)
{
try
{
C.WriteLine();
C.WriteLine("Removing invalid friend references from '{0}'...", assemblyPath);

var info = SigningHelper.GetAssemblyInfo(assemblyPath);
if (SigningHelper.RemoveInvalidFriendAssemblies(assemblyPath))
if (SigningHelper.RemoveInvalidFriendAssemblies(assemblyPath, keyFile, keyFilePassword))
{
C.ForegroundColor = ConsoleColor.Green;
C.WriteLine("Invalid friend assemblies removed successfully!");
Expand Down
6 changes: 4 additions & 2 deletions src/Brutal.Dev.StrongNameSigner.UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
int referenceFixes = 0;
var assemblyPaths = e.Argument as IEnumerable<string>;
var processedAssemblyPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var signedAssemblyPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

if (assemblyPaths != null)
{
Expand All @@ -377,6 +378,7 @@ private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
assemblyPair.NewInfo = SigningHelper.SignAssembly(filePath, keyFile, outputPath, password);
log.Append("Strong-name signed successfully.").AppendLine();
signedAssemblyPaths.Add(filePath);
signedFiles++;
}
else
Expand Down Expand Up @@ -429,7 +431,7 @@ private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
}

// Go through all processed assemblies and remove invalid friend references.
foreach (var filePath in processedAssemblyPaths)
foreach (var filePath in signedAssemblyPaths)
{
backgroundWorker.ReportProgress(Convert.ToInt32((++progress / progressMax) * 100));

Expand All @@ -440,7 +442,7 @@ private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
}

log.AppendFormat("Removing invalid friend references from '{0}'...", filePath).AppendLine();
if (SigningHelper.RemoveInvalidFriendAssemblies(filePath))
if (SigningHelper.RemoveInvalidFriendAssemblies(filePath, keyFile, password))
{
log.Append("Invalid friend assemblies removed.").AppendLine();
referenceFixes++;
Expand Down
27 changes: 26 additions & 1 deletion src/Brutal.Dev.StrongNameSigner/SigningHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,24 @@ public static bool FixAssemblyReference(string assemblyPath, string referenceAss
/// Could not find provided assembly file.
/// </exception>
public static bool RemoveInvalidFriendAssemblies(string assemblyPath)
{
return RemoveInvalidFriendAssemblies(assemblyPath, string.Empty, string.Empty);
}

/// <summary>
/// Removes any friend assembly references (InternalsVisibleTo attributes) that do not have public keys.
/// </summary>
/// <param name="assemblyPath">The path to the assembly you want to remove friend references from.</param>
/// <param name="keyPath">The path to the strong-name key file you want to use (.snk or .pfx).</param>
/// <param name="keyFilePassword">The password for the provided strong-name key file.</param>
/// <returns><c>true</c> if any invalid friend references were found and fixed, <c>false</c> if no invalid friend references was found.</returns>
/// <exception cref="System.ArgumentNullException">
/// assemblyPath was not provided.
/// </exception>
/// <exception cref="System.IO.FileNotFoundException">
/// Could not find provided assembly file.
/// </exception>
public static bool RemoveInvalidFriendAssemblies(string assemblyPath, string keyPath, string keyFilePassword)
{
// Verify assembly path was passed in.
if (string.IsNullOrWhiteSpace(assemblyPath))
Expand Down Expand Up @@ -343,7 +361,14 @@ public static bool RemoveInvalidFriendAssemblies(string assemblyPath)

if (fixApplied)
{
a.Write(assemblyPath);
if (!string.IsNullOrEmpty(keyPath))
{
a.Write(assemblyPath, new WriterParameters { StrongNameKeyPair = GetStrongNameKeyPair(keyPath, keyFilePassword) });
}
else
{
a.Write(assemblyPath);
}
}

return fixApplied;
Expand Down

0 comments on commit 6ed4e9e

Please sign in to comment.