Skip to content

Commit

Permalink
PublicSuffixService.valid? should return false if the domain is not d…
Browse files Browse the repository at this point in the history
…efined or not allowed (closes #4, closes #5)
  • Loading branch information
weppos committed Oct 9, 2010
1 parent f0b0e46 commit 5c93081
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 63 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
Expand Up @@ -6,6 +6,8 @@


* FIXED: RuleList cache is not recreated when a new rule is appended to the list (#6) * FIXED: RuleList cache is not recreated when a new rule is appended to the list (#6)


* FIXED: PublicSuffixService.valid? should return false if the domain is not defined or not allowed (#4, #5)



== Release 0.6.0 == Release 0.6.0


Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -121,7 +121,7 @@ begin
desc "Publish YARD documentation to the site" desc "Publish YARD documentation to the site"
task :publish => ["yardoc:clobber", "yardoc"] do task :publish => ["yardoc:clobber", "yardoc"] do
ENV["username"] || raise(ArgumentError, "Missing ssh username") ENV["username"] || raise(ArgumentError, "Missing ssh username")
sh "rsync -avz --delete yardoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/yardoc" sh "rsync -avz --delete yardoc/ #{ENV["username"]}@code:/var/www/apps/code/#{PKG_NAME}/api"
end end


desc "Remove YARD products" desc "Remove YARD products"
Expand Down
35 changes: 24 additions & 11 deletions lib/public_suffix_service.rb
Expand Up @@ -56,14 +56,20 @@ module PublicSuffixService
# #
# @raise [PublicSuffixService::Error] # @raise [PublicSuffixService::Error]
# If domain is not a valid domain. # If domain is not a valid domain.
# @raise [PublicSuffixService::DomainNotAllowed]
# If a rule for +domain+ is found, but the rule
# doesn't allow +domain+.
# #
def self.parse(domain) def self.parse(domain)
rule = RuleList.default.find(domain) || raise(DomainInvalid, "`#{domain}' is not a valid domain") rule = RuleList.default.find(domain)
if rule.nil?
raise(DomainInvalid, "`#{domain}' is not a valid domain")
end
if !rule.allow?(domain)
raise DomainNotAllowed, "`#{domain}' is not allowed according to Registry policy"
end


left, right = rule.decompose(domain) left, right = rule.decompose(domain)
if right.nil?
raise DomainNotAllowed, "Rule `#{rule.name}' doesn't allow `#{domain}'"
end


parts = left.split(".") parts = left.split(".")
# If we have 0 parts left, there is just a tld and no domain or subdomain # If we have 0 parts left, there is just a tld and no domain or subdomain
Expand All @@ -76,7 +82,7 @@ def self.parse(domain)
Domain.new(tld, sld, trd) Domain.new(tld, sld, trd)
end end


# Checks whether +domain+ is a valid domain name, # Checks whether +domain+ is assigned and allowed,
# without actually parsing it. # without actually parsing it.
# #
# This method doesn't care whether domain is a domain or subdomain. # This method doesn't care whether domain is a domain or subdomain.
Expand All @@ -88,23 +94,30 @@ def self.parse(domain)
# @return [Boolean] # @return [Boolean]
# #
# @example Check a valid domain # @example Check a valid domain
# PublicSuffixService.valid?("google.com") # PublicSuffixService.valid?("example.com")
# # => true # # => true
# #
# @example Check a valid subdomain # @example Check a valid subdomain
# PublicSuffixService.valid?("www.google.com") # PublicSuffixService.valid?("www.example.com")
# # => true # # => true
# #
# @example Check an invalid domain # @example Check a not-assigned domain
# PublicSuffixService.valid?("x.yz") # PublicSuffixService.valid?("example.zip")
# # => false # # => false
# #
# @example Check a not-allowed domain
# PublicSuffixService.valid?("example.do")
# # => false
# PublicSuffixService.valid?("www.example.do")
# # => true
#
# @example Check an URL (which is not a valid domain) # @example Check an URL (which is not a valid domain)
# PublicSuffixService.valid?("http://www.google.com") # PublicSuffixService.valid?("http://www.example.com")
# # => false # # => false
# #
def self.valid?(domain) def self.valid?(domain)
!RuleList.default.find(domain).nil? rule = RuleList.default.find(domain)
!rule.nil? && rule.allow?(domain)
end end


end end
25 changes: 23 additions & 2 deletions lib/public_suffix_service/domain.rb
Expand Up @@ -303,17 +303,38 @@ def is_a_subdomain?
subdomain? subdomain?
end end


# Checks whether <tt>self</tt> is valid # Checks whether <tt>self</tt> is assigned and allowed
# according to default {RuleList}. # according to default {RuleList}.
# #
# This method triggers a new rule lookup in the default {RuleList}, # This method triggers a new rule lookup in the default {RuleList},
# which is a quite intensive task. # which is a quite intensive task.
# #
# @return [Boolean] # @return [Boolean]
#
# @example Check a valid domain
# Domain.new("com", "example").valid?
# # => true
#
# @example Check a valid subdomain
# Domain.new("com", "example", "www").valid?
# # => true
#
# @example Check a not-assigned domain
# Domain.new("zip", "example").valid?
# # => false
#
# @example Check a not-allowed domain
# Domain.new("do", "example").valid?
# # => false
# Domain.new("do", "example", "www").valid?
# # => true
#
def valid? def valid?
!rule.nil? r = rule
!r.nil? && r.allow?(name)
end end



# Checks whether <tt>self</tt> looks like a domain and validates # Checks whether <tt>self</tt> looks like a domain and validates
# according to default {RuleList}. # according to default {RuleList}.
# #
Expand Down
36 changes: 35 additions & 1 deletion lib/public_suffix_service/rule.rb
Expand Up @@ -167,18 +167,47 @@ def ==(other)
alias :eql? :== alias :eql? :==




# Checks whether this rule matches +domain+. # Checks if this rule matches +domain+.
# #
# @param [String, #to_s] domain # @param [String, #to_s] domain
# The domain name to check. # The domain name to check.
# #
# @return [Boolean] # @return [Boolean]
#
# @example
# rule = Rule.factory("com")
# # #<PublicSuffixService::Rule::Normal>
# rule.match?("example.com")
# # => true
# rule.match?("example.net")
# # => false
#
def match?(domain) def match?(domain)
l1 = labels l1 = labels
l2 = Domain.domain_to_labels(domain) l2 = Domain.domain_to_labels(domain)
odiff(l1, l2).empty? odiff(l1, l2).empty?
end end


# Checks if this rule allows +domain+.
#
# @param [String, #to_s] domain
# The domain name to check.
#
# @return [Boolean]
#
# @example
# rule = Rule.factory("*.do")
# # #<PublicSuffixService::Rule::Wildcard>
# rule.allow?("example.do")
# # => false
# rule.allow?("www.example.do")
# # => true
#
def allow?(domain)
!decompose(domain).last.nil?
end


# Gets the length of this rule for comparison. # Gets the length of this rule for comparison.
# The length usually matches the number of rule +parts+. # The length usually matches the number of rule +parts+.
# #
Expand All @@ -189,12 +218,17 @@ def length
parts.length parts.length
end end


#
# @raise [NotImplementedError] # @raise [NotImplementedError]
# @abstract # @abstract
def parts def parts
raise NotImplementedError raise NotImplementedError
end end


#
# @param [String, #to_s] domain
# The domain name to decompose.
#
# @return [Array<String, nil>] # @return [Array<String, nil>]
# #
# @raise [NotImplementedError] # @raise [NotImplementedError]
Expand Down
17 changes: 12 additions & 5 deletions test/public_suffix_service/domain_test.rb
Expand Up @@ -121,12 +121,19 @@ def test_is_a_subdomain_question
end end


def test_valid_question def test_valid_question
assert @klass.new("com").valid? assert !@klass.new("com").valid?
assert @klass.new("com", "google").valid? assert @klass.new("com", "example").valid?
assert @klass.new("com", "google", "www").valid? assert @klass.new("com", "example", "www").valid?

# not-assigned
assert !@klass.new("zip").valid? assert !@klass.new("zip").valid?
assert !@klass.new("zip", "google").valid? assert !@klass.new("zip", "example").valid?
assert !@klass.new("zip", "google", "www").valid? assert !@klass.new("zip", "example", "www").valid?

# not-allowed
assert !@klass.new("do").valid?
assert !@klass.new("do", "example").valid?
assert @klass.new("do", "example", "www").valid?
end end


def test_valid_domain_question def test_valid_domain_question
Expand Down
73 changes: 44 additions & 29 deletions test/public_suffix_service/rule_test.rb
Expand Up @@ -60,15 +60,15 @@ def test_equality_with_internals




def test_match def test_match
assert @klass.new("uk").match?("google.uk") assert @klass.new("uk").match?("example.uk")
assert !@klass.new("gk").match?("google.uk") assert !@klass.new("gk").match?("example.uk")
assert !@klass.new("google").match?("google.uk") assert !@klass.new("example").match?("example.uk")
assert @klass.new("uk").match?("google.co.uk") assert @klass.new("uk").match?("example.co.uk")
assert !@klass.new("gk").match?("google.co.uk") assert !@klass.new("gk").match?("example.co.uk")
assert !@klass.new("co").match?("google.co.uk") assert !@klass.new("co").match?("example.co.uk")
assert @klass.new("co.uk").match?("google.co.uk") assert @klass.new("co.uk").match?("example.co.uk")
assert !@klass.new("uk.co").match?("google.co.uk") assert !@klass.new("uk.co").match?("example.co.uk")
assert !@klass.new("go.uk").match?("google.co.uk") assert !@klass.new("go.uk").match?("example.co.uk")
end end


def test_length def test_length
Expand Down Expand Up @@ -104,17 +104,24 @@ def test_initialize




def test_match def test_match
assert @klass.new("uk").match?("google.uk") assert @klass.new("uk").match?("example.uk")
assert !@klass.new("gk").match?("google.uk") assert !@klass.new("gk").match?("example.uk")
assert !@klass.new("google").match?("google.uk") assert !@klass.new("example").match?("example.uk")
assert @klass.new("uk").match?("google.co.uk") assert @klass.new("uk").match?("example.co.uk")
assert !@klass.new("gk").match?("google.co.uk") assert !@klass.new("gk").match?("example.co.uk")
assert !@klass.new("co").match?("google.co.uk") assert !@klass.new("co").match?("example.co.uk")
assert @klass.new("co.uk").match?("google.co.uk") assert @klass.new("co.uk").match?("example.co.uk")
assert !@klass.new("uk.co").match?("google.co.uk") assert !@klass.new("uk.co").match?("example.co.uk")
assert !@klass.new("go.uk").match?("google.co.uk") assert !@klass.new("go.uk").match?("example.co.uk")
end end


def test_allow
assert !@klass.new("com").allow?("com")
assert @klass.new("com").allow?("example.com")
assert @klass.new("com").allow?("www.example.com")
end


def test_length def test_length
assert_equal 1, @klass.new("com").length assert_equal 1, @klass.new("com").length
assert_equal 2, @klass.new("co.com").length assert_equal 2, @klass.new("co.com").length
Expand All @@ -128,8 +135,8 @@ def test_parts
end end


def test_decompose def test_decompose
assert_equal %w(google com), @klass.new("com").decompose("google.com") assert_equal %w(example com), @klass.new("com").decompose("example.com")
assert_equal %w(foo.google com), @klass.new("com").decompose("foo.google.com") assert_equal %w(foo.example com), @klass.new("com").decompose("foo.example.com")
end end


end end
Expand All @@ -153,14 +160,15 @@ def test_initialize




def test_match def test_match
assert @klass.new("!uk").match?("google.co.uk") assert @klass.new("!uk").match?("example.co.uk")
assert !@klass.new("!gk").match?("google.co.uk") assert !@klass.new("!gk").match?("example.co.uk")
assert @klass.new("!co.uk").match?("google.co.uk") assert @klass.new("!co.uk").match?("example.co.uk")
assert !@klass.new("!go.uk").match?("google.co.uk") assert !@klass.new("!go.uk").match?("example.co.uk")
assert @klass.new("!british-library.uk").match?("british-library.uk") assert @klass.new("!british-library.uk").match?("british-library.uk")
assert !@klass.new("!british-library.uk").match?("google.co.uk") assert !@klass.new("!british-library.uk").match?("example.co.uk")
end end



def test_length def test_length
assert_equal 1, @klass.new("!british-library.uk").length assert_equal 1, @klass.new("!british-library.uk").length
assert_equal 2, @klass.new("!foo.british-library.uk").length assert_equal 2, @klass.new("!foo.british-library.uk").length
Expand Down Expand Up @@ -197,12 +205,19 @@ def test_initialize




def test_match def test_match
assert @klass.new("*.uk").match?("google.uk") assert @klass.new("*.uk").match?("example.uk")
assert @klass.new("*.uk").match?("google.co.uk") assert @klass.new("*.uk").match?("example.co.uk")
assert @klass.new("*.co.uk").match?("google.co.uk") assert @klass.new("*.co.uk").match?("example.co.uk")
assert !@klass.new("*.go.uk").match?("google.co.uk") assert !@klass.new("*.go.uk").match?("example.co.uk")
end end


def test_allow
assert !@klass.new("*.com").allow?("com")
assert !@klass.new("*.com").allow?("example.com")
assert @klass.new("*.com").allow?("www.example.com")
end


def test_length def test_length
assert_equal 2, @klass.new("*.uk").length assert_equal 2, @klass.new("*.uk").length
assert_equal 3, @klass.new("*.co.uk").length assert_equal 3, @klass.new("*.co.uk").length
Expand Down

0 comments on commit 5c93081

Please sign in to comment.