Skip to content

Add Xml default resolver parameter for XslCompiledTransform.Transform #8655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Tasks.UnitTests/XslTransformation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public sealed class XslTransformation_Tests
private readonly string _xslDocument = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\" exclude-result-prefixes=\"msxsl\"><xsl:output method=\"xml\" indent=\"yes\"/><xsl:template match=\"@* | node()\"><surround><xsl:copy><xsl:apply-templates select=\"@* | node()\"/></xsl:copy></surround></xsl:template></xsl:stylesheet>";


#if FEATURE_COMPILED_XSL
/// <summary>
/// The contents of another xsl document for tests
/// </summary>
private readonly string _xslDocument2 = "<?xml version = \"1.0\" ?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match = \"myInclude\"><xsl:apply-templates select = \"document(@path)\"/></xsl:template><xsl:template match = \"@*|node()\"><xsl:copy><xsl:apply-templates select = \"@*|node()\"/></xsl:copy></xsl:template></xsl:stylesheet>";
#endif
/// <summary>
/// The contents of xslparameters for tests.
/// </summary>
Expand Down Expand Up @@ -1040,7 +1038,6 @@ public void MultipleXmlInputs_NotMatching()
CleanUp(dir);
}

#if FEATURE_COMPILED_XSL
/// <summary>
/// Validate that the XslTransformation task allows use of the document function
/// </summary>
Expand Down Expand Up @@ -1094,7 +1091,6 @@ public void XslDocumentFunctionWorks()

CleanUp(dir);
}
#endif

/// <summary>
/// Prepares the test environment, creates necessary files.
Expand Down
13 changes: 11 additions & 2 deletions src/Tasks/XslTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -165,7 +166,7 @@ public override bool Execute()
{
using (XmlReader xr = xmlinput.CreateReader(i))
{
xslct.Transform(xr, arguments, xmlWriter);
xslct.Transform(xr, arguments, xmlWriter, new XmlUrlResolver());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels weird to me to always pass the resolver, even when not trusted. I see though that it's also always passed to the constructors of the XslCompiledTransform.

Should we pass it only when UseTrustedSettings is true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since .NET Core does not allow resolving external URIs for XML by default. So, add that default resolver explicitly. If the UseTrustedSettings is false, it won't allow the XSLT document() function. The XmlUrlResolver that resolves the document function won't work.
If the UseTrustedSettings is false and there is document, it will report the following error before resolve the document.
image

}

xmlWriter.Close();
Expand All @@ -174,7 +175,15 @@ public override bool Execute()
}
catch (Exception e) when (!ExceptionHandling.IsCriticalException(e))
{
Log.LogErrorWithCodeFromResources("XslTransform.TransformError", e.Message);
StringBuilder flattenedMessage = new StringBuilder(e.Message);
Exception excep = e;
while (excep.InnerException != null)
{
excep = excep.InnerException;
flattenedMessage.Append(" ---> ").Append(excep.Message);
}
Log.LogErrorWithCodeFromResources("XslTransform.TransformError", flattenedMessage.ToString());
Log.LogMessage(MessageImportance.Low, e.ToString());
return false;
}

Expand Down