Skip to content

Commit 4042adb

Browse files
committed
Add multiple rule support to markup sanitizer
Resolves: 9888 Signed-off-by: Alexander Scheel <[email protected]>
1 parent 58e779a commit 4042adb

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

modules/setting/markup.go

+25-33
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newMarkup() {
4444
continue
4545
}
4646

47-
if name == "sanitizer" {
47+
if name == "sanitizer" || strings.HasPrefix(name, "sanitizer.") {
4848
newMarkupSanitizer(name, sec)
4949
} else {
5050
newMarkupRenderer(name, sec)
@@ -67,44 +67,36 @@ func newMarkupSanitizer(name string, sec *ini.Section) {
6767
return
6868
}
6969

70-
elements := sec.Key("ELEMENT").ValueWithShadows()
71-
allowAttrs := sec.Key("ALLOW_ATTR").ValueWithShadows()
72-
regexps := sec.Key("REGEXP").ValueWithShadows()
70+
elements := sec.Key("ELEMENT").Value()
71+
allowAttrs := sec.Key("ALLOW_ATTR").Value()
72+
regexpStr := sec.Key("REGEXP").Value()
7373

74-
if len(elements) != len(allowAttrs) ||
75-
len(elements) != len(regexps) {
76-
log.Error("All three keys in markup.%s (ELEMENT, ALLOW_ATTR, REGEXP) must be defined the same number of times! Got %d, %d, and %d respectively.", name, len(elements), len(allowAttrs), len(regexps))
74+
if regexpStr == "" {
75+
rule := MarkupSanitizerRule{
76+
Element: elements,
77+
AllowAttr: allowAttrs,
78+
Regexp: nil,
79+
}
80+
81+
ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
7782
return
7883
}
7984

80-
ExternalSanitizerRules = make([]MarkupSanitizerRule, 0, len(elements))
81-
82-
for index, pattern := range regexps {
83-
if pattern == "" {
84-
rule := MarkupSanitizerRule{
85-
Element: elements[index],
86-
AllowAttr: allowAttrs[index],
87-
Regexp: nil,
88-
}
89-
ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
90-
continue
91-
}
92-
93-
// Validate when parsing the config that this is a valid regular
94-
// expression. Then we can use regexp.MustCompile(...) later.
95-
compiled, err := regexp.Compile(pattern)
96-
if err != nil {
97-
log.Error("In module.%s: REGEXP at definition %d failed to compile: %v", name, index+1, err)
98-
continue
99-
}
85+
// Validate when parsing the config that this is a valid regular
86+
// expression. Then we can use regexp.MustCompile(...) later.
87+
compiled, err := regexp.Compile(regexpStr)
88+
if err != nil {
89+
log.Error("In module.%s: REGEXP (%s) at definition %d failed to compile: %v", regexpStr, name, err)
90+
return
91+
}
10092

101-
rule := MarkupSanitizerRule{
102-
Element: elements[index],
103-
AllowAttr: allowAttrs[index],
104-
Regexp: compiled,
105-
}
106-
ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
93+
rule := MarkupSanitizerRule{
94+
Element: elements,
95+
AllowAttr: allowAttrs,
96+
Regexp: compiled,
10797
}
98+
99+
ExternalSanitizerRules = append(ExternalSanitizerRules, rule)
108100
}
109101

110102
func newMarkupRenderer(name string, sec *ini.Section) {

0 commit comments

Comments
 (0)