stop_it 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +4 -0
- data/README.md +7 -5
- data/Rakefile +2 -2
- data/lib/stop_it.rb +12 -12
- data/spec/stop_it_spec.rb +44 -65
- data/stop_it.gemspec +13 -10
- metadata +54 -17
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2beceff367a5d206d1daa97f3fcf7bc141d9476c
|
4
|
+
data.tar.gz: 8d5b8f2c8ca7bad5b286fe752b8764f78e089912
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7d52ebcf3bb58c69f0ab9fc2b88c2be84565c850df69e9ed3ad3570fff72f4796901488ea758a79f3a02a9fc72cce44153f0b4b625b250055c35cd0741022a18
|
7
|
+
data.tar.gz: 867189721681082da02d571fbdbe56f3206b5b0369a01ce88d0ab1244669e9ecd769178f40d00b85799d290d6f87a60a1ab4888ac21b8820acd98e2e88c3a560
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -36,22 +36,24 @@ so that the file contains code similar to this:
|
|
36
36
|
|
37
37
|
To configure which requests should be stopped add config/initializers/stop_it.rb file to your Ruby on Rails app with the following content:
|
38
38
|
|
39
|
-
StopIt.stop do |
|
39
|
+
StopIt.stop do |opts|
|
40
40
|
|
41
41
|
end
|
42
42
|
|
43
|
+
@Opts@ is a hash with the following keys: @path_info@, @remote_addr@, @query_string@, @request_method@, @http_user_agent@.
|
44
|
+
|
43
45
|
If the block in stop method returns true then the request will be blocked. If it returns false then the request will be passed to the next middleware. In the following example all requests to /forbidden will be blocked.
|
44
46
|
|
45
|
-
StopIt.stop do |
|
46
|
-
path_info == "/forbidden"
|
47
|
+
StopIt.stop do |opts|
|
48
|
+
opts[:path_info] == "/forbidden"
|
47
49
|
end
|
48
50
|
|
49
51
|
Requests can be blocked by request path, remote address, query string, HTTP method, and user agent.
|
50
52
|
|
51
53
|
The block in stop method may return a rake app response like this:
|
52
54
|
|
53
|
-
StopIt.stop do |
|
54
|
-
if remote_addr ==
|
55
|
+
StopIt.stop do |opts|
|
56
|
+
if opts[:remote_addr] == '127.0.0.2'
|
55
57
|
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
|
56
58
|
end
|
57
59
|
end
|
data/Rakefile
CHANGED
data/lib/stop_it.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
+
# Middleware to block unwanted requests to a Rake app
|
1
2
|
class StopIt
|
2
|
-
|
3
3
|
class << self
|
4
|
-
def stop
|
4
|
+
def stop(&block)
|
5
5
|
if block_given?
|
6
6
|
@stop = block
|
7
7
|
else
|
@@ -15,26 +15,26 @@ class StopIt
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def call(env)
|
18
|
-
|
18
|
+
should_be_stopped = request_should_be_stopped?(env)
|
19
19
|
|
20
|
-
if
|
20
|
+
if should_be_stopped == true
|
21
21
|
[200, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
|
22
|
-
elsif !
|
22
|
+
elsif !should_be_stopped
|
23
23
|
@app.call(env)
|
24
24
|
else
|
25
|
-
|
25
|
+
should_be_stopped
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
|
-
def
|
31
|
+
def request_should_be_stopped?(env)
|
32
32
|
StopIt.stop && StopIt.stop.call(
|
33
|
-
env[
|
34
|
-
env[
|
35
|
-
env[
|
36
|
-
env[
|
37
|
-
env[
|
33
|
+
path_info: env['PATH_INFO'],
|
34
|
+
remote_addr: env['REMOTE_ADDR'],
|
35
|
+
query_string: env['QUERY_STRING'],
|
36
|
+
request_method: env['REQUEST_METHOD'],
|
37
|
+
http_user_agent: env['HTTP_USER_AGENT']
|
38
38
|
)
|
39
39
|
end
|
40
40
|
end
|
data/spec/stop_it_spec.rb
CHANGED
@@ -1,99 +1,78 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe StopIt do
|
4
|
-
let!(:app) {
|
4
|
+
let!(:app) { double('App', call: nil) }
|
5
5
|
|
6
6
|
subject(:middleware) { StopIt.new(app) }
|
7
7
|
|
8
|
-
shared_examples_for
|
9
|
-
|
10
|
-
|
11
|
-
expect(app).not_to have_received(:call)
|
12
|
-
end
|
8
|
+
shared_examples_for 'blocker' do |env|
|
9
|
+
before { middleware.call(env) }
|
10
|
+
specify { expect(app).not_to have_received(:call) }
|
13
11
|
end
|
14
12
|
|
15
|
-
shared_examples_for
|
16
|
-
|
17
|
-
|
18
|
-
expect(app).to have_received(:call)
|
19
|
-
end
|
13
|
+
shared_examples_for 'non-blocker' do |env|
|
14
|
+
before { middleware.call(env) }
|
15
|
+
specify { expect(app).to have_received(:call) }
|
20
16
|
end
|
21
17
|
|
22
|
-
describe
|
23
|
-
context
|
24
|
-
before { StopIt.instance_variable_set(
|
25
|
-
|
18
|
+
describe 'middleware response' do
|
19
|
+
context 'stop block not specified' do
|
20
|
+
before { StopIt.instance_variable_set('@stop', nil) }
|
21
|
+
it_behaves_like 'non-blocker', {}
|
26
22
|
end
|
27
23
|
|
28
|
-
context
|
29
|
-
before { StopIt.stop {
|
30
|
-
|
24
|
+
context 'stop block returns false' do
|
25
|
+
before { StopIt.stop { false } }
|
26
|
+
it_behaves_like 'non-blocker', {}
|
31
27
|
end
|
32
28
|
|
33
|
-
context
|
34
|
-
before { StopIt.stop {
|
35
|
-
|
29
|
+
context 'stop block returns true' do
|
30
|
+
before { StopIt.stop { true } }
|
31
|
+
it_behaves_like 'blocker', {}
|
36
32
|
end
|
37
33
|
|
38
|
-
context
|
39
|
-
let(:response)
|
40
|
-
|
41
|
-
|
34
|
+
context 'stop block returns rake status' do
|
35
|
+
let(:response) do
|
36
|
+
[403, { 'Content-Type' => 'text/html', 'Content-Length' => '0' }, []]
|
37
|
+
end
|
38
|
+
|
39
|
+
before { StopIt.stop { response } }
|
40
|
+
specify { expect(middleware.call({})) == response }
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
describe
|
46
|
-
before
|
47
|
-
StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
|
48
|
-
path_info == "/forbidden"
|
49
|
-
end
|
50
|
-
end
|
44
|
+
describe 'filter requests by PATH_INFO env variable' do
|
45
|
+
before { StopIt.stop { |env| env[:path_info] == '/forbidden' } }
|
51
46
|
|
52
|
-
|
53
|
-
|
47
|
+
it_behaves_like 'blocker', 'PATH_INFO' => '/forbidden'
|
48
|
+
it_behaves_like 'non-blocker', 'PATH_INFO' => '/public'
|
54
49
|
end
|
55
50
|
|
56
|
-
describe
|
57
|
-
before
|
58
|
-
StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
|
59
|
-
remote_addr == "192.168.0.1"
|
60
|
-
end
|
61
|
-
end
|
51
|
+
describe 'filter requests by REMOTE_ADDR env variable' do
|
52
|
+
before { StopIt.stop { |env| env[:remote_addr] == '192.168.0.1' } }
|
62
53
|
|
63
|
-
|
64
|
-
|
54
|
+
it_behaves_like 'blocker', 'REMOTE_ADDR' => '192.168.0.1'
|
55
|
+
it_behaves_like 'non-blocker', 'REMOTE_ADDR' => '127.0.0.1'
|
65
56
|
end
|
66
57
|
|
67
|
-
describe
|
68
|
-
before
|
69
|
-
StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
|
70
|
-
query_string == "?block"
|
71
|
-
end
|
72
|
-
end
|
58
|
+
describe 'filter requests by QUERY_STRING env variable' do
|
59
|
+
before { StopIt.stop { |env| env[:query_string] == '?block' } }
|
73
60
|
|
74
|
-
|
75
|
-
|
61
|
+
it_behaves_like 'blocker', 'QUERY_STRING' => '?block'
|
62
|
+
it_behaves_like 'non-blocker', 'QUERY_STRING' => ''
|
76
63
|
end
|
77
64
|
|
78
|
-
describe
|
79
|
-
before
|
80
|
-
StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
|
81
|
-
request_method == "POST"
|
82
|
-
end
|
83
|
-
end
|
65
|
+
describe 'filter requests by REQUEST_METHOD env variable' do
|
66
|
+
before { StopIt.stop { |env| env[:request_method] == 'POST' } }
|
84
67
|
|
85
|
-
|
86
|
-
|
68
|
+
it_behaves_like 'blocker', 'REQUEST_METHOD' => 'POST'
|
69
|
+
it_behaves_like 'non-blocker', 'REQUEST_METHOD' => 'GET'
|
87
70
|
end
|
88
71
|
|
89
|
-
describe
|
90
|
-
before
|
91
|
-
StopIt.stop do |path_info, remote_addr, query_string, request_method, user_agent|
|
92
|
-
user_agent == "evil robot"
|
93
|
-
end
|
94
|
-
end
|
72
|
+
describe 'filter requests by HTTP_USER_AGENT env variable' do
|
73
|
+
before { StopIt.stop { |env| env[:http_user_agent] == 'evil robot' } }
|
95
74
|
|
96
|
-
|
97
|
-
|
75
|
+
it_behaves_like 'blocker', 'HTTP_USER_AGENT' => 'evil robot'
|
76
|
+
it_behaves_like 'non-blocker', 'HTTP_USER_AGENT' => 'IE'
|
98
77
|
end
|
99
78
|
end
|
data/stop_it.gemspec
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
|
-
gem.authors = [
|
3
|
-
gem.email = [
|
4
|
-
gem.description =
|
5
|
-
|
6
|
-
gem.
|
2
|
+
gem.authors = ['Andrei Gridnev']
|
3
|
+
gem.email = ['andrew.gridnev@gmail.com']
|
4
|
+
gem.description = 'Middleware for blocking requests to rake apps
|
5
|
+
based on user agent, remote IP, and other environment variables.'
|
6
|
+
gem.summary = 'Middleware for blocking requests to rake apps.'
|
7
|
+
gem.homepage = 'https://github.com/andrewgr/stop_it/'
|
7
8
|
gem.license = 'MIT'
|
8
9
|
|
9
|
-
gem.files = `git ls-files`.split(
|
10
|
+
gem.files = `git ls-files`.split($ORS)
|
10
11
|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
11
|
-
gem.test_files = gem.files.grep(%r{^
|
12
|
-
gem.name =
|
13
|
-
gem.require_paths = [
|
14
|
-
gem.version =
|
12
|
+
gem.test_files = gem.files.grep(%r{^spec/})
|
13
|
+
gem.name = 'stop_it'
|
14
|
+
gem.require_paths = ['lib']
|
15
|
+
gem.version = '2.0.0'
|
15
16
|
|
16
17
|
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'rubocop', '~> 0.30'
|
19
|
+
gem.add_development_dependency 'cane', '~> 2.6', '>= 2.6.1'
|
17
20
|
end
|
metadata
CHANGED
@@ -1,35 +1,74 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stop_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
-
|
7
|
+
- Andrei Gridnev
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-04-19 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.30'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.30'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cane
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.6'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.6.1
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.6'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.6.1
|
61
|
+
description: |-
|
62
|
+
Middleware for blocking requests to rake apps
|
63
|
+
based on user agent, remote IP, and other environment variables.
|
26
64
|
email:
|
27
65
|
- andrew.gridnev@gmail.com
|
28
66
|
executables: []
|
29
67
|
extensions: []
|
30
68
|
extra_rdoc_files: []
|
31
69
|
files:
|
32
|
-
- .gitignore
|
70
|
+
- ".gitignore"
|
71
|
+
- ".rspec"
|
33
72
|
- Gemfile
|
34
73
|
- LICENSE
|
35
74
|
- README.md
|
@@ -41,29 +80,27 @@ files:
|
|
41
80
|
homepage: https://github.com/andrewgr/stop_it/
|
42
81
|
licenses:
|
43
82
|
- MIT
|
83
|
+
metadata: {}
|
44
84
|
post_install_message:
|
45
85
|
rdoc_options: []
|
46
86
|
require_paths:
|
47
87
|
- lib
|
48
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
89
|
requirements:
|
51
|
-
- -
|
90
|
+
- - ">="
|
52
91
|
- !ruby/object:Gem::Version
|
53
92
|
version: '0'
|
54
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
94
|
requirements:
|
57
|
-
- -
|
95
|
+
- - ">="
|
58
96
|
- !ruby/object:Gem::Version
|
59
97
|
version: '0'
|
60
98
|
requirements: []
|
61
99
|
rubyforge_project:
|
62
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.2.2
|
63
101
|
signing_key:
|
64
|
-
specification_version:
|
102
|
+
specification_version: 4
|
65
103
|
summary: Middleware for blocking requests to rake apps.
|
66
104
|
test_files:
|
67
105
|
- spec/spec_helper.rb
|
68
106
|
- spec/stop_it_spec.rb
|
69
|
-
has_rdoc:
|