Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tpl/tplimpl: Prevent overloading of embedded comment shortcode #13277

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions common/paths/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,16 @@ func ToSlashPreserveLeading(s string) string {
func IsSameFilePath(s1, s2 string) bool {
return path.Clean(ToSlashTrim(s1)) == path.Clean(ToSlashTrim(s2))
}

// ToSlashNoExtensions returns the result of replacing each separator character
// in path s with a slash ('/') character, then removing all file extensions.
// For example, "/a/b/c.d/e.f.g" becomes "/a/b/c.d/e".
func ToSlashNoExtensions(s string) string {
s = filepath.ToSlash(s)
d, f := path.Split(s)
i := strings.Index(f, ".")
if i >= 0 {
f = f[:i]
}
return path.Join(d, f)
}
39 changes: 39 additions & 0 deletions common/paths/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package paths

import (
"path/filepath"
"strconv"
"testing"

qt "github.com/frankban/quicktest"
Expand Down Expand Up @@ -311,3 +312,41 @@ func TestIsSameFilePath(t *testing.T) {
c.Assert(IsSameFilePath(filepath.FromSlash(this.a), filepath.FromSlash(this.b)), qt.Equals, this.expected, qt.Commentf("a: %s b: %s", this.a, this.b))
}
}

func TestToSlashNoExtensions(t *testing.T) {
tests := []struct {
path string
want string
}{
{"a", "a"},
{"a/", "a"},
{"/a", "/a"},
{"/a/", "/a"},
{"a.b", "a"},
{"a.b/", "a.b"},
{"/a.b", "/a"},
{"/a.b/", "/a.b"},
{"a/b", "a/b"},
{"a/b/", "a/b"},
{"a/b", "a/b"},
{"/a/b/", "/a/b"},
{"a/b/c.d", "a/b/c"},
{"a/b/c.d", "a/b/c"},
{"a/b/c.d.e", "a/b/c"},
{"a/b/c.d/e", "a/b/c.d/e"},
{"a/b/c.d/e.f", "a/b/c.d/e"},
{"a/b/c.d/e.f.g", "a/b/c.d/e"},
{"a.", "a"},
{".a", ""},
{"/", "/"},
{".", ""},
{"", ""},
}
for k, tt := range tests {
t.Run(strconv.Itoa(k), func(t *testing.T) {
if got := ToSlashNoExtensions(tt.path); got != tt.want {
t.Errorf("ToSlashNoExtensions() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions tpl/tplimpl/embedded/templates/shortcodes/comment.html
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{{- /* DO NOT REMOVE THIS SHORTCODE FROM THE CODE BASE. */ -}}
{{- /* DOING SO WOULD EXPOSE HIDDEN INFORMATION. */ -}}
{{- $noop := .Inner -}}
22 changes: 22 additions & 0 deletions tpl/tplimpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
"sort"
"strings"
"sync"
Expand All @@ -31,6 +32,7 @@ import (
"unicode/utf8"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/paths"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/output/layouts"

Expand Down Expand Up @@ -72,6 +74,13 @@ var embeddedTemplatesAliases = map[string][]string{
"shortcodes/twitter.html": {"shortcodes/tweet.html"},
}

// These paths are reserved for embedded templates. Reserved paths follow the
// format "directory/name" (no extensions) relative to the layouts directory.
// Templates with these paths are not loaded from user space.
var reservedTemplatePaths = []string{
"shortcodes/comment",
}

var (
_ tpl.TemplateManager = (*templateExec)(nil)
_ tpl.TemplateHandler = (*templateExec)(nil)
Expand Down Expand Up @@ -825,6 +834,12 @@ func (t *templateHandler) loadTemplates() error {

name := strings.TrimPrefix(filepath.ToSlash(path), "/")
filename := filepath.Base(path)

if t.isReservedTemplatePath(path) {
t.Log.Infof("template not loaded: the path %q is reserved for embedded templates", name)
return nil
}

outputFormats := t.Conf.GetConfigSection("outputFormats").(output.Formats)
outputFormat, found := outputFormats.FromFilename(filename)

Expand All @@ -849,6 +864,13 @@ func (t *templateHandler) loadTemplates() error {
return nil
}

// isReservedTemplatePath reports whether the given template path is reserved
// for embedded templates. Reserved paths follow the format "directory/name"
// (no extensions) relative to the layouts directory.
func (t *templateHandler) isReservedTemplatePath(path string) bool {
return slices.Contains(reservedTemplatePaths, paths.ToSlashNoExtensions(path))
}

func (t *templateHandler) nameIsText(name string) (string, bool) {
isText := strings.HasPrefix(name, textTmplNamePrefix)
if isText {
Expand Down
44 changes: 44 additions & 0 deletions tpl/tplimpl/tplimpl_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,3 +858,47 @@ title: p5
}
b.Assert(htmlFiles, hqt.IsAllElementsEqual)
}

func TestReservedTemplatePaths(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
-- layouts/LAYOUT --
-- layouts/home.html --
{{- .Content -}}
-- layouts/shortcodes/a.html --
shortcode a
-- content/_index.md --
---
title: home
---
a {{< comment >}} b {{< /comment >}} c|{{< a >}}
`

filesOriginal := files

layouts := []string{
"shortcodes/comment.html",
"shortcodes/comment.html.html",
"shortcodes/comment.en.html.html",
"shortcodes/comment.de.html.html",
"shortcodes/comment.json",
"shortcodes/comment.json.json",
"shortcodes/comment.en.json.json",
"shortcodes/comment.de.json.json",
}

for _, layout := range layouts {
files = strings.ReplaceAll(filesOriginal, "LAYOUT", layout)
b := hugolib.Test(t, files, hugolib.TestOptInfo())
b.AssertLogContains("INFO template not loaded: the path", layout, "is reserved for embedded templates")
b.AssertFileContent("public/index.html", "<p>a c|shortcode a</p>")
}

files = strings.ReplaceAll(filesOriginal, "LAYOUT", "shortcodes/foo.html")
b := hugolib.Test(t, files, hugolib.TestOptInfo())
b.AssertLogContains("! INFO template not loaded: the path", "! is reserved for embedded templates")
b.AssertFileContent("public/index.html", "<p>a c|shortcode a</p>")
}
Loading