]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Escape .Title in built-in image and link render hooks
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 Apr 2024 08:56:02 +0000 (10:56 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 22 Apr 2024 14:54:24 +0000 (16:54 +0200)
Co-authored-by: Joe Mooring <joe@mooring.com>
.hugo_build.lock [new file with mode: 0644]
hugolib/content_render_hooks_test.go
tpl/tplimpl/embedded/templates/_default/_markup/render-image.html
tpl/tplimpl/embedded/templates/_default/_markup/render-link.html

diff --git a/.hugo_build.lock b/.hugo_build.lock
new file mode 100644 (file)
index 0000000..e69de29
index 36d1e626f6cba0cc0fc5d1d1345d6b556f6e2337..abe305762595f2833200a9cc94e170130f172394 100644 (file)
@@ -14,6 +14,7 @@
 package hugolib
 
 import (
+       "fmt"
        "strings"
        "testing"
 )
@@ -241,3 +242,52 @@ iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAA
                        "p1|<p><a href=\"p2\">P2</a>", "<img src=\"pixel.png\" alt=\"Pixel\">")
        })
 }
+
+func TestRenderHooksDefaultEscape(t *testing.T) {
+       files := `
+-- hugo.toml --
+[markup.goldmark.renderHooks]
+[markup.goldmark.renderHooks.image]
+  enableDefault = ENABLE
+[markup.goldmark.renderHooks.link]
+enableDefault = ENABLE
+[markup.goldmark.parser]
+wrapStandAloneImageWithinParagraph = false
+[markup.goldmark.parser.attribute]
+block = true
+title = true
+-- content/_index.md --
+---
+title: "Home"
+---
+Link: [text-"<>&](/destination-"<> 'title-"<>&')
+
+Image: ![alt-"<>&](/destination-"<> 'title-"<>&')
+{class="><script>alert()</script>" id="baz"}
+
+-- layouts/index.html --
+{{ .Content }}
+`
+
+       for _, enabled := range []bool{true, false} {
+               enabled := enabled
+               t.Run(fmt.Sprint(enabled), func(t *testing.T) {
+                       t.Parallel()
+                       b := Test(t, strings.ReplaceAll(files, "ENABLE", fmt.Sprint(enabled)))
+
+                       // The escaping is slightly different between the two.
+                       if enabled {
+                               b.AssertFileContent("public/index.html",
+                                       "Link: <a href=\"/destination-%22%3C%3E\" title=\"title-&#34;&lt;&gt;&amp;\">text-&quot;&lt;&gt;&amp;</a>",
+                                       "img alt=\"alt-&quot;&lt;&gt;&amp;\" src=\"/destination-%22%3C%3E\" title=\"title-&#34;&lt;&gt;&amp;\">",
+                                       "&gt;&lt;script&gt;",
+                               )
+                       } else {
+                               b.AssertFileContent("public/index.html",
+                                       "Link: <a href=\"/destination-%22%3C%3E\" title=\"title-&quot;&lt;&gt;&amp;\">text-&quot;&lt;&gt;&amp;</a>",
+                                       "Image: <img src=\"/destination-%22%3C%3E\" alt=\"alt-&quot;&lt;&gt;&amp;\" title=\"title-&quot;&lt;&gt;&amp;\">",
+                               )
+                       }
+               })
+       }
+}
index 013e31235a4d19c048a8b49a459a10161517e609..8757639100513324157fee98b6b5cae3ccc48472 100644 (file)
@@ -5,7 +5,7 @@
     {{- $src = .RelPermalink -}}
   {{- end -}}
 {{- end -}}
-{{- $attributes := merge .Attributes (dict "alt" .Text "src" $src "title" .Title) -}}
+{{- $attributes := merge .Attributes (dict "alt" .Text "src" $src "title" (.Title | transform.HTMLEscape)) -}}
 <img
   {{- range $k, $v := $attributes -}}
     {{- if $v -}}
index 8903d3dfbbb5f353ccdc0637cc9981b6a44c80c9..30e4d2660c67fb4bde170c8f1cacc16976823d96 100644 (file)
@@ -17,7 +17,7 @@
     {{- end -}}
   {{- end -}}
 {{- end -}}
-{{- $attributes := dict "href" $href "title" .Title -}}
+{{- $attributes := dict "href" $href "title" (.Title | transform.HTMLEscape) -}}
 <a
   {{- range $k, $v := $attributes -}}
     {{- if $v -}}