Browse Source

过滤输入

xyxie 1 year ago
parent
commit
9582cba7b0
4 changed files with 117 additions and 10 deletions
  1. 19 1
      controllers/english_report/report.go
  2. 31 4
      controllers/report.go
  3. 6 0
      services/report.go
  4. 61 5
      utils/common.go

+ 19 - 1
controllers/english_report/report.go

@@ -65,6 +65,12 @@ func (this *EnglishReportController) Add() {
 
 	var contentSub string
 	if req.Content != "" {
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
 		content, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"
@@ -182,6 +188,12 @@ func (this *EnglishReportController) Edit() {
 	}
 	var contentSub string
 	if req.Content != "" {
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
 		content, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"
@@ -956,7 +968,7 @@ func (this *EnglishReportController) Delete() {
 	go func() {
 		_ = services.ResetPPTReport(req.ReportIds, true)
 	}()
-	
+
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "删除成功"
@@ -1013,6 +1025,12 @@ func (this *EnglishReportController) SaveReportContent() {
 			content = this.GetString("Content")
 		}
 		if content != "" {
+			e := utils.ContentXssFilter(req.Content)
+			if e != nil {
+				br.Msg = "存在非法标签"
+				br.ErrMsg = "存在非法标签, Err: " + e.Error()
+				return
+			}
 			contentClean, e := services.FilterReportContentBr(req.Content)
 			if e != nil {
 				br.Msg = "内容去除前后空格失败"

+ 31 - 4
controllers/report.go

@@ -525,8 +525,13 @@ func (this *ReportController) Add() {
 	}
 	var contentSub string
 	if req.Content != "" {
-		cleanContent := utils.ContentXssFilter(req.Content)
-		content, e := services.FilterReportContentBr(cleanContent)
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
+		content, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"
 			br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
@@ -658,8 +663,12 @@ func (this *ReportController) Edit() {
 	}
 	var contentSub string
 	if req.Content != "" {
-		req.Content = html.UnescapeString(req.Content)
-		req.Content = utils.ContentXssFilter(req.Content)
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
 		content, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"
@@ -1206,6 +1215,12 @@ func (this *ReportController) SaveReportContent() {
 			content = this.GetString("Content")
 		}
 		if content != "" {
+			e := utils.ContentXssFilter(req.Content)
+			if e != nil {
+				br.Msg = "存在非法标签"
+				br.ErrMsg = "存在非法标签, Err: " + e.Error()
+				return
+			}
 			contentClean, e := services.FilterReportContentBr(req.Content)
 			if e != nil {
 				br.Msg = "内容去除前后空格失败"
@@ -2432,6 +2447,12 @@ func (this *ReportController) EditDayWeekChapter() {
 	// 更新章节及指标
 	contentSub := ""
 	if req.Content != "" {
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
 		contentClean, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"
@@ -2903,6 +2924,12 @@ func (this *ReportController) PublishDayWeekReportChapter() {
 	// 更新章节信息
 	contentSub := ""
 	if req.Content != "" {
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			br.Msg = "存在非法标签"
+			br.ErrMsg = "存在非法标签, Err: " + e.Error()
+			return
+		}
 		contentClean, e := services.FilterReportContentBr(req.Content)
 		if e != nil {
 			br.Msg = "内容去除前后空格失败"

+ 6 - 0
services/report.go

@@ -937,6 +937,12 @@ func PcCreateAndUploadSunCode(scene, page string) (imgUrl string, err error) {
 func CreateNewReport(req models.AddReq, adminInfo *system.Admin) (newReportId int64, reportCode, errMsg string, err error) {
 	contentSub := ""
 	if req.Content != "" {
+		e := utils.ContentXssFilter(req.Content)
+		if e != nil {
+			errMsg = "存在非法标签"
+			err = errors.New("存在非法标签, Err: " + e.Error())
+			return
+		}
 		contentClean, e := FilterReportContentBr(req.Content)
 		if e != nil {
 			errMsg = "内容去除前后空格失败"

+ 61 - 5
utils/common.go

@@ -13,9 +13,8 @@ import (
 	"errors"
 	"fmt"
 	"github.com/PuerkitoBio/goquery"
-	"github.com/microcosm-cc/bluemonday"
 	"github.com/shopspring/decimal"
-	"html"
+	"golang.org/x/net/html"
 	"image"
 	"image/png"
 	"io"
@@ -2299,9 +2298,66 @@ func GetColorMap() map[int]string {
 	return colorMap
 }
 
+// 检查src属性是否以http或data:image开头
+func isValidSrc(src string) bool {
+	validSchemes := regexp.MustCompile(`^(http|https|data:image)/`)
+	return validSchemes.MatchString(src)
+}
+
 // ContentXssFilter 过滤文本中的JS代码
-func ContentXssFilter(content string) (clean string) {
-	p := bluemonday.UGCPolicy()
-	clean = p.Sanitize(content)
+func ContentXssFilter(content string) (err error) {
+	// 解析HTML内容
+	node, err := html.Parse(strings.NewReader(content))
+	if err != nil {
+		err = fmt.Errorf(" html.Parse Err: %v", err)
+		return
+	}
+
+	// 遍历解析后的节点树,查找特定标签
+	var visit func(n *html.Node)
+	visit = func(n *html.Node) {
+		if n.Type == html.ElementNode {
+			lowerData := strings.ToLower(n.Data)
+			switch lowerData {
+			case "script", "javascript":
+				err = fmt.Errorf(" script is forbidden")
+				return
+			case "img", "input", "iframe":
+				// 查找并过滤src属性
+				for _, attr := range n.Attr {
+					lowerKey := strings.ToLower(attr.Key)
+					if lowerKey == "src" {
+						if !isValidSrc(attr.Val) {
+							err = fmt.Errorf("invalid src attribute value: %s", attr.Val)
+							return
+						}
+					}
+				}
+			default:
+				for _, attr := range n.Attr { //判断事件
+					lowerKey := strings.ToLower(attr.Key)
+					if lowerKey == "onmouseover" || lowerKey == "onclick" || lowerKey == "onerror" {
+						err = fmt.Errorf("the event is forbidden: %s:%s", attr.Key, attr.Val)
+						return
+					}
+				}
+				/*	case "src":
+					// 如果<src>是某个标签的属性,你可能需要递归检查其父节点
+					// 这里简单起见,我们假设<src>不是有效的HTML标签,并忽略它
+					// 在实际中,你可能需要更复杂的逻辑来处理这种情况
+					fmt.Println("Warning: Unexpected 'src' tag found.")*/
+			}
+		}
+		for c := n.FirstChild; c != nil; c = c.NextSibling {
+			visit(c)
+			if err != nil {
+				return
+			}
+		}
+	}
+	visit(node)
+	if err != nil {
+		return
+	}
 	return
 }