Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Blacklist mechanism is needed. #6

Merged
merged 1 commit into from
Jan 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/controllers/quotes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions app/models/blacklist.rb
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions spec/controllers/quotes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions spec/models/blacklist_spec.rb
Original file line number Diff line number Diff line change
@@ -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