ruby-changes:68291
From: Jeremy <ko1@a...>
Date: Thu, 7 Oct 2021 18:35:49 +0900 (JST)
Subject: [ruby-changes:68291] abad5e10e8 (master): [ruby/ipaddr] Disallow leading zeros in mask
https://git.ruby-lang.org/ruby.git/commit/?id=abad5e10e8 From abad5e10e80c06f2e756667cd2d7f7d9b6e78ba5 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Tue, 4 Jun 2019 18:58:15 -0700 Subject: [ruby/ipaddr] Disallow leading zeros in mask https://github.com/ruby/ipaddr/commit/f49d2d49a4 --- lib/ipaddr.rb | 5 ++++- test/test_ipaddr.rb | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 6385d69c6a..6c0ded2a77 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -495,8 +495,11 @@ class IPAddr https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L495 def mask!(mask) case mask when String - if mask =~ /\A\d+\z/ + case mask + when /\A(0|[1-9]+\d*)\z/ prefixlen = mask.to_i + when /\A\d+\z/ + raise InvalidPrefixError, "leading zeros in prefix" else m = IPAddr.new(mask) if m.family != @family diff --git a/test/test_ipaddr.rb b/test/test_ipaddr.rb index 3c3d70e3f2..2b6e5c7524 100644 --- a/test/test_ipaddr.rb +++ b/test/test_ipaddr.rb @@ -92,6 +92,8 @@ class TC_IPAddr < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_ipaddr.rb#L92 assert_raise(IPAddr::InvalidAddressError) { IPAddr.new("[2001:200:300::]\nINVALID") } assert_raise(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.1/32\nINVALID") } assert_raise(IPAddr::InvalidAddressError) { IPAddr.new("192.168.0.1/32/20") } + assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/032") } + assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("::1/0128") } assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("::1/255.255.255.0") } assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("::1/129") } assert_raise(IPAddr::InvalidPrefixError) { IPAddr.new("192.168.0.1/33") } -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/