Skip to content

Commit dae7f1e

Browse files
authored
Remove unnecessary SanitizeHTML from code (#29575)
* "mail/issue/default.tmpl": the body is rendered by backend `markdown.RenderString() HTML`, it has been already sanitized * "repo/settings/webhook/base_list.tmpl": "Description" is prepared by backend `ctx.Tr`, it doesn't need to be sanitized
1 parent e917334 commit dae7f1e

File tree

7 files changed

+7
-14
lines changed

7 files changed

+7
-14
lines changed

docs/content/administration/mail-templates.en-us.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ Please check [Gitea's logs](administration/logging-config.md) for error messages
224224
{{if not (eq .Body "")}}
225225
<h3>Message content</h3>
226226
<hr>
227-
{{.Body | SanitizeHTML}}
227+
{{.Body}}
228228
{{end}}
229229
</p>
230230
<hr>

docs/content/administration/mail-templates.zh-cn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ _主题_ 和 _邮件正文_ 由 [Golang的模板引擎](https://go.dev/pkg/text/
207207
{{if not (eq .Body "")}}
208208
<h3>消息内容:</h3>
209209
<hr>
210-
{{.Body | SanitizeHTML}}
210+
{{.Body}}
211211
{{end}}
212212
</p>
213213
<hr>

modules/templates/helper.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,8 @@ func SafeHTML(s any) template.HTML {
208208
}
209209

210210
// SanitizeHTML sanitizes the input by pre-defined markdown rules
211-
func SanitizeHTML(s any) template.HTML {
212-
switch v := s.(type) {
213-
case string:
214-
return template.HTML(markup.Sanitize(v))
215-
case template.HTML:
216-
return template.HTML(markup.Sanitize(string(v)))
217-
}
218-
panic(fmt.Sprintf("unexpected type %T", s))
211+
func SanitizeHTML(s string) template.HTML {
212+
return template.HTML(markup.Sanitize(s))
219213
}
220214

221215
func HTMLEscape(s any) template.HTML {

modules/templates/helper_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,4 @@ func TestHTMLFormat(t *testing.T) {
6464

6565
func TestSanitizeHTML(t *testing.T) {
6666
assert.Equal(t, template.HTML(`<a href="/" rel="nofollow">link</a> xss <div>inline</div>`), SanitizeHTML(`<a href="/">link</a> <a href="javascript:">xss</a> <div style="dangerous">inline</div>`))
67-
assert.Equal(t, template.HTML(`<a href="/" rel="nofollow">link</a> xss <div>inline</div>`), SanitizeHTML(template.HTML(`<a href="/">link</a> <a href="javascript:">xss</a> <div style="dangerous">inline</div>`)))
6867
}

templates/mail/issue/default.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
{{.locale.Tr "mail.issue.action.new" .Doer.Name .Issue.Index}}
5959
{{end}}
6060
{{else}}
61-
{{.Body | SanitizeHTML}}
61+
{{.Body}}
6262
{{end -}}
6363
{{- range .ReviewComments}}
6464
<hr>

templates/repo/settings/webhook/base_list.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="ui attached segment">
1111
<div class="ui list">
1212
<div class="item">
13-
{{.Description | SanitizeHTML}}
13+
{{.Description}}
1414
</div>
1515
{{range .Webhooks}}
1616
<div class="item truncated-item-container">

templates/status/500.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{/* This page should only depend the minimal template functions/variables, to avoid triggering new panics.
2-
* base template functions: AppName, AssetUrlPrefix, AssetVersion, AppSubUrl, ThemeName, SanitizeHTML
2+
* base template functions: AppName, AssetUrlPrefix, AssetVersion, AppSubUrl, ThemeName
33
* ctx.Locale
44
* .Flash
55
* .ErrorMsg

0 commit comments

Comments
 (0)