Jaa


CA2133: Delegates must bind to methods with consistent transparency

Item Value
RuleId CA2133
Category Microsoft.Security
Breaking change Breaking

Note

This warning is only applied to code that is running the CoreCLR (the version of the CLR that is specific to Silverlight web applications).

Cause

This warning fires on a method that binds a delegate that is marked with the SecurityCriticalAttribute to a method that is transparent or that is marked with the SecuritySafeCriticalAttribute. The warning also fires a method that binds a delegate that is transparent or safe-critical to a critical method.

Note

This rule has been deprecated. For more information, see Deprecated rules.

Rule description

Delegate types and the methods that they bind to must have consistent transparency. Transparent and safe-critical delegates may only bind to other transparent or safe-critical methods. Similarly, critical delegates may only bind to critical methods. These binding rules ensure that the only code that can invoke a method via a delegate could have also invoked the same method directly. For example, binding rules prevent transparent code from calling critical code directly via a transparent delegate.

How to fix violations

To fix a violation of this warning, change the transparency of the delegate or of the method that it binds so that the transparency of the two are equivalent.

When to suppress warnings

Do not suppress a warning from this rule.

Code

using System;
using System.Security;

namespace TransparencyWarningsDemo
{

    public delegate void TransparentDelegate();

    [SecurityCritical]
    public delegate void CriticalDelegate();

    public class TransparentType
    {
        void DelegateBinder()
        {
            // CA2133 violation - binding a transparent delegate to a critical method
            TransparentDelegate td = new TransparentDelegate(CriticalTarget);

            // CA2133 violation - binding a critical delegate to a transparent method
            CriticalDelegate cd = new CriticalDelegate(TransparentTarget);
        }

        [SecurityCritical]
        void CriticalTarget() { }

        void TransparentTarget() { }
    }
}