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

Reject non-FQDN SNI values #129

Merged
merged 1 commit into from
Sep 18, 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
5 changes: 5 additions & 0 deletions pkg/https.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func handleTLS(c *Config, conn net.Conn, httpslog zerolog.Logger) error {
httpslog.Err(err)
return err
}
if !isValidFQDN(sni) {
httpslog.Warn().Msgf("Invalid SNI: %s", sni)
conn.Close()
return nil
}
connInfo := acl.ConnInfo{
SrcIP: conn.RemoteAddr(),
Domain: sni,
Expand Down
12 changes: 11 additions & 1 deletion pkg/https_sni.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package sniproxy

import "fmt"
import (
"fmt"
"regexp"
)

var tlsHeaderLength = 5

Expand Down Expand Up @@ -147,4 +150,11 @@ func getExtensionBlock(data []byte) ([]byte, error) {
return data[index:], nil
}

// isValidFQDN validates if the given hostname is a valid FQDN
func isValidFQDN(hostname string) bool {
// Regular expression to match a valid FQDN
var fqdnRegex = regexp.MustCompile(`^(?i:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+(?:[a-z]{2,})$`)
return fqdnRegex.MatchString(hostname)
}

// vim: foldmethod=marker