You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sanitizer.go 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Copyright 2017 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package markup
  6. import (
  7. "bytes"
  8. "io"
  9. "regexp"
  10. "sync"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/microcosm-cc/bluemonday"
  13. )
  14. // Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow
  15. // any modification to the underlying policies once it's been created.
  16. type Sanitizer struct {
  17. policy *bluemonday.Policy
  18. init sync.Once
  19. }
  20. var sanitizer = &Sanitizer{}
  21. // NewSanitizer initializes sanitizer with allowed attributes based on settings.
  22. // Multiple calls to this function will only create one instance of Sanitizer during
  23. // entire application lifecycle.
  24. func NewSanitizer() {
  25. sanitizer.init.Do(func() {
  26. ReplaceSanitizer()
  27. })
  28. }
  29. // ReplaceSanitizer replaces the current sanitizer to account for changes in settings
  30. func ReplaceSanitizer() {
  31. sanitizer.policy = bluemonday.UGCPolicy()
  32. // We only want to allow HighlightJS specific classes for code blocks
  33. sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^language-\w+$`)).OnElements("code")
  34. // Checkboxes
  35. sanitizer.policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
  36. sanitizer.policy.AllowAttrs("checked", "disabled").OnElements("input")
  37. // Custom URL-Schemes
  38. sanitizer.policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
  39. // Allow keyword markup
  40. sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`^` + keywordClass + `$`)).OnElements("span")
  41. // Allow <kbd> tags for keyboard shortcut styling
  42. sanitizer.policy.AllowElements("kbd")
  43. // Custom keyword markup
  44. for _, rule := range setting.ExternalSanitizerRules {
  45. if rule.Regexp != nil {
  46. sanitizer.policy.AllowAttrs(rule.AllowAttr).Matching(rule.Regexp).OnElements(rule.Element)
  47. } else {
  48. sanitizer.policy.AllowAttrs(rule.AllowAttr).OnElements(rule.Element)
  49. }
  50. }
  51. }
  52. // Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist.
  53. func Sanitize(s string) string {
  54. NewSanitizer()
  55. return sanitizer.policy.Sanitize(s)
  56. }
  57. // SanitizeReader sanitizes a Reader
  58. func SanitizeReader(r io.Reader) *bytes.Buffer {
  59. NewSanitizer()
  60. return sanitizer.policy.SanitizeReader(r)
  61. }
  62. // SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
  63. func SanitizeBytes(b []byte) []byte {
  64. if len(b) == 0 {
  65. // nothing to sanitize
  66. return b
  67. }
  68. NewSanitizer()
  69. return sanitizer.policy.SanitizeBytes(b)
  70. }