sanitizer.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package markup
  2. import (
  3. "gitote/gitote/pkg/setting"
  4. "regexp"
  5. "sync"
  6. "github.com/microcosm-cc/bluemonday"
  7. )
  8. // Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow
  9. // any modification to the underlying policies once it's been created.
  10. type Sanitizer struct {
  11. policy *bluemonday.Policy
  12. init sync.Once
  13. }
  14. var sanitizer = &Sanitizer{
  15. policy: bluemonday.UGCPolicy(),
  16. }
  17. // NewSanitizer initializes sanitizer with allowed attributes based on settings.
  18. // Multiple calls to this function will only create one instance of Sanitizer during
  19. // entire application lifecycle.
  20. func NewSanitizer() {
  21. sanitizer.init.Do(func() {
  22. // We only want to allow HighlightJS specific classes for code blocks
  23. sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code")
  24. // Checkboxes
  25. sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
  26. sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
  27. // Data URLs
  28. sanitizer.policy.AllowURLSchemes("data")
  29. // Custom URL-Schemes
  30. sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
  31. })
  32. }
  33. // Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist.
  34. func Sanitize(s string) string {
  35. return sanitizer.policy.Sanitize(s)
  36. }
  37. // SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
  38. func SanitizeBytes(b []byte) []byte {
  39. return sanitizer.policy.SanitizeBytes(b)
  40. }