rack-exclusive-verbs 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f72c1f274f61ec40a559d42df8543e70f3d24154
4
+ data.tar.gz: 38d0d006ef0ea505790add8acf9b9805b4775d7a
5
+ SHA512:
6
+ metadata.gz: fc87b22c2ecf08a6a270b9622fd29cddb1b14d315f621047b8cb81fe00228f8f68a80c476c92eb49b34429ffc8256b1176dd3a97e67ca438ff3d4e9d8cd5a531
7
+ data.tar.gz: f32b4c689db4fd506509465cbd2e239191e9041cb323555024f4ac5695a3b7040de7bcd83f3c0eb727185ca5457655b0206822fb70e14c8f69cd21c1793d1e94
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ script: rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-exclusive-verbs.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Steve Jabour <steve@jabour.me>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Rack::ExclusiveVerbs [![Build Status](https://travis-ci.org/atsjj/rack-exclusive-verbs.svg?branch=master)](https://travis-ci.org/atsjj/rack-exclusive-verbs)
2
+
3
+ Rack middleware implementing an IP whitelist of HTTP verbs.
4
+
5
+ ## Usage:
6
+
7
+ ```ruby
8
+ require 'rack/exclusive_verbs'
9
+
10
+ use Rack::ExclusiveVerbs do
11
+ resolver { Socket.ip_address_list.select { |addr| addr.ipv4_private? }.collect(&:ip_address) } ## optional
12
+ allow only: '10.0.0.1', to: [:put, :post]
13
+ allow only: '10.0.0.1', to: :post
14
+ allow only: '10.0.0.1', to: :be_safe ## get, head, options, trace
15
+ allow only: '10.0.0.1', to: :be_unsafe ## delete, patch, post, put
16
+ allow range: '10.0.0.0/24', to: [:put, :post]
17
+ allow range: '10.0.0.0/24', to: :post
18
+ allow range: '10.0.0.0/24', to: :be_safe ## get, head, options, trace
19
+ allow range: '10.0.0.0/24', to: :be_unsafe ## delete, patch, post, put
20
+ end
21
+ ```
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'rack-exclusive-verbs'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install rack-exclusive-verbs
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it ( https://github.com/atsjj/rack-exclusive-verbs )
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/exclusive/verbs"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,100 @@
1
+ require 'ipaddr'
2
+ require 'set'
3
+
4
+ module Rack
5
+ # Rack middleware implementing an IP whitelist of HTTP verbs
6
+ #
7
+ # Usage:
8
+ # use Rack::ExclusiveVerbs do
9
+ # resolver { Socket.ip_address_list.select { |addr| addr.ipv4_private? }.collect(&:ip_address) }
10
+ # allow only: '10.0.0.1', to: [:put, :post]
11
+ # allow only: '10.0.0.1', to: :post
12
+ # allow only: '10.0.0.1', to: :be_safe ## get, head, options, trace
13
+ # allow only: '10.0.0.1', to: :be_unsafe ## delete, patch, post, put
14
+ # allow range: '10.0.0.0/24', to: [:put, :post]
15
+ # allow range: '10.0.0.0/24', to: :post
16
+ # allow range: '10.0.0.0/24', to: :be_safe ## get, head, options, trace
17
+ # allow range: '10.0.0.0/24', to: :be_unsafe ## delete, patch, post, put
18
+ # end
19
+ #
20
+ class ExclusiveVerbs
21
+ def initialize(app, &block)
22
+ @app = app
23
+ @rules = {}
24
+ @resolve = Proc.new { |request| [IPAddr.new(request.ip)] }
25
+ instance_eval(&block)
26
+ end
27
+
28
+ def call(env)
29
+ if is_allowed?(env)
30
+ @app.call(env)
31
+ else
32
+ [403, {"Content-Type" => "text/plain"}, ["403 Forbidden"]]
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ def is_allowed?(env)
39
+ request = Rack::Request.new(env)
40
+
41
+ verb = request.request_method.downcase.to_sym
42
+ ips = [@resolve.call(request)].flatten.map do |ip|
43
+ if ip.kind_of?(IPAddr)
44
+ ip
45
+ else
46
+ IPAddr.new(ip)
47
+ end
48
+ end
49
+
50
+ if @rules.has_key?(verb)
51
+ return @rules[verb].any? { |rule| ips.any? { |ip| rule.include?(ip) } }
52
+ else
53
+ return false
54
+ end
55
+ end
56
+
57
+ def allow(config)
58
+ options = {
59
+ only: nil,
60
+ range: nil,
61
+ to: [:head, :get, :options, :trace]
62
+ }.merge(config)
63
+
64
+ compound_verbs = {
65
+ be_safe: [:get, :head, :options, :trace],
66
+ be_unsafe: [:delete, :patch, :post, :put]
67
+ }
68
+
69
+ # Use options[:only] as a syntax sugar, prefer options[:range]
70
+ options[:range] ||= options[:only]
71
+
72
+ # Replace options[:to] values of :be_unsafe or :be_safe with real verbs
73
+ if compound_verbs.has_key?(options[:to])
74
+ options[:to] = compound_verbs[options[:to]]
75
+ end
76
+
77
+ # Wrap options[:to] in an array, if only a symbol was passed
78
+ unless options[:to].kind_of?(Array)
79
+ options[:to] = [options[:to]]
80
+ end
81
+
82
+ options[:to].each do |verb|
83
+ unless @rules.has_key?(verb.to_sym)
84
+ @rules[verb.to_sym] = Set.new
85
+ end
86
+
87
+ ip = IPAddr.new(options[:range])
88
+ @rules[verb.to_sym].add(ip)
89
+ end
90
+ end
91
+
92
+ def resolver(&block)
93
+ @resolve = block;
94
+ end
95
+
96
+ class << self
97
+ def VERSION; "1.0.0"; end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/exclusive_verbs'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-exclusive-verbs"
8
+ spec.version = Rack::ExclusiveVerbs.VERSION
9
+ spec.authors = ["Steve Jabour"]
10
+ spec.email = ["steve@jabour.me"]
11
+ spec.license = 'MIT'
12
+
13
+ spec.summary = %q{Rack middleware implementing an IP whitelist of HTTP verbs.}
14
+ spec.description = %q{Rack middleware implementing an IP whitelist of HTTP verbs.}
15
+ spec.homepage = "https://github.com/atsjj/rack-exclusive-verbs"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'rack', '>= 0.9.1'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-exclusive-verbs
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Steve Jabour
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: Rack middleware implementing an IP whitelist of HTTP verbs.
70
+ email:
71
+ - steve@jabour.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/rack/exclusive_verbs.rb
86
+ - rack-exclusive-verbs.gemspec
87
+ homepage: https://github.com/atsjj/rack-exclusive-verbs
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Rack middleware implementing an IP whitelist of HTTP verbs.
111
+ test_files: []