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

caddyfile: Reject global request matchers earlier #6339

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion caddyconfig/caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ func (p *parser) addresses() error {
value := p.Val()
token := p.Token()

// special case: import directive replaces tokens during parse-time
// Reject request matchers if trying to define them globally
if strings.HasPrefix(value, "@") {
return p.Errf("request matchers may not be defined globally, they must be in a site block; found %s", value)
}

// Special case: import directive replaces tokens during parse-time
if value == "import" && p.isNewLine() {
err := p.doImport(0)
if err != nil {
Expand Down
23 changes: 23 additions & 0 deletions caddyconfig/caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,29 @@ func TestSnippetAcrossMultipleFiles(t *testing.T) {
}
}

func TestRejectsGlobalMatcher(t *testing.T) {
p := testParser(`
@rejected path /foo

(common) {
gzip foo
errors stderr
}

http://example.com {
import common
}
`)
_, err := p.parseAll()
if err == nil {
t.Fatal("Expected an error, but got nil")
}
expected := "request matchers may not be defined globally, they must be in a site block; found @rejected, at Testfile:2"
if err.Error() != expected {
t.Errorf("Expected error to be '%s' but got '%v'", expected, err)
}
}

func testParser(input string) parser {
return parser{Dispenser: NewTestDispenser(input)}
}
Loading