Add ip validation to nameserver

This commit is contained in:
Martin Lensment 2014-08-05 17:44:46 +03:00
parent 38f0528d70
commit da950f5df7
6 changed files with 49 additions and 5 deletions

View file

@ -63,7 +63,7 @@ module Epp::DomainsHelper
'2302' => [:epp_domain_taken, :epp_domain_reserved], '2302' => [:epp_domain_taken, :epp_domain_reserved],
'2306' => [:blank, [:out_of_range, {min: 1, max: 13}]], '2306' => [:blank, [:out_of_range, {min: 1, max: 13}]],
'2303' => [:not_found], '2303' => [:not_found],
'2005' => [:hostname_invalid] '2005' => [:hostname_invalid, :ip_invalid]
}, @domain }, @domain
) )
end end

View file

@ -68,9 +68,15 @@ class Domain < ActiveRecord::Base
if ns.is_a?(Hash) if ns.is_a?(Hash)
attrs = {hostname: ns[:hostName]} attrs = {hostname: ns[:hostName]}
ns[:hostAddr].each do |ip| if ns[:hostAddr]
attrs[:ip] = ip unless attrs[:ip] if ns[:hostAddr].is_a?(Array)
end if ns[:hostAddr] ns[:hostAddr].each do |ip|
attrs[:ip] = ip unless attrs[:ip]
end
else
attrs[:ip] = ns[:hostAddr]
end
end
self.nameservers.build(attrs) self.nameservers.build(attrs)
#ns with just hostname #ns with just hostname

View file

@ -3,5 +3,5 @@ class Nameserver < ActiveRecord::Base
has_and_belongs_to_many :domains has_and_belongs_to_many :domains
validates :hostname, hostname: true validates :hostname, hostname: true
validates :ip, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_nil: true} validates :ip, format: { with: /\A(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\z/, allow_nil: true, message: :ip_invalid}
end end

View file

@ -49,10 +49,13 @@ en:
blank: 'Required parameter missing - nameserver' blank: 'Required parameter missing - nameserver'
out_of_range: 'Domain must have %{min}-%{max} nameservers' out_of_range: 'Domain must have %{min}-%{max} nameservers'
hostname_invalid: 'Hostname is invalid' hostname_invalid: 'Hostname is invalid'
ip_invalid: 'IP is invalid'
nameserver: nameserver:
attributes: attributes:
hostname: hostname:
invalid: 'Hostname is invalid' invalid: 'Hostname is invalid'
ip:
ip_invalid: 'IP is invalid'
errors: errors:

View file

@ -92,6 +92,12 @@ describe 'EPP Domain', epp: true do
ns = Domain.first.nameservers.first ns = Domain.first.nameservers.first
expect(ns.ip).to eq('192.0.2.2') expect(ns.ip).to eq('192.0.2.2')
end end
it 'returns error when nameserver has invalid ip' do
response = epp_request('domains/create_w_invalid_ns_ip.xml')
expect(response[:results][0][:result_code]).to eq '2005'
expect(response[:results][0][:msg]).to eq 'IP is invalid'
end
end end
context 'with juridical persion as an owner' do context 'with juridical persion as an owner' do

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">
<command>
<create>
<domain:create
xmlns:domain="urn:ietf:params:xml:ns:domain-1.0">
<domain:name>example.ee</domain:name>
<domain:period unit="y">1</domain:period>
<domain:ns>
<domain:hostAttr>
<domain:hostName>ns1.example.net</domain:hostName>
<domain:hostAddr ip="v4">192.0.2.2.invalid</domain:hostAddr>
</domain:hostAttr>
<domain:hostAttr>
<domain:hostName>ns2.example.net</domain:hostName>
</domain:hostAttr>
</domain:ns>
<domain:registrant>jd1234</domain:registrant>
<domain:contact type="admin">sh8013</domain:contact>
<domain:contact type="tech">sh8013</domain:contact>
<domain:contact type="tech">sh801333</domain:contact>
<domain:authInfo>
<domain:pw>2fooBAR</domain:pw>
</domain:authInfo>
</domain:create>
</create>
<clTRID>ABC-12345</clTRID>
</command>
</epp>