18 Star 40 Fork 8

JacksonBruce / AntiXssUF

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

AntiXssUF

NuGet version Build status GitHub license GitHub version

netstandard2.1 netstandard2.0 netcoreapp2.1 netcoreapp3.1 netframework4.6.1

跨站脚本攻击(XSS)过滤器,以白名单的过滤策略,支持多种过滤策略,可以根据业务场景选择适合的过滤策略,或者根据用户角色动态绑定过滤策略,支持OwaspAntisamy项目的配置,支持json格式的配置; 使用方法:

. 在启动类Startup.cs上添加依赖注入

        public void ConfigureServices(IServiceCollection services)
        {
            //添加策略和设置默认策略
            services.AddXssFilter(opt=>opt.DefaultSchemeName= "DefaultPolicy")
                .AddScheme<AntisamyPolicy>("antisamy", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy.xml")))
                .AddScheme<AntisamyPolicy>("anythinggoes", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy-anythinggoes.xml")))
                .AddScheme<AntisamyPolicy>("ebay", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy-ebay.xml")))
                .AddScheme<AntisamyPolicy>("myspace", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy-myspace.xml")))
                .AddScheme<AntisamyPolicy>("slashdot", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy-slashdot.xml")))
                .AddScheme<AntisamyPolicy>("test", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/antisamy-test.xml")))
                .AddScheme<JsonFilterPolicy>("DefaultPolicy", () => File.ReadAllTextAsync(Path.Combine(HostEnvironment.ContentRootPath, "resources/DefaultPolicy.json")));
            ;
            //添加模型绑定器
            services.AddControllers(options =>
            {
                options.ModelBinderProviders.Insert(0, new RichTextBinderProvider());
            });
            services.AddControllersWithViews();
        }
        

. 在构造函数注入依赖

        //依赖注入
        public HomeController(IFilterPolicyFactory policyFactory)
        {
            this.policyFactory = policyFactory;
        }
        public async Task<IActionResult> Test(string source) {
            var policyName="ebay"//策略名称
            var filter=await policyFactory.CreateHtmlFilter(policyName);//创建过滤器
            var clean = filter.Filters(source);//过滤危险代码
            return Content(clean);
        }
        

. 使用模型绑定器

        //模型绑定过滤策略
        public class TestModel
        {
           public string Name { get; set; }
            [XssSchemeName("ebay")]
            public RichText RichText { get; set; }
        }        
        

. 在控制器上直接使用

        public IActionResult Test(TestModel model)
        {
            string clean = model?.RichText;//这里自动过滤危险代码
            return Content(clean??string.Empty);
        }
        //使用参数绑定过滤策略
        public IActionResult Test([XssSchemeName("ebay")] RichText richText)
        {
            string clean = richText;//这里自动过滤危险代码
            return Content(clean??string.Empty);
        }
        

不使用依赖注入,直接使用

  1. 使用内置的默认策略
        //使用参数绑定过滤策略,这里需要添加模型绑定器
        public IActionResult Test(RichText richText)
        {
            string clean = richText;//这里自动过滤危险代码
            return Content(clean??string.Empty);
        }
        //这里不需要添加模型绑定器
        public IActionResult Test(string source)
        {
            RichText richText=source;
            string clean = richText;//这里自动过滤危险代码
            return Content(clean??string.Empty);
        }
        
  1. 指定策略

        public IActionResult Test(string source)
        {
            var policy = new AntisamyPolicy();//json格式用JsonFilterPolicy类
            policy.Init(File.ReadAllText("c:/www/resources/antisamy-ebay.xml"),"ebay");
            var filter=new HtmlFilter(policy);
            var clean = filter.Filters(source);//过滤危险代码
            return Content(clean??string.Empty);
        }
        
MIT License Copyright (c) 2020 Jackson.Bruce Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

跨站脚本攻击(XSS)过滤器,以白名单的过滤策略,支持多种过滤策略,可以根据业务场景选择适合的过滤策略,或者根据用户角色动态绑定过滤策略,支持OwaspAntisamy项目的配置,支持json格式的配置 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/ufangx/AntiXssUF.git
git@gitee.com:ufangx/AntiXssUF.git
ufangx
AntiXssUF
AntiXssUF
master

搜索帮助

14c37bed 8189591 565d56ea 8189591