CA2123: Las peticiones de vínculos de invalidaciones deben ser idénticas a la base
Elemento | Valor |
---|---|
RuleId | CA2123 |
Category | Microsoft.Security |
Cambio importante | Problemático |
Causa
Un método público o protegido en un tipo público invalida un método o implementa una interfaz y no tiene las mismas Peticiones de vínculos que la interfaz o el método virtual.
Nota
Esta regla está en desuso. Para más información, consulte Reglas en desuso.
Descripción de la regla
Esta regla compara un método con su método base, que es una interfaz o un método virtual de otro tipo y, a continuación, compara las solicitudes de vínculos en cada uno. Se notifica una infracción si el método o el método base tienen una petición de vínculo y el otro no.
Si se infringe esta regla, un llamador malintencionado puede omitir la petición de vínculo tan solo con llamar al método no seguro.
Cómo corregir infracciones
Para corregir una infracción de esta regla, aplique la misma petición de vínculo al método de invalidación o a la implementación. Si esto no es posible, marque el método con una petición completa o quite el atributo por completo.
Cuándo suprimir las advertencias
No suprima las advertencias de esta regla.
Ejemplo
En el ejemplo siguiente se muestran varias infracciones de esta regla.
using System.Security;
using System.Security.Permissions;
using System;
namespace SecurityRulesLibrary
{
public interface ITestOverrides
{
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
Object GetFormat(Type formatType);
}
public class OverridesAndSecurity : ITestOverrides
{
// Rule violation: The interface has security, and this implementation does not.
object ITestOverrides.GetFormat(Type formatType)
{
return (formatType == typeof(OverridesAndSecurity) ? this : null);
}
// These two methods are overridden by DerivedClass and DoublyDerivedClass.
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
public virtual void DoSomething()
{
Console.WriteLine("Doing something.");
}
public virtual void DoSomethingElse()
{
Console.WriteLine("Doing some other thing.");
}
}
public class DerivedClass : OverridesAndSecurity, ITestOverrides
{
// Rule violation: The interface has security, and this implementation does not.
public object GetFormat(Type formatType)
{
return (formatType == typeof(OverridesAndSecurity) ? this : null);
}
// Rule violation: This does not have security, but the base class version does.
public override void DoSomething()
{
Console.WriteLine("Doing some derived thing.");
}
// Rule violation: This has security, but the base class version does not.
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
public override void DoSomethingElse()
{
Console.WriteLine("Doing some other derived thing.");
}
}
public class DoublyDerivedClass : DerivedClass
{
// The OverridesAndSecurity version of this method does not have security.
// Base class DerivedClass's version does.
// The DoublyDerivedClass version does not violate the rule, but the
// DerivedClass version does violate the rule.
public override void DoSomethingElse()
{
Console.WriteLine("Doing some other derived thing.");
}
}
}
Consulte también
Comentarios
https://aka.ms/ContentUserFeedback.
Próximamente: A lo largo de 2024 iremos eliminando gradualmente GitHub Issues como mecanismo de comentarios sobre el contenido y lo sustituiremos por un nuevo sistema de comentarios. Para más información, vea:Enviar y ver comentarios de