Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 06:45 UTC
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.
Y.Horie committed
Jan 22, 2023 at 02:22 UTC
d6d7511699f6a35e3246918023af5ba3bc2cbc67
6 files changed
+50
caddyconfig/caddyfile/parse.go
+20
@@ -389,6 +389,20 @@ func (p *parser) doImport() error {
389
} else {
390
return p.Errf("File to import not found: %s", importPattern)
391
}
392
+ } else {
393
+ // See issue #5295 - should skip any files that start with a . when iterating over them.
394
+ sep := string(filepath.Separator)
395
+ segGlobPattern := strings.Split(globPattern, sep)
396
+ if strings.HasPrefix(segGlobPattern[len(segGlobPattern)-1], "*") {
397
+ var tmpMatches []string
398
+ for _, m := range matches {
399
+ seg := strings.Split(m, sep)
400
+ if !strings.HasPrefix(seg[len(seg)-1], ".") {
401
+ tmpMatches = append(tmpMatches, m)
402
+ }
403
+ }
404
+ matches = tmpMatches
405
+ }
406
}
407
408
// collect all the imported tokens
@@ -451,6 +465,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
465
return nil, p.Errf("Could not read imported file %s: %v", importFile, err)
466
}
467
468
+ // only warning in case of empty files
469
+ if len(input) == 0 || len(strings.TrimSpace(string(input))) == 0 {
470
+ caddy.Log().Warn("Import file is empty", zap.String("file", importFile))
471
+ return []Token{}, nil
472
+ }
473
+
474
importedTokens, err := allTokens(importFile, input)
475
if err != nil {
476
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
caddyconfig/caddyfile/parse_test.go
+17
@@ -187,6 +187,23 @@ func TestParseOneAndImport(t *testing.T) {
187
188
{`import testdata/not_found.txt`, true, []string{}, []int{}},
189
190
+ // empty file should just log a warning, and result in no tokens
191
+ {`import testdata/empty.txt`, false, []string{}, []int{}},
192
+
193
+ {`import testdata/only_white_space.txt`, false, []string{}, []int{}},
194
+
195
+ // import path/to/dir/* should skip any files that start with a . when iterating over them.
196
+ {`localhost
197
+ dir1 arg1
198
+ import testdata/glob/*`, false, []string{
199
+ "localhost",
200
+ }, []int{2, 3, 1}},
201
+
202
+ // import path/to/dir/.* should continue to read all dotfiles in a dir.
203
+ {`import testdata/glob/.*`, false, []string{
204
+ "host1",
205
+ }, []int{1, 2}},
206
+
207
{`""`, false, []string{}, []int{}},
208
209
{``, false, []string{}, []int{}},
caddyconfig/caddyfile/testdata/empty.txt
caddyconfig/caddyfile/testdata/glob/.dotfile.txt
new
+4
@@ -0,0 +1,4 @@
1
+host1 {
2
+ dir1
3
+ dir2 arg1
4
+}
caddyconfig/caddyfile/testdata/glob/import_test1.txt
new
+2
@@ -0,0 +1,2 @@
1
+dir2 arg1 arg2
2
+dir3
\ No newline at end of file
caddyconfig/caddyfile/testdata/only_white_space.txt
new
+7
@@ -0,0 +1,7 @@
1
+
2
+
3
+
4
+
5
+
6
+
7
+