CA2123: Ustawienie atrybutu LinkDemand powinny być identyczne base
TypeName |
OverrideLinkDemandsShouldBeIdenticalToBase |
CheckId |
CA2123 |
Kategoria |
Microsoft.Security |
Zmiana kluczowa |
Kluczowa |
Przyczyna
Metoda publiczna lub chroniona w typie publicznym zastępuje metodę lub implementuje interfejs i nie zawiera takiego samego LinkDemand jako interfejsu lub metody wirtualnej.
Opis reguły
Ta reguła dopasowuje metodę do jej metody podstawowej, która jest interfejsem lub metodą wirtualna w innym typie, a następnie porównuje zapotrzebowania na łącza na każdym z nich.Naruszenie jest zgłaszane jeśli jedna metoda lub metoda podstawowa posiada zapotrzebowanie na łącze, a druga nie posiada.
Jeśli zasada ta jest naruszona, złośliwy wywołujący może pominąć zapotrzebowanie na łącza tylko poprzez wywołanie niezabezpieczonej metody.
Jak naprawić naruszenia
W celu naprawy naruszenia tej reguły, należy zastosować to samo zapotrzebowanie na łącze, aby zastąpić metodę lub implementację.Jeżeli nie jest to możliwe, należy oznaczyć tę metodę pełnym zapotrzebowaniem lub całkowicie usunąć atrybut.
Kiedy pominąć ostrzeżenia
Nie należy pomijać ostrzeżenia dotyczącego tej reguły.
Przykład
W poniższym przykładzie przedstawiono różne naruszenia tej reguły.
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.");
}
}
}