From d49286f9426830be7cdfbd6f98cd0b490da6c702 Mon Sep 17 00:00:00 2001 From: mallowlabs Date: Tue, 28 Jan 2014 21:31:43 +0900 Subject: [PATCH] Added: blacklist mechanism refs #6 --- app/controllers/quotes_controller.rb | 6 ++++-- app/models/blacklist.rb | 10 ++++++++++ spec/controllers/quotes_controller_spec.rb | 5 +++++ spec/models/blacklist_spec.rb | 20 ++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 app/models/blacklist.rb create mode 100644 spec/models/blacklist_spec.rb diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb index b722d07..9273632 100644 --- a/app/controllers/quotes_controller.rb +++ b/app/controllers/quotes_controller.rb @@ -41,8 +41,10 @@ def html def show @url = params[:u] - @thumbnail = ThumbnailRule.quote @url - @page = HtmlRule.quote @url + unless Blacklist.include?(@url) + @thumbnail = ThumbnailRule.quote @url + @page = HtmlRule.quote @url + end render status: 404, text: '404 Not found' unless @page end diff --git a/app/models/blacklist.rb b/app/models/blacklist.rb new file mode 100644 index 0000000..45143f5 --- /dev/null +++ b/app/models/blacklist.rb @@ -0,0 +1,10 @@ +class Blacklist + DEFAULT_LIST = ['localhost', '127.0.0.1'] + + def self.include?(url) + additional_list = (ENV['BLACKLIST'] || '').split + uri = URI.parse(url) + (DEFAULT_LIST + additional_list).include?(uri.host) + end +end + diff --git a/spec/controllers/quotes_controller_spec.rb b/spec/controllers/quotes_controller_spec.rb index 1b781df..cceba11 100644 --- a/spec/controllers/quotes_controller_spec.rb +++ b/spec/controllers/quotes_controller_spec.rb @@ -6,4 +6,9 @@ subject { response } its(:response_code) { should eq 404 } end + context 'show with blacklisted url' do + before { get :show, :u => 'http://localhost/' } + subject { response } + its(:response_code) { should eq 404 } + end end diff --git a/spec/models/blacklist_spec.rb b/spec/models/blacklist_spec.rb new file mode 100644 index 0000000..3d7a5ad --- /dev/null +++ b/spec/models/blacklist_spec.rb @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +require 'spec_helper' + +describe Blacklist do + describe "include?" do + context "matched" do + subject { Blacklist.include?('http://localhost/') } + it { should be_true } + end + context "unmatched" do + subject { Blacklist.include?('http://twitter.com/') } + it { should be_false } + end + context "use ENV" do + before { ENV['BLACKLIST'] = 'www.codefirst.org' } + subject { Blacklist.include?('http://www.codefirst.org/') } + it { should be_true } + end + end +end