Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
9 changes: 7 additions & 2 deletions caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ type parser struct {
}

// maxSnippetExpansions is a hard cap to prevent excessively deep or cyclic snippet imports.
const maxSnippetExpansions = 1000
// set as a variable to allow modifications for testing
var maxSnippetExpansions = 10000

// maxFileExpansions is a hard cap to prevent excessively deep or cyclic file imports.
// set as a variable to allow modifications for testing
var maxFileExpansions = 100000

func (p *parser) parseAll() ([]ServerBlock, error) {
var blocks []ServerBlock
Expand Down Expand Up @@ -268,7 +273,7 @@ func (p *parser) doImport() error {
p.snippetExpansions++
importedTokens = p.definedSnippets[importPattern]
} else {
if p.fileExpansions >= maxSnippetExpansions {
if p.fileExpansions >= maxFileExpansions {
return p.Errf("maximum file import depth (%d) exceeded", maxSnippetExpansions)
}
p.fileExpansions++
Expand Down
31 changes: 31 additions & 0 deletions caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
package caddyfile

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)

func init() {
// set a lower limit for testing only
maxSnippetExpansions = 10
maxFileExpansions = 10
}

func TestAllTokens(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -801,3 +808,27 @@ func TestFileImportCycleError(t *testing.T) {
t.Fatalf("expected error for file import cycle, got nil")
}
}

func TestFileImportDir(t *testing.T) {
dir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

// create 10x the maxFileExpansions files
// a single import with a glob should not error
for i := 0; i < maxFileExpansions*10; i++ {
fp := filepath.Join(dir, filepath.Base(dir)+"_"+fmt.Sprintf("%d", i))
if err := ioutil.WriteFile(fp, []byte(""), 0644); err != nil {
t.Fatal(err)
}
}

input := "import " + filepath.Join(dir, "*")
p := testParser(input)
_, err = p.parseAll()
if err != nil {
t.Fatalf("unexpected error importing temp dir via glob: %v", err)
}
}