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

httpcaddyfile: Ignore the import empty dotfile. #5320

Merged
merged 2 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ func (p *parser) doImport() error {
} else {
return p.Errf("File to import not found: %s", importPattern)
}
} else {
// See issue #5295 - should skip any files that start with a . when iterating over them.
sep := string(filepath.Separator)
segGlobPattern := strings.Split(globPattern, sep)
if strings.HasPrefix(segGlobPattern[len(segGlobPattern)-1], "*") {
var tmpMatches []string
for _, m := range matches {
seg := strings.Split(m, sep)
if !strings.HasPrefix(seg[len(seg)-1], ".") {
tmpMatches = append(tmpMatches, m)
}
}
matches = tmpMatches
}
}

// collect all the imported tokens
Expand Down Expand Up @@ -451,6 +465,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
}

// only warning in case of empty files
if len(input) == 0 {
caddy.Log().Warn("import file is empty", zap.String("file", importFile))
return []Token{}, nil
}
francislavoie marked this conversation as resolved.
Show resolved Hide resolved

importedTokens, err := allTokens(importFile, input)
if err != nil {
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
Expand Down
15 changes: 15 additions & 0 deletions caddyconfig/caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ func TestParseOneAndImport(t *testing.T) {

{`import testdata/not_found.txt`, true, []string{}, []int{}},

// empty file should just log a warning, and result in no tokens
{`import testdata/empty.txt`, false, []string{}, []int{}},

// import path/to/dir/* should skip any files that start with a . when iterating over them.
{`localhost
dir1 arg1
import testdata/glob/*`, false, []string{
"localhost",
}, []int{2, 3, 1}},

// import path/to/dir/.* should continue to read all dotfiles in a dir.
{`import testdata/glob/.*`, false, []string{
"host1",
}, []int{1, 2}},

{`""`, false, []string{}, []int{}},

{``, false, []string{}, []int{}},
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions caddyconfig/caddyfile/testdata/glob/.dotfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
host1 {
dir1
dir2 arg1
}
2 changes: 2 additions & 0 deletions caddyconfig/caddyfile/testdata/glob/import_test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dir2 arg1 arg2
dir3