65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET MVC 中防止 URL 篡改

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3投票s)

2016年9月14日

CPOL

8分钟阅读

viewsIcon

45263

downloadIcon

342

本文介绍了关于带有纯 ID 和唯一标识符的 URL 的安全问题,并使用 ASP.NET MVC 5 中的自定义属性提出了一种解决方案。

引言

本文提出了一种防止 ASP.NET MVC Web 应用程序中 URL 修改的方法。安全性是 Web 应用程序的主要关注点之一,不同的框架提供了许多内置功能,使应用程序更加健壮和防错。但仍有一些领域需要开发人员根据应用程序的性质思考哪种技术最适合应用程序。在这里,我们讨论 URL 中的开放 ID 和其他唯一标识符(主要在查询字符串中)的问题,这些标识符很容易被用户操纵,从而导致数据泄露。微软开发团队意识到了这个问题,因此在 ASP.NET 的近期版本中,尤其是在 MVC 中,提供了许多安全功能,例如身份验证、授权属性和防伪标记。然而,阻止用户手动更改查询字符串参数仍然需要开发人员自己处理。在这里,我演示了一种使用 MVC 过滤器和属性的解决方案,这是解决此问题为数不多的方法之一。它基于 ASP.NET MVC 5,并使用 Ninject IOC 容器。

背景

在从 ASP.NET Web Forms 过渡到 MVC 的过程中,发生了许多变化,现在 Web 应用程序的编写方式更加清晰,并且具有明确的关注点分离。MVC 的众多强大功能之一是 URL 路由,它真正改变了 Web URL,使其对用户简单、易于理解。借助强大的路由引擎,我们已经从丑陋的长 URL 转向了精美的短 URL,但在过程中,我们也向用户暴露了许多技术信息;URL 在浏览器地址栏中越简单,就越容易伪造。例如,让我们看这个返回订单详细信息的 URL http://websitename.com/Order/1,即使是非技术人员也能很容易地理解这个 URL 的作用,如果有人简单地更改末尾的 ID,他/她就可以获得其他用户的订单详细信息。这个问题已经被解决,并且有许多不同的方法来解决它。在这里,我们简要讨论其中几种,然后转向我们提出的解决方案。

1) 加密: 解决此问题的一种非常流行的方法是加密 URL 或其一部分。这是一个很好的解决方案,URL 看起来像这样 http://websitename.com/Order/Aj129Lo0)3387TRW,用户很难猜到。但加密算法应该是非常好的,并且能够生成长而强大的哈希,否则,如果有人进行暴力攻击,安全性可能会受到损害。此外,加密和解密会产生开销,这可能会影响性能。

2) GUID: 许多专家认可的另一种解决方案是使用 GUID 作为数据库中的主 ID,而不是简单的整数 ID。如果您从头开始编写应用程序,这很好,但对于具有旧数据库的遗留应用程序来说,实施起来很困难。

3) 授权: MVC 授权属性有助于确定用户是否已授权,但它不能告诉用户是否已授权访问我们案例中的特定资源“客户订单”。

建议解决方案
本文中的解决方案使用 MVC 自定义属性实现,该属性从操作方法的参数列表中识别资源类型,然后调用服务方法来查看用户是否已授权访问特定资源。这种对数据库或任何持久化存储容器(如会话或缓存)的实时检查使其防错。

使用代码

整个想法是在操作方法执行之前,对存储容器(即数据库、会话、缓存等)进行实时检查。该解决方案基于 ASP.NET MVC 平台,并使用 Ninject IOC 容器进行依赖注入。此外,它还使用了一个名为“Ninject.MVC5”的 Nuget 包(由 Remo Gloor 提供),用于在属性中进行依赖注入。在这里,我使用 Ninject 是因为它对属性注入有很好的支持,但该解决方案不依赖于 Ninject,并且可以替换为任何其他 IOC 容器。也可以在没有依赖注入的情况下实现这一点,但我倾向于不这样做。

在附加的演示项目中,我使用了一个简单的客户及其产品订单的示例。为简单起见,模型只包含必需的属性并在视图中使用。实际上,这可能是一个具有适当域模型和视图模型的非常复杂的应用程序,但这里的重点是解释如何防止 URL 修改。该应用程序是 Visual Studio 2015 MVC 模板创建的默认 MVC 应用程序,其中 Home 控制器已被修改,以显示用户名 **testuser@test.com** 的客户订单列表,以及用户单击详细信息链接时的订单详细信息。

要使用演示项目,请运行该应用程序,然后首先注册一个用户,其用户名/电子邮件等于 testuser@test.com,然后使用该用户登录以查看订单列表。我已硬编码了该用户预先填充的两个订单(在 CustomerService 构造函数中),以避免数据库调用,保持简单。

