Skip to content

Hollowed out public types extending from System.Security.Permissions types #869

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
Jun 13, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

namespace System.Xaml.Permissions
{
/// <SecurityNote>
/// This class is immutable. Various consumers copy references into SecurityCritical fields,
/// and their security depends on the immutability of the members defined here.
/// Derived classes may add mutable members, those have no impact on the consumers of this class.
/// </SecurityNote>
[Serializable]
public class XamlAccessLevel
{
Expand All @@ -23,59 +18,28 @@ private XamlAccessLevel(string assemblyName, string typeName)

public static XamlAccessLevel AssemblyAccessTo(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
return new XamlAccessLevel(assembly.FullName, null);
}

public static XamlAccessLevel AssemblyAccessTo(AssemblyName assemblyName)
{
if (assemblyName == null)
{
throw new ArgumentNullException(nameof(assemblyName));
}
ValidateAssemblyName(assemblyName, "assemblyName");
return new XamlAccessLevel(assemblyName.FullName, null);
}

public static XamlAccessLevel PrivateAccessTo(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
return new XamlAccessLevel(type.Assembly.FullName, type.FullName);
}

public static XamlAccessLevel PrivateAccessTo(string assemblyQualifiedTypeName)
{
if (assemblyQualifiedTypeName == null)
{
throw new ArgumentNullException(nameof(assemblyQualifiedTypeName));
}
int nameBoundary = assemblyQualifiedTypeName.IndexOf(',');
if (nameBoundary < 0)
{
throw new ArgumentException(SR.Get(SRID.ExpectedQualifiedTypeName, assemblyQualifiedTypeName), nameof(assemblyQualifiedTypeName));
}

string typeName = assemblyQualifiedTypeName.Substring(0, nameBoundary).Trim();
string assemblyFullName = assemblyQualifiedTypeName.Substring(nameBoundary + 1).Trim();
AssemblyName assemblyName = new AssemblyName(assemblyFullName);
ValidateAssemblyName(assemblyName, "assemblyQualifiedTypeName");

return new XamlAccessLevel(assemblyName.FullName, typeName);
}

// Read-only: these properties should not be allowed to be modified once this object
// has been passed to XamlLoadPermission

// Stored as string: we need to store the assembly and type names, rather than Assembly or
// Type references, because permissions can be serialized, and we don't want to force an
// assembly load on deserialization in a different AppDomain.

}

public AssemblyName AssemblyAccessToAssemblyName
{
get { return new AssemblyName(AssemblyNameString); }
Expand All @@ -84,67 +48,5 @@ public AssemblyName AssemblyAccessToAssemblyName
public string PrivateAccessToTypeName { get; private set; }

internal string AssemblyNameString { get; private set; }

internal XamlAccessLevel AssemblyOnly()
{
return new XamlAccessLevel(AssemblyNameString, null);
}

internal static XamlAccessLevel FromXml(SecurityElement elem)
{
if (elem.Tag != XmlConstants.XamlAccessLevel)
{
throw new ArgumentException(SR.Get(SRID.SecurityXmlUnexpectedTag, elem.Tag, XmlConstants.XamlAccessLevel), nameof(elem));
}

string assemblyNameString = elem.Attribute(XmlConstants.AssemblyName);
if (assemblyNameString == null)
{
throw new ArgumentException(SR.Get(SRID.SecurityXmlMissingAttribute, XmlConstants.AssemblyName), nameof(elem));
}
AssemblyName assemblyName = new AssemblyName(assemblyNameString);
ValidateAssemblyName(assemblyName, "elem");

string typeName = elem.Attribute(XmlConstants.TypeName);
if (typeName != null)
{
typeName = typeName.Trim();
}

return new XamlAccessLevel(assemblyName.FullName, typeName);
}

internal bool Includes(XamlAccessLevel other)
{
return other.AssemblyNameString == AssemblyNameString &&
(other.PrivateAccessToTypeName == null || other.PrivateAccessToTypeName == PrivateAccessToTypeName);
}

internal SecurityElement ToXml()
{
SecurityElement element = new SecurityElement(XmlConstants.XamlAccessLevel);
element.AddAttribute(XmlConstants.AssemblyName, AssemblyNameString);
if (PrivateAccessToTypeName != null)
{
element.AddAttribute(XmlConstants.TypeName, PrivateAccessToTypeName);
}
return element;
}

private static void ValidateAssemblyName(AssemblyName assemblyName, string argName)
{
if (assemblyName.Name == null || assemblyName.Version == null ||
assemblyName.CultureInfo == null || assemblyName.GetPublicKeyToken() == null)
{
throw new ArgumentException(SR.Get(SRID.ExpectedQualifiedAssemblyName, assemblyName.FullName), argName);
}
}

private static class XmlConstants
{
public const string XamlAccessLevel = "XamlAccessLevel";
public const string AssemblyName = "AssemblyName";
public const string TypeName = "TypeName";
}
}
}
Loading