Browse Source

敏感词检查记录日志

懒得勤快 5 years ago
parent
commit
981de4a11c

+ 4 - 1
src/Masuit.MyBlogs.Core/Controllers/CommentController.cs

@@ -10,6 +10,7 @@ using Masuit.MyBlogs.Core.Models.ViewModel;
 using Masuit.Tools;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Html;
+using Masuit.Tools.Logging;
 using Masuit.Tools.Strings;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
@@ -44,8 +45,10 @@ namespace Masuit.MyBlogs.Core.Controllers
         [HttpPost, ValidateAntiForgeryToken]
         public async Task<ActionResult> Submit(CommentCommand dto)
         {
-            if (Regex.Match(dto.NickName + dto.Content, CommonHelper.BanRegex).Length > 0)
+            var match = Regex.Match(dto.NickName + dto.Content, CommonHelper.BanRegex);
+            if (match.Success)
             {
+                LogManager.Info($"提交内容:{dto.NickName}/{dto.Content},敏感词:{match.Value}");
                 return ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!");
             }
 

+ 4 - 1
src/Masuit.MyBlogs.Core/Controllers/MsgController.cs

@@ -10,6 +10,7 @@ using Masuit.MyBlogs.Core.Models.ViewModel;
 using Masuit.Tools;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Html;
+using Masuit.Tools.Logging;
 using Masuit.Tools.Strings;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Mvc;
@@ -109,8 +110,10 @@ namespace Masuit.MyBlogs.Core.Controllers
         [HttpPost, ValidateAntiForgeryToken]
         public ActionResult Submit(LeaveMessageCommand dto)
         {
-            if (Regex.Match(dto.NickName + dto.Content, CommonHelper.BanRegex).Length > 0)
+            var match = Regex.Match(dto.NickName + dto.Content, CommonHelper.BanRegex);
+            if (match.Success)
             {
+                LogManager.Info($"提交内容:{dto.NickName}/{dto.Content},敏感词:{match.Value}");
                 return ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!");
             }
 

+ 5 - 1
src/Masuit.MyBlogs.Core/Controllers/PostController.cs

@@ -17,6 +17,7 @@ using Masuit.MyBlogs.Core.Models.ViewModel;
 using Masuit.Tools;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Html;
+using Masuit.Tools.Logging;
 using Masuit.Tools.Security;
 using Masuit.Tools.Strings;
 using Masuit.Tools.Systems;
@@ -216,8 +217,10 @@ namespace Masuit.MyBlogs.Core.Controllers
                 return ResultData(null, false, "验证码错误!");
             }
 
-            if (Regex.Match(post.Title + post.Author + post.Content, CommonHelper.BanRegex).Length > 0)
+            var match = Regex.Match(post.Title + post.Author + post.Content, CommonHelper.BanRegex);
+            if (match.Success)
             {
+                LogManager.Info($"提交内容:{post.Title}/{post.Author}/{post.Content},敏感词:{match.Value}");
                 return ResultData(null, false, "您提交的内容包含敏感词,被禁止发表,请检查您的内容后尝试重新提交!");
             }
 
@@ -410,6 +413,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             }
 
             #endregion
+
             #region 直接合并
 
             if (post.Email.Equals(dto.ModifierEmail))

+ 3 - 2
src/Masuit.MyBlogs.Core/Extensions/RequestInterceptMiddleware.cs

@@ -34,7 +34,8 @@ namespace Masuit.MyBlogs.Core.Extensions
             var request = context.Request;
             var path = HttpUtility.UrlDecode(request.Path + request.QueryString, Encoding.UTF8);
             var requestUrl = HttpUtility.UrlDecode(request.Scheme + "://" + request.Host + path);
-            if (Regex.Match(path ?? "", CommonHelper.BanRegex).Length > 0)
+            var match = Regex.Match(path ?? "", CommonHelper.BanRegex);
+            if (match.Length > 0)
             {
                 BackgroundJob.Enqueue(() => HangfireBackJob.InterceptLog(new IpIntercepter()
                 {
@@ -42,7 +43,7 @@ namespace Masuit.MyBlogs.Core.Extensions
                     RequestUrl = requestUrl,
                     Time = DateTime.Now,
                     UserAgent = request.Headers[HeaderNames.UserAgent],
-                    Remark = "检测到敏感词拦截"
+                    Remark = $"检测到敏感词拦截:{match.Value}"
                 }));
                 context.Response.StatusCode = 400;
                 await context.Response.WriteAsync("参数不合法!", Encoding.UTF8);

+ 6 - 4
src/Masuit.MyBlogs.Core/Models/Validation/SubmitCheckAttribute.cs

@@ -1,7 +1,7 @@
-using Masuit.Tools.Html;
-using System;
+using Masuit.MyBlogs.Core.Common;
+using Masuit.Tools.Html;
+using Masuit.Tools.Logging;
 using System.ComponentModel.DataAnnotations;
-using System.IO;
 using System.Text.RegularExpressions;
 
 namespace Masuit.MyBlogs.Core.Models.Validation
@@ -76,8 +76,10 @@ namespace Masuit.MyBlogs.Core.Models.Validation
                 }
             }
 
-            if (_checkContent && Regex.Match(content, File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data", "ban.txt"))).Length > 0)
+            var match = Regex.Match(content, CommonHelper.BanRegex);
+            if (_checkContent && match.Success)
             {
+                LogManager.Info($"提交内容:{content},敏感词:{match.Value}");
                 ErrorMessage = "您提交的内容包含有非法的词汇,被禁止发表,请检查您要提交的内容!";
                 return false;
             }