From 428728a36800295aa615e5794d8c548a059a8bc6 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sat, 4 Jun 2016 08:34:48 -0500 Subject: [PATCH 1/2] (GH-769) API - Resolve Merged Assemblies When using the API, it should resolve merged assemblies but it may not if there is an assembly handler registered. Before loading up code that resolves external assemblies, register an AssemblyResolve handler with the purpose of determining if the assembly has Chocolatey's Public Key token or not. If it does, use Chocolatey's merged assembly instead of the conflicting assembly. --- src/chocolatey/AssemblyExtensions.cs | 10 +++++++++- src/chocolatey/GetChocolatey.cs | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/AssemblyExtensions.cs b/src/chocolatey/AssemblyExtensions.cs index 95465bc2e9..67450b770d 100644 --- a/src/chocolatey/AssemblyExtensions.cs +++ b/src/chocolatey/AssemblyExtensions.cs @@ -17,6 +17,7 @@ namespace chocolatey { using System.IO; using System.Linq; + using System.Reflection; using infrastructure.adapters; /// @@ -71,7 +72,14 @@ public static string get_public_key_token(this IAssembly assembly) { if (assembly == null) return string.Empty; - byte[] publicKeyToken = assembly.GetName().GetPublicKeyToken(); + return assembly.GetName().get_public_key_token(); + } + + public static string get_public_key_token(this AssemblyName assemblyName) + { + if (assemblyName == null) return string.Empty; + + byte[] publicKeyToken = assemblyName.GetPublicKeyToken(); if (publicKeyToken == null || publicKeyToken.Length == 0) return string.Empty; diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index 5110c34487..7490ec351b 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -17,10 +17,9 @@ namespace chocolatey { using System; using System.Collections.Generic; + using System.Reflection; using infrastructure.licensing; - using NuGet; using SimpleInjector; - using infrastructure.adapters; using infrastructure.app; using infrastructure.app.builders; using infrastructure.app.configuration; @@ -30,6 +29,7 @@ namespace chocolatey using infrastructure.logging; using infrastructure.registration; using resources; + using Assembly = infrastructure.adapters.Assembly; using IFileSystem = infrastructure.filesystem.IFileSystem; // ReSharper disable InconsistentNaming @@ -41,8 +41,27 @@ public static class Lets { public static GetChocolatey GetChocolatey() { + add_assembly_resolver(); return new GetChocolatey(); } + + private static ResolveEventHandler _handler = null; + + private static void add_assembly_resolver() + { + _handler = (sender, args) => + { + var requestedAssembly = new AssemblyName(args.Name); + if (requestedAssembly.get_public_key_token().is_equal_to(ApplicationParameters.OfficialChocolateyPublicKey)) + { + return typeof(Lets).Assembly; + } + + return null; + }; + + AppDomain.CurrentDomain.AssemblyResolve += _handler; + } } /// From f2aa36d2cbbb7e5f53cd457ce0ddc55f230cf9d0 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Sat, 4 Jun 2016 08:35:42 -0500 Subject: [PATCH 2/2] (maint) use case insensitive compare for token --- src/chocolatey/infrastructure.app/runners/GenericRunner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs index 7ec17fd60b..773c212985 100644 --- a/src/chocolatey/infrastructure.app/runners/GenericRunner.cs +++ b/src/chocolatey/infrastructure.app/runners/GenericRunner.cs @@ -82,7 +82,7 @@ private ICommand find_command(ChocolateyConfiguration config, Container containe } var token = Assembly.GetExecutingAssembly().get_public_key_token(); - if (string.IsNullOrWhiteSpace(token) || token != ApplicationParameters.OfficialChocolateyPublicKey) + if (string.IsNullOrWhiteSpace(token) || !token.is_equal_to(ApplicationParameters.OfficialChocolateyPublicKey)) { if (!config.AllowUnofficialBuild) {