Skip to content

Commit ac84bb7

Browse files
6543KN4CK3R
andauthored
Fix data URI scramble (#16098) (#16118)
* Fix data URI scramble (#16098) * Removed unused method. * No prefix for data uris. * Added test to prevent regressions. Co-authored-by: KN4CK3R <[email protected]>
1 parent 3be67e9 commit ac84bb7

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

modules/markup/html.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,24 +403,19 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
403403
}
404404
case html.ElementNode:
405405
if node.Data == "img" {
406-
attrs := node.Attr
407-
for idx, attr := range attrs {
406+
for _, attr := range node.Attr {
408407
if attr.Key != "src" {
409408
continue
410409
}
411-
link := []byte(attr.Val)
412-
if len(link) > 0 && !IsLink(link) {
410+
if len(attr.Val) > 0 && !isLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
413411
prefix := ctx.urlPrefix
414412
if ctx.isWikiMarkdown {
415413
prefix = util.URLJoin(prefix, "wiki", "raw")
416414
}
417415
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
418416

419-
lnk := string(link)
420-
lnk = util.URLJoin(prefix, lnk)
421-
link = []byte(lnk)
417+
attr.Val = util.URLJoin(prefix, attr.Val)
422418
}
423-
node.Attr[idx].Val = string(link)
424419
}
425420
} else if node.Data == "a" {
426421
visitText = false

modules/markup/html_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,20 @@ func Test_ParseClusterFuzz(t *testing.T) {
408408

409409
assert.NotContains(t, string(val), "<html")
410410
}
411+
412+
func TestIssue16020(t *testing.T) {
413+
setting.AppURL = AppURL
414+
setting.AppSubURL = AppSubURL
415+
416+
var localMetas = map[string]string{
417+
"user": "go-gitea",
418+
"repo": "gitea",
419+
}
420+
421+
data := `<img src="data:image/png;base64,i//V"/>`
422+
423+
// func PostProcess(rawHTML []byte, urlPrefix string, metas map[string]string, isWikiMarkdown bool) ([]byte, error)
424+
res, err := PostProcess([]byte(data), "https://example.com", localMetas, false)
425+
assert.NoError(t, err)
426+
assert.Equal(t, data, string(res))
427+
}

0 commit comments

Comments
 (0)