From 9b4fc523788587a331a889f7029e07180ac1a58b Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Tue, 19 May 2020 20:23:15 -0400 Subject: [PATCH] httpcaddyfile: Improve error on matcher declared outside site block --- caddyconfig/httpcaddyfile/httptype.go | 3 +++ caddyconfig/httpcaddyfile/httptype_test.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go index fddb095239c..42fee71f4bf 100644 --- a/caddyconfig/httpcaddyfile/httptype.go +++ b/caddyconfig/httpcaddyfile/httptype.go @@ -54,6 +54,9 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock, originalServerBlocks := make([]serverBlock, 0, len(inputServerBlocks)) for i, sblock := range inputServerBlocks { for j, k := range sblock.Keys { + if j == 0 && strings.HasPrefix(k, "@") { + return nil, warnings, fmt.Errorf("cannot define a matcher outside of a site block: '%s'", k) + } if _, ok := sbKeys[k]; ok { return nil, warnings, fmt.Errorf("duplicate site address not allowed: '%s' in %v (site block %d, key %d)", k, sblock.Keys, i, j) } diff --git a/caddyconfig/httpcaddyfile/httptype_test.go b/caddyconfig/httpcaddyfile/httptype_test.go index 1bc2ddf812e..b0ee3ac0140 100644 --- a/caddyconfig/httpcaddyfile/httptype_test.go +++ b/caddyconfig/httpcaddyfile/httptype_test.go @@ -57,6 +57,15 @@ func TestMatcherSyntax(t *testing.T) { expectWarn: false, expectError: false, }, + { + input: `@matcher { + path /matcher-not-allowed/outside-of-site-block/* + } + http://localhost + `, + expectWarn: false, + expectError: true, + }, } { adapter := caddyfile.Adapter{