Skip to content

Commit

Permalink
caddytls: Fix sni_regexp matcher to obtain layer4 contexts (#6804)
Browse files Browse the repository at this point in the history
* caddytls: Fix sni_regexp matcher

* caddytls: Refactor sni_regexp matcher
  • Loading branch information
vnxme authored Jan 25, 2025
1 parent 30743c3 commit 7b8f350
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions modules/caddytls/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package caddytls

import (
"context"
"crypto/tls"
"fmt"
"net"
Expand Down Expand Up @@ -224,15 +225,28 @@ func (MatchServerNameRE) CaddyModule() caddy.ModuleInfo {

// Match matches hello based on SNI using a regular expression.
func (m MatchServerNameRE) Match(hello *tls.ClientHelloInfo) bool {
repl := caddy.NewReplacer()
// caddytls.TestServerNameMatcher calls this function without any context
if ctx := hello.Context(); ctx != nil {
// Note: caddytls.TestServerNameMatcher calls this function without any context
ctx := hello.Context()
if ctx == nil {
// layer4.Connection implements GetContext() to pass its context here,
// since hello.Context() returns nil
if mayHaveContext, ok := hello.Conn.(interface{ GetContext() context.Context }); ok {
ctx = mayHaveContext.GetContext()
}
}

var repl *caddy.Replacer
if ctx != nil {
// In some situations the existing context may have no replacer
if replAny := ctx.Value(caddy.ReplacerCtxKey); replAny != nil {
repl = replAny.(*caddy.Replacer)
}
}

if repl == nil {
repl = caddy.NewReplacer()
}

return m.MatchRegexp.Match(hello.ServerName, repl)
}

Expand Down

0 comments on commit 7b8f350

Please sign in to comment.