CA2123: Przesłonięcia żądań konsolidacji powinny być identyczne z bazowym
Pozycja | Wartość |
---|---|
Ruleid | CA2123 |
Kategoria | Microsoft.Security |
Zmiana powodująca niezgodność | Kluczowa |
Przyczyna
Metoda publiczna lub chroniona w typie publicznym zastępuje metodę lub implementuje interfejs i nie ma tych samych żądań linków co interfejs lub metoda wirtualna.
Uwaga
Ta reguła została przestarzała. Aby uzyskać więcej informacji, zobacz Przestarzałe reguły.
Opis reguły
Ta reguła dopasowuje metodę do jej metody podstawowej, która jest interfejsem lub metodą wirtualną innego typu, a następnie porównuje zapotrzebowania na łącza na każdym z nich. Naruszenie jest zgłaszane, jeśli metoda lub metoda podstawowa ma żądanie łącza, a druga nie.
Jeśli ta reguła zostanie naruszona, złośliwy obiekt wywołujący może pominąć żądanie łącza tylko przez wywołanie niezabezpieczonej metody.
Jak naprawić naruszenia
Aby naprawić naruszenie tej reguły, zastosuj to samo żądanie łącza do metody zastąpienia lub implementacji. Jeśli nie jest to możliwe, oznacz metodę za pomocą pełnego żądania lub całkowicie usuń atrybut.
Kiedy pomijać ostrzeżenia
Nie pomijaj ostrzeżeń dla 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.");
}
}
}
Zobacz też
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla