You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Source net: {100.100.0.0 ffff0000}
Source IP length: 4
Source mask length: 4
Source range: 100.100.0.0 - 100.100.255.255
Dest net: {100.100.0.0 ffff0000}
Dest IP length: 16
Dest mask length: 4
Dest range: 100.100.0.0 - 0:ffff:ffff:ffff:ffff:ffff:ffff:ffff
Cause
The problem arises because you're relying on len(ip) to determine if ip is v4 or v6, which is not a reliable way because IPv4 can be represented in 16 bytes (IPv4 as IPv6). For example, 100.100.0.0 is represented in 16 bytes in the following way: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 0, 0], and in this case len(ip) will return 16, although it's obviously IPv4. In short: instead of using len(ip) == 4 to test if it's an IPv4 address, you should use net.To4(ip) != nil (len(net.To4(ip)) in this example will be equal to 4).
But how IPv4 of length 16 happens in the first place? There are many ways but in this particular case, it happened because json.Unmarshal always deserializes IPs to length 16, no matter if it's IPv4 or IPv6.
The bug from this example is in ipToInt function, but it may happen that you're using len(ip) check in some other places in your code.
The text was updated successfully, but these errors were encountered:
Expected behavior
If we have
net.IPNet
which contains100.100.0.0/16
,cidr.AddressRange
function should always return100.100.0.0
and100.100.255.255
Actual behavior
The method sometimes returns
100.100.0.0
and0:ffff:ffff:ffff:ffff:ffff:ffff:ffff
Steps to reproduce
produces the following output:
Cause
The problem arises because you're relying on
len(ip)
to determine ifip
is v4 or v6, which is not a reliable way because IPv4 can be represented in 16 bytes (IPv4 as IPv6). For example,100.100.0.0
is represented in 16 bytes in the following way:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 0, 0]
, and in this caselen(ip)
will return16
, although it's obviously IPv4. In short: instead of usinglen(ip) == 4
to test if it's an IPv4 address, you should usenet.To4(ip) != nil
(len(net.To4(ip))
in this example will be equal to4
).But how IPv4 of length 16 happens in the first place? There are many ways but in this particular case, it happened because
json.Unmarshal
always deserializes IPs to length 16, no matter if it's IPv4 or IPv6.The bug from this example is in
ipToInt
function, but it may happen that you're usinglen(ip)
check in some other places in your code.The text was updated successfully, but these errors were encountered: