@marcel91200 / caddy Mirror · read-only / commits / f6d2c293
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 12:00 UTC

caddyfile: Reject global request matchers earlier (#6339)

Francis Lavoie committed May 23, 2024 at 22:06 UTC f6d2c293e752254769efe21c8d06a16ebad4845e
2 files changed +29 -1
caddyconfig/caddyfile/parse.go
+6 -1
@@ -214,7 +214,12 @@ func (p *parser) addresses() error {
214 value := p.Val()
215 token := p.Token()
216
217 - // special case: import directive replaces tokens during parse-time
217 + // Reject request matchers if trying to define them globally
218 + if strings.HasPrefix(value, "@") {
219 + return p.Errf("request matchers may not be defined globally, they must be in a site block; found %s", value)
220 + }
221 +
222 + // Special case: import directive replaces tokens during parse-time
223 if value == "import" && p.isNewLine() {
224 err := p.doImport(0)
225 if err != nil {
caddyconfig/caddyfile/parse_test.go
+23
@@ -857,6 +857,29 @@ func TestSnippetAcrossMultipleFiles(t *testing.T) {
857 }
858 }
859
860 +func TestRejectsGlobalMatcher(t *testing.T) {
861 + p := testParser(`
862 + @rejected path /foo
863 +
864 + (common) {
865 + gzip foo
866 + errors stderr
867 + }
868 +
869 + http://example.com {
870 + import common
871 + }
872 + `)
873 + _, err := p.parseAll()
874 + if err == nil {
875 + t.Fatal("Expected an error, but got nil")
876 + }
877 + expected := "request matchers may not be defined globally, they must be in a site block; found @rejected, at Testfile:2"
878 + if err.Error() != expected {
879 + t.Errorf("Expected error to be '%s' but got '%v'", expected, err)
880 + }
881 +}
882 +
883 func testParser(input string) parser {
884 return parser{Dispenser: NewTestDispenser(input)}
885 }