Skip to content

Commit

Permalink
markup/goldmark: Exclude event attributes from markdown render hook
Browse files Browse the repository at this point in the history
Fixes #9511
  • Loading branch information
jmooring authored Feb 16, 2022
1 parent b2a827c commit ff545f4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
40 changes: 37 additions & 3 deletions markup/goldmark/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/gohugoio/hugo/hugolib"
)

// Issue 9463
func TestAttributeExclusion(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -55,9 +56,42 @@ foo
).Build()

b.AssertFileContent("public/p1/index.html", `
<h2 class="a" id="heading">
<blockquote class="b">
<div class="highlight" id="c">
<h2 class="a" id="heading">
<blockquote class="b">
<div class="highlight" id="c">
`)
}

// Issue 9511
func TestAttributeExclusionWithRenderHook(t *testing.T) {
t.Parallel()

files := `
-- content/p1.md --
---
title: "p1"
---
## Heading {onclick="alert('renderhook')" data-foo="bar"}
-- layouts/_default/single.html --
{{ .Content }}
-- layouts/_default/_markup/render-heading.html --
<h{{ .Level }}
{{- range $k, $v := .Attributes -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
>{{ .Text | safeHTML }}</h{{ .Level }}>
`

b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
NeedsOsFS: false,
},
).Build()

b.AssertFileContent("public/p1/index.html", `
<h2 data-foo="bar" id="heading">Heading</h2>
`)
}

Expand Down
3 changes: 3 additions & 0 deletions markup/goldmark/render_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func (a *attributesHolder) Attributes() map[string]string {
a.attributesInit.Do(func() {
a.attributes = make(map[string]string)
for _, attr := range a.astAttributes {
if strings.HasPrefix(string(attr.Name), "on") {
continue
}
a.attributes[string(attr.Name)] = string(util.EscapeHTML(attr.Value.([]byte)))
}
})
Expand Down

0 comments on commit ff545f4

Please sign in to comment.