Skip to content

Commit e2bd987

Browse files
committed
Do not warn on potentially unsafe HTML comments when unsafe=false
We will still not render these comments, so from a safety perspective this is the same, but HTML comments are very common also inside Markdown and too useful to throw away. Updates gohugoio#13278
1 parent 8de4ffb commit e2bd987

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

markup/goldmark/goldmark_integration_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -851,3 +851,54 @@ title: "p1"
851851
b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
852852
b.AssertLogContains("! WARN")
853853
}
854+
855+
// See https://github.com/gohugoio/hugo/issues/13278#issuecomment-2603280548
856+
func TestGoldmarkRawHTMLCommentNoWarning(t *testing.T) {
857+
files := `
858+
-- hugo.toml --
859+
disableKinds = ['home','rss','section','sitemap','taxonomy','term']
860+
markup.goldmark.renderer.unsafe = false
861+
-- content/p1.md --
862+
---
863+
title: "p1"
864+
---
865+
# HTML comments
866+
867+
## Simple
868+
<!-- This is a comment -->
869+
870+
<!-- This is a comment indented -->
871+
872+
**Hello**<!-- This is a comment indented with markup surrounding. -->_world_.
873+
## With HTML
874+
875+
<!-- <p>This is another paragraph </p> -->
876+
877+
## With HTML and JS
878+
879+
<!-- <script>alert('hello');</script> -->
880+
881+
## With Block
882+
883+
<!--
884+
<p>Look at this cool image:</p>
885+
<img border="0" src="pic_trulli.jpg" alt="Trulli">
886+
-->
887+
888+
XSS
889+
890+
<!-- --><script>alert("I just escaped the HTML comment")</script><!-- -->
891+
892+
-- layouts/_default/single.html --
893+
{{ .Content }}
894+
`
895+
896+
b := hugolib.Test(t, files, hugolib.TestOptWarn())
897+
898+
b.AssertFileContent("public/p1/index.html", "! <!-- raw HTML omitted -->")
899+
b.AssertLogContains("! Raw HTML omitted")
900+
901+
b = hugolib.Test(t, strings.ReplaceAll(files, "markup.goldmark.renderer.unsafe = false", "markup.goldmark.renderer.unsafe = true"), hugolib.TestOptWarn())
902+
b.AssertFileContent("public/p1/index.html", "<!-- This is a comment -->")
903+
b.AssertLogContains("! WARN")
904+
}

markup/goldmark/hugocontext/hugocontext.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ func (r *hugoContextRenderer) renderHTMLBlock(
174174
w util.BufWriter, source []byte, node ast.Node, entering bool,
175175
) (ast.WalkStatus, error) {
176176
n := node.(*ast.HTMLBlock)
177+
isHTMLComment := func(b []byte) bool {
178+
return len(b) > 4 && b[0] == '<' && b[1] == '!' && b[2] == '-' && b[3] == '-'
179+
}
177180
if entering {
178181
if r.Unsafe {
179182
l := n.Lines().Len()
@@ -188,16 +191,24 @@ func (r *hugoContextRenderer) renderHTMLBlock(
188191
r.Writer.SecureWrite(w, linev)
189192
}
190193
} else {
191-
r.logRawHTMLEmittedWarn(w)
192-
_, _ = w.WriteString("<!-- raw HTML omitted -->\n")
194+
l := n.Lines().At(0)
195+
v := l.Value(source)
196+
if !isHTMLComment(v) {
197+
r.logRawHTMLEmittedWarn(w)
198+
_, _ = w.WriteString("<!-- raw HTML omitted -->\n")
199+
}
193200
}
194201
} else {
195202
if n.HasClosure() {
196203
if r.Unsafe {
197204
closure := n.ClosureLine
198205
r.Writer.SecureWrite(w, closure.Value(source))
199206
} else {
200-
_, _ = w.WriteString("<!-- raw HTML omitted -->\n")
207+
l := n.Lines().At(0)
208+
v := l.Value(source)
209+
if !isHTMLComment(v) {
210+
_, _ = w.WriteString("<!-- raw HTML omitted -->\n")
211+
}
201212
}
202213
}
203214
}

0 commit comments

Comments
 (0)