mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-03-13 10:26:06 +00:00
fix(dns): handle empty and invalid IP addresses gracefully
Add guard checks to prevent panics when processing client info with empty IP addresses. Replace netip.MustParseAddr with ParseAddr to handle invalid IP addresses gracefully instead of panicking. Add test to verify queryFromSelf handles IP addresses safely.
This commit is contained in:
committed by
Cuong Manh Le
parent
bdb8bedba1
commit
eb6ac8617b
@@ -1158,7 +1158,12 @@ func (p *prog) getClientInfo(remoteIP string, msg *dns.Msg) *ctrld.ClientInfo {
|
||||
} else {
|
||||
ci.Hostname = p.ciTable.LookupHostname(ci.IP, ci.Mac)
|
||||
}
|
||||
ci.Self = p.queryFromSelf(ci.IP)
|
||||
|
||||
if ci.IP == "" {
|
||||
p.Debug().Msgf("client info entry with empty IP address: %v", ci)
|
||||
} else {
|
||||
ci.Self = p.queryFromSelf(ci.IP)
|
||||
}
|
||||
// If this is a query from self, but ci.IP is not loopback IP,
|
||||
// try using hostname mapping for lookback IP if presents.
|
||||
if ci.Self {
|
||||
@@ -1275,7 +1280,12 @@ func (p *prog) queryFromSelf(ip string) bool {
|
||||
if val, ok := p.queryFromSelfMap.Load(ip); ok {
|
||||
return val.(bool)
|
||||
}
|
||||
netIP := netip.MustParseAddr(ip)
|
||||
netIP, err := netip.ParseAddr(ip)
|
||||
if err != nil {
|
||||
p.Debug().Err(err).Msgf("could not parse IP: %q", ip)
|
||||
return false
|
||||
}
|
||||
|
||||
regularIPs, loopbackIPs, err := netmon.LocalAddresses()
|
||||
if err != nil {
|
||||
p.Warn().Err(err).Msg("Could not get local addresses")
|
||||
|
||||
@@ -794,6 +794,16 @@ func Test_handleRecovery_Integration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_prog_queryFromSelf(t *testing.T) {
|
||||
p := newTestProg(t)
|
||||
require.NotPanics(t, func() {
|
||||
p.queryFromSelf("")
|
||||
})
|
||||
require.NotPanics(t, func() {
|
||||
p.queryFromSelf("foo")
|
||||
})
|
||||
}
|
||||
|
||||
// newTestProg creates a properly initialized *prog for testing.
|
||||
func newTestProg(t *testing.T) *prog {
|
||||
p := &prog{cfg: testhelper.SampleConfig(t)}
|
||||
|
||||
Reference in New Issue
Block a user