Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit f4332ab

Browse files
authored
Change DefaultResolver in XmlReaderSettings to XmlUrlResolver (#19803)
* Change DefaultResolver in XmlReaderSettings to XmlUrlResolver * Address feedback and fix failures
1 parent bfe3363 commit f4332ab

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

src/System.Private.Xml/src/System/Xml/Core/XmlReaderSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ private void Initialize(XmlResolver resolver)
569569

570570
private static XmlResolver CreateDefaultResolver()
571571
{
572-
return new XmlSystemPathResolver();
572+
return new XmlUrlResolver();
573573
}
574574

575575
internal XmlReader AddValidation(XmlReader reader)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" standalone="no" ?>
2+
<!DOCTYPE doc [
3+
<!ENTITY otherFile SYSTEM "ResolveDTD_2.xml">
4+
]>
5+
<doc>
6+
<foo>
7+
<bar>&otherFile;</bar>
8+
</foo>
9+
</doc>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" standalone="no" ?>
2+
<doc>
3+
<foo>
4+
<bar><baz>this is my content</baz></bar>
5+
</foo>
6+
</doc>

src/System.Private.Xml/tests/XmlReader/Tests/AsyncReaderLateInitTests.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,45 @@ public static void ReadAfterInitializationWithTextReaderOnAsyncReaderDoesNotThro
6868

6969
[Fact]
7070
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap | //[ActiveIssue(13121)] // Path cannot be resolved in UWP
71-
TargetFrameworkMonikers.NetFramework)] // Full framework uses XmlUrlResolver instead of SystemPathResolver (which doesn't allow access to this path)
71+
TargetFrameworkMonikers.NetFramework)] //[ActiveIssue(19806)] // Full framework uses WebRequest instead of HttpRequest
7272
public static void ReadAsyncAfterInitializationWithUriThrows()
7373
{
7474
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
7575
{
76-
Assert.Throws<XmlException>(() => reader.ReadAsync().GetAwaiter().GetResult());
76+
Assert.Throws<System.Net.Http.HttpRequestException>(() => reader.ReadAsync().GetAwaiter().GetResult());
77+
}
78+
}
79+
80+
[Fact]
81+
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] // Full framework uses WebRequest instead of HttpRequest
82+
public static void ReadAsyncAfterInitializationWithUriThrows_FullFramework()
83+
{
84+
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
85+
{
86+
Assert.Throws<System.Net.WebException>(() => reader.ReadAsync().GetAwaiter().GetResult());
7787
}
7888
}
7989

8090
[Fact]
8191
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap | //[ActiveIssue(13121)] // Path cannot be resolved in UWP
82-
TargetFrameworkMonikers.NetFramework)] // Full framework uses XmlUrlResolver instead of SystemPathResolver (which doesn't allow access to this path)
92+
TargetFrameworkMonikers.NetFramework)] //[ActiveIssue(19806)] // Full framework uses WebRequest instead of HttpRequest
8393
public static void ReadAfterInitializationWithUriOnAsyncReaderTrows()
8494
{
8595
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
8696
{
87-
Assert.Throws<XmlException>(() => reader.Read());
97+
AggregateException ae = Assert.Throws<System.AggregateException>(() => reader.Read());
98+
Assert.Equal(typeof(System.Net.Http.HttpRequestException), ae.InnerException.GetType());
99+
}
100+
}
101+
102+
[Fact]
103+
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] // Full framework uses WebRequest instead of HttpRequest
104+
public static void ReadAfterInitializationWithUriOnAsyncReaderTrows_FullFramework()
105+
{
106+
using (XmlReader reader = XmlReader.Create("http://test.test/test.html", new XmlReaderSettings() { Async = true }))
107+
{
108+
AggregateException ae = Assert.Throws<System.AggregateException>(() => reader.Read());
109+
Assert.Equal(typeof(System.Net.WebException), ae.InnerException.GetType());
88110
}
89111
}
90112
}

src/System.Private.Xml/tests/XmlReader/XmlResolver/System.Xml.RW.XmlSystemPathResolver.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
</PropertyGroup>
1212
<ItemGroup>
1313
<Compile Include="XmlSystemPathResolverTests.cs" />
14+
<SupplementalTestData Include="..\TestFiles\**\*.*">
15+
<Link>TestFiles\%(RecursiveDir)%(Filename)%(Extension)</Link>
16+
<DestinationDir>TestFiles\%(RecursiveDir)</DestinationDir>
17+
</SupplementalTestData>
1418
</ItemGroup>
1519
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
1620
</Project>

src/System.Private.Xml/tests/XmlReader/XmlResolver/XmlSystemPathResolverTests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,34 @@ public static void TestResolveInvalidPaths()
8787

8888
[Fact]
8989
[SkipOnTargetFramework(TargetFrameworkMonikers.Uap | //[ActiveIssue(13121)] // Access to path is denied in UWP
90-
TargetFrameworkMonikers.NetFramework)] // Full framework uses XmlUrlResolver instead of SystemPathResolver (which doesn't allow access to this path)
90+
TargetFrameworkMonikers.NetFramework)] //[ActiveIssue(19806)] // Full framework uses WebRequest instead of HttpRequest
9191
public static void TestResolveInvalidPath()
9292
{
93-
AssertInvalidPath("ftp://www.bing.com");
93+
Assert.Throws<System.ArgumentException>(() => XmlReader.Create("ftp://www.bing.com")); // Only 'http' and 'https' schemes are allowed.
94+
}
95+
96+
[Fact]
97+
[SkipOnTargetFramework(~TargetFrameworkMonikers.NetFramework)] // Full framework uses WebRequest instead of HttpRequest
98+
public static void TestResolveInvalidPath_FullFramework()
99+
{
100+
Assert.Throws<System.Net.WebException>(() => XmlReader.Create("ftp://www.bing.com"));
101+
}
102+
103+
[Fact]
104+
public static void TestResolveDTD_Default()
105+
{
106+
XmlReaderSettings settings = new XmlReaderSettings();
107+
XmlReader reader = XmlReader.Create("TestFiles/ResolveDTD_1.xml", settings);
108+
Assert.Throws<XmlException>(() => reader.ReadToDescendant("baz")); // For security reasons DTD is prohibited in this XML document.
109+
}
110+
111+
[Fact]
112+
public static void TestResolveDTD_AllowDTDProcessing()
113+
{
114+
XmlReaderSettings settings = new XmlReaderSettings();
115+
settings.DtdProcessing = DtdProcessing.Parse;
116+
XmlReader reader = XmlReader.Create("TestFiles/ResolveDTD_1.xml", settings);
117+
reader.ReadToDescendant("baz");
94118
}
95119
}
96120
}

0 commit comments

Comments
 (0)