I'm currently trying to create a secure code access solution for our project.
Therefore, I created the CustomPermissionAttribute attribute, 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 instance of CustomPermission.
The Demand method of the CustomPermission class should check the protection 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 of the above is in signed assembly A. "
Unsigned assembly B contains the following CustomPrincipial implementation, which implements ICustomPrincipial 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 should know something about the user type. If I placed the CustomPrincipial class in Assembly A, then all assemblies with custom material should also be signed ... otherwise I cannot compile assembly A)
When the application starts, a new instance of CustomPrincipial is assigned to Thread.Current.CurrentPrincipial.
Two questions:
Can problems caused by the ICustomPermission public interface in Assembly A be safe?
Is it absolutely necessary to fully implement all members of IPermission? Especially in ToXML and FromXML methods ... The CreatePermission () method still runs every time I run MethodABC () at runtime.
EDIT: ad 1: I think of the following situation: "Assembly C" contains MethodXY, which is protected by the CustomPermissionAttribute attribute. To gain access to this protected method, an attacker could create a new application, links to Assembly A and Assembly C, and could make his own implementation of the public interface ICustomPrincipial Assembly A (-> HasPermission (), which returns true all the time). He could assign an instance of his implementation to his own Thread.Current.CurrentPrincipial. If the Assembly A Demand () method checks Thread.Current.CurrentPrincipial, an attacker could gain access to MethodXY. This may be a possible situation ..!?