Validate origin domains #2849

This commit is contained in:
Martin Lensment 2015-08-25 16:33:47 +03:00
parent 71b2763df6
commit 5ba39fb406
23 changed files with 99 additions and 24 deletions

View file

@ -29,7 +29,6 @@ class Epp::DomainsController < EppController
handle_errors(@domain) and return if @domain.errors.any? handle_errors(@domain) and return if @domain.errors.any?
handle_errors and return unless balance_ok?('create') handle_errors and return unless balance_ok?('create')
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain? if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain?
current_user.registrar.debit!({ current_user.registrar.debit!({

View file

@ -9,22 +9,27 @@ class DomainNameValidator < ActiveModel::EachValidator
class << self class << self
def validate_format(value) def validate_format(value)
return true if value == 'ee'
return true unless value return true unless value
value = value.mb_chars.downcase.strip value = value.mb_chars.downcase.strip
general_domains = /(.pri.ee|.com.ee|.fie.ee|.med.ee|.ee)/ origins = ZonefileSetting.pluck(:origin)
# if someone tries to register an origin domain, let this validation pass
# the error will be catched in blocked domains validator
return true if origins.include?(value)
general_domains = /(#{origins.join('|')})/
# general_domains = /(.pri.ee|.com.ee|.fie.ee|.med.ee|.ee)/
# it's punycode # it's punycode
if value[2] == '-' && value[3] == '-' if value[2] == '-' && value[3] == '-'
regexp = /\Axn--[a-zA-Z0-9-]{0,59}#{general_domains}\z/ regexp = /\Axn--[a-zA-Z0-9-]{0,59}\.#{general_domains}\z/
return false unless value =~ regexp return false unless value =~ regexp
value = SimpleIDN.to_unicode(value).mb_chars.downcase.strip value = SimpleIDN.to_unicode(value).mb_chars.downcase.strip
end end
# rubocop: disable Metrics/LineLength # rubocop: disable Metrics/LineLength
unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ # äõöüšž unicode_chars = /\u00E4\u00F5\u00F6\u00FC\u0161\u017E/ # äõöüšž
regexp = /\A[a-zA-Z0-9#{unicode_chars.source}][a-zA-Z0-9#{unicode_chars.source}-]{0,61}[a-zA-Z0-9#{unicode_chars.source}]#{general_domains.source}\z/ regexp = /\A[a-zA-Z0-9#{unicode_chars.source}][a-zA-Z0-9#{unicode_chars.source}-]{0,61}[a-zA-Z0-9#{unicode_chars.source}]\.#{general_domains.source}\z/
# rubocop: enable Metrics/LineLength # rubocop: enable Metrics/LineLength
# rubocop: disable Style/DoubleNegation # rubocop: disable Style/DoubleNegation
!!(value =~ regexp) !!(value =~ regexp)

View file

@ -3,6 +3,12 @@ require 'rails_helper'
describe 'EPP Contact', epp: true do describe 'EPP Contact', epp: true do
before :all do before :all do
@xsd = Nokogiri::XML::Schema(File.read('lib/schemas/contact-eis-1.0.xsd')) @xsd = Nokogiri::XML::Schema(File.read('lib/schemas/contact-eis-1.0.xsd'))
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
Fabricate(:zonefile_setting, origin: 'med.ee')
Fabricate(:zonefile_setting, origin: 'fie.ee')
Fabricate(:zonefile_setting, origin: 'com.ee')
@registrar1 = Fabricate(:registrar1) @registrar1 = Fabricate(:registrar1)
@registrar2 = Fabricate(:registrar2) @registrar2 = Fabricate(:registrar2)
@epp_xml = EppXml::Contact.new(cl_trid: 'ABC-12345') @epp_xml = EppXml::Contact.new(cl_trid: 'ABC-12345')

View file

@ -4,6 +4,13 @@ describe 'EPP Domain', epp: true do
before(:all) do before(:all) do
@xsd = Nokogiri::XML::Schema(File.read('lib/schemas/domain-eis-1.0.xsd')) @xsd = Nokogiri::XML::Schema(File.read('lib/schemas/domain-eis-1.0.xsd'))
@epp_xml = EppXml.new(cl_trid: 'ABC-12345') @epp_xml = EppXml.new(cl_trid: 'ABC-12345')
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
Fabricate(:zonefile_setting, origin: 'med.ee')
Fabricate(:zonefile_setting, origin: 'fie.ee')
Fabricate(:zonefile_setting, origin: 'com.ee')
@registrar1 = Fabricate(:registrar1, code: 'REGDOMAIN1') @registrar1 = Fabricate(:registrar1, code: 'REGDOMAIN1')
@registrar1.credit!({ sum: 10000 }) @registrar1.credit!({ sum: 10000 })
@registrar2 = Fabricate(:registrar2, code: 'REGDOMAIN2') @registrar2 = Fabricate(:registrar2, code: 'REGDOMAIN2')

View file

@ -2,6 +2,7 @@ require 'rails_helper'
describe 'EPP Keyrelay', epp: true do describe 'EPP Keyrelay', epp: true do
before(:all) do before(:all) do
Fabricate(:zonefile_setting, origin: 'ee')
@registrar1 = Fabricate(:registrar1) @registrar1 = Fabricate(:registrar1)
@registrar2 = Fabricate(:registrar2) @registrar2 = Fabricate(:registrar2)
@domain = Fabricate(:domain, registrar: @registrar2) @domain = Fabricate(:domain, registrar: @registrar2)

View file

@ -2,6 +2,7 @@ require 'rails_helper'
feature 'BlockedDomain', type: :feature do feature 'BlockedDomain', type: :feature do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@user = Fabricate(:admin_user) @user = Fabricate(:admin_user)
end end

View file

@ -2,6 +2,8 @@ require 'rails_helper'
feature 'Domain', type: :feature do feature 'Domain', type: :feature do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
@user = Fabricate(:admin_user) @user = Fabricate(:admin_user)
end end

View file

@ -2,6 +2,7 @@ require 'rails_helper'
feature 'ReservedDomain', type: :feature do feature 'ReservedDomain', type: :feature do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@user = Fabricate(:admin_user) @user = Fabricate(:admin_user)
end end

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
feature 'DomainDeleteConfirm', type: :feature do feature 'DomainDeleteConfirm', type: :feature do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
context 'as unknown user with domain without token' do context 'as unknown user with domain without token' do
before :all do before :all do
@domain = Fabricate(:domain) @domain = Fabricate(:domain)

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
feature 'DomainUpdateConfirm', type: :feature do feature 'DomainUpdateConfirm', type: :feature do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
context 'as unknown user with domain without update token' do context 'as unknown user with domain without update token' do
before :all do before :all do
@domain = Fabricate(:domain) @domain = Fabricate(:domain)

View file

@ -2,6 +2,8 @@ require 'rails_helper'
feature 'Domains', type: :feature do feature 'Domains', type: :feature do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
@user = Fabricate(:api_user) @user = Fabricate(:api_user)
end end
@ -58,7 +60,9 @@ feature 'Domains', type: :feature do
it 'should search domains' do it 'should search domains' do
# having shared state across tests is really annoying sometimes... # having shared state across tests is really annoying sometimes...
click_link "#{@user} (#{@user.roles.first}) - #{@user.registrar}" within('.dropdown-menu') do
click_link "#{@user} (#{@user.roles.first}) - #{@user.registrar}"
end
Fabricate(:domain, name: 'abcde.ee', registrar: @user.registrar) Fabricate(:domain, name: 'abcde.ee', registrar: @user.registrar)
Fabricate(:domain, name: 'abcdee.ee', registrar: @user.registrar) Fabricate(:domain, name: 'abcdee.ee', registrar: @user.registrar)

View file

@ -2,6 +2,7 @@ require 'rails_helper'
feature 'Sessions', type: :feature do feature 'Sessions', type: :feature do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@user = Fabricate(:ee_user) @user = Fabricate(:ee_user)
@registrar1 = Fabricate(:registrar1) @registrar1 = Fabricate(:registrar1)
@registrar2 = Fabricate(:registrar2) @registrar2 = Fabricate(:registrar2)

View file

@ -26,6 +26,7 @@ describe ContactMailer do
describe 'email changed notification' do describe 'email changed notification' do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@domain = Fabricate(:domain) @domain = Fabricate(:domain)
@contact = @domain.registrant @contact = @domain.registrant
@contact.reload # until figured out why registrant_domains not loaded @contact.reload # until figured out why registrant_domains not loaded

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe DomainMailer do describe DomainMailer do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
describe 'pending update request for an old registrant when delivery turned off' do describe 'pending update request for an old registrant when delivery turned off' do
before :all do before :all do
@registrant = Fabricate(:registrant, email: 'test@example.com') @registrant = Fabricate(:registrant, email: 'test@example.com')

View file

@ -2,6 +2,7 @@ require 'rails_helper'
describe Contact do describe Contact do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@api_user = Fabricate(:api_user) @api_user = Fabricate(:api_user)
end end
@ -383,6 +384,7 @@ end
describe Contact, '.destroy_orphans' do describe Contact, '.destroy_orphans' do
before do before do
Fabricate(:zonefile_setting, origin: 'ee')
@contact_1 = Fabricate(:contact, code: 'asd12') @contact_1 = Fabricate(:contact, code: 'asd12')
@contact_2 = Fabricate(:contact, code: 'asd13') @contact_2 = Fabricate(:contact, code: 'asd13')
end end

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe Dnskey do describe Dnskey do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
it { should belong_to(:domain) } it { should belong_to(:domain) }
context 'with invalid attribute' do context 'with invalid attribute' do

View file

@ -1,6 +1,14 @@
require 'rails_helper' require 'rails_helper'
describe Domain do describe Domain do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
Fabricate(:zonefile_setting, origin: 'pri.ee')
Fabricate(:zonefile_setting, origin: 'med.ee')
Fabricate(:zonefile_setting, origin: 'fie.ee')
Fabricate(:zonefile_setting, origin: 'com.ee')
end
it { should belong_to(:registrar) } it { should belong_to(:registrar) }
it { should have_many(:nameservers) } it { should have_many(:nameservers) }
it { should belong_to(:registrant) } it { should belong_to(:registrant) }
@ -573,16 +581,17 @@ describe Domain do
end end
it 'should not create zone origin domain' do it 'should not create zone origin domain' do
zs = Fabricate(:zonefile_setting)
d = Fabricate.build(:domain, name: 'ee') d = Fabricate.build(:domain, name: 'ee')
d.save.should == false d.save.should == false
d.errors.full_messages.should match_array([ d.errors.full_messages.should match_array([
"Data management policy violation: Domain name is blocked [name]" "Data management policy violation: Domain name is blocked [name]"
]) ])
zs.destroy d = Fabricate.build(:domain, name: 'bla')
d.save.should == false
d.save.should == true d.errors.full_messages.should match_array([
"Domain name Domain name is invalid"
])
end end
# d = Domain.new # d = Domain.new

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe DomainTransfer do describe DomainTransfer do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
it { should belong_to(:domain) } it { should belong_to(:domain) }
context 'with invalid attribute' do context 'with invalid attribute' do

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe Keyrelay do describe Keyrelay do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
it { should belong_to(:domain) } it { should belong_to(:domain) }
it { should belong_to(:requester) } it { should belong_to(:requester) }
it { should belong_to(:accepter) } it { should belong_to(:accepter) }

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe Nameserver do describe Nameserver do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
it { should belong_to(:domain) } it { should belong_to(:domain) }
context 'with invalid attribute' do context 'with invalid attribute' do

View file

@ -1,6 +1,9 @@
require 'rails_helper' require 'rails_helper'
describe RegistrantVerification do describe RegistrantVerification do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
context 'with invalid attribute' do context 'with invalid attribute' do
before :all do before :all do
@registrant_verification = RegistrantVerification.new @registrant_verification = RegistrantVerification.new

View file

@ -1,6 +1,10 @@
require 'rails_helper' require 'rails_helper'
describe WhoisRecord do describe WhoisRecord do
before :all do
Fabricate(:zonefile_setting, origin: 'ee')
end
context 'with invalid attribute' do context 'with invalid attribute' do
before :all do before :all do
@whois_record = WhoisRecord.new @whois_record = WhoisRecord.new

View file

@ -2,6 +2,7 @@ require 'rails_helper'
describe Repp::DomainV1 do describe Repp::DomainV1 do
before :all do before :all do
Fabricate(:zonefile_setting, origin: 'ee')
@registrar1 = Fabricate(:registrar1) @registrar1 = Fabricate(:registrar1)
@api_user = Fabricate(:gitlab_api_user, registrar: @registrar1) @api_user = Fabricate(:gitlab_api_user, registrar: @registrar1)
Fabricate.times(2, :domain, registrar: @api_user.registrar) Fabricate.times(2, :domain, registrar: @api_user.registrar)