summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2023-06-22 09:11:03 -0700
committergit <svn-admin@ruby-lang.org>2023-06-22 16:40:46 +0000
commitde51a4a13eab2c4c5917de923edde33dfed6f22f (patch)
treeab3c4ed0fd5cff5326a095953d9980164a3c76b0 /lib
parent711cabec26eee20a30a2d8642b9f05ad6e7eeb49 (diff)
[ruby/ipaddr] Consider IPv4-mapped IPv6 addresses private if IPv4 address is private
Fixes [Bug #19479] https://github.com/ruby/ipaddr/commit/7faa0768d3
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddr.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb
index 7a5cf94830..b7877970e4 100644
--- a/lib/ipaddr.rb
+++ b/lib/ipaddr.rb
@@ -263,7 +263,8 @@ class IPAddr
# Returns true if the ipaddr is a private address. IPv4 addresses
# in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC
# 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC
- # 4193 are considered private.
+ # 4193 are considered private. Private IPv4 addresses in the
+ # IPv4-mapped IPv6 address range are also considered private.
def private?
case @family
when Socket::AF_INET
@@ -271,7 +272,12 @@ class IPAddr
@addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12
@addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16
when Socket::AF_INET6
- @addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000
+ @addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000 ||
+ (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
+ @addr & 0xff000000 == 0x0a000000 || # ::ffff:10.0.0.0/8
+ @addr & 0xfff00000 == 0xac100000 || # ::ffff::172.16.0.0/12
+ @addr & 0xffff0000 == 0xc0a80000 # ::ffff::192.168.0.0/16
+ ))
else
raise AddressFamilyError, "unsupported address family"
end