gitea源码

sanitizer_default.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup
  4. import (
  5. "html/template"
  6. "io"
  7. "net/url"
  8. "regexp"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/microcosm-cc/bluemonday"
  11. )
  12. func (st *Sanitizer) createDefaultPolicy() *bluemonday.Policy {
  13. policy := bluemonday.UGCPolicy()
  14. // NOTICE: DO NOT add special "class" regexp rules here anymore, use RenderInternal.SafeAttr instead
  15. // General safe SVG attributes
  16. policy.AllowAttrs("viewBox", "width", "height", "aria-hidden", "data-attr-class").OnElements("svg")
  17. policy.AllowAttrs("fill-rule", "d").OnElements("path")
  18. // Checkboxes
  19. policy.AllowAttrs("type").Matching(regexp.MustCompile(`^checkbox$`)).OnElements("input")
  20. policy.AllowAttrs("checked", "disabled", "data-source-position").OnElements("input")
  21. // Chroma always uses 1-2 letters for style names, we could tolerate it at the moment
  22. policy.AllowAttrs("class").Matching(regexp.MustCompile(`^\w{0,2}$`)).OnElements("span")
  23. // Custom URL-Schemes
  24. if len(setting.Markdown.CustomURLSchemes) > 0 {
  25. policy.AllowURLSchemes(setting.Markdown.CustomURLSchemes...)
  26. } else {
  27. policy.AllowURLSchemesMatching(st.allowAllRegex)
  28. // Even if every scheme is allowed, these three are blocked for security reasons
  29. disallowScheme := func(*url.URL) bool {
  30. return false
  31. }
  32. policy.AllowURLSchemeWithCustomPolicy("javascript", disallowScheme)
  33. policy.AllowURLSchemeWithCustomPolicy("vbscript", disallowScheme)
  34. policy.AllowURLSchemeWithCustomPolicy("data", disallowScheme)
  35. }
  36. // Allow classes for org mode list item status.
  37. policy.AllowAttrs("class").Matching(regexp.MustCompile(`^(unchecked|checked|indeterminate)$`)).OnElements("li")
  38. // Allow 'color' and 'background-color' properties for the style attribute on text elements.
  39. policy.AllowStyles("color", "background-color").OnElements("div", "span", "p", "tr", "th", "td")
  40. policy.AllowAttrs("src", "autoplay", "controls").OnElements("video")
  41. policy.AllowAttrs("loading").OnElements("img")
  42. // Allow generally safe attributes (reference: https://github.com/jch/html-pipeline)
  43. generalSafeAttrs := []string{
  44. "abbr", "accept", "accept-charset",
  45. "accesskey", "action", "align", "alt",
  46. "aria-describedby", "aria-hidden", "aria-label", "aria-labelledby",
  47. "axis", "border", "cellpadding", "cellspacing", "char",
  48. "charoff", "charset", "checked",
  49. "clear", "cols", "colspan", "color",
  50. "compact", "coords", "datetime", "dir",
  51. "disabled", "enctype", "for", "frame",
  52. "headers", "height", "hreflang",
  53. "hspace", "ismap", "label", "lang",
  54. "maxlength", "media", "method",
  55. "multiple", "name", "nohref", "noshade",
  56. "nowrap", "open", "prompt", "readonly", "rel", "rev",
  57. "rows", "rowspan", "rules", "scope",
  58. "selected", "shape", "size", "span",
  59. "start", "summary", "tabindex", "target",
  60. "title", "type", "usemap", "valign", "value",
  61. "vspace", "width", "itemprop", "itemscope", "itemtype",
  62. "data-markdown-generated-content", "data-attr-class",
  63. }
  64. generalSafeElements := []string{
  65. "h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "br", "b", "i", "strong", "em", "a", "pre", "code", "img", "tt",
  66. "div", "ins", "del", "sup", "sub", "p", "ol", "ul", "table", "thead", "tbody", "tfoot", "blockquote", "label",
  67. "dl", "dt", "dd", "kbd", "q", "samp", "var", "hr", "ruby", "rt", "rp", "li", "tr", "td", "th", "s", "strike", "summary",
  68. "details", "caption", "figure", "figcaption",
  69. "abbr", "bdo", "cite", "dfn", "mark", "small", "span", "time", "video", "wbr",
  70. }
  71. // FIXME: Need to handle longdesc in img but there is no easy way to do it
  72. policy.AllowAttrs(generalSafeAttrs...).OnElements(generalSafeElements...)
  73. // Custom keyword markup
  74. defaultSanitizer.addSanitizerRules(policy, setting.ExternalSanitizerRules)
  75. return policy
  76. }
  77. // Sanitize use default sanitizer policy to sanitize a string
  78. func Sanitize(s string) template.HTML {
  79. return template.HTML(GetDefaultSanitizer().defaultPolicy.Sanitize(s))
  80. }
  81. // SanitizeReader sanitizes a Reader
  82. func SanitizeReader(r io.Reader, renderer string, w io.Writer) error {
  83. policy, exist := GetDefaultSanitizer().rendererPolicies[renderer]
  84. if !exist {
  85. policy = GetDefaultSanitizer().defaultPolicy
  86. }
  87. return policy.SanitizeReaderToWriter(r, w)
  88. }