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

sanitizer.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Allow classes for anchors
  44. sanitizer.policy.AllowAttrs("class").Matching(regexp.MustCompile(`ref-issue`)).OnElements("a")
  45. // Custom keyword markup
  46. for _, rule := range setting.ExternalSanitizerRules {
  47. if rule.Regexp != nil {
  48. sanitizer.policy.AllowAttrs(rule.AllowAttr).Matching(rule.Regexp).OnElements(rule.Element)
  49. } else {
  50. sanitizer.policy.AllowAttrs(rule.AllowAttr).OnElements(rule.Element)
  51. }
  52. }
  53. }
  54. // Sanitize takes a string that contains a HTML fragment or document and applies policy whitelist.
  55. func Sanitize(s string) string {
  56. NewSanitizer()
  57. return sanitizer.policy.Sanitize(s)
  58. }
  59. // SanitizeReader sanitizes a Reader
  60. func SanitizeReader(r io.Reader) *bytes.Buffer {
  61. NewSanitizer()
  62. return sanitizer.policy.SanitizeReader(r)
  63. }
  64. // SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
  65. func SanitizeBytes(b []byte) []byte {
  66. if len(b) == 0 {
  67. // nothing to sanitize
  68. return b
  69. }
  70. NewSanitizer()
  71. return sanitizer.policy.SanitizeBytes(b)
  72. }