Skip to content

Commit f5e2555

Browse files
adjust XmlReader.Create to passed path with potentially invalid character (#9028)
Fixes #8972 Context #8931 fixed one instance of the issue with build issues caused by localized characters in OS paths. This PR attempts to address the rest of the same unintended string -> uri conversion Changes Made Passing Stream to XmlReader.Create instead of path in order to prevent unintended string -> uri conversion Testing N/A
1 parent 91a3521 commit f5e2555

File tree

14 files changed

+62
-47
lines changed

14 files changed

+62
-47
lines changed

src/Build/Construction/Solution/ProjectInSolution.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,11 @@ internal bool CanBeMSBuildProjectFile(out string errorMessage)
299299
try
300300
{
301301
// Read project thru a XmlReader with proper setting to avoid DTD processing
302-
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
302+
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
303303
var projectDocument = new XmlDocument();
304304

305-
using (XmlReader xmlReader = XmlReader.Create(AbsolutePath, xrSettings))
305+
FileStream fs = File.OpenRead(AbsolutePath);
306+
using (XmlReader xmlReader = XmlReader.Create(fs, xrSettings))
306307
{
307308
// Load the project file and get the first node
308309
projectDocument.Load(xmlReader);

src/Build/Construction/Solution/SolutionFile.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,11 @@ internal void ParseEtpProject(ProjectInSolution etpProj)
888888
*</EFPROJECT>
889889
**********************************************************************************/
890890
// Make sure the XML reader ignores DTD processing
891-
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
891+
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
892892

893893
// Load the .etp project file thru the XML reader
894-
using (XmlReader xmlReader = XmlReader.Create(fullPathToEtpProj, readerSettings))
894+
FileStream fs = File.OpenRead(fullPathToEtpProj);
895+
using (XmlReader xmlReader = XmlReader.Create(fs, readerSettings))
895896
{
896897
etpProjectDocument.Load(xmlReader);
897898
}

src/Tasks/AppConfig/AppConfig.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ internal sealed class AppConfig
1919
/// <summary>
2020
/// Read the .config from a file.
2121
/// </summary>
22-
/// <param name="appConfigFile"></param>
23-
internal void Load(string appConfigFile)
22+
/// <param name="appConfigFilePath"></param>
23+
internal void Load(string appConfigFilePath)
2424
{
2525
XmlReader reader = null;
2626
try
@@ -29,11 +29,11 @@ internal void Load(string appConfigFile)
2929

3030
// it's important to normalize the path as it may contain two slashes
3131
// see https://github.com/dotnet/msbuild/issues/4335 for details.
32-
appConfigFile = FileUtilities.NormalizePath(appConfigFile);
32+
appConfigFilePath = FileUtilities.NormalizePath(appConfigFilePath);
3333

3434
// Need a filestream as the XmlReader doesn't support nonstandard unicode characters in path.
3535
// No need to dispose - as 'CloseInput' was passed to XmlReaderSettings
36-
FileStream fs = File.OpenRead(appConfigFile);
36+
FileStream fs = File.OpenRead(appConfigFilePath);
3737
reader = XmlReader.Create(fs, readerSettings);
3838
Read(reader);
3939
}
@@ -48,7 +48,7 @@ internal void Load(string appConfigFile)
4848
linePosition = info.LinePosition;
4949
}
5050

51-
throw new AppConfigException(e.Message, appConfigFile, lineNumber, linePosition, e);
51+
throw new AppConfigException(e.Message, appConfigFilePath, lineNumber, linePosition, e);
5252
}
5353
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
5454
{
@@ -61,7 +61,7 @@ internal void Load(string appConfigFile)
6161
linePosition = info.LinePosition;
6262
}
6363

64-
throw new AppConfigException(e.Message, appConfigFile, lineNumber, linePosition, e);
64+
throw new AppConfigException(e.Message, appConfigFilePath, lineNumber, linePosition, e);
6565
}
6666
finally
6767
{

src/Tasks/BootstrapperUtil/BootstrapperBuilder.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,15 @@ private void RefreshResources()
532532
foreach (string subDirectory in Directory.GetDirectories(startDirectory))
533533
{
534534
string resourceDirectory = System.IO.Path.Combine(startDirectory, subDirectory);
535-
string resourceFile = System.IO.Path.Combine(resourceDirectory, SETUP_RESOURCES_FILE);
536-
if (FileSystems.Default.FileExists(resourceFile))
535+
string resourceFilePath = System.IO.Path.Combine(resourceDirectory, SETUP_RESOURCES_FILE);
536+
if (FileSystems.Default.FileExists(resourceFilePath))
537537
{
538538
var resourceDoc = new XmlDocument();
539539
try
540540
{
541-
var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
542-
using (var xr = XmlReader.Create(resourceFile, xrs))
541+
var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
542+
FileStream fs = File.OpenRead(resourceFilePath);
543+
using (var xr = XmlReader.Create(fs, xrs))
543544
{
544545
resourceDoc.Load(xr);
545546
}
@@ -836,8 +837,9 @@ private XmlDocument LoadAndValidateXmlDocument(string filePath, bool validateFil
836837
#pragma warning disable 618 // Using XmlValidatingReader. TODO: We need to switch to using XmlReader.Create() with validation.
837838
var validatingReader = new XmlValidatingReader(xmlReader);
838839
#pragma warning restore 618
839-
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
840-
using (XmlReader xr = XmlReader.Create(schemaPath, xrSettings))
840+
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
841+
FileStream fs = File.OpenRead(schemaPath);
842+
using (XmlReader xr = XmlReader.Create(fs, xrSettings))
841843
{
842844
try
843845
{

src/Tasks/ManifestUtil/ApplicationManifest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,9 @@ private void ValidateConfig()
527527
if (!TrustInfo.IsFullTrust)
528528
{
529529
var document = new XmlDocument();
530-
var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
531-
using (XmlReader xr = XmlReader.Create(configFile.ResolvedPath, xrs))
530+
var xrs = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
531+
FileStream fs = File.OpenRead(configFile.ResolvedPath);
532+
using (XmlReader xr = XmlReader.Create(fs, xrs))
532533
{
533534
document.Load(xr);
534535
}

src/Tasks/ManifestUtil/AssemblyIdentity.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ public static AssemblyIdentity FromManifest(string path)
193193
var document = new XmlDocument();
194194
try
195195
{
196-
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
197-
using (XmlReader xmlReader = XmlReader.Create(path, readerSettings))
196+
var readerSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
197+
FileStream fs = File.OpenRead(path);
198+
using (XmlReader xmlReader = XmlReader.Create(fs, readerSettings))
198199
{
199200
document.Load(xmlReader);
200201
}

src/Tasks/ManifestUtil/DeployManifest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ private static string GetInstallableFramework(string redistListFilePath)
213213
try
214214
{
215215
var doc = new XmlDocument();
216-
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
217-
using (XmlReader xr = XmlReader.Create(redistListFilePath, xrSettings))
216+
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
217+
FileStream fs = File.OpenRead(redistListFilePath);
218+
using (XmlReader xr = XmlReader.Create(fs, xrSettings))
218219
{
219220
doc.Load(xr);
220221
XmlNode fileListNode = doc.DocumentElement;

src/Tasks/ManifestUtil/Manifest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ internal bool TreatUnfoundNativeAssembliesAsPrerequisites
376376
internal static void UpdateEntryPoint(string inputPath, string outputPath, string updatedApplicationPath, string applicationManifestPath, string targetFrameworkVersion)
377377
{
378378
var document = new XmlDocument();
379-
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
380-
using (XmlReader xr = XmlReader.Create(inputPath, xrSettings))
379+
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
380+
FileStream fs = File.OpenRead(inputPath);
381+
using (XmlReader xr = XmlReader.Create(fs, xrSettings))
381382
{
382383
document.Load(xr);
383384
}

src/Tasks/ManifestUtil/ManifestReader.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,17 @@ private static XmlDocument GetXmlDocument(string path)
6161
// if first two bytes are "MZ" then we're looking at an .exe or a .dll not a .manifest
6262
if ((buffer[0] == 0x4D) && (buffer[1] == 0x5A))
6363
{
64-
Stream m = EmbeddedManifestReader.Read(path);
65-
if (m == null)
64+
using (Stream m = EmbeddedManifestReader.Read(path))
6665
{
67-
throw new BadImageFormatException(null, path);
68-
}
66+
if (m == null)
67+
{
68+
throw new BadImageFormatException(null, path);
69+
}
6970

70-
using (XmlReader xr = XmlReader.Create(m, xrSettings))
71-
{
72-
document.Load(xr);
71+
using (XmlReader xr = XmlReader.Create(m, xrSettings))
72+
{
73+
document.Load(xr);
74+
}
7375
}
7476
}
7577
else

src/Tasks/ManifestUtil/SecurityUtil.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,10 @@ private static void SignFileInternal(X509Certificate2 cert,
694694
try
695695
{
696696
var doc = new XmlDocument { PreserveWhitespace = true };
697-
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
698-
using (XmlReader xr = XmlReader.Create(path, xrSettings))
697+
var xrSettings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore, CloseInput = true };
698+
FileStream fs = File.OpenRead(path);
699+
700+
using (XmlReader xr = XmlReader.Create(fs, xrSettings))
699701
{
700702
doc.Load(xr);
701703
}

0 commit comments

Comments
 (0)