From 956441aa68ce7516bce69fda616a0fe060c83882 Mon Sep 17 00:00:00 2001 From: "David J. Quiroga" Date: Mon, 8 Oct 2018 14:41:04 -0300 Subject: [PATCH] Ignore created URI if Assembly.CodeBase contains a fragment (#8556) * Fixes #8367 --- .../RelatedAssemblyAttribute.cs | 3 +- .../RelatedAssemblyPartTest.cs | 50 ++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/RelatedAssemblyAttribute.cs b/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/RelatedAssemblyAttribute.cs index b9f6cdb2d7..294d7b9ac8 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/RelatedAssemblyAttribute.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/RelatedAssemblyAttribute.cs @@ -115,7 +115,8 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts internal static string GetAssemblyLocation(Assembly assembly) { - if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) && result.IsFile) + if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) && + result.IsFile && string.IsNullOrWhiteSpace(result.Fragment)) { return result.LocalPath; } diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/RelatedAssemblyPartTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/RelatedAssemblyPartTest.cs index efce8dfaad..6819818bf1 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/RelatedAssemblyPartTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/ApplicationParts/RelatedAssemblyPartTest.cs @@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts { // Arrange var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var codeBase = "file://x/file/Assembly.dll"; + var codeBase = "file://x:/file/Assembly.dll"; var expected = new Uri(codeBase).LocalPath; var assembly = new TestAssembly { @@ -109,6 +109,54 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts Assert.Equal(expected, actual); } + [Fact] + public void GetAssemblyLocation_CodeBase_HasPoundCharacterUnixPath() + { + var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); + var expected = @"/etc/#NIN/dotnetcore/tryx/try1.dll"; + var assembly = new TestAssembly + { + CodeBaseSettable = "file:///etc/#NIN/dotnetcore/tryx/try1.dll", + LocationSettable = expected, + }; + + // Act + var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); + Assert.Equal(expected, actual); + } + + [Fact] + public void GetAssemblyLocation_CodeBase_HasPoundCharacterUNCPath() + { + var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); + var expected = @"\\server\#NIN\dotnetcore\tryx\try1.dll"; + var assembly = new TestAssembly + { + CodeBaseSettable = "file://server/#NIN/dotnetcore/tryx/try1.dll", + LocationSettable = expected, + }; + + // Act + var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); + Assert.Equal(expected, actual); + } + + [Fact] + public void GetAssemblyLocation_CodeBase_HasPoundCharacterDOSPath() + { + var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); + var expected = @"C:\#NIN\dotnetcore\tryx\try1.dll"; + var assembly = new TestAssembly + { + CodeBaseSettable = "file:///C:/#NIN/dotnetcore/tryx/try1.dll", + LocationSettable = expected, + }; + + // Act + var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); + Assert.Equal(expected, actual); + } + private class TestAssembly : Assembly { public override AssemblyName GetName()