I'm currently trying to build a custom code access security solution for our project.
Therefore i created a CustomPermissionAttribute which should be used as shown:
[CustomPermissionAttribute(SecurityAction.Demand, Permission="PermMethodABC")]
public void MethodABC()
{
}
The CreatePermission()
method of the Attribute creates and returns a new CustomPermission instance.
The Demand Method of the CustomPermission class should check the security against my custom IPrincipial Implementation in Thread.Current.CurrentPrincipial:
public sealed class CustomPermission : IPermission
{
private string _RequiredPermission;
...
public void Demand()
{
ICustomPrincipial _pr = Thread.Current.CurrentPrincipial as ICustomPrincipial;
if (_pr == null) throw...
if (!_pr.HasPermission(_RequiredPermission)) throw...
}
}
public interface ICustomPrincipial : IPrincipial
{
bool HasPermission(string RequiredPermission);
}
All above is in the signed "Assembly A".
The unsigned Assembly B contains the following CustomPrincipial implementation which implements ICustomPrincipial of Assembly A:
public sealed class CustomPrincipial : ICustomPrincipial
{
User _User;
...
public bool HasPermission(string RequiredPermission)
{
if (_User has permission defined with "PermMethodABC") ...
return true/false;
}
...
}
(Now Assembly A has to know anything about the User-type. If i place the CustomPrincipial class into Assembly A, then all Assemblys with the user stuff also would have to be signed... otherwise i cannot compile Assembly A)
On Application startup, a new instance of the CustomPrincipial is assigned to Thread.Current.CurrentPrincipial.
Two Questions:
Could saftey problems caused by the public ICustomPermission interface in Assembly A?
Is it absolutely necessary to fully implement all IPermission Members? Especially the ToXML and FromXML Methods... The CreatePermission() Method gets anyway invoked everytime i access MethodABC() at runtime.
EDIT: ad 1: I'm thinking of the following situation: "Assembly C" contains MethodXY which is protected with a CustomPermissionAttribute. To get access to this protected Method, an attacker could create a new application, references to Assembly A & Assembly C and could make an own implementation of the public ICustomPrincipial interface of Assembly A (-> HasPermission() returning true all the time). He could assign an instance of his implementation to his own Thread.Current.CurrentPrincipial. If Assembly A's Demand() Method checks against Thread.Current.CurrentPrincipial an attacker could get access to MethodXY. That might be a possible situation..!?