xss.go 1.2 KB

1234567891011121314151617181920212223242526272829
  1. package service
  2. import (
  3. "regexp"
  4. "github.com/microcosm-cc/bluemonday"
  5. )
  6. // uat-i0 i0 ...
  7. var bfsRegexp = regexp.MustCompile(`//.{1,6}\.hdslb+\.com/.+(?:jpg|gif|png|webp|jpeg)$`)
  8. func xssFilter(content string) string {
  9. p := bluemonday.NewPolicy()
  10. p.AllowElements("b", "br", "del")
  11. p.AllowAttrs("target", "href").OnElements("a")
  12. p.AllowAttrs("class").OnElements("caption", "dl", "dd", "dt", "h2", "h3", "h4", "h5", "h6", "li", "ol", "strong", "ul")
  13. p.AllowAttrs("class", "style").OnElements("h1", "p", "span")
  14. p.AllowAttrs("class", "cite").OnElements("blockquote")
  15. p.AllowAttrs("class", "contenteditable").OnElements("figure", "figcaption", "code")
  16. p.AllowAttrs("class", "contenteditable", "aid", "style").OnElements("div")
  17. p.AllowAttrs("color", "size", "face").OnElements("font")
  18. p.AllowAttrs("class", "contenteditable", "data-lang").OnElements("pre")
  19. p.AllowAttrs("src", "alt", "title", "width", "aid", "class", "height", "id", "_src", "type", "data-size", "data-vote-id").OnElements("img")
  20. p.RequireParseableURLs(true)
  21. p.AllowRelativeURLs(true) // support //i0.hdslb.com
  22. p.AllowURLSchemes("http", "https", "bilibili")
  23. p.AllowAttrs("src").Matching(bfsRegexp).OnElements("img")
  24. return p.Sanitize(content)
  25. }