Skip to content

Commit 21cde5c

Browse files
authored
Fix data URI scramble (#16098)
* Removed unused method. * No prefix for data uris. * Added test to prevent regressions.
1 parent 0909695 commit 21cde5c

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

modules/markup/html.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -364,24 +364,19 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
364364
}
365365
case html.ElementNode:
366366
if node.Data == "img" {
367-
attrs := node.Attr
368-
for idx, attr := range attrs {
367+
for _, attr := range node.Attr {
369368
if attr.Key != "src" {
370369
continue
371370
}
372-
link := []byte(attr.Val)
373-
if len(link) > 0 && !IsLink(link) {
371+
if len(attr.Val) > 0 && !isLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
374372
prefix := ctx.URLPrefix
375373
if ctx.IsWiki {
376374
prefix = util.URLJoin(prefix, "wiki", "raw")
377375
}
378376
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
379377

380-
lnk := string(link)
381-
lnk = util.URLJoin(prefix, lnk)
382-
link = []byte(lnk)
378+
attr.Val = util.URLJoin(prefix, attr.Val)
383379
}
384-
node.Attr[idx].Val = string(link)
385380
}
386381
} else if node.Data == "a" {
387382
visitText = false

modules/markup/html_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,23 @@ func Test_ParseClusterFuzz(t *testing.T) {
444444
assert.NoError(t, err)
445445
assert.NotContains(t, res.String(), "<html")
446446
}
447+
448+
func TestIssue16020(t *testing.T) {
449+
setting.AppURL = AppURL
450+
setting.AppSubURL = AppSubURL
451+
452+
var localMetas = map[string]string{
453+
"user": "go-gitea",
454+
"repo": "gitea",
455+
}
456+
457+
data := `<img src="data:image/png;base64,i//V"/>`
458+
459+
var res strings.Builder
460+
err := PostProcess(&RenderContext{
461+
URLPrefix: "https://example.com",
462+
Metas: localMetas,
463+
}, strings.NewReader(data), &res)
464+
assert.NoError(t, err)
465+
assert.Equal(t, data, res.String())
466+
}

modules/markup/sanitizer.go

-10
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,3 @@ func SanitizeReader(r io.Reader) *bytes.Buffer {
131131
NewSanitizer()
132132
return sanitizer.policy.SanitizeReader(r)
133133
}
134-
135-
// SanitizeBytes takes a []byte slice that contains a HTML fragment or document and applies policy whitelist.
136-
func SanitizeBytes(b []byte) []byte {
137-
if len(b) == 0 {
138-
// nothing to sanitize
139-
return b
140-
}
141-
NewSanitizer()
142-
return sanitizer.policy.SanitizeBytes(b)
143-
}

modules/markup/sanitizer_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func Test_Sanitizer(t *testing.T) {
4949

5050
for i := 0; i < len(testCases); i += 2 {
5151
assert.Equal(t, testCases[i+1], Sanitize(testCases[i]))
52-
assert.Equal(t, testCases[i+1], string(SanitizeBytes([]byte(testCases[i]))))
5352
}
5453
}
5554

0 commit comments

Comments
 (0)