diff options
author | Y.Horie <[email protected]> | 2023-01-22 02:22:36 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2023-01-21 10:22:36 -0700 |
commit | d6d7511699f6a35e3246918023af5ba3bc2cbc67 (patch) | |
tree | e66396854c444346989408fafd73a27b6f2f38e2 /caddyconfig/caddyfile/parse.go | |
parent | 8d6870fd068aa3ea472a46f34d219f41703e2ddb (diff) | |
download | caddy-d6d7511699f6a35e3246918023af5ba3bc2cbc67.tar.gz caddy-d6d7511699f6a35e3246918023af5ba3bc2cbc67.zip |
httpcaddyfile: Warn on importing empty file; skip dotfiles (#5320)
* httpcaddyfile: Change the parse rules when empty file or dotfile with a glob.
* Fixes #5295
* Empty file should just log a warning, and result in no tokens.
* The last segment of the path is '*', it should skip any dotfiles.
* The last segment of the path is '.*', it should read all dotfiles in a dir.
* httpcaddyfile: Regard empty files as import files which include only white space.
Diffstat (limited to 'caddyconfig/caddyfile/parse.go')
-rw-r--r-- | caddyconfig/caddyfile/parse.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go index 82c515b97..edc86f28a 100644 --- a/caddyconfig/caddyfile/parse.go +++ b/caddyconfig/caddyfile/parse.go @@ -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 @@ -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 || len(strings.TrimSpace(string(input))) == 0 { + caddy.Log().Warn("Import file is empty", zap.String("file", importFile)) + return []Token{}, nil + } + importedTokens, err := allTokens(importFile, input) if err != nil { return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err) |