From 2ad90a593a10c40437d561088f805f0c3aeceefb Mon Sep 17 00:00:00 2001 From: steadytao Date: Sat, 17 Oct 2026 18:39:23 +1000 Subject: [PATCH] caddyhttp: preserve method matcher case --- modules/caddyhttp/celmatcher_test.go | 18 ++++++ modules/caddyhttp/matchers.go | 13 ++-- modules/caddyhttp/matchers_test.go | 88 ++++++++++++++++++++++++---- 3 files changed, 99 insertions(+), 20 deletions(-) diff --git a/modules/caddyhttp/celmatcher_test.go b/modules/caddyhttp/celmatcher_test.go index 7c8e706d3..4331fbdf7 100644 --- a/modules/caddyhttp/celmatcher_test.go +++ b/modules/caddyhttp/celmatcher_test.go @@ -186,6 +186,24 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV httpMethod: "PUT", wantResult: true, }, + { + name: "extension method matches exact case (MatchMethod)", + expression: &MatchExpression{ + Expr: `method('foo')`, + }, + urlTarget: "https://foo.example.com", + httpMethod: "foo", + wantResult: true, + }, + { + name: "extension method does not fold case (MatchMethod)", + expression: &MatchExpression{ + Expr: `method('foo')`, + }, + urlTarget: "https://foo.example.com", + httpMethod: "FOO", + wantResult: false, + }, { name: "method error not enough arguments (MatchMethod)", expression: &MatchExpression{ diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index c4a42bd27..08648f885 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -117,7 +117,7 @@ type ( // (potentially leading to collisions). MatchPathRE struct{ MatchRegexp } - // MatchMethod matches requests by the method. + // MatchMethod matches requests by the case-sensitive HTTP method token. MatchMethod []string // MatchQuery matches requests by the URI's query string. It takes a JSON object @@ -897,14 +897,9 @@ func (MatchMethod) CaddyModule() caddy.ModuleInfo { } } -// Provision uppercases all configured methods to ensure case-insensitive -// matching against incoming requests, whose methods are always uppercase. -func (m MatchMethod) Provision(_ caddy.Context) error { - for i := range m { - m[i] = strings.ToUpper(m[i]) - } - return nil -} +// Provision is retained for compatibility. HTTP method tokens are +// case-sensitive and need no preprocessing. +func (MatchMethod) Provision(_ caddy.Context) error { return nil } // UnmarshalCaddyfile implements caddyfile.Unmarshaler. func (m *MatchMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go index d22c134da..6582c44e5 100644 --- a/modules/caddyhttp/matchers_test.go +++ b/modules/caddyhttp/matchers_test.go @@ -15,16 +15,21 @@ package caddyhttp import ( + "bufio" "context" + "encoding/json" "fmt" "net/http" "net/http/httptest" "net/url" "os" "runtime" + "slices" + "strings" "testing" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" ) func TestHostMatcher(t *testing.T) { @@ -1347,15 +1352,21 @@ func TestMethodMatcher(t *testing.T) { expect: true, }, { - scenario: "match lowercase get after Provision uppercases it", - match: MatchMethod{"get"}, - input: http.MethodGet, + scenario: "match lowercase extension method", + match: MatchMethod{"foo"}, + input: "foo", expect: true, }, { - scenario: "match mixed case Post after Provision uppercases it", - match: MatchMethod{"Post"}, - input: http.MethodPost, + scenario: "do not fold lowercase extension method", + match: MatchMethod{"foo"}, + input: "FOO", + expect: false, + }, + { + scenario: "match mixed case extension method", + match: MatchMethod{"MiXeD"}, + input: "MiXeD", expect: true, }, { @@ -1366,15 +1377,13 @@ func TestMethodMatcher(t *testing.T) { }, { scenario: "match one of multiple methods", - match: MatchMethod{"get", "post"}, - input: http.MethodPost, + match: MatchMethod{"GET", "foo"}, + input: "foo", expect: true, }, } { req := &http.Request{Method: tc.input} - if err := tc.match.Provision(caddy.Context{}); err != nil { - t.Errorf("Test %d %v: provisioning failed: %v", i, tc.scenario, err) - } + tc.match = provisionMethodMatcher(t, tc.match) actual, err := tc.match.MatchWithError(req) if err != nil { t.Errorf("Test %d %v: matching failed: %v", i, tc.scenario, err) @@ -1386,6 +1395,63 @@ func TestMethodMatcher(t *testing.T) { } } +func TestMethodMatcherConfigurationPreservesCase(t *testing.T) { + want := MatchMethod{"GET", "foo", "MiXeD"} + for _, tc := range []struct { + name string + load func(*testing.T) MatchMethod + }{ + { + name: "native JSON", + load: func(t *testing.T) MatchMethod { + var matcher MatchMethod + if err := json.Unmarshal([]byte(`["GET","foo","MiXeD"]`), &matcher); err != nil { + t.Fatal(err) + } + return matcher + }, + }, + { + name: "Caddyfile", + load: func(t *testing.T) MatchMethod { + var matcher MatchMethod + if err := matcher.UnmarshalCaddyfile(caddyfile.NewTestDispenser("method GET foo MiXeD")); err != nil { + t.Fatal(err) + } + return matcher + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + got := provisionMethodMatcher(t, tc.load(t)) + if !slices.Equal(got, want) { + t.Errorf("configured methods = %q, want %q", got, want) + } + }) + } +} + +func TestMethodMatcherPreservesWireCase(t *testing.T) { + req, err := http.ReadRequest(bufio.NewReader(strings.NewReader("foo / HTTP/1.1\r\nHost: example.com\r\n\r\n"))) + if err != nil { + t.Fatal(err) + } + defer req.Body.Close() + + matcher := provisionMethodMatcher(t, MatchMethod{"foo"}) + if matched, err := matcher.MatchWithError(req); err != nil || !matched { + t.Fatalf("matching lower-case wire method: matched=%t, error=%v", matched, err) + } +} + +func provisionMethodMatcher(t *testing.T, matcher MatchMethod) MatchMethod { + t.Helper() + if err := matcher.Provision(caddy.Context{}); err != nil { + t.Fatalf("provisioning method matcher: %v", err) + } + return matcher +} + func BenchmarkLargeHostMatcher(b *testing.B) { // this benchmark simulates a large host matcher (thousands of entries) where each // value is an exact hostname (not a placeholder or wildcard) - compare the results