mirror of
https://github.com/internetee/registry.git
synced 2025-06-07 21:25:39 +02:00
Merge pull request #1834 from internetee/1833-adding-only-ipv6-with-empty-ipv4
IP Whitelist: Fix IPv4/IPv6 parsing for empty string
This commit is contained in:
commit
7d69f2fad9
2 changed files with 22 additions and 3 deletions
|
@ -4,8 +4,13 @@ class WhiteIp < ApplicationRecord
|
|||
|
||||
validate :valid_ipv4?
|
||||
validate :valid_ipv6?
|
||||
|
||||
validate :validate_ipv4_and_ipv6
|
||||
before_save :normalize_blank_values
|
||||
|
||||
def normalize_blank_values
|
||||
%i[ipv4 ipv6].each { |c| self[c].present? || self[c] = nil }
|
||||
end
|
||||
|
||||
def validate_ipv4_and_ipv6
|
||||
return if ipv4.present? || ipv6.present?
|
||||
errors.add(:base, I18n.t(:ipv4_or_ipv6_must_be_present))
|
||||
|
@ -50,10 +55,10 @@ class WhiteIp < ApplicationRecord
|
|||
def ids_including(ip)
|
||||
ipv4 = ipv6 = []
|
||||
if check_ip4(ip).present?
|
||||
ipv4 = select { |white_ip| IPAddr.new(white_ip.ipv4, Socket::AF_INET) === check_ip4(ip) }
|
||||
ipv4 = select { |white_ip| check_ip4(white_ip.ipv4) === check_ip4(ip) }
|
||||
end
|
||||
if check_ip6(ip).present?
|
||||
ipv6 = select { |white_ip| IPAddr.new(white_ip.ipv6, Socket::AF_INET6) === check_ip6(ip) }
|
||||
ipv6 = select { |white_ip| check_ip6(white_ip.ipv6) === check_ip6(ip) }
|
||||
end
|
||||
(ipv4 + ipv6).pluck(:id).flatten.uniq
|
||||
end
|
||||
|
|
|
@ -38,6 +38,20 @@ class WhiteIpTest < ActiveSupport::TestCase
|
|||
assert white_ip.valid?
|
||||
end
|
||||
|
||||
def test_validates_include_empty_ipv4
|
||||
white_ip = WhiteIp.new
|
||||
|
||||
white_ip.ipv4 = nil
|
||||
white_ip.ipv6 = '001:0db8:85a3:0000:0000:8a2e:0370:7334'
|
||||
white_ip.registrar = registrars(:bestnames)
|
||||
|
||||
assert_nothing_raised { white_ip.save }
|
||||
assert white_ip.valid?
|
||||
|
||||
assert WhiteIp.include_ip?(white_ip.ipv6)
|
||||
assert_not WhiteIp.include_ip?('192.168.1.1')
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid_white_ip
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue