rack-check_http_method_allowed 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +9 -0
- data/lib/rack/check_http_method_allowed.rb +40 -0
- data/lib/rack/check_http_method_allowed/version.rb +5 -0
- data/rack-check_http_method_allowed.gemspec +24 -0
- data/test/rack/check_http_method_allowed_test.rb +43 -0
- data/test/test_helper.rb +7 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1fe90a9daecb9047daf1f0158c4f5608a0b59ee0
|
4
|
+
data.tar.gz: 7eddd0f8df4a84c1dffc87cbbd287fa0bae333e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c595a1c47ddaf887e378e1efedd7467c4095f40ee1d77c152af1be6f01d6d67eefc5106077dbebbb8da501b181a694a428c907006d058a2b48d98e49f820d90
|
7
|
+
data.tar.gz: f486361a44719baa3256d67b414280e4bd5d9be14a5448da1ab2a07a9e8626341a96d83f5d8f298b4971bfc668912944316dbb6359a1cb6c431409cf2d4e9ce1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 David Jones
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# Rack::CheckHttpMethodAllowed
|
2
|
+
|
3
|
+
Rack middleware to check HTTP request methods and reject ones you don't want.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rack-check_http_method_allowed'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rack-check_http_method_allowed
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In `config/application.rb` inside the main configuration block, add the line:
|
22
|
+
|
23
|
+
config.middleware.use 'Rack::CheckHttpMethodAllowed'
|
24
|
+
|
25
|
+
By default, this middleware only allows through methods conforming to [RFC2616](http://www.ietf.org/rfc/rfc2616.txt) and [RFC5789](http://www.ietf.org/rfc/rfc5789.txt), which should cover most applications, since the rest are mainly for [WebDAV](http://en.wikipedia.org/wiki/WebDAV) support.
|
26
|
+
|
27
|
+
If you need to support a different set of HTTP methods, you can pass them in like so:
|
28
|
+
|
29
|
+
config.middleware.use 'Rack::CheckHttpMethodAllowed', ['GET', 'POST', 'SOMETHINGELSE']
|
30
|
+
|
31
|
+
and any methods not listed will be rejected.
|
32
|
+
|
33
|
+
Rails maintains a list of HTTP verbs that it can handle in [ActionController::Request::HTTP_METHODS](https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/http/request.rb#L76), (including WebDAV methods), you can use this instead like so:
|
34
|
+
|
35
|
+
config.middleware.use 'Rack::CheckHttpMethodAllowed', ActionController::Request::HTTP_METHODS
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it ( http://github.com/getfretless/rack-check_http_method_allowed/fork )
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rack/check_http_method_allowed/version'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class CheckHttpMethodAllowed
|
5
|
+
|
6
|
+
RFC2616 = %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT)
|
7
|
+
RFC5789 = %w(PATCH)
|
8
|
+
DEFAULT = RFC2616 + RFC5789
|
9
|
+
|
10
|
+
def initialize(app, allowed_methods=nil)
|
11
|
+
@app = app
|
12
|
+
@allowed_methods = allowed_methods || DEFAULT
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
if method_not_allowed?(env['REQUEST_METHOD'])
|
17
|
+
reject_request(env)
|
18
|
+
else
|
19
|
+
@app.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def method_not_allowed?(request_method)
|
26
|
+
!@allowed_methods.include?(request_method.upcase)
|
27
|
+
end
|
28
|
+
|
29
|
+
def log_rejection(env)
|
30
|
+
message = "Rack::CheckHttpMethodAllowed Method Not Allowed: #{env.inspect}"
|
31
|
+
Rails.logger.info(message) if defined?(Rails)
|
32
|
+
end
|
33
|
+
|
34
|
+
def reject_request(env)
|
35
|
+
log_rejection(env)
|
36
|
+
[405, {'Content-Type' => 'text/plain'}, ['Method Not Allowed']]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rack/check_http_method_allowed/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rack-check_http_method_allowed"
|
8
|
+
spec.version = Rack::CheckHttpMethodAllowed::VERSION
|
9
|
+
spec.authors = ["David Jones"]
|
10
|
+
spec.email = ["david@getfretless.com"]
|
11
|
+
spec.summary = %q{Rack middleware to check HTTP request methods and reject ones you don't want}
|
12
|
+
spec.homepage = "https://github.com/getfretless/rack-check_http_method_allowed"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rack"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper.rb')
|
2
|
+
|
3
|
+
class CheckHttpMethodAllowedTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
App = lambda { |env| [200, {'Content-Type' => 'text/plain'}, Rack::Request.new(env)] }
|
6
|
+
|
7
|
+
def test_allowed_request
|
8
|
+
env = request_env('GET')
|
9
|
+
middleware = Rack::CheckHttpMethodAllowed.new(App, ['GET'])
|
10
|
+
status, headers, body = middleware.call(env)
|
11
|
+
assert_equal 200, status
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_allowed_request_without_method_list
|
15
|
+
env = request_env('GET')
|
16
|
+
middleware = Rack::CheckHttpMethodAllowed.new(App)
|
17
|
+
status, headers, body = middleware.call(env)
|
18
|
+
assert_equal 200, status
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_not_allowed_request
|
22
|
+
env = request_env('GET')
|
23
|
+
middleware = Rack::CheckHttpMethodAllowed.new(App, ['POST','PUT','PATCH','DELETE'])
|
24
|
+
status, headers, body = middleware.call(env)
|
25
|
+
assert_equal 405, status
|
26
|
+
assert_equal 'Method Not Allowed', body.join
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_not_allowed_request_without_method_list
|
30
|
+
env = request_env('PROPFIND')
|
31
|
+
middleware = Rack::CheckHttpMethodAllowed.new(App)
|
32
|
+
status, headers, body = middleware.call(env)
|
33
|
+
assert_equal 405, status
|
34
|
+
assert_equal 'Method Not Allowed', body.join
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def request_env(method)
|
40
|
+
Rack::MockRequest.env_for('/', {:method => method})
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-check_http_method_allowed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Jones
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-23 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'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- david@getfretless.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/rack/check_http_method_allowed.rb
|
68
|
+
- lib/rack/check_http_method_allowed/version.rb
|
69
|
+
- rack-check_http_method_allowed.gemspec
|
70
|
+
- test/rack/check_http_method_allowed_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
homepage: https://github.com/getfretless/rack-check_http_method_allowed
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Rack middleware to check HTTP request methods and reject ones you don't want
|
96
|
+
test_files:
|
97
|
+
- test/rack/check_http_method_allowed_test.rb
|
98
|
+
- test/test_helper.rb
|