项目中的两个核心组件是(i)ResourceTypeAttribute 和(ii)CustomAuthorizeAttributeResourceTypeAttribute 用于标识用户请求的资源,而 CustomAuthorizeAttribute 在执行操作之前执行实际检查,以查看用户是否对该资源具有权限。除了这些属性之外,还有一个 CustomerService 类,它实现 ICustomerService 接口来检索数据并执行检查。

ResourceTypeAttribute

这是一个参数属性,放置在项目中的 Attributes 文件夹下。

    [AttributeUsage(AttributeTargets.Parameter)]
    public class ResourceTypeAttribute : System.Attribute
    {
        public readonly ResourceTypeEnum ResourceType;
        public ResourceTypeAttribute(ResourceTypeEnum resourceType)
        {
            ResourceType = resourceType;
        }

    }

一个典型的应用程序由不同类型的资源组成。在这个客户订单示例中,一些资源是客户、产品、订单等。通常,URL 的查询字符串包含资源 ID,因此在请求时,应该有一种方法来识别请求 ID 或唯一标识符的资源类型。此属性将用于操作方法的参数,以区分类型并触发授权检查。将资源类型的 enum 传递给属性以指定类型。

public ActionResult OrderDetails([ResourceType(ResourceTypeEnum.OrderId)] int orderId)
{
     Order orderDetails = _customerService.GetOrderDetails(orderId);
     return View(orderDetails);
}

CustomAuthorizeAttribute

在将参数属性应用于操作方法之后,接下来重要的是如何触发属性在操作方法执行之前执行以授权请求。这可以通过扩展现有的 MVC 授权属性来完成,但挑战在于将客户服务注入到属性中,为了实现这一点,我们使用了操作过滤器和属性的组合,称为“CustomAuthorizeAttribute”和“CustomAuthorizeFilter”。在这里,属性仅用于触发目的,实际的代码实现包含在操作过滤器中。

using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using PreventUrlModifications.Attributes;
using PreventUrlModifications.Services;

namespace PreventUrlModifications.Filters
{

    ///marker attribute
    public class CustomAuthorizeAttribute : FilterAttribute { }

    public class CustomAuthorizeFilter : IActionFilter
    {
        private readonly ICustomerService _customerService;

        public CustomAuthorizeFilter(ICustomerService customerService)
        {
            _customerService = customerService;
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {           

        }


        public void OnActionExecuting(ActionExecutingContext filterContext)
        {

            var user = filterContext.HttpContext.User;

            if (user != null && user.Identity.IsAuthenticated)
            {

                foreach (var parameter in filterContext.ActionDescriptor.GetParameters())
                {

                    foreach (var attribute in parameter.GetCustomAttributes(false))
                    {
                        var paramAttribute = attribute as ResourceTypeAttribute;

                        if (paramAttribute != null)
                        {

                            ResourceTypeAttribute resource = paramAttribute;

                            var type = resource.ResourceType;

                            object value = null;


                            if (filterContext.ActionParameters.ContainsKey(parameter.ParameterName))
                            {
                                value = filterContext.ActionParameters.FirstOrDefault(x => x.Key == parameter.ParameterName).Value;
                            }

                            bool authorised = _customerService.IsCustomerAuthorised(type, value, user.Identity.GetUserName());

                            if (!authorised)
                            {
                                // if it's an ajax call
                                if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
                                {
                                    //Sign Off
                                    HttpContext.Current.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                                    JavaScriptResult result = new JavaScriptResult();
                                    result.Script = "window.location = '/'";
                                    filterContext.Result = result;

                                }
                                else
                                {
                                    HttpContext.Current.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                                    filterContext.Result = new RedirectResult("~/Account/Login");
                                }
                            }
                        }
                    }

                }
            }
        }

    }

}

此操作过滤器是解决方案的核心。在“OnActionExecuting”方法中,它首先检查用户是否已认证,然后获取操作方法的参数列表,然后搜索任何带有“ResourceTypeAttribute”的参数。如果它找到带有该属性的参数,则获取其值并将该信息传递给 CustomerService 方法进行授权。如果授权成功,则不执行任何操作,否则会注销用户并将其重定向到登录页面。

客户服务

这只是一个用于演示目的的示例服务,用于检索数据。在实际应用程序中,它可以被负责数据检索和授权检查的其他服务或服务替换。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PreventUrlModifications.Models;
using PreventUrlModifications.Enums;

namespace PreventUrlModifications.Services
{
    public class CustomerService : ICustomerService
    {
        private readonly List<Customer> _customers = new List<Customer>();
        private readonly List<Order> _customerOrders = new List<Order>();

        public CustomerService()
        {

            _customers.Add(new Customer() {Username = "testuser@test.com", Name = "ABC"});
            _customers.Add(new Customer() { Username = "secondtestuser@test.com", Name = "XYZ" });
            _customerOrders.Add(new Order() {Id = 1, CustomerUsername = "testuser@test.com", ProductName = "Product1", OrderDate = DateTime.Now.AddYears(1), Cost = 10});
            _customerOrders.Add(new Order() { Id = 2, CustomerUsername = "testuser@test.com", ProductName = "Product2", OrderDate = DateTime.Now.AddYears(2), Cost = 20 });
            _customerOrders.Add(new Order() { Id = 3, CustomerUsername = "secondtestuser@test.com", ProductName = "Product3", OrderDate = DateTime.Now.AddMonths(2), Cost = 5 });

        }


        public IEnumerable<Order> GetAllCustomerOrders(string customerId)
        {
            return _customerOrders.Where(x => x.CustomerUsername == customerId);
        }


        public Order GetOrderDetails(int orderId)
        {
            return _customerOrders.FirstOrDefault(x => x.Id == orderId);
        }
      
        public bool IsCustomerAuthorised(ResourceTypeEnum resourceType, object resourceId, string username)
        {

            bool authorised = false;           

            if (resourceId == null)
                return false;           

            switch (resourceType)
            {
                case ResourceTypeEnum.OrderId:
                    int orderId = 0;
                    Int32.TryParse(resourceId.ToString(), out orderId);
                    authorised = _customerOrders.Any(x => x.Id == orderId && x.CustomerUsername == username);
                    break;
            }

            return authorised;

        }

    }

}

Ninject 绑定

最后,为了将属性应用于操作方法并注入依赖项,我们使用 Ninject。我们依赖于名为“Ninject.MVC5”的 Nuget 包(由 Remo Gloor 提供),该包提供了绑定过滤器扩展,用于在属性和操作过滤器中注入依赖项。当您添加此包时,它将在 App_Start 文件夹中创建一个名为“NinjectWebCommon”的文件,该文件负责所有引导程序。您只需在 RegisterServices 方法中添加您的绑定。要获取有关 **Ninject** 绑定的更多信息,请访问此 wiki 页面 https://github.com/ninject/Ninject.Web.Mvc/wiki/Dependency-injection-for-filters

using System.Web.Mvc;
using Ninject.Planning.Bindings;
using Ninject.Web.Mvc.FilterBindingSyntax;
using PreventUrlModifications.Filters;
using PreventUrlModifications.Services;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PreventUrlModifications.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(PreventUrlModifications.App_Start.NinjectWebCommon), "Stop")]

namespace PreventUrlModifications.App_Start
{

    using System;
    using System.Web;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon
    {

        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>

        public static void Start()
        {

            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));

            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));

            bootstrapper.Initialize(CreateKernel);

        }

        /// <summary>
        /// Stops the application.
        /// </summary>

        public static void Stop()
        {

            bootstrapper.ShutDown();

        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>

        private static IKernel CreateKernel()
        {

            var kernel = new StandardKernel();

            try

            {

                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);

                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();


                RegisterServices(kernel);

                return kernel;

            }

            catch

            {

                kernel.Dispose();

                throw;

            }

        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ICustomerService>().To<CustomerService>();
            kernel.BindFilter<CustomAuthorizeFilter>(FilterScope.Controller, 0).WhenControllerHas<CustomAuthorizeAttribute>();

        }       

    }

}

将所有内容连接起来

创建所有这些属性、服务和绑定后,您需要执行以下步骤将它们全部连接起来。

  1. 首先,您需要将 MVC 内置的 Authorize 属性应用于可以处理用户身份验证的控制器。
  2. 第二步是将“CustomAuthorizeAttribute”应用于需要资源授权的控制器。
  3. ResourceTypeAttribute 应用于操作方法的参数。

运行并测试应用程序

此项目中没有单元测试,但要查看属性的实际效果,请遵循以下步骤。

  1. 清除与 localhost 相关的所有 cookie,以避免任何冲突。
  2. 生成应用程序,它将还原所有 nuget 包。
  3. 运行应用程序,并使用用户名 testuser@test.com 注册一个用户,然后使用该用户登录。
  4. 单击主页,它将显示所有订单的列表(本例中有 2 个)。
  5. 然后单击其中一个订单的“单击查看订单详细信息”链接。它将带您到订单详细信息视图,URL 将类似于 https://:53572/OrderDetails/1
  6. 现在,如果您将 URL 末尾的“Id”更改为任何大于 2 的数字,例如 https://:53572/OrderDetails/3 并按 Enter 键,系统将将您重定向到登录页面,因为订单 ID 3 未与用户 testuser@test.com 关联。

关注点

为了改进代码,还有一些事情要做,例如,如果操作接受视图模型而不是 ID 怎么办?我还没有实现这一点,但思路是扩展 ResourceTypeAttribute 并接受模型中资源的属性名。如果属性名已知,我们可以从模型中获取该属性的值。

另一项增强功能是,如果服务每次授权检查都进行数据库查询,那么在首次从数据库获取数据后缓存数据,以减少数据库访问次数。

本文讨论的技术也可以使用 AOP(面向切面编程)来实现,但并非每个人都喜欢 AOP。PostSharp(https://www.postsharp.net/aspects)是在 .NET 中实现切面的一种不错的框架。

© . All rights reserved.