Files

52 lines
1.4 KiB
Go
Raw Permalink Normal View History

2017-04-13 04:52:24 +02:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// Copyright 2017 The Gogs Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2017-04-13 04:52:24 +02:00
package markup
2017-04-13 04:52:24 +02:00
import (
"regexp"
"sync"
"github.com/microcosm-cc/bluemonday"
)
// Sanitizer is a protection wrapper of *bluemonday.Policy which does not allow
// any modification to the underlying policies once it's been created.
type Sanitizer struct {
defaultPolicy *bluemonday.Policy
descriptionPolicy *bluemonday.Policy
rendererPolicies map[string]*bluemonday.Policy
allowAllRegex *regexp.Regexp
2017-04-13 04:52:24 +02:00
}
var (
defaultSanitizer *Sanitizer
defaultSanitizerOnce sync.Once
)
2017-04-13 04:52:24 +02:00
func GetDefaultSanitizer() *Sanitizer {
defaultSanitizerOnce.Do(func() {
defaultSanitizer = &Sanitizer{
rendererPolicies: map[string]*bluemonday.Policy{},
allowAllRegex: regexp.MustCompile(".+"),
2023-07-18 17:18:37 +02:00
}
for name, renderer := range renderers {
sanitizerRules := renderer.SanitizerRules()
if len(sanitizerRules) > 0 {
policy := defaultSanitizer.createDefaultPolicy()
defaultSanitizer.addSanitizerRules(policy, sanitizerRules)
defaultSanitizer.rendererPolicies[name] = policy
2021-06-23 23:09:51 +02:00
}
2019-12-07 14:49:04 -05:00
}
defaultSanitizer.defaultPolicy = defaultSanitizer.createDefaultPolicy()
defaultSanitizer.descriptionPolicy = defaultSanitizer.createRepoDescriptionPolicy()
})
return defaultSanitizer
2017-04-13 04:52:24 +02:00
}
func ResetDefaultSanitizerForTesting() {
defaultSanitizer = nil
defaultSanitizerOnce = sync.Once{}
}