Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making AuthorizeAttribute deny users by default if Roles is empty

I'm rather surprised at the default behaviour of AuthorizeAttribute; if you don't supply it any Roles property, it just appears to allow any authorized user to access the controller/action. I want whitelist behaviour instead; if Roles is null or empty, deny all users access. How can I make this behaviour occur?

like image 452
Jez Avatar asked Oct 10 '12 14:10

Jez


2 Answers

public class AuthorizeExAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (string.IsNullOrWhiteSpace(Roles))
            return false;
        return base.AuthorizeCore(httpContext);
    }
}

Now use [AuthorizeEx] on your controllers/actions

like image 200
Andras Zoltan Avatar answered Oct 31 '22 16:10

Andras Zoltan


Here's what I came up with eventually, as a filter I add to the global filter collection for an MVC application:

/// <summary>
/// This filter should be applied to an MVC application as a global filter in RegisterGlobalFilters, not applied to individual actions/controllers.
/// It will cause access to every action to be DENIED by default.
/// If an AllowAnonymousAttribute is applied, all authorization checking is skipped (this takes precedence over AuthorizeSafeAttribute).
/// If an AuthorizeSafeAttribute is applied, only the roles specified in AuthorizeSafeAttribute's Roles property will be allowed access.
/// </summary>
public sealed class AuthorizeSafeFilter : AuthorizeAttribute {
    public override void OnAuthorization(AuthorizationContext filterContext) {
        if (!string.IsNullOrEmpty(this.Roles) || !string.IsNullOrEmpty(this.Users)) {
            throw new Exception("This class is intended to be applied to an MVC application as a global filter in RegisterGlobalFilters, not applied to individual actions/controllers.  Use the AuthorizeSafeAttribute with individual actions/controllers.");
        }

        // Disable caching for this request
        filterContext.HttpContext.Response.Cache.SetNoServerCaching();
        filterContext.HttpContext.Response.Cache.SetNoStore();

        // If AllowAnonymousAttribute applied, skip authorization
        if (
            filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
            filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
        ) {
            return;
        }

        // Backup original roles
        string rolesBackup = this.Roles;

        // Look for AuthorizeSafeAttribute roles
        bool foundRoles = false;
        string foundRolesString = null;
        object[] actionCustomAttributes = filterContext.ActionDescriptor.GetCustomAttributes(false);
        object[] controllerCustomAttributes = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(false);

        if (actionCustomAttributes.Any(attr => attr is AuthorizeSafeAttribute)) {
            AuthorizeSafeAttribute foundAttr = (AuthorizeSafeAttribute)(actionCustomAttributes.First(attr => attr is AuthorizeSafeAttribute));
            foundRoles = true;
            foundRolesString = foundAttr.Roles;
        }
        else if (controllerCustomAttributes.Any(attr => attr is AuthorizeSafeAttribute)) {
            AuthorizeSafeAttribute foundAttr = (AuthorizeSafeAttribute)(controllerCustomAttributes.First(attr => attr is AuthorizeSafeAttribute));
            foundRoles = true;
            foundRolesString = foundAttr.Roles;
        }

        if (foundRoles && !string.IsNullOrWhiteSpace(foundRolesString)) {
            // Found valid roles string; use it as our own Roles property and auth normally
            this.Roles = foundRolesString;
            base.OnAuthorization(filterContext);
        }
        else {
            // Didn't find valid roles string; DENY all access by default
            filterContext.Result = new HttpUnauthorizedResult();
        }

        // Restore original roles
        this.Roles = rolesBackup;
    }
}

I also define this attribute:

/// <summary>
/// Represents an attribute that is used to restrict access by callers to an action method, in conjunction
/// with a global AuthorizeSafeFilter, DENYING all access by default.
/// </summary>
public class AuthorizeSafeAttribute : Attribute {
    public string Roles { get; set; }
}

I apply AllowAnonymousAttribute to my login actions/controllers and AuthorizeSafeAttribute to other ones, but if I forget to apply these, access is denied by default. I wish ASP.NET MVC were as secure as this by default. :-)

like image 39
Jez Avatar answered Oct 31 '22 18:10

Jez