gitea源码

1234567891011121314151617181920212223242526272829303132
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "regexp"
  6. "strings"
  7. "code.gitea.io/gitea/modules/setting"
  8. "github.com/microcosm-cc/bluemonday"
  9. )
  10. func (st *Sanitizer) addSanitizerRules(policy *bluemonday.Policy, rules []setting.MarkupSanitizerRule) {
  11. for _, rule := range rules {
  12. if rule.AllowDataURIImages {
  13. policy.AllowDataURIImages()
  14. }
  15. if rule.Element != "" {
  16. if rule.Regexp != "" {
  17. if !strings.HasPrefix(rule.Regexp, "^") || !strings.HasSuffix(rule.Regexp, "$") {
  18. panic("Markup sanitizer rule regexp must start with ^ and end with $ to be strict")
  19. }
  20. policy.AllowAttrs(rule.AllowAttr).Matching(regexp.MustCompile(rule.Regexp)).OnElements(rule.Element)
  21. } else {
  22. policy.AllowAttrs(rule.AllowAttr).OnElements(rule.Element)
  23. }
  24. }
  25. }
  26. }