2017-04-13 02:52:24 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Copyright 2017 The Gogs Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-04-13 02:52:24 +00:00
|
|
|
|
2017-09-16 17:17:57 +00:00
|
|
|
package markup
|
2017-04-13 02:52:24 +00: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 {
|
2023-11-23 16:34:25 +00:00
|
|
|
defaultPolicy *bluemonday.Policy
|
|
|
|
descriptionPolicy *bluemonday.Policy
|
|
|
|
rendererPolicies map[string]*bluemonday.Policy
|
2024-05-31 13:54:14 +00:00
|
|
|
allowAllRegex *regexp.Regexp
|
2017-04-13 02:52:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 15:17:07 +00:00
|
|
|
var (
|
2024-05-31 13:54:14 +00:00
|
|
|
defaultSanitizer *Sanitizer
|
|
|
|
defaultSanitizerOnce sync.Once
|
2023-05-19 15:17:07 +00:00
|
|
|
)
|
2017-04-13 02:52:24 +00:00
|
|
|
|
2024-05-31 13:54:14 +00:00
|
|
|
func GetDefaultSanitizer() *Sanitizer {
|
|
|
|
defaultSanitizerOnce.Do(func() {
|
|
|
|
defaultSanitizer = &Sanitizer{
|
|
|
|
rendererPolicies: map[string]*bluemonday.Policy{},
|
|
|
|
allowAllRegex: regexp.MustCompile(".+"),
|
2023-07-18 15:18:37 +00:00
|
|
|
}
|
2024-05-31 13:54:14 +00: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 21:09:51 +00:00
|
|
|
}
|
2019-12-07 19:49:04 +00:00
|
|
|
}
|
2024-05-31 13:54:14 +00:00
|
|
|
defaultSanitizer.defaultPolicy = defaultSanitizer.createDefaultPolicy()
|
|
|
|
defaultSanitizer.descriptionPolicy = defaultSanitizer.createRepoDescriptionPolicy()
|
|
|
|
})
|
|
|
|
return defaultSanitizer
|
2017-04-13 02:52:24 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 13:54:14 +00:00
|
|
|
func ResetDefaultSanitizerForTesting() {
|
|
|
|
defaultSanitizer = nil
|
|
|
|
defaultSanitizerOnce = sync.Once{}
|
2019-12-31 01:53:28 +00:00
|
|
|
}
|