From 92baa14fd3f45c0917c5988235cd1a0f8692f171 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sat, 30 Mar 2019 17:08:25 +0100 Subject: hugolib: Allow page-relative aliases Fixes #5757 --- hugolib/alias.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'hugolib/alias.go') diff --git a/hugolib/alias.go b/hugolib/alias.go index 599821c0a..972f7b01c 100644 --- a/hugolib/alias.go +++ b/hugolib/alias.go @@ -18,6 +18,7 @@ import ( "fmt" "html/template" "io" + "path" "path/filepath" "runtime" "strings" @@ -28,8 +29,6 @@ import ( "github.com/gohugoio/hugo/publisher" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/tpl" - - "github.com/gohugoio/hugo/helpers" ) const ( @@ -132,13 +131,14 @@ func (a aliasHandler) targetPathAlias(src string) (string, error) { return "", fmt.Errorf("alias \"\" is an empty string") } - alias := filepath.Clean(src) - components := strings.Split(alias, helpers.FilePathSeparator) + alias := path.Clean(filepath.ToSlash(src)) - if !a.allowRoot && alias == helpers.FilePathSeparator { + if !a.allowRoot && alias == "/" { return "", fmt.Errorf("alias \"%s\" resolves to website root directory", originalAlias) } + components := strings.Split(alias, "/") + // Validate against directory traversal if components[0] == ".." { return "", fmt.Errorf("alias \"%s\" traverses outside the website root directory", originalAlias) @@ -182,15 +182,12 @@ func (a aliasHandler) targetPathAlias(src string) (string, error) { } // Add the final touch - alias = strings.TrimPrefix(alias, helpers.FilePathSeparator) - if strings.HasSuffix(alias, helpers.FilePathSeparator) { + alias = strings.TrimPrefix(alias, "/") + if strings.HasSuffix(alias, "/") { alias = alias + "index.html" } else if !strings.HasSuffix(alias, ".html") { - alias = alias + helpers.FilePathSeparator + "index.html" - } - if originalAlias != alias { - a.log.INFO.Printf("Alias \"%s\" translated to \"%s\"\n", originalAlias, alias) + alias = alias + "/" + "index.html" } - return alias, nil + return filepath.FromSlash(alias), nil } -- cgit v1.2.3