Merge pull request #1628 from internetee/1625-single-character-domains

Add support of single-character domains
This commit is contained in:
Timo Võhmar 2020-07-03 13:04:49 +03:00 committed by GitHub
commit 72619d7e23
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View file

@ -1,4 +1,6 @@
class DomainNameValidator < ActiveModel::EachValidator
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/LineLength
def validate_each(record, attribute, value)
if !self.class.validate_format(value)
record.errors[attribute] << (options[:message] || record.errors.generate_message(attribute, :invalid))
@ -27,8 +29,9 @@ class DomainNameValidator < ActiveModel::EachValidator
end
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/
!!(value =~ regexp)
regexp = /\A[a-zA-Z0-9#{unicode_chars.source}][a-zA-Z0-9#{unicode_chars.source}-]{0,62}\.#{general_domains.source}\z/
end_regexp = /\-\.#{general_domains.source}\z/ # should not contain dash as a closing char
!!(value =~ regexp && value !~ end_regexp)
end
def validate_blocked(value)
@ -39,4 +42,6 @@ class DomainNameValidator < ActiveModel::EachValidator
DNS::Zone.where(origin: value).count.zero?
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/LineLength
end

View file

@ -0,0 +1,20 @@
class FillSingleCharactedBlockedDomains < ActiveRecord::Migration[6.0]
DOMAIN_NAMES = %w[a.ee b.ee c.ee d.ee e.ee f.ee g.ee h.ee i.ee j.ee k.ee l.ee m.ee n.ee o.ee
p.ee q.ee r.ee s.ee š.ee z.ee ž.ee t.ee u.ee v.ee w.ee õ.ee ä.ee ö.ee ü.ee
x.ee y.ee 0.ee 1.ee 2.ee 3.ee 4.ee 5.ee 6.ee 7.ee 8.ee 9.ee].freeze
def up
BlockedDomain.transaction do
DOMAIN_NAMES.each do |name|
BlockedDomain.find_or_create_by(name: name)
end
end
end
def down
BlockedDomain.transaction do
BlockedDomain.by_domain(DOMAIN_NAMES).delete_all
end
end
end

View file

@ -31,7 +31,7 @@ class DomainTest < ActiveSupport::TestCase
def test_validates_name_format
assert_equal dns_zones(:one).origin, 'test'
domain = valid_domain
subdomain_min_length = 2
subdomain_min_length = 1
subdomain_max_length = 63
domain.name = '!invalid'
@ -46,9 +46,6 @@ class DomainTest < ActiveSupport::TestCase
domain.name = 'example-.test'
assert domain.invalid?
domain.name = "#{'a' * subdomain_min_length.pred}.test"
assert domain.invalid?
domain.name = "#{'a' * subdomain_max_length.next}.test"
assert domain.invalid?