Browse Source

cname lengths issues

pull/1/head
Claudius Nicolae 12 years ago
parent
commit
3c27918dda
  1. 4
      app/models/cname.rb
  2. 47
      app/validators/hostname2_validator.rb

4
app/models/cname.rb

@ -8,7 +8,9 @@
# Obtained from http://www.zytrax.com/books/dns/ch8/cname.html
#
class CNAME < Record
validates :name, :hostname => {:allow_underscore => true, :allow_wildcard_hostname => true}
validates :name,
:length => { :maximum => 1024 },
:hostname2 => { :allow_underscore => true, :allow_wildcard_hostname => true }
validates :content, :presence => true, :hostname => true
end

47
app/validators/hostname2_validator.rb

@ -0,0 +1,47 @@
# skips length validations
class Hostname2Validator < PAK::ValidatesHostname::HostnameValidator
def validate_each(record, attribute, value)
value ||= ''
# split each hostname into labels and do various checks
if value.is_a?(String)
labels = value.split '.'
labels.each_with_index do |label, index|
# CHECK 2: hostname label cannot begin or end with hyphen
add_error(record, attribute, :label_begins_or_ends_with_hyphen) if label =~ /^[-]/i or label =~ /[-]$/
# Take care of wildcard first label
next if options[:allow_wildcard_hostname] and label == '*' and index == 0
# CHECK 3: hostname can only contain characters:
# a-z, 0-9, hyphen, optional underscore, optional asterisk
valid_chars = 'a-z0-9\-'
valid_chars << '_' if options[:allow_underscore] == true
add_error(record, attribute, :label_contains_invalid_characters, :valid_chars => valid_chars) unless label =~ /^[#{valid_chars}]+$/i
end
# CHECK 4: the unqualified hostname portion cannot consist of
# numeric values only
if options[:allow_numeric_hostname] == false
is_numeric_only = (
(
Integer(labels[0]) rescue false
) ? true : false
)
add_error(record, attribute, :hostname_label_is_numeric) if is_numeric_only
end
# CHECK 5: in order to be fully qualified, the full hostname's
# TLD must be valid
if options[:require_valid_tld] == true
has_tld = options[:valid_tlds].select {
|tld| tld =~ /^#{Regexp.escape(labels.last || '')}$/i
}.empty? ? false : true
add_error(record, attribute, :hostname_is_not_fqdn) unless has_tld
end
end
end
end
Loading…
Cancel
Save