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.

markup.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package setting
  5. import (
  6. "regexp"
  7. "strings"
  8. "code.gitea.io/gitea/modules/log"
  9. "gopkg.in/ini.v1"
  10. )
  11. // ExternalMarkupParsers represents the external markup parsers
  12. var (
  13. ExternalMarkupParsers []MarkupParser
  14. ExternalSanitizerRules []MarkupSanitizerRule
  15. )
  16. // MarkupParser defines the external parser configured in ini
  17. type MarkupParser struct {
  18. Enabled bool
  19. MarkupName string
  20. Command string
  21. FileExtensions []string
  22. IsInputFile bool
  23. }
  24. // MarkupSanitizerRule defines the policy for whitelisting attributes on
  25. // certain elements.
  26. type MarkupSanitizerRule struct {
  27. Element string
  28. AllowAttr string
  29. Regexp *regexp.Regexp
  30. }
  31. func newMarkup() {
  32. for _, sec := range Cfg.Section("markup").ChildSections() {
  33. name := strings.TrimPrefix(sec.Name(), "markup.")
  34. if name == "" {
  35. log.Warn("name is empty, markup " + sec.Name() + "ignored")
  36. continue
  37. }
  38. if name == "sanitizer" || strings.HasPrefix(name, "sanitizer.") {
  39. newMarkupSanitizer(name, sec)
  40. } else {
  41. newMarkupRenderer(name, sec)
  42. }
  43. }
  44. }
  45. func newMarkupSanitizer(name string, sec *ini.Section) {
  46. haveElement := sec.HasKey("ELEMENT")
  47. haveAttr := sec.HasKey("ALLOW_ATTR")
  48. haveRegexp := sec.HasKey("REGEXP")
  49. if !haveElement && !haveAttr && !haveRegexp {
  50. log.Warn("Skipping empty section: markup.%s.", name)
  51. return
  52. }
  53. if !haveElement || !haveAttr || !haveRegexp {
  54. log.Error("Missing required keys from markup.%s. Must have all three of ELEMENT, ALLOW_ATTR, and REGEXP defined!", name)
  55. return
  56. }
  57. elements := sec.Key("ELEMENT").Value()
  58. allowAttrs := sec.Key("ALLOW_ATTR").Value()
  59. regexpStr := sec.Key("REGEXP").Value()
  60. if regexpStr == "" {
  61. rule := MarkupSanitizerRule{
  62. Element: elements,
  63. AllowAttr: allowAttrs,
  64. Regexp: nil,
  65. }
  66. ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
  67. return
  68. }
  69. // Validate when parsing the config that this is a valid regular
  70. // expression. Then we can use regexp.MustCompile(...) later.
  71. compiled, err := regexp.Compile(regexpStr)
  72. if err != nil {
  73. log.Error("In module.%s: REGEXP (%s) at definition %d failed to compile: %v", regexpStr, name, err)
  74. return
  75. }
  76. rule := MarkupSanitizerRule{
  77. Element: elements,
  78. AllowAttr: allowAttrs,
  79. Regexp: compiled,
  80. }
  81. ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
  82. }
  83. func newMarkupRenderer(name string, sec *ini.Section) {
  84. extensionReg := regexp.MustCompile(`\.\w`)
  85. extensions := sec.Key("FILE_EXTENSIONS").Strings(",")
  86. var exts = make([]string, 0, len(extensions))
  87. for _, extension := range extensions {
  88. if !extensionReg.MatchString(extension) {
  89. log.Warn(sec.Name() + " file extension " + extension + " is invalid. Extension ignored")
  90. } else {
  91. exts = append(exts, extension)
  92. }
  93. }
  94. if len(exts) == 0 {
  95. log.Warn(sec.Name() + " file extension is empty, markup " + name + " ignored")
  96. return
  97. }
  98. command := sec.Key("RENDER_COMMAND").MustString("")
  99. if command == "" {
  100. log.Warn(" RENDER_COMMAND is empty, markup " + name + " ignored")
  101. return
  102. }
  103. ExternalMarkupParsers = append(ExternalMarkupParsers, MarkupParser{
  104. Enabled: sec.Key("ENABLED").MustBool(false),
  105. MarkupName: name,
  106. FileExtensions: exts,
  107. Command: command,
  108. IsInputFile: sec.Key("IS_INPUT_FILE").MustBool(false),
  109. })
  110. }