Skip to content

Commit ab60f40

Browse files
committed
fix
1 parent 0d7d2ed commit ab60f40

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

modules/htmlutil/html.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ParseSizeAndClass(defaultSize int, defaultClass string, others ...any) (int
3030
return size, class
3131
}
3232

33-
func HTMLFormat(s string, rawArgs ...any) template.HTML {
33+
func HTMLFormat(s template.HTML, rawArgs ...any) template.HTML {
3434
args := slices.Clone(rawArgs)
3535
for i, v := range args {
3636
switch v := v.(type) {
@@ -44,5 +44,5 @@ func HTMLFormat(s string, rawArgs ...any) template.HTML {
4444
args[i] = template.HTMLEscapeString(fmt.Sprint(v))
4545
}
4646
}
47-
return template.HTML(fmt.Sprintf(s, args...))
47+
return template.HTML(fmt.Sprintf(string(s), args...))
4848
}

modules/markup/internal/renderinternal.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *RenderInternal) ProtectSafeAttrs(content template.HTML) template.HTML {
7676
return template.HTML(reAttrClass().ReplaceAllString(string(content), `$1 data-attr-class="`+r.secureIDPrefix+`$2"$3`))
7777
}
7878

79-
func (r *RenderInternal) FormatWithSafeAttrs(w io.Writer, fmt string, a ...any) error {
79+
func (r *RenderInternal) FormatWithSafeAttrs(w io.Writer, fmt template.HTML, a ...any) error {
8080
_, err := w.Write([]byte(r.ProtectSafeAttrs(htmlutil.HTMLFormat(fmt, a...))))
8181
return err
8282
}

modules/markup/markdown/math/block_renderer.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package math
55

66
import (
7+
"html/template"
8+
79
"code.gitea.io/gitea/modules/markup/internal"
810
giteaUtil "code.gitea.io/gitea/modules/util"
911

@@ -50,7 +52,7 @@ func (r *BlockRenderer) renderBlock(w util.BufWriter, source []byte, node gast.N
5052
n := node.(*Block)
5153
if entering {
5254
code := giteaUtil.Iif(n.Inline, "", `<pre class="code-block is-loading">`) + `<code class="language-math display">`
53-
_ = r.renderInternal.FormatWithSafeAttrs(w, code)
55+
_ = r.renderInternal.FormatWithSafeAttrs(w, template.HTML(code))
5456
r.writeLines(w, source, n)
5557
} else {
5658
_, _ = w.WriteString(`</code>` + giteaUtil.Iif(n.Inline, "", `</pre>`) + "\n")

modules/markup/orgmode/orgmode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (r *orgWriter) resolveLink(kind, link string) string {
147147
func (r *orgWriter) WriteRegularLink(l org.RegularLink) {
148148
link := r.resolveLink(l.Kind(), l.URL)
149149

150-
printHTML := func(html string, a ...any) {
150+
printHTML := func(html template.HTML, a ...any) {
151151
_, _ = fmt.Fprint(r, htmlutil.HTMLFormat(html, a...))
152152
}
153153
// Inspired by https://github.com/niklasfasching/go-org/blob/6eb20dbda93cb88c3503f7508dc78cbbc639378f/org/html_writer.go#L406-L427

modules/templates/helper.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewFuncMap() template.FuncMap {
3838
"Iif": iif,
3939
"Eval": evalTokens,
4040
"SafeHTML": safeHTML,
41-
"HTMLFormat": htmlutil.HTMLFormat,
41+
"HTMLFormat": htmlFormat,
4242
"HTMLEscape": htmlEscape,
4343
"QueryEscape": queryEscape,
4444
"QueryBuild": QueryBuild,
@@ -207,6 +207,20 @@ func htmlEscape(s any) template.HTML {
207207
panic(fmt.Sprintf("unexpected type %T", s))
208208
}
209209

210+
func htmlFormat(s any, args ...any) template.HTML {
211+
if len(args) == 0 {
212+
// to prevent developers from calling "HTMLFormat $userInput" by mistake which will lead to XSS
213+
panic("missing arguments for HTMLFormat")
214+
}
215+
switch v := s.(type) {
216+
case string:
217+
return htmlutil.HTMLFormat(template.HTML(v), args...)
218+
case template.HTML:
219+
return htmlutil.HTMLFormat(v, args...)
220+
}
221+
panic(fmt.Sprintf("unexpected type %T", s))
222+
}
223+
210224
func jsEscapeSafe(s string) template.HTML {
211225
return template.HTML(template.JSEscapeString(s))
212226
}

modules/templates/helper_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99
"testing"
1010

11-
"code.gitea.io/gitea/modules/htmlutil"
1211
"code.gitea.io/gitea/modules/util"
1312

1413
"github.com/stretchr/testify/assert"
@@ -88,7 +87,7 @@ func TestTemplateIif(t *testing.T) {
8887
func TestTemplateEscape(t *testing.T) {
8988
execTmpl := func(code string) string {
9089
tmpl := template.New("test")
91-
tmpl.Funcs(template.FuncMap{"QueryBuild": QueryBuild, "HTMLFormat": htmlutil.HTMLFormat})
90+
tmpl.Funcs(template.FuncMap{"QueryBuild": QueryBuild, "HTMLFormat": htmlFormat})
9291
template.Must(tmpl.Parse(code))
9392
w := &strings.Builder{}
9493
assert.NoError(t, tmpl.Execute(w, nil))

0 commit comments

Comments
 (0)