Skip to content

[X] Protect some xmlns #28909

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions src/Controls/src/Build.Tasks/XmlTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
Expand Down Expand Up @@ -26,14 +27,24 @@ static IList<XmlnsDefinitionAttribute> GatherXmlnsDefinitionAttributes(ModuleDef
if (ca.AttributeType.FullName == typeof(XmlnsDefinitionAttribute).FullName)
{
var attr = GetXmlnsDefinition(ca, asmDef);
//maui, and x: xmlns are protected
if (attr.XmlNamespace.StartsWith("http://schemas.microsoft.com/", StringComparison.OrdinalIgnoreCase) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Would using https:// instead of http:// allow me to bypass this check?

I'm not exactly sure if this is supposed to be a hard check or a soft check.

!attr.AssemblyName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase) &&
!attr.AssemblyName.StartsWith("System", StringComparison.OrdinalIgnoreCase) &&
!attr.AssemblyName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase))
{
throw new BuildException(BuildExceptionCode.InvalidXaml, null, null,
$"Protected Xmlns {attr.XmlNamespace}. Can't add assembly {attr.AssemblyName}.");
}

xmlnsDefinitions.Add(attr);
}
}
}
}
else
{
// Use standard XF assemblies
// Use standard MAUI assemblies
// (Should only happen in unit tests)
var requiredAssemblies = new[] {
typeof(XamlLoader).Assembly,
Expand All @@ -42,7 +53,16 @@ static IList<XmlnsDefinitionAttribute> GatherXmlnsDefinitionAttributes(ModuleDef
foreach (var assembly in requiredAssemblies)
foreach (XmlnsDefinitionAttribute attribute in assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), false))
{
attribute.AssemblyName = attribute.AssemblyName ?? assembly.FullName;
attribute.AssemblyName ??= assembly.FullName;
//maui, and x: xmlns are protected
if (attribute.XmlNamespace.StartsWith("http://schemas.microsoft.com/", StringComparison.OrdinalIgnoreCase) &&
!attribute.AssemblyName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase) &&
!attribute.AssemblyName.StartsWith("System", StringComparison.OrdinalIgnoreCase) &&
!attribute.AssemblyName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase))
{
throw new BuildException(BuildExceptionCode.InvalidXaml, null, null,
$"Protected Xmlns {attribute.XmlNamespace}. Can't add assembly {attribute.AssemblyName}."); }

xmlnsDefinitions.Add(attribute);
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/Controls/src/Xaml/XamlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,25 @@ static IValueNode GetValueNode(object value, XmlReader reader)
static void GatherXmlnsDefinitionAttributes()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
s_xmlnsDefinitions = new List<XmlnsDefinitionAttribute>();
s_xmlnsDefinitions = [];
Copy link
Preview

Copilot AI Apr 10, 2025

Choose a reason for hiding this comment

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

Using [] to initialize s_xmlnsDefinitions is not valid in C#. Please replace it with a proper List initialization, e.g., 'new List();'.

Suggested change
s_xmlnsDefinitions = [];
s_xmlnsDefinitions = new List<XmlnsDefinitionAttribute>();

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Copy link
Member

Choose a reason for hiding this comment

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

Try the latest c# Mr AI


foreach (var assembly in assemblies)
{
try
{
foreach (XmlnsDefinitionAttribute attribute in assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute)))
{
attribute.AssemblyName ??= assembly.FullName;
//maui, and x: xmlns are protected
if (attribute.XmlNamespace.StartsWith("http://schemas.microsoft.com/", StringComparison.Ordinal) &&
!attribute.AssemblyName.StartsWith("Microsoft", StringComparison.Ordinal) &&
!attribute.AssemblyName.StartsWith("System", StringComparison.Ordinal) &&
!attribute.AssemblyName.StartsWith("mscorlib", StringComparison.Ordinal))
{
Debug.WriteLine($"Can not overloadxmlns {attribute.XmlNamespace}. cause it's protected.");
continue;
}
s_xmlnsDefinitions.Add(attribute);
attribute.AssemblyName = attribute.AssemblyName ?? assembly.FullName;
}
}
catch (Exception ex)
Expand Down
Loading