Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is .Net best practice to design custom attributes as sealed?

I'm reading Pro C# 2010 and the .Net 4 Platform by Andrew Troelsen.

In Chapter 15 about Attributes exists a note:

Note: For security reasons, it is considered a .Net best practice to design all custom attributes as sealed.

The author doesn't explain why, can someone explain why?

like image 461
Acaz Souza Avatar asked Oct 23 '11 18:10

Acaz Souza


People also ask

Why do we need custom attributes in C#?

Attributes are metadata extensions that give additional information to the compiler about the elements in the program code at runtime.

What is custom attributes in C#?

The class name AuthorAttribute is the attribute's name, Author , plus the Attribute suffix. It is derived from System. Attribute , so it is a custom attribute class. The constructor's parameters are the custom attribute's positional parameters.

What is the use of custom attributes?

A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.

How do I create a custom attribute in .NET core?

Declaring Custom AttributesWe can define an attribute by creating a class. This class should inherit from the Attribute class. Microsoft recommends appending the 'Attribute' suffix to the end of the class's name. After that, each property of our derived class will be a parameter of the desired data type.


2 Answers

CA1813: Avoid unsealed attributes: The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy; for example Attribute.GetCustomAttribute searches for the specified attribute type, or any attribute type that extends the specified attribute type. Sealing the attribute eliminates the search through the inheritance hierarchy, and can improve performance.

Ref: https://docs.microsoft.com/visualstudio/code-quality/ca1813-avoid-unsealed-attributes

Attributes are simply metadata discovered at runtime. As it is quoted, if someone else derives from your custom attribute class, by default .NET will find them too, which may imply a security risk if the derived attribute class is modifying the behavior of your original attribute in a way to you never intended to.

Even though performance is the prime reason to seal attribute classes, here is a formidable article dealing with its security side.

like image 170
Teoman Soygul Avatar answered Oct 10 '22 11:10

Teoman Soygul


There is one more reason to seal attributes.

Consider the following attribute:

[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false)] public class Attr1 : Attribute { } 

Here you allow only single attribute decoration: AllowMultiple = false

Compiler won't allow this:

[Attr1] [Attr1] public class Foo { } 

Later in your code you can safely call memberInfo.GetCustomAttribute() which will throw AmbiguousMatchException if more then one attribute of the given type was found.

Let's now inherit:

public class Attr2 : Attr1 { } 

Now compiler is silent.

[Attr1] [Attr2] public class Foo { } 

So if later somebody inherits from your attribute and passes back to your code some entity marked with both attributes unexpected exception will be thrown.

Full example:

class Program {     static void Main(params string[] args)     {         typeof(Foo).GetCustomAttribute<Attr1>();     }      [AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]     public class Attr1 : Attribute     {     }      public class Attr2 : Attr1     {     }      [Attr1]     [Attr2]     public class Foo     {     }      [Attr1]     public class Bar : Foo     {     } } 
like image 38
Pavel Voronin Avatar answered Oct 10 '22 11:10

Pavel Voronin