Skip to content

Commit 7b8f350

Browse files
authored
caddytls: Fix sni_regexp matcher to obtain layer4 contexts (#6804)
* caddytls: Fix sni_regexp matcher * caddytls: Refactor sni_regexp matcher
1 parent 30743c3 commit 7b8f350

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

modules/caddytls/matchers.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package caddytls
1616

1717
import (
18+
"context"
1819
"crypto/tls"
1920
"fmt"
2021
"net"
@@ -224,15 +225,28 @@ func (MatchServerNameRE) CaddyModule() caddy.ModuleInfo {
224225

225226
// Match matches hello based on SNI using a regular expression.
226227
func (m MatchServerNameRE) Match(hello *tls.ClientHelloInfo) bool {
227-
repl := caddy.NewReplacer()
228-
// caddytls.TestServerNameMatcher calls this function without any context
229-
if ctx := hello.Context(); ctx != nil {
228+
// Note: caddytls.TestServerNameMatcher calls this function without any context
229+
ctx := hello.Context()
230+
if ctx == nil {
231+
// layer4.Connection implements GetContext() to pass its context here,
232+
// since hello.Context() returns nil
233+
if mayHaveContext, ok := hello.Conn.(interface{ GetContext() context.Context }); ok {
234+
ctx = mayHaveContext.GetContext()
235+
}
236+
}
237+
238+
var repl *caddy.Replacer
239+
if ctx != nil {
230240
// In some situations the existing context may have no replacer
231241
if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
232242
repl = replAny.(*caddy.Replacer)
233243
}
234244
}
235245

246+
if repl == nil {
247+
repl = caddy.NewReplacer()
248+
}
249+
236250
return m.MatchRegexp.Match(hello.ServerName, repl)
237251
}
238252

0 commit comments

Comments
 (0)