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

Conversation

JaynieBai
Copy link
Member

@JaynieBai JaynieBai commented Apr 12, 2023

Fixes #8570 part 1

Context

UseTrustedSettings parameter for XslTransformation task is ignored when using dotnet build since the resolver is not setting.

Changes Made

Use an XslCompiledTransform.Transform overload that takes an XmlResolver parameter.
Add the info log message when the UseTrustedSettings is opted-in on the Task

Testing

Enable the earlier disabled test XslDocumentFunctionWorks() on net7.0

Notes

Copy link
Member

@JanKrivanek JanKrivanek left a comment

Choose a reason for hiding this comment

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

Looks good, security approved offline.
I have few minor things for consideration

  • FEATURE_COMPILED_XSL removal

  • Do we actually need to explictly opt-in in our code via?

AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);
  • Additionaly - it was advised to us offline that it'd be nice to log a message pointing to the doc (https://aka.ms/msbuild/xsltransformation-task) in case the UseTrustedSettings is opted in (so that users are pointed to the further details, informing about security implications and considerations).

@JaynieBai JaynieBai marked this pull request as draft April 25, 2023 10:11
…g log or binlog. And the recursively (concatenated) error in the error message.
@JanKrivanek
Copy link
Member

Thanks @JaynieBai for updating this PR.

Can you please as well add the info log message when the UseTrustedSettings is opted-in on the Task?

This was suggested by security during the approval for this. The formulation can be something like: "You are enabling relaxed XSLT security settings via UseTrustedSettings parameter. For more details on security implications of this settings please see https://aka.ms/msbuild/xsltransformation-task". It should be localized.

@JaynieBai
Copy link
Member Author

Thanks @JaynieBai for updating this PR.

Can you please as well add the info log message when the UseTrustedSettings is opted-in on the Task?

This was suggested by security during the approval for this. The formulation can be something like: "You are enabling relaxed XSLT security settings via UseTrustedSettings parameter. For more details on security implications of this settings please see https://aka.ms/msbuild/xsltransformation-task". It should be localized.

Have added the log before the transform. Here is output
image

@JaynieBai JaynieBai marked this pull request as ready for review April 28, 2023 01:50
@@ -2329,6 +2329,9 @@
</data>
<data name="XslTransform.UseTrustedSettings" xml:space="preserve">
<value>The usage of the document() method and embedded scripts is prohibited by default, due to risks of foreign code execution. If "{0}" is a trusted source that requires those constructs, please set the "UseTrustedSettings" parameter to "true" to allow their execution.</value>
</data>
<data name="XslTransform.SecuritySettingsViaUseTrustedSettings" xml:space="preserve">
<value>You are enabling relaxed XSLT security settings via UseTrustedSettings parameter. For more details on security implications of this settings please see https://aka.ms/msbuild/xsltransformation-task</value>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<value>You are enabling relaxed XSLT security settings via UseTrustedSettings parameter. For more details on security implications of this settings please see https://aka.ms/msbuild/xsltransformation-task</value>
<value>You are enabling relaxed XSLT security settings via the UseTrustedSettings parameter. For more details on security implications of this setting please see https://aka.ms/msbuild/xsltransformation-task</value>

for (int i = 0; i < xmlinput.Count; i++)
{
using (XmlWriter xmlWriter = XmlWriter.Create(_outputPaths[i].ItemSpec, xslct.OutputSettings))
{
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

@edstegman
Copy link

@JaynieBai

I'm sorry to bother you, but how and when does this fix get delivered? I see the fix was merged almost 2 months ago, but the Azure DevOps build is still failing, as is my local build.

The ADO build and my local build report using MSBuild version 17.6.8+c70978d4d for .NET

@rainersigwald
Copy link
Member

@edstegman the fix will be in MSBuild 17.7, which will release with Visual Studio 17.7 "soon".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: UseTrustedSettings parameter for XslTransformation task is ignored when using dotnet build
4 participants