From dc483c88af50be5afc58ad947322ca023c8ac8cd Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Fri, 31 Mar 2023 13:50:10 +0200 Subject: [PATCH 1/6] Add switch: Do not resolve URLs by defaults for XML --- .../src/ILLink/ILLink.Substitutions.xml | 8 +++ .../src/System.Private.Xml.csproj | 13 +++-- .../src/System/Xml/AppContextSwitchHelper.cs | 14 +++++ .../Xml/Core/LocalAppContextSwitches.cs | 1 + .../src/System/Xml/Core/XmlReader.cs | 2 +- .../src/System/Xml/Core/XmlReaderSettings.cs | 9 ++- .../src/System/Xml/Core/XmlTextReaderImpl.cs | 2 +- .../Xml/Core/XmlValidatingReaderImpl.cs | 2 +- .../src/System/Xml/Schema/XmlSchemaSet.cs | 4 +- .../Xml/XmlResolver.FileSystemResolver.cs | 55 +++++++++++++++++++ .../src/System/Xml/Xsl/Xslt/XsltLoader.cs | 2 +- .../System/Xml/Xslt/XslCompiledTransform.cs | 2 +- .../src/System/Xml/Xslt/XslTransform.cs | 2 +- .../ref/System.Xml.ReaderWriter.cs | 1 + 14 files changed, 103 insertions(+), 14 deletions(-) create mode 100644 src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml create mode 100644 src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs create mode 100644 src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs diff --git a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml new file mode 100644 index 0000000000000..cd2ef99048380 --- /dev/null +++ b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj index 2bbb69bb379cd..35098d40e0e44 100644 --- a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj @@ -4,11 +4,18 @@ System.Xml true false + + $(MSBuildThisFileDirectory)ILLink\ + + + + + @@ -42,6 +49,7 @@ True TextUtf8RawTextWriter.tt + TextTemplatingFileGenerator HtmlEncodedRawTextWriter.cs @@ -759,10 +767,7 @@ - + diff --git a/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs b/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs new file mode 100644 index 0000000000000..26dc83299799a --- /dev/null +++ b/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Xml +{ + internal static class AppContextSwitchHelper + { + public static bool AllowResolvingUrlsByDefault { get; } = + AppContext.TryGetSwitch( + switchName: "System.Xml.AllowResolvingUrlsByDefault", + isEnabled: out bool value) + ? value : true; + } +} diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs index 8bc1f54dc8b50..3cc6b36a7bec6 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace System diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs index e0f76fe5fad1c..80a1c18372ab7 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReader.cs @@ -1612,7 +1612,7 @@ public static XmlReader Create(string inputUri) // Avoid using XmlReader.Create(string, XmlReaderSettings), as it references a lot of types // that then can't be trimmed away. - return new XmlTextReaderImpl(inputUri, XmlReaderSettings.s_defaultReaderSettings, null, new XmlUrlResolver()); + return new XmlTextReaderImpl(inputUri, XmlReaderSettings.s_defaultReaderSettings, null, XmlReaderSettings.GetDefaultPermissiveResolver()); } // Creates an XmlReader according to the settings for parsing XML from the given Uri. diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs index 68adf4d128807..17aafa8dc93f8 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs @@ -321,7 +321,7 @@ internal XmlReader CreateReader(string inputUri, XmlParserContext? inputContext) ArgumentException.ThrowIfNullOrEmpty(inputUri); // resolve and open the url - XmlResolver tmpResolver = GetXmlResolver() ?? new XmlUrlResolver(); + XmlResolver tmpResolver = GetXmlResolver() ?? GetDefaultPermissiveResolver(); // create text XML reader XmlReader reader = new XmlTextReaderImpl(inputUri, this, inputContext, tmpResolver); @@ -436,7 +436,7 @@ internal XmlReader AddValidation(XmlReader reader) if (resolver == null && !IsXmlResolverSet) { - resolver = new XmlUrlResolver(); + resolver = GetDefaultPermissiveResolver(); } } @@ -623,6 +623,11 @@ private XmlReader AddConformanceWrapper(XmlReader baseReader) return baseReader; } + internal static XmlResolver GetDefaultPermissiveResolver() + { + return AppContextSwitchHelper.AllowResolvingUrlsByDefault ? new XmlUrlResolver() : XmlResolver.FileSystemResolver; + } + [DoesNotReturn] [StackTraceHidden] private static void ThrowArgumentOutOfRangeException(string paramName) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs index 4de62beab8966..1c118faafb865 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @@ -2532,7 +2532,7 @@ private bool IsResolverNull private XmlResolver GetTempResolver() { - return _xmlResolver ?? new XmlUrlResolver(); + return _xmlResolver ?? XmlReaderSettings.GetDefaultPermissiveResolver(); } internal bool DtdParserProxy_PushEntity(IDtdEntityInfo entity, out int entityId) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlValidatingReaderImpl.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlValidatingReaderImpl.cs index bb6d8f8337c9f..f2c8caf707a1c 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlValidatingReaderImpl.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlValidatingReaderImpl.cs @@ -1082,7 +1082,7 @@ private void SetupValidation(ValidationType valType) if (tempResolver == null && !_coreReaderImpl.IsResolverSet) { // it is safe to return valid resolver as it'll be used in the schema validation - return s_tempResolver ??= new XmlUrlResolver(); + return s_tempResolver ??= XmlReaderSettings.GetDefaultPermissiveResolver(); } return tempResolver; diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs index 0012e4a3c0312..f8292c5e70a12 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Schema/XmlSchemaSet.cs @@ -106,7 +106,7 @@ public XmlSchemaSet(XmlNameTable nameTable) if (_readerSettings.GetXmlResolver() == null) { // The created resolver will be used in the schema validation only - _readerSettings.XmlResolver = new XmlUrlResolver(); + _readerSettings.XmlResolver = XmlReaderSettings.GetDefaultPermissiveResolver(); _readerSettings.IsXmlResolverSet = false; } @@ -232,7 +232,7 @@ internal Hashtable SchemaLocations lock (InternalSyncObject) { //Check if schema from url has already been added - XmlResolver tempResolver = _readerSettings.GetXmlResolver() ?? new XmlUrlResolver(); + XmlResolver tempResolver = _readerSettings.GetXmlResolver() ?? XmlReaderSettings.GetDefaultPermissiveResolver(); Uri tempSchemaUri = tempResolver.ResolveUri(null, schemaUri); if (IsSchemaLoaded(tempSchemaUri, targetNamespace, out schema)) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs new file mode 100644 index 0000000000000..3f7404e1e2e8b --- /dev/null +++ b/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs @@ -0,0 +1,55 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.IO; +using System.Threading.Tasks; + +namespace System.Xml +{ + public abstract partial class XmlResolver + { + /// + /// Gets an XML resolver which resolves only file system URIs. + /// + /// An XML resolver which resolves only file system URIs. + /// + /// Calling or on the + /// instance returned by this property will resolve only URIs which scheme is file. + /// + public static XmlResolver FileSystemResolver => XmlFileSystemResolver.s_singleton; + + // An XmlResolver that forbids all external entity resolution. + private sealed class XmlFileSystemResolver : XmlResolver + { + internal static readonly XmlFileSystemResolver s_singleton = new(); + + // Private constructor ensures existing only one instance of XmlFileSystemResolver + private XmlFileSystemResolver() { } + + public override object? GetEntity(Uri absoluteUri, string? role, Type? ofObjectToReturn) + { + if ((ofObjectToReturn is null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(object)) + && absoluteUri.Scheme == "file") + { + return new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1); + } + + throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); + } + + public override async Task GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) + { + if (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(object)) + { + if (absoluteUri.Scheme == "file") + { + return await Task.Run(() => new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)) + .ConfigureAwait(false); + } + } + + throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); + } + } + } +} diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs index 42f7802e23f9e..d2f2e2e707745 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xsl/Xslt/XsltLoader.cs @@ -71,7 +71,7 @@ public void Load(Compiler compiler, object stylesheet, XmlResolver? xmlResolver, // for reads beyond the initial stylesheet read) will use a throwing resolver as its // default, as shown at the very top of this method. - origResolver ??= new XmlUrlResolver(); + origResolver ??= XmlReaderSettings.GetDefaultPermissiveResolver(); Uri resolvedUri = origResolver.ResolveUri(null, uri); if (resolvedUri == null) { diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs index c4990141922ce..7c5ca1d4ca350 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslCompiledTransform.cs @@ -406,7 +406,7 @@ private static XmlResolver CreateDefaultResolver() { if (LocalAppContextSwitches.AllowDefaultResolver) { - return new XmlUrlResolver(); + return XmlReaderSettings.GetDefaultPermissiveResolver(); } return XmlResolver.ThrowingResolver; diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslTransform.cs b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslTransform.cs index 15826ec1d9c4f..2d5cfeabf0579 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslTransform.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Xslt/XslTransform.cs @@ -260,7 +260,7 @@ private static XmlResolver CreateDefaultResolver() { if (LocalAppContextSwitches.AllowDefaultResolver) { - return new XmlUrlResolver(); + return XmlReaderSettings.GetDefaultPermissiveResolver(); } else { diff --git a/src/libraries/System.Xml.ReaderWriter/ref/System.Xml.ReaderWriter.cs b/src/libraries/System.Xml.ReaderWriter/ref/System.Xml.ReaderWriter.cs index 68368f91a265e..0c8cb022dc2ca 100644 --- a/src/libraries/System.Xml.ReaderWriter/ref/System.Xml.ReaderWriter.cs +++ b/src/libraries/System.Xml.ReaderWriter/ref/System.Xml.ReaderWriter.cs @@ -952,6 +952,7 @@ public abstract partial class XmlResolver protected XmlResolver() { } public virtual System.Net.ICredentials Credentials { set { } } public static System.Xml.XmlResolver ThrowingResolver { get { throw null; } } + public static System.Xml.XmlResolver FileSystemResolver { get { throw null; } } public abstract object? GetEntity(System.Uri absoluteUri, string? role, System.Type? ofObjectToReturn); public virtual System.Threading.Tasks.Task GetEntityAsync(System.Uri absoluteUri, string? role, System.Type? ofObjectToReturn) { throw null; } public virtual System.Uri ResolveUri(System.Uri? baseUri, string? relativeUri) { throw null; } From 0dc5170647f7440f43107b49af2a082400fea5a5 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 4 Apr 2023 12:49:06 +0200 Subject: [PATCH 2/6] feedback, test --- .../src/ILLink/ILLink.Substitutions.xml | 2 +- .../src/System.Private.Xml.csproj | 8 +- .../src/System/Xml/AppContextSwitchHelper.cs | 14 ---- .../Xml/Core/LocalAppContextSwitches.cs | 84 ++++++++----------- .../src/System/Xml/Core/XmlReaderSettings.cs | 2 +- .../src/System/Xml/XmlDownloadManager.cs | 44 +++++++++- .../src/System/Xml/XmlDownloadManagerAsync.cs | 53 ------------ .../Xml/XmlResolver.FileSystemResolver.cs | 7 +- .../src/System/Xml/XmlUrlResolver.cs | 21 +++-- .../src/System/Xml/XmlUrlResolverAsync.cs | 22 ----- .../System.Private.Xml.TrimmingTests.proj | 6 ++ ...verDefaults.AllowResolvingUrlsByDefault.cs | 44 ++++++++++ ...Defaults.DisallowResolvingUrlsByDefault.cs | 44 ++++++++++ 13 files changed, 190 insertions(+), 161 deletions(-) delete mode 100644 src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs delete mode 100644 src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManagerAsync.cs delete mode 100644 src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolverAsync.cs create mode 100644 src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs create mode 100644 src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs diff --git a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml index cd2ef99048380..b7039268d3390 100644 --- a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml +++ b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml @@ -1,6 +1,6 @@  - + diff --git a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj index 35098d40e0e44..882a2c9bd01a4 100644 --- a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj @@ -4,8 +4,6 @@ System.Xml true false - - $(MSBuildThisFileDirectory)ILLink\ @@ -15,7 +13,6 @@ - @@ -49,7 +46,6 @@ True TextUtf8RawTextWriter.tt - TextTemplatingFileGenerator HtmlEncodedRawTextWriter.cs @@ -127,7 +123,6 @@ - @@ -138,10 +133,10 @@ + - @@ -749,7 +744,6 @@ - diff --git a/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs b/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs deleted file mode 100644 index 26dc83299799a..0000000000000 --- a/src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Xml -{ - internal static class AppContextSwitchHelper - { - public static bool AllowResolvingUrlsByDefault { get; } = - AppContext.TryGetSwitch( - switchName: "System.Xml.AllowResolvingUrlsByDefault", - isEnabled: out bool value) - ? value : true; - } -} diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs index 3cc6b36a7bec6..a07b1270db699 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs @@ -1,61 +1,45 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Linq.Expressions; -using System.Runtime.CompilerServices; - -namespace System +namespace System.Xml { - internal static partial class LocalAppContextSwitches + internal static class LocalAppContextSwitches { - private static int s_dontThrowOnInvalidSurrogatePairs; - public static bool DontThrowOnInvalidSurrogatePairs - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return GetCachedSwitchValue("Switch.System.Xml.DontThrowOnInvalidSurrogatePairs", ref s_dontThrowOnInvalidSurrogatePairs); - } - } + public static bool DontThrowOnInvalidSurrogatePairs { get; } = + AppContext.TryGetSwitch( + switchName: "Switch.System.Xml.DontThrowOnInvalidSurrogatePairs", + isEnabled: out bool value) + ? value : false; + + public static bool IgnoreEmptyKeySequences { get; } = + AppContext.TryGetSwitch( + switchName: "Switch.System.Xml.IgnoreEmptyKeySequences", + isEnabled: out bool value) + ? value : false; + + public static bool IgnoreKindInUtcTimeSerialization { get; } = + AppContext.TryGetSwitch( + switchName: "Switch.System.Xml.IgnoreKindInUtcTimeSerialization", + isEnabled: out bool value) + ? value : false; - private static int s_ignoreEmptyKeySequences; - public static bool IgnoreEmptyKeySequences - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return GetCachedSwitchValue("Switch.System.Xml.IgnoreEmptyKeySequencess", ref s_ignoreEmptyKeySequences); - } - } + public static bool LimitXPathComplexity { get; } = + AppContext.TryGetSwitch( + switchName: "Switch.System.Xml.LimitXPathComplexity", + isEnabled: out bool value) + ? value : false; - private static int s_ignoreKindInUtcTimeSerialization; - public static bool IgnoreKindInUtcTimeSerialization - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return GetCachedSwitchValue("Switch.System.Xml.IgnoreKindInUtcTimeSerialization", ref s_ignoreKindInUtcTimeSerialization); - } - } + public static bool AllowDefaultResolver { get; } = + AppContext.TryGetSwitch( + switchName: "Switch.System.Xml.AllowDefaultResolver", + isEnabled: out bool value) + ? value : false; - private static int s_limitXPathComplexity; - public static bool LimitXPathComplexity - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return GetCachedSwitchValue("Switch.System.Xml.LimitXPathComplexity", ref s_limitXPathComplexity); - } - } - private static int s_allowDefaultResolver; - public static bool AllowDefaultResolver - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return GetCachedSwitchValue("Switch.System.Xml.AllowDefaultResolver", ref s_allowDefaultResolver); - } - } + public static bool AllowResolvingUrlsByDefault { get; } = + AppContext.TryGetSwitch( + switchName: "System.Xml.AllowResolvingUrlsByDefault", + isEnabled: out bool value) + ? value : true; } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs index 17aafa8dc93f8..a068fefd5331c 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs @@ -625,7 +625,7 @@ private XmlReader AddConformanceWrapper(XmlReader baseReader) internal static XmlResolver GetDefaultPermissiveResolver() { - return AppContextSwitchHelper.AllowResolvingUrlsByDefault ? new XmlUrlResolver() : XmlResolver.FileSystemResolver; + return LocalAppContextSwitches.AllowResolvingUrlsByDefault ? new XmlUrlResolver() : XmlResolver.FileSystemResolver; } [DoesNotReturn] diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManager.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManager.cs index 83ebba25199ec..304a346bdaf6f 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManager.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManager.cs @@ -1,13 +1,14 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.IO; using System.Net; +using System.Net.Http; +using System.Threading.Tasks; namespace System.Xml { - internal sealed partial class XmlDownloadManager + internal static class XmlDownloadManager { internal static Stream GetStream(Uri uri, ICredentials? credentials, IWebProxy? proxy) { @@ -22,5 +23,44 @@ internal static Stream GetStream(Uri uri, ICredentials? credentials, IWebProxy? return GetNonFileStreamAsync(uri, credentials, proxy).GetAwaiter().GetResult(); } } + + internal static Task GetStreamAsync(Uri uri, ICredentials? credentials, IWebProxy? proxy) + { + if (uri.Scheme == "file") + { + Uri fileUri = uri; + return Task.FromResult(new FileStream(fileUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)); + } + else + { + return GetNonFileStreamAsync(uri, credentials, proxy); + } + } + + private static async Task GetNonFileStreamAsync(Uri uri, ICredentials? credentials, IWebProxy? proxy) + { + var handler = new HttpClientHandler(); + using (var client = new HttpClient(handler)) + { +#pragma warning disable CA1416 // Validate platform compatibility, 'credentials' and 'proxy' will not be set for browser, so safe to suppress + if (credentials != null) + { + handler.Credentials = credentials; + } + if (proxy != null) + { + handler.Proxy = proxy; + } +#pragma warning restore CA1416 + + using (Stream respStream = await client.GetStreamAsync(uri).ConfigureAwait(false)) + { + var result = new MemoryStream(); + respStream.CopyTo(result); + result.Position = 0; + return result; + } + } + } } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManagerAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManagerAsync.cs deleted file mode 100644 index c2d774d0f4006..0000000000000 --- a/src/libraries/System.Private.Xml/src/System/Xml/XmlDownloadManagerAsync.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.IO; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; - -namespace System.Xml -{ - internal sealed partial class XmlDownloadManager - { - internal static Task GetStreamAsync(Uri uri, ICredentials? credentials, IWebProxy? proxy) - { - if (uri.Scheme == "file") - { - Uri fileUri = uri; - return Task.Run(() => new FileStream(fileUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)); - } - else - { - return GetNonFileStreamAsync(uri, credentials, proxy); - } - } - - private static async Task GetNonFileStreamAsync(Uri uri, ICredentials? credentials, IWebProxy? proxy) - { - var handler = new HttpClientHandler(); - using (var client = new HttpClient(handler)) - { -#pragma warning disable CA1416 // Validate platform compatibility, 'credentials' and 'proxy' will not be set for browser, so safe to suppress - if (credentials != null) - { - handler.Credentials = credentials; - } - if (proxy != null) - { - handler.Proxy = proxy; - } -#pragma warning restore CA1416 - - using (Stream respStream = await client.GetStreamAsync(uri).ConfigureAwait(false)) - { - var result = new MemoryStream(); - respStream.CopyTo(result); - result.Position = 0; - return result; - } - } - } - } -} diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs index 3f7404e1e2e8b..8785d09e9c12c 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs @@ -18,7 +18,7 @@ public abstract partial class XmlResolver /// public static XmlResolver FileSystemResolver => XmlFileSystemResolver.s_singleton; - // An XmlResolver that forbids all external entity resolution. + // An XML resolver that resolves only file system URIs. private sealed class XmlFileSystemResolver : XmlResolver { internal static readonly XmlFileSystemResolver s_singleton = new(); @@ -37,14 +37,13 @@ private XmlFileSystemResolver() { } throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); } - public override async Task GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) + public override Task GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) { if (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(object)) { if (absoluteUri.Scheme == "file") { - return await Task.Run(() => new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)) - .ConfigureAwait(false); + return Task.FromResult(new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)); } } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolver.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolver.cs index 558ad2e4b0218..d915266db483b 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolver.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolver.cs @@ -5,21 +5,17 @@ using System.Net; using System.Net.Cache; using System.Runtime.Versioning; +using System.Threading.Tasks; +using System.IO; namespace System.Xml { // Resolves external XML resources named by a Uniform Resource Identifier (URI). public partial class XmlUrlResolver : XmlResolver { - private static XmlDownloadManager? s_downloadManager; private ICredentials? _credentials; private IWebProxy? _proxy; - private static XmlDownloadManager DownloadManager => - s_downloadManager ?? - Interlocked.CompareExchange(ref s_downloadManager, new XmlDownloadManager(), null) ?? - s_downloadManager; - public XmlUrlResolver() { } [UnsupportedOSPlatform("browser")] @@ -42,7 +38,7 @@ public RequestCachePolicy CachePolicy // Maps a URI to an Object containing the actual resource. public override object? GetEntity(Uri absoluteUri, string? role, Type? ofObjectToReturn) { - if (ofObjectToReturn is null || ofObjectToReturn == typeof(System.IO.Stream) || ofObjectToReturn == typeof(object)) + if (ofObjectToReturn is null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(object)) { return XmlDownloadManager.GetStream(absoluteUri, _credentials, _proxy); } @@ -50,6 +46,17 @@ public RequestCachePolicy CachePolicy throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); } + // Maps a URI to an Object containing the actual resource. + public override async Task GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) + { + if (ofObjectToReturn == null || ofObjectToReturn == typeof(Stream) || ofObjectToReturn == typeof(object)) + { + return await XmlDownloadManager.GetStreamAsync(absoluteUri, _credentials, _proxy).ConfigureAwait(false); + } + + throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); + } + public override Uri ResolveUri(Uri? baseUri, string? relativeUri) => base.ResolveUri(baseUri, relativeUri); } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolverAsync.cs b/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolverAsync.cs deleted file mode 100644 index 17ac98df3eb8b..0000000000000 --- a/src/libraries/System.Private.Xml/src/System/Xml/XmlUrlResolverAsync.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.Versioning; -using System.Threading.Tasks; - -namespace System.Xml -{ - public partial class XmlUrlResolver : XmlResolver - { - // Maps a URI to an Object containing the actual resource. - public override async Task GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) - { - if (ofObjectToReturn == null || ofObjectToReturn == typeof(System.IO.Stream) || ofObjectToReturn == typeof(object)) - { - return await XmlDownloadManager.GetStreamAsync(absoluteUri, _credentials, _proxy).ConfigureAwait(false); - } - - throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); - } - } -} diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj b/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj index 181be44826a6a..6af9fd99525b7 100644 --- a/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj +++ b/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj @@ -8,6 +8,12 @@ ExtraTrimmerArgs="--enable-opt sealer" /> + + System.Xml.AllowResolvingUrlsByDefault + + + System.Xml.AllowResolvingUrlsByDefault + diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs new file mode 100644 index 0000000000000..6363bae5833a9 --- /dev/null +++ b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +using System; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; + +class XmlUrlResolverDefaults +{ + public static int Main() + { + File.WriteAllText("file.xml", """ + + + test-value + + """); + + XDocument doc = XDocument.Load("file.xml"); + + string value = doc.Descendants("some-element").Single().Value; + if (value == "test-value") + { + Type? urlResolver = GetXmlType("System.Xml.XmlUrlResolver"); + if (urlResolver != null) + { + // we should preserve the type but we want to avoid doing web requests during the test + return 100; + } + + return -1; + } + + return -2; + } + + // The intention of this method is to ensure the trimmer doesn't preserve the Type. + private static Type? GetXmlType(string name) => + typeof(XmlReader).Assembly.GetType(name, throwOnError: false); +} diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs new file mode 100644 index 0000000000000..683b1d960be37 --- /dev/null +++ b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs @@ -0,0 +1,44 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +using System; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; + +class XmlUrlResolverDefaults +{ + public static int Main() + { + File.WriteAllText("file.xml", """ + + + test-value + + """); + + XDocument doc = XDocument.Load("file.xml"); + + string value = doc.Descendants("some-element").Single().Value; + if (value == "test-value") + { + Type? urlResolver = GetXmlType("System.Xml.XmlUrlResolver"); + if (urlResolver == null) + { + // we should not preserve the type + return 100; + } + + return -1; + } + + return -2; + } + + // The intention of this method is to ensure the trimmer doesn't preserve the Type. + private static Type? GetXmlType(string name) => + typeof(XmlReader).Assembly.GetType(name, throwOnError: false); +} From 5c49b266ebdc5669bb637d85013cb572592c70f2 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Mon, 17 Jul 2023 15:23:13 +0200 Subject: [PATCH 3/6] rename switch per API review --- .../src/ILLink/ILLink.Substitutions.xml | 4 ++-- .../src/System/Xml/Core/LocalAppContextSwitches.cs | 4 ++-- .../src/System/Xml/Core/XmlReaderSettings.cs | 2 +- .../TrimmingTests/System.Private.Xml.TrimmingTests.proj | 8 ++++---- ...lverDefaults.Disabled.IsNetworkingEnabledByDefault.cs} | 0 ...mlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs} | 0 6 files changed, 9 insertions(+), 9 deletions(-) rename src/libraries/System.Private.Xml/tests/TrimmingTests/{XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs => XmlUrlResolverDefaults.Disabled.IsNetworkingEnabledByDefault.cs} (100%) rename src/libraries/System.Private.Xml/tests/TrimmingTests/{XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs => XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs} (100%) diff --git a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml index b7039268d3390..52cf62fc63618 100644 --- a/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml +++ b/src/libraries/System.Private.Xml/src/ILLink/ILLink.Substitutions.xml @@ -1,8 +1,8 @@  - + diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs index a07b1270db699..7299c2ba41ca2 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs @@ -36,9 +36,9 @@ internal static class LocalAppContextSwitches ? value : false; - public static bool AllowResolvingUrlsByDefault { get; } = + public static bool IsNetworkingEnabledByDefault { get; } = AppContext.TryGetSwitch( - switchName: "System.Xml.AllowResolvingUrlsByDefault", + switchName: "System.Xml.XmlResolver.IsNetworkingEnabledByDefault", isEnabled: out bool value) ? value : true; } diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs index a068fefd5331c..f4d873161c1f2 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs @@ -625,7 +625,7 @@ private XmlReader AddConformanceWrapper(XmlReader baseReader) internal static XmlResolver GetDefaultPermissiveResolver() { - return LocalAppContextSwitches.AllowResolvingUrlsByDefault ? new XmlUrlResolver() : XmlResolver.FileSystemResolver; + return LocalAppContextSwitches.IsNetworkingEnabledByDefault ? new XmlUrlResolver() : XmlResolver.FileSystemResolver; } [DoesNotReturn] diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj b/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj index 6af9fd99525b7..acb15fc654be9 100644 --- a/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj +++ b/src/libraries/System.Private.Xml/tests/TrimmingTests/System.Private.Xml.TrimmingTests.proj @@ -8,11 +8,11 @@ ExtraTrimmerArgs="--enable-opt sealer" /> - - System.Xml.AllowResolvingUrlsByDefault + + System.Xml.XmlResolver.IsNetworkingEnabledByDefault - - System.Xml.AllowResolvingUrlsByDefault + + System.Xml.XmlResolver.IsNetworkingEnabledByDefault diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.Disabled.IsNetworkingEnabledByDefault.cs similarity index 100% rename from src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.DisallowResolvingUrlsByDefault.cs rename to src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.Disabled.IsNetworkingEnabledByDefault.cs diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs similarity index 100% rename from src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.AllowResolvingUrlsByDefault.cs rename to src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs From 7dcbdbd464e14368cb0137af7141608e9d9e09c0 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Mon, 17 Jul 2023 19:54:24 +0200 Subject: [PATCH 4/6] Apply missed feedback, fix test failure --- .../System/LocalAppContextSwitches.Common.cs | 5 ++ .../src/System.Private.Xml.csproj | 1 + .../Xml/Core/LocalAppContextSwitches.cs | 88 ++++++++++++------- .../tests/AllowDefaultResolverContext.cs | 2 +- 4 files changed, 64 insertions(+), 32 deletions(-) diff --git a/src/libraries/Common/src/System/LocalAppContextSwitches.Common.cs b/src/libraries/Common/src/System/LocalAppContextSwitches.Common.cs index 555751c6947a4..19806ceee1c79 100644 --- a/src/libraries/Common/src/System/LocalAppContextSwitches.Common.cs +++ b/src/libraries/Common/src/System/LocalAppContextSwitches.Common.cs @@ -55,6 +55,11 @@ private static bool GetSwitchDefaultValue(string switchName) return true; } + if (switchName == "System.Xml.XmlResolver.IsNetworkingEnabledByDefault") + { + return true; + } + return false; } } diff --git a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj index 882a2c9bd01a4..b05a319099867 100644 --- a/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/libraries/System.Private.Xml/src/System.Private.Xml.csproj @@ -744,6 +744,7 @@ + diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs index 7299c2ba41ca2..308761649e355 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs @@ -1,45 +1,71 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; +using SwitchesHelpers = System.LocalAppContextSwitches; + namespace System.Xml { internal static class LocalAppContextSwitches { - public static bool DontThrowOnInvalidSurrogatePairs { get; } = - AppContext.TryGetSwitch( - switchName: "Switch.System.Xml.DontThrowOnInvalidSurrogatePairs", - isEnabled: out bool value) - ? value : false; - - public static bool IgnoreEmptyKeySequences { get; } = - AppContext.TryGetSwitch( - switchName: "Switch.System.Xml.IgnoreEmptyKeySequences", - isEnabled: out bool value) - ? value : false; + private static int s_dontThrowOnInvalidSurrogatePairs; + public static bool DontThrowOnInvalidSurrogatePairs + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.DontThrowOnInvalidSurrogatePairs", ref s_dontThrowOnInvalidSurrogatePairs); + } + } - public static bool IgnoreKindInUtcTimeSerialization { get; } = - AppContext.TryGetSwitch( - switchName: "Switch.System.Xml.IgnoreKindInUtcTimeSerialization", - isEnabled: out bool value) - ? value : false; + private static int s_ignoreEmptyKeySequences; + public static bool IgnoreEmptyKeySequences + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.IgnoreEmptyKeySequencess", ref s_ignoreEmptyKeySequences); + } + } - public static bool LimitXPathComplexity { get; } = - AppContext.TryGetSwitch( - switchName: "Switch.System.Xml.LimitXPathComplexity", - isEnabled: out bool value) - ? value : false; + private static int s_ignoreKindInUtcTimeSerialization; + public static bool IgnoreKindInUtcTimeSerialization + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.IgnoreKindInUtcTimeSerialization", ref s_ignoreKindInUtcTimeSerialization); + } + } - public static bool AllowDefaultResolver { get; } = - AppContext.TryGetSwitch( - switchName: "Switch.System.Xml.AllowDefaultResolver", - isEnabled: out bool value) - ? value : false; + private static int s_limitXPathComplexity; + public static bool LimitXPathComplexity + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.LimitXPathComplexity", ref s_limitXPathComplexity); + } + } + private static int s_allowDefaultResolver; + public static bool AllowDefaultResolver + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.AllowDefaultResolver", ref s_allowDefaultResolver); + } + } - public static bool IsNetworkingEnabledByDefault { get; } = - AppContext.TryGetSwitch( - switchName: "System.Xml.XmlResolver.IsNetworkingEnabledByDefault", - isEnabled: out bool value) - ? value : true; + private static int s_isNetworkingEnabledByDefault; + public static bool IsNetworkingEnabledByDefault + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return SwitchesHelpers.GetCachedSwitchValue("System.Xml.XmlResolver.IsNetworkingEnabledByDefault", ref s_isNetworkingEnabledByDefault); + } + } } } diff --git a/src/libraries/System.Private.Xml/tests/AllowDefaultResolverContext.cs b/src/libraries/System.Private.Xml/tests/AllowDefaultResolverContext.cs index 9d56769158039..24e626e67b135 100644 --- a/src/libraries/System.Private.Xml/tests/AllowDefaultResolverContext.cs +++ b/src/libraries/System.Private.Xml/tests/AllowDefaultResolverContext.cs @@ -32,7 +32,7 @@ private static void ClearCachedSwitch() // the default, or alternatively separate all such tests into a separate test project // where that project contains only those tests that require the switch set. - Type t = typeof(XmlConvert).Assembly.GetType("System.LocalAppContextSwitches"); + Type t = typeof(XmlConvert).Assembly.GetType("System.Xml.LocalAppContextSwitches"); Assert.NotNull(t); FieldInfo fi = t.GetField("s_allowDefaultResolver", BindingFlags.NonPublic | BindingFlags.Static); From 3792282f32578e6619ae88614acdd9f2424e94c8 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 18 Jul 2023 08:10:07 +0200 Subject: [PATCH 5/6] Fix typo in IgnoreEmptyKeySequences MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Cantú --- .../src/System/Xml/Core/LocalAppContextSwitches.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs index 308761649e355..a95f9105051da 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs @@ -24,7 +24,7 @@ public static bool IgnoreEmptyKeySequences [MethodImpl(MethodImplOptions.AggressiveInlining)] get { - return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.IgnoreEmptyKeySequencess", ref s_ignoreEmptyKeySequences); + return SwitchesHelpers.GetCachedSwitchValue("Switch.System.Xml.IgnoreEmptyKeySequences", ref s_ignoreEmptyKeySequences); } } From 608da910af0e6ed04062983494269c478cd689cf Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 18 Jul 2023 08:10:23 +0200 Subject: [PATCH 6/6] Update src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: David Cantú --- .../XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs index 6363bae5833a9..6dafb02774e37 100644 --- a/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs +++ b/src/libraries/System.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs @@ -38,7 +38,7 @@ public static int Main() return -2; } - // The intention of this method is to ensure the trimmer doesn't preserve the Type. + // The intention of this method is to ensure the trimmer preserves the Type. private static Type? GetXmlType(string name) => typeof(XmlReader).Assembly.GetType(name, throwOnError: false); }