Skip to content

Commit

Permalink
detect and validate Pinterest [closes #1]
Browse files Browse the repository at this point in the history
  • Loading branch information
alaz committed May 18, 2018
1 parent 1c4d009 commit 3cedab6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/legitbot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
require_relative 'legitbot/duckduckgo'
require_relative 'legitbot/facebook'
require_relative 'legitbot/google'
require_relative 'legitbot/pinterest'
require_relative 'legitbot/yandex'
13 changes: 13 additions & 0 deletions lib/legitbot/pinterest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Legitbot
# https://help.pinterest.com/en/articles/about-pinterest-crawler-0

class Pinterest < BotMatch
ValidDomains = ["pinterest.com."]

def valid?
subdomain_of?(*Pinterest::ValidDomains) && reverse_resolves?
end
end

rule Legitbot::Pinterest, %w(Pinterestbot Pinterest)
end
37 changes: 37 additions & 0 deletions test/pinterest_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'minitest/autorun'
require 'legitbot'

class PinterestTest < Minitest::Test
def test_malicious_ip
ip = "149.210.164.47"
match = Legitbot::Pinterest.new ip
reverse_name = match.reverse_name
assert !match.subdomain_of?("pinterest.com."), msg: "#{reverse_name} is not a subdomain of pinterest.com"
assert !match.valid?, msg: "#{ip} is not a real Pinterest IP"
end

def test_valid_ip
ip = "54.236.1.11"
match = Legitbot::Pinterest.new ip
reverse_name = match.reverse_name
assert match.subdomain_of?("pinterest.com."), msg: "#{reverse_name} is a subdomain of pinterest.com"
assert match.valid?, msg: "#{ip} is a valid Pinterest IP"
end

def test_malicious_ua
bot = Legitbot.bot("Mozilla/5.0 (compatible; Pinterestbot/1.0; +https://www.pinterest.com/bot.html)", "149.210.164.47")
assert bot, msg: "Pinterest detected from User-Agent"
assert !bot.valid?, msg: "Not a valid Pinterest"
end

def test_valid_ua
bot = Legitbot.bot("Mozilla/5.0 (compatible; Pinterestbot/1.0; +https://www.pinterest.com/bot.html)", "54.236.1.11")
assert bot, msg: "Pinterest detected from User-Agent"
assert bot.valid?, msg: "Valid Pinterest"
end

def test_engine_name
bot = Legitbot.bot("Mozilla/5.0 (compatible; Pinterestbot/1.0; +https://www.pinterest.com/bot.html)", "54.236.1.11")
assert_equal "Pinterest", bot.detected_as
end
end

0 comments on commit 3cedab6

Please sign in to comment.