Skip to content

Commit e1298a6

Browse files
committed
do not handle CIDRs as hostnames in allowFrom list (see #51)
1 parent e8794be commit e1298a6

2 files changed

Lines changed: 24 additions & 17 deletions

File tree

cmd/socket-proxy/handlehttprequest.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log/slog"
66
"net"
77
"net/http"
8-
"strings"
98
)
109

1110
// handleHTTPRequest checks if the request is allowed and sends it to the proxy.
@@ -34,7 +33,7 @@ func handleHTTPRequest(w http.ResponseWriter, r *http.Request) {
3433
return
3534
}
3635

37-
// finally log and proxy the request
36+
// finally, log and proxy the request
3837
slog.Debug("allowed request", "method", r.Method, "URL", r.URL, "client", r.RemoteAddr)
3938
socketProxy.ServeHTTP(w, r) // proxy the request
4039
}
@@ -53,31 +52,35 @@ func isAllowedClient(remoteAddr string) (bool, error) {
5352
return false, errors.New("invalid IP format")
5453
}
5554

56-
_, allowedIPNet, err := net.ParseCIDR(cfg.AllowFrom)
57-
if err == nil {
58-
// AllowFrom is a valid CIDR, so check if IP address is in allowed network
59-
return allowedIPNet.Contains(clientIP), nil
60-
}
55+
for _, allowFromItem := range cfg.AllowFrom {
56+
57+
// first try to handle as an CIDR
58+
_, allowedIPNet, err := net.ParseCIDR(allowFromItem)
59+
if err == nil {
60+
// AllowFrom is a valid CIDR, so check if IP address is in allowed network
61+
return allowedIPNet.Contains(clientIP), nil
62+
}
6163

62-
// AllowFrom is not a valid CIDR, so try to resolve it via DNS
63-
// split over comma to support multiple hostnames
64-
allowFromList := strings.Split(cfg.AllowFrom, ",")
65-
for _, allowFrom := range allowFromList {
66-
ips, err := net.LookupIP(allowFrom)
64+
// AllowFrom is not a valid CIDR, so try to resolve it via DNS
65+
// We intentionally do not cache the DNS lookups.
66+
// In our use case, the resolver should be a local service, and we don't want to cause DNS caching errors.
67+
ips, err := net.LookupIP(allowFromItem)
6768
if err != nil {
68-
slog.Warn("error looking up allowed client hostname", "hostname", allowFrom, "error", err.Error())
69+
slog.Warn("error looking up allowed client hostname", "hostname", allowFromItem, "error", err.Error())
6970
}
7071
for _, ip := range ips {
71-
// Check if IP address is one of the resolved IPs
72+
// Check if the IP address is one of the resolved IPs
7273
if ip.Equal(clientIP) {
7374
return true, nil
7475
}
7576
}
7677
}
78+
79+
// If we get here, the IP address is not allowed
7780
return false, nil
7881
}
7982

80-
// sendHTTPError sends a HTTP error with the given status code.
83+
// sendHTTPError sends an HTTP error with the given status code.
8184
func sendHTTPError(w http.ResponseWriter, status int) {
8285
http.Error(w, http.StatusText(status), status)
8386
}

internal/config/config.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131

3232
type Config struct {
3333
AllowedRequests map[string]*regexp.Regexp
34-
AllowFrom string
34+
AllowFrom []string
3535
AllowHealthcheck bool
3636
LogJSON bool
3737
StopOnWatchdog bool
@@ -70,6 +70,7 @@ var mr = []methodRegex{
7070
func InitConfig() (*Config, error) {
7171
var (
7272
cfg Config
73+
allowFromString string
7374
listenIP string
7475
proxyPort uint
7576
logLevel string
@@ -133,7 +134,7 @@ func InitConfig() (*Config, error) {
133134
}
134135
}
135136

136-
flag.StringVar(&cfg.AllowFrom, "allowfrom", defaultAllowFrom, "allowed IPs or hostname to connect to the proxy")
137+
flag.StringVar(&allowFromString, "allowfrom", defaultAllowFrom, "allowed IPs or hostname to connect to the proxy")
137138
flag.BoolVar(&cfg.AllowHealthcheck, "allowhealthcheck", defaultAllowHealthcheck, "allow health check requests (HEAD http://localhost:55555/health)")
138139
flag.BoolVar(&cfg.LogJSON, "logjson", defaultLogJSON, "log in JSON format (otherwise log in plain text")
139140
flag.StringVar(&listenIP, "listenip", defaultListenIP, "ip address to listen on")
@@ -156,6 +157,9 @@ func InitConfig() (*Config, error) {
156157
}
157158
flag.Parse()
158159

160+
// parse comma-separeted allowFromString into allowFrom slice
161+
cfg.AllowFrom = strings.Split(allowFromString, ",")
162+
159163
// check listenIP and proxyPort
160164
if net.ParseIP(listenIP) == nil {
161165
return nil, fmt.Errorf("invalid IP \"%s\" for listenip", listenIP)

0 commit comments

Comments
 (0)