mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-15 15:26:27 +03:00
templates: fix inconsistent nested includes (#4452)
This commit is contained in:
parent
f55b123d63
commit
ec14ccdd40
3 changed files with 109 additions and 12 deletions
|
@ -12,6 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build gofuzz
|
||||||
// +build gofuzz
|
// +build gofuzz
|
||||||
|
|
||||||
package templates
|
package templates
|
||||||
|
|
|
@ -91,7 +91,12 @@ func (c TemplateContext) OriginalReq() http.Request {
|
||||||
// trusted files. If it is not trusted, be sure to use escaping functions
|
// trusted files. If it is not trusted, be sure to use escaping functions
|
||||||
// in your template.
|
// in your template.
|
||||||
func (c TemplateContext) funcInclude(filename string, args ...interface{}) (string, error) {
|
func (c TemplateContext) funcInclude(filename string, args ...interface{}) (string, error) {
|
||||||
bodyBuf, err := c.readFileToBuffer(filename)
|
|
||||||
|
bodyBuf := bufPool.Get().(*bytes.Buffer)
|
||||||
|
bodyBuf.Reset()
|
||||||
|
defer bufPool.Put(bodyBuf)
|
||||||
|
|
||||||
|
err := c.readFileToBuffer(filename, bodyBuf)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -107,28 +112,24 @@ func (c TemplateContext) funcInclude(filename string, args ...interface{}) (stri
|
||||||
return bodyBuf.String(), nil
|
return bodyBuf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// readFileToBuffer returns the contents of filename relative to root as a buffer
|
// readFileToBuffer reads a file into a buffer
|
||||||
func (c TemplateContext) readFileToBuffer(filename string) (*bytes.Buffer, error) {
|
func (c TemplateContext) readFileToBuffer(filename string, bodyBuf *bytes.Buffer) error {
|
||||||
if c.Root == nil {
|
if c.Root == nil {
|
||||||
return nil, fmt.Errorf("root file system not specified")
|
return fmt.Errorf("root file system not specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := c.Root.Open(filename)
|
file, err := c.Root.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
bodyBuf := bufPool.Get().(*bytes.Buffer)
|
|
||||||
bodyBuf.Reset()
|
|
||||||
defer bufPool.Put(bodyBuf)
|
|
||||||
|
|
||||||
_, err = io.Copy(bodyBuf, file)
|
_, err = io.Copy(bodyBuf, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return bodyBuf, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// funcHTTPInclude returns the body of a virtual (lightweight) request
|
// funcHTTPInclude returns the body of a virtual (lightweight) request
|
||||||
|
@ -185,7 +186,12 @@ func (c TemplateContext) funcHTTPInclude(uri string) (string, error) {
|
||||||
// {{ template }} from the standard template library. If the imported file has
|
// {{ template }} from the standard template library. If the imported file has
|
||||||
// no {{ define }} blocks, the name of the import will be the path
|
// no {{ define }} blocks, the name of the import will be the path
|
||||||
func (c *TemplateContext) funcImport(filename string) (string, error) {
|
func (c *TemplateContext) funcImport(filename string) (string, error) {
|
||||||
bodyBuf, err := c.readFileToBuffer(filename)
|
|
||||||
|
bodyBuf := bufPool.Get().(*bytes.Buffer)
|
||||||
|
bodyBuf.Reset()
|
||||||
|
defer bufPool.Put(bodyBuf)
|
||||||
|
|
||||||
|
err := c.readFileToBuffer(filename, bodyBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -187,6 +188,95 @@ func TestImport(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNestedInclude(t *testing.T) {
|
||||||
|
for i, test := range []struct {
|
||||||
|
child string
|
||||||
|
childFile string
|
||||||
|
parent string
|
||||||
|
parentFile string
|
||||||
|
shouldErr bool
|
||||||
|
expect string
|
||||||
|
child2 string
|
||||||
|
child2File string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
// include in parent
|
||||||
|
child: `{{ include "file1" }}`,
|
||||||
|
childFile: "file0",
|
||||||
|
parent: `{{ $content := "file2" }}{{ $p := include $content}}`,
|
||||||
|
parentFile: "file1",
|
||||||
|
shouldErr: false,
|
||||||
|
expect: ``,
|
||||||
|
child2: `This shouldn't show`,
|
||||||
|
child2File: "file2",
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
context := getContextOrFail(t)
|
||||||
|
var absFilePath string
|
||||||
|
var absFilePath0 string
|
||||||
|
var absFilePath1 string
|
||||||
|
var buf *bytes.Buffer
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// create files and for test case
|
||||||
|
if test.parentFile != "" {
|
||||||
|
absFilePath = filepath.Join(fmt.Sprintf("%s", context.Root), test.parentFile)
|
||||||
|
if err := ioutil.WriteFile(absFilePath, []byte(test.parent), os.ModePerm); err != nil {
|
||||||
|
os.Remove(absFilePath)
|
||||||
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.childFile != "" {
|
||||||
|
absFilePath0 = filepath.Join(fmt.Sprintf("%s", context.Root), test.childFile)
|
||||||
|
if err := ioutil.WriteFile(absFilePath0, []byte(test.child), os.ModePerm); err != nil {
|
||||||
|
os.Remove(absFilePath0)
|
||||||
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if test.child2File != "" {
|
||||||
|
absFilePath1 = filepath.Join(fmt.Sprintf("%s", context.Root), test.child2File)
|
||||||
|
if err := ioutil.WriteFile(absFilePath1, []byte(test.child2), os.ModePerm); err != nil {
|
||||||
|
os.Remove(absFilePath0)
|
||||||
|
t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = bufPool.Get().(*bytes.Buffer)
|
||||||
|
buf.Reset()
|
||||||
|
defer bufPool.Put(buf)
|
||||||
|
buf.WriteString(test.child)
|
||||||
|
err = context.executeTemplateInBuffer(test.childFile, buf)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
if !test.shouldErr {
|
||||||
|
t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
|
||||||
|
}
|
||||||
|
} else if test.shouldErr {
|
||||||
|
t.Errorf("Test %d: Expected error but had none", i)
|
||||||
|
} else if buf.String() != test.expect {
|
||||||
|
//
|
||||||
|
t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, buf.String())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if absFilePath != "" {
|
||||||
|
if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if absFilePath0 != "" {
|
||||||
|
if err := os.Remove(absFilePath0); err != nil && !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if absFilePath1 != "" {
|
||||||
|
if err := os.Remove(absFilePath1); err != nil && !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInclude(t *testing.T) {
|
func TestInclude(t *testing.T) {
|
||||||
for i, test := range []struct {
|
for i, test := range []struct {
|
||||||
fileContent string
|
fileContent string
|
||||||
|
|
Loading…
Reference in a new issue