mirror of
https://github.com/caddyserver/caddy.git
synced 2026-06-27 12:21:47 +00:00
httpcaddyfile: fix incorrect error message on duplicate matchers (#7780)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Has been cancelled
Tests / test (s390x on IBM Z) (push) Has been cancelled
Tests / goreleaser-check (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, aix) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, linux) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, windows) (push) Has been cancelled
Lint / lint (push) Has been cancelled
Lint / lint-1 (push) Has been cancelled
Lint / lint-2 (push) Has been cancelled
Lint / govulncheck (push) Has been cancelled
Lint / dependency-review (push) Has been cancelled
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.26.0, macos-14, 0, 1.26, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy, ~1.26.0, ubuntu-latest, 0, 1.26, linux) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.26.0, windows-latest, True, 1.26, windows) (push) Has been cancelled
Tests / test (s390x on IBM Z) (push) Has been cancelled
Tests / goreleaser-check (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, aix) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, linux) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Has been cancelled
Cross-Build / build (~1.26.0, 1.26, windows) (push) Has been cancelled
Lint / lint (push) Has been cancelled
Lint / lint-1 (push) Has been cancelled
Lint / lint-2 (push) Has been cancelled
Lint / govulncheck (push) Has been cancelled
Lint / dependency-review (push) Has been cancelled
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Parse each matcher segment individually using NewDispenser(segment) instead of DispenseDirective(dir), which coalesced all same-name segments into one token stream. This caused the second definition name to be misinterpreted as a matcher module name, producing 'module not registered: http.matchers.@name' instead of the correct 'matcher is defined more than once' error. By parsing segments individually, the existing duplicate check in parseMatcherDefinitions naturally catches the duplicate on the second pass. Signed-off-by: Brunotlps <brunoteixlps@gmail.com>
This commit is contained in:
parent
3eb8e48ff0
commit
0e8eb41b87
2 changed files with 40 additions and 3 deletions
|
|
@ -108,7 +108,7 @@ func (st ServerType) Setup(
|
|||
matcherDefs := make(map[string]caddy.ModuleMap)
|
||||
for _, segment := range sb.block.Segments {
|
||||
if dir := segment.Directive(); strings.HasPrefix(dir, matcherPrefix) {
|
||||
d := sb.block.DispenseDirective(dir)
|
||||
d := caddyfile.NewDispenser(segment)
|
||||
err := parseMatcherDefinitions(d, matcherDefs)
|
||||
if err != nil {
|
||||
return nil, warnings, err
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package httpcaddyfile
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
||||
|
|
@ -10,8 +11,9 @@ import (
|
|||
|
||||
func TestMatcherSyntax(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
input string
|
||||
expectError bool
|
||||
input string
|
||||
expectError bool
|
||||
expectContains string
|
||||
}{
|
||||
{
|
||||
input: `http://localhost
|
||||
|
|
@ -53,6 +55,34 @@ func TestMatcherSyntax(t *testing.T) {
|
|||
`,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `http://localhost {
|
||||
@test {
|
||||
path /test
|
||||
}
|
||||
@test {
|
||||
path /other
|
||||
}
|
||||
respond @test "hello"
|
||||
}
|
||||
`,
|
||||
expectError: true,
|
||||
expectContains: "is defined more than once",
|
||||
},
|
||||
{
|
||||
input: `(snippet) {
|
||||
@{args[0]} {
|
||||
path /{args[0]}
|
||||
}
|
||||
respond @{args[0]} "hello"
|
||||
}
|
||||
http://localhost {
|
||||
import snippet foo
|
||||
import snippet bar
|
||||
}
|
||||
`,
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
input: `@matcher {
|
||||
path /matcher-not-allowed/outside-of-site-block/*
|
||||
|
|
@ -73,6 +103,13 @@ func TestMatcherSyntax(t *testing.T) {
|
|||
t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err != nil && tc.expectContains != "" {
|
||||
if !strings.Contains(err.Error(), tc.expectContains) {
|
||||
t.Errorf("Test %d error message mismatch: expected to contain %q, got %q",
|
||||
i, tc.expectContains, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue