Skip to content

Commit

Permalink
Fix data URI scramble (go-gitea#16098)
Browse files Browse the repository at this point in the history
* Removed unused method.

* No prefix for data uris.

* Added test to prevent regressions.
  • Loading branch information
KN4CK3R authored and 6543 committed Jun 9, 2021
1 parent ce2ade0 commit 8bca89a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
11 changes: 3 additions & 8 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,24 +403,19 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
}
case html.ElementNode:
if node.Data == "img" {
attrs := node.Attr
for idx, attr := range attrs {
for _, attr := range node.Attr {
if attr.Key != "src" {
continue
}
link := []byte(attr.Val)
if len(link) > 0 && !IsLink(link) {
if len(attr.Val) > 0 && !isLinkStr(attr.Val) && !strings.HasPrefix(attr.Val, "data:image/") {
prefix := ctx.urlPrefix
if ctx.isWikiMarkdown {
prefix = util.URLJoin(prefix, "wiki", "raw")
}
prefix = strings.Replace(prefix, "/src/", "/media/", 1)

lnk := string(link)
lnk = util.URLJoin(prefix, lnk)
link = []byte(lnk)
attr.Val = util.URLJoin(prefix, attr.Val)
}
node.Attr[idx].Val = string(link)
}
} else if node.Data == "a" {
visitText = false
Expand Down
20 changes: 20 additions & 0 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,23 @@ func Test_ParseClusterFuzz(t *testing.T) {

assert.NotContains(t, string(val), "<html")
}

func TestIssue16020(t *testing.T) {
setting.AppURL = AppURL
setting.AppSubURL = AppSubURL

var localMetas = map[string]string{
"user": "go-gitea",
"repo": "gitea",
}

data := `<img src="data:image/png;base64,i//V"/>`

var res strings.Builder
err := PostProcess(&RenderContext{
URLPrefix: "https://example.com",
Metas: localMetas,
}, strings.NewReader(data), &res)
assert.NoError(t, err)
assert.Equal(t, data, res.String())
}

0 comments on commit 8bca89a

Please sign in to comment.