diff --git a/src/BannedSymbols.txt b/src/BannedSymbols.txt index c369fe9d42b..1713f85376d 100644 --- a/src/BannedSymbols.txt +++ b/src/BannedSymbols.txt @@ -5,3 +5,5 @@ M:System.Xml.XmlReader.Create(System.String,System.Xml.XmlReaderSettings);Do not M:System.Xml.XmlReader.Create(System.String,System.Xml.XmlReaderSettings,System.Xml.XmlParserContext);Do not pass paths to XmlReader.Create--use the Stream overload M:System.Xml.XPath.XPathDocument.#ctor(System.String);Do not pass string paths to XPathDocument ctor--use the Stream overload M:System.Xml.XPath.XPathDocument.#ctor(System.String,System.Xml.XmlSpace);Do not pass string paths to XPathDocument ctor--use the Stream overload +M:System.Xml.Linq.XDocument.Load(System.String);Do not pass uri to XDocument.Load, use overload with XmlReader instead +M:System.Xml.Linq.XDocument.Load(System.String, System.Xml.Linq.LoadOptions);Do not pass uri to XDocument.Load, use overload with XmlReader instead diff --git a/src/Shared/UnitTests/TestAssemblyInfo.cs b/src/Shared/UnitTests/TestAssemblyInfo.cs index 368eb99d6c6..08353749def 100644 --- a/src/Shared/UnitTests/TestAssemblyInfo.cs +++ b/src/Shared/UnitTests/TestAssemblyInfo.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Xml; using System.Xml.Linq; using Microsoft.Build.Shared; using Microsoft.Build.Shared.FileSystem; @@ -103,7 +104,13 @@ private static void SetDotnetHostPath(TestEnvironment testEnvironment) string potentialVersionsPropsPath = Path.Combine(currentFolder, "build", "Versions.props"); if (FileSystems.Default.FileExists(potentialVersionsPropsPath)) { - var doc = XDocument.Load(potentialVersionsPropsPath); + XDocument doc = null; + var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true, IgnoreWhitespace = true }; + using (XmlReader xr = XmlReader.Create(File.OpenRead(potentialVersionsPropsPath), xrs)) + { + doc = XDocument.Load(xr); + } + var ns = doc.Root.Name.Namespace; var cliVersionElement = doc.Root.Elements(ns + "PropertyGroup").Elements(ns + "DotNetCliVersion").FirstOrDefault(); if (cliVersionElement != null) diff --git a/src/Tasks.UnitTests/GenerateBindingRedirects_Tests.cs b/src/Tasks.UnitTests/GenerateBindingRedirects_Tests.cs index a417cabd783..07b1ce57a0f 100644 --- a/src/Tasks.UnitTests/GenerateBindingRedirects_Tests.cs +++ b/src/Tasks.UnitTests/GenerateBindingRedirects_Tests.cs @@ -259,6 +259,22 @@ public void AppConfigInvalidIfDependentAssemblyNodeIsEmpty() redirectResults.Engine.AssertLogContains("MSB3835"); } + [Fact] + public void AppConfigWhenFilePlacedInLocationWithGB18030Characters() + { + using (TestEnvironment env = TestEnvironment.Create()) + { + TransientTestFolder rootTestFolder = env.CreateFolder(); + TransientTestFolder testFolder = env.CreateFolder(Path.Combine(rootTestFolder.Path, "\uD873\uDD02\u9FA8\u82D8\u722B\u9EA4\u03C5\u33D1\uE038\u486B\u0033")); + string appConfigContents = WriteAppConfigRuntimeSection(string.Empty, testFolder); + string outputAppConfigFile = env.ExpectFile(".config").Path; + + TaskItemMock redirect = new TaskItemMock("System, Version=10.0.0.0, Culture=Neutral, PublicKeyToken='b77a5c561934e089'", "40.0.0.0"); + + _ = Should.NotThrow(() => GenerateBindingRedirects(appConfigContents, outputAppConfigFile, redirect)); + } + } + [Fact] public void AppConfigFileNotSavedWhenIdentical() { @@ -306,12 +322,11 @@ private BindingRedirectsExecutionResult GenerateBindingRedirects(string appConfi GenerateBindingRedirects bindingRedirects = new GenerateBindingRedirects { BuildEngine = engine, - SuggestedRedirects = suggestedRedirects ?? System.Array.Empty(), + SuggestedRedirects = suggestedRedirects ?? Array.Empty(), AppConfigFile = new TaskItem(appConfigFile), OutputAppConfigFile = new TaskItem(outputAppConfig) }; - bool executionResult = bindingRedirects.Execute(); return new BindingRedirectsExecutionResult @@ -324,7 +339,9 @@ private BindingRedirectsExecutionResult GenerateBindingRedirects(string appConfi }; } - private string WriteAppConfigRuntimeSection(string runtimeSection) + private string WriteAppConfigRuntimeSection( + string runtimeSection, + TransientTestFolder transientTestFolder = null) { string formatString = @" @@ -334,7 +351,7 @@ private string WriteAppConfigRuntimeSection(string runtimeSection) "; string appConfigContents = string.Format(formatString, runtimeSection); - string appConfigFile = _env.CreateFile(".config").Path; + string appConfigFile = _env.CreateFile(transientTestFolder ?? new TransientTestFolder(), ".config").Path; File.WriteAllText(appConfigFile, appConfigContents); return appConfigFile; } diff --git a/src/Tasks/AssemblyDependency/GenerateBindingRedirects.cs b/src/Tasks/AssemblyDependency/GenerateBindingRedirects.cs index c108bb142f0..99307761f8f 100644 --- a/src/Tasks/AssemblyDependency/GenerateBindingRedirects.cs +++ b/src/Tasks/AssemblyDependency/GenerateBindingRedirects.cs @@ -10,6 +10,7 @@ using Microsoft.Build.Shared; using Microsoft.Build.Shared.FileSystem; using System.IO; +using System.Xml; #nullable disable @@ -335,7 +336,12 @@ private XDocument LoadAppConfig(ITaskItem appConfigItem) } else { - document = XDocument.Load(appConfigItem.ItemSpec); + var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true, IgnoreWhitespace = true }; + using (XmlReader xr = XmlReader.Create(File.OpenRead(appConfigItem.ItemSpec), xrs)) + { + document = XDocument.Load(xr); + } + if (document.Root == null || document.Root.Name != "configuration") { Log.LogErrorWithCodeFromResources("GenerateBindingRedirects.MissingConfigurationNode");