Code Access Security (CAS) is a security model in the .NET Framework that determines the permissions granted to code based on its identity and the security policy of the system. CAS allows the .NET runtime to enforce security constraints on code based on where it comes from and how trusted it is. This model helps protect against harmful actions by untrusted code, even when it runs in the same application domain as trusted code.
CAS is particularly important when dealing with scenarios like running code downloaded from the internet, or running code that is part of a distributed system. It ensures that code can only perform actions that it is explicitly allowed to do, based on security policy settings.
CAS works by controlling the permissions of code based on the following aspects:
Code's Identity: CAS uses the concept of evidence to determine the identity of the code. Evidence is information about the code such as its location (URL), strong name, or digital signature.
Code's Permissions: Permissions are actions or resources that the code is allowed to access. For example, reading from a file, accessing the network, or modifying the registry.
Security Policy: The security policy defines which permissions are granted to code with specific evidence. This is configured in policy files (e.g., machine.config, app.config).
When code is executed in the .NET runtime (Common Language Runtime, or CLR), it is assigned a set of permissions based on its evidence. The CLR uses the security policy to determine what permissions should be granted to the code.
Evidence: This includes information about the code, such as its origin (e.g., file path, URL), its strong name (if signed), and whether it is fully trusted.
Permission Set: Based on the evidence, the CLR assigns a permission set that defines what the code is allowed to do. For example, code from the internet might be granted limited permissions (like reading from certain files) while code from a trusted publisher might be granted full permissions.
Security Policy: The security policy defines the rules that associate evidence with permissions. Policy can be defined at the machine, user, or application level.
Permission: A permission is a specific right that the code has (e.g., FileIOPermission, SecurityPermission, NetworkPermission). If the code attempts an action outside its granted permissions, a SecurityException is thrown.
The .NET framework defines different security zones that categorize the origin of the code. The most common zones are:
The evidence used to determine the zone could be:
Permissions in CAS are granted to code based on its identity and the security policy. Common permissions include:
Permissions can be categorized into:
Evidence provides information about the identity and origin of the code. Common evidence types include:
The security policy defines what permissions should be granted to code based on its evidence. The .NET framework uses policy levels to configure the security policy:
Code Access Security (CAS) was heavily used in the .NET Framework, but it has been deprecated in .NET Core and .NET 5+. The reason for this is that CAS introduced significant overhead and complexities that were difficult to manage in modern cloud-based and distributed applications.
In .NET Core and later versions, security is managed through other mechanisms such as:
However, CAS still exists in .NET Framework, and you may encounter it when working with legacy applications.
To configure security permissions for an assembly in .NET, the PermissionSet attribute or SecurityAction attributes are commonly used.
Here is an example of how to define the permissions for an assembly:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class SecureClass
{
public void PerformAction()
{
// Code that requires full trust goes here
}
}
In this example, the PermissionSet attribute demands that the code be run with FullTrust. If the security policy denies full trust, a SecurityException will be thrown.
If a piece of code tries to access a resource or perform an operation that it does not have permission for, the .NET runtime throws a SecurityException.
try
{
File.ReadAllText("somefile.txt");
}
catch (SecurityException secEx)
{
Console.WriteLine("SecurityException: " + secEx.Message);
}
This exception occurs if the code doesn’t have sufficient permissions to perform the action.
Code Access Security (CAS) provides a way to enforce fine-grained security by controlling what resources code can access based on its identity and the security policy of the system. While CAS was a cornerstone of security in .NET Framework, its role has been reduced in .NET Core and later versions. Instead, modern .NET applications use a combination of role-based security, encryption, and secure coding practices to secure applications and data.
If you're working with legacy applications that target the .NET Framework, understanding CAS and configuring the correct permissions and security policy is important for ensuring that the application is both secure and compliant with organizational security policies.
Open this section to load past papers