summaryrefslogtreecommitdiffstats
path: root/modules/markup/sanitizer_description.go
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-05-31 21:54:14 +0800
committerGitHub <noreply@github.com>2024-05-31 13:54:14 +0000
commitb6280f4d21309cfae7cc07f74173354c664d5e10 (patch)
treef9ff58e9d23f589ebb533c507b4ebc30c399d3b1 /modules/markup/sanitizer_description.go
parent1987c86f3ce8335a3c1f4754b37872c3ca137b3d (diff)
downloadgitea-b6280f4d21309cfae7cc07f74173354c664d5e10.tar.gz
gitea-b6280f4d21309cfae7cc07f74173354c664d5e10.zip
Split sanitizer functions and fine-tune some tests (#31192) (#31200)
Backport #31192 by wxiaoguang Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules/markup/sanitizer_description.go')
-rw-r--r--modules/markup/sanitizer_description.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/modules/markup/sanitizer_description.go b/modules/markup/sanitizer_description.go
new file mode 100644
index 0000000000..f8b51f2d9a
--- /dev/null
+++ b/modules/markup/sanitizer_description.go
@@ -0,0 +1,37 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package markup
+
+import (
+ "regexp"
+
+ "github.com/microcosm-cc/bluemonday"
+)
+
+// createRepoDescriptionPolicy returns a minimal more strict policy that is used for
+// repository descriptions.
+func (st *Sanitizer) createRepoDescriptionPolicy() *bluemonday.Policy {
+ policy := bluemonday.NewPolicy()
+ policy.AllowStandardURLs()
+
+ // Allow italics and bold.
+ policy.AllowElements("i", "b", "em", "strong")
+
+ // Allow code.
+ policy.AllowElements("code")
+
+ // Allow links
+ policy.AllowAttrs("href", "target", "rel").OnElements("a")
+
+ // Allow classes for emojis
+ policy.AllowAttrs("class").Matching(regexp.MustCompile(`^emoji$`)).OnElements("img", "span")
+ policy.AllowAttrs("aria-label").OnElements("span")
+
+ return policy
+}
+
+// SanitizeDescription sanitizes the HTML generated for a repository description.
+func SanitizeDescription(s string) string {
+ return GetDefaultSanitizer().descriptionPolicy.Sanitize(s)
+}