rack-taint 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ /.bundle
2
+ /Gemfile.lock
3
+ /bin
4
+ /pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Tim Pope
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.
@@ -0,0 +1,29 @@
1
+ # Rack::Taint
2
+
3
+ Rack::Taint is Rack middleware to taint the the query string (and thus
4
+ GET parameters), input (and thus POST parameters), headers (and thus
5
+ cookies), and everything else that comes in on a request. Among other
6
+ use cases, this may prove helpful as a component in a [scheme that
7
+ limits mass assignment in Rails][mass assignment scheme].
8
+
9
+ [mass assignment scheme]: http://jkfill.com/2012/03/10/preventing-mass-assignment-injection-in-rails/
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'rack-taint'
16
+
17
+ On Rails, the Railtie takes care of everything else. On everything
18
+ else, you'll need to add the `Rack::Taint` middleware to the stack
19
+ yourself.
20
+
21
+ ## Contributing
22
+
23
+ Please follow [Git commit message best practices][practices] when
24
+ submitting a pull request.
25
+
26
+ [practices]: http://stopwritingramblingcommitmessages.com/
27
+
28
+ If I provide you with feedback on your pull request, generally you should
29
+ squash your changes into the previous commit when submitting a second request.
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'test/**/*_test.rb'
7
+ end
8
+ task :default => :test
@@ -0,0 +1,5 @@
1
+ require "rack/taint"
2
+
3
+ if defined?(Rails::Railtie)
4
+ require 'rack/taint/railtie'
5
+ end
@@ -0,0 +1,29 @@
1
+ module Rack
2
+ class Taint
3
+
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ dup._call(env)
10
+ end
11
+
12
+ def _call(env)
13
+ env.each do |k, v|
14
+ v.taint unless k.include?('.')
15
+ end
16
+ input = env['rack.input'].taint
17
+ if input.respond_to?(:string)
18
+ require 'rack/taint/readable'
19
+ input.extend(Readable).string.taint
20
+ end
21
+ # Some middleware (e.g., Rack::MethodOverride) may cause parameter
22
+ # parsing before we taint.
23
+ env.delete('rack.request.form_input')
24
+ env.delete('rack.request.query_string')
25
+ @app.call(env)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ class Taint
3
+ class Railtie < Rails::Railtie
4
+ initializer 'rack.taint.configure_rails_initialization' do |app|
5
+ app.middleware.insert_before(Rack::MethodOverride, Rack::Taint)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module Rack
2
+ class Taint
3
+ module Readable
4
+ %w(getc gets read readpartial read_nonblock readline readlines sysread).each do |method|
5
+ class_eval <<-ruby, __FILE__, __LINE__.succ
6
+ def #{method}(*args)
7
+ if tainted?
8
+ super.taint
9
+ else
10
+ super
11
+ end
12
+ end
13
+ ruby
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Tim Pope"]
5
+ gem.email = ["code@tpope.net"]
6
+ gem.summary = %q{Rack middleware to taint headers, parameters, and input}
7
+ gem.homepage = "https://github.com/tpope/rack-taint"
8
+
9
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
+ gem.name = "rack-taint"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = '1.0.0'
15
+ gem.add_development_dependency 'minitest'
16
+ gem.add_development_dependency 'rack'
17
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../test_helper.rb', __FILE__)
2
+
3
+ class RackTaintTest < MiniTest::Unit::TestCase
4
+
5
+ def test_skip_dotted
6
+ env = app.call(
7
+ 'QUERY_STRING' => 'a=1&b=2',
8
+ 'rack.url_scheme' => 'http'
9
+ )
10
+ assert_tainted env['QUERY_STRING']
11
+ refute_tainted env['rack.url_scheme']
12
+ end
13
+
14
+ def test_input
15
+ env = app.call('rack.input' => StringIO.new)
16
+ assert_tainted env['rack.input'].read
17
+ end
18
+
19
+ def test_integration
20
+ require 'rack/request'
21
+ request = Rack::Request.new(app.call(
22
+ 'QUERY_STRING' => 'get=1',
23
+ 'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
24
+ 'rack.input' => StringIO.new('post[nested][]=2')
25
+ ))
26
+ assert_tainted request.GET['get']
27
+ assert_tainted request.POST['post']['nested'].first
28
+ end
29
+
30
+ end
@@ -0,0 +1,22 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'minitest/autorun'
5
+ require 'stringio'
6
+ require 'rack/taint'
7
+
8
+ class MiniTest::Unit::TestCase
9
+ def app
10
+ Rack::Taint.new(lambda { |env| env })
11
+ end
12
+
13
+ def assert_tainted(obj, msg = nil)
14
+ msg = message(msg) { "Expected #{mu_pp(obj)} to be tainted" }
15
+ assert obj.tainted?, msg
16
+ end
17
+
18
+ def refute_tainted(obj, msg = nil)
19
+ msg = message(msg) { "Expected #{mu_pp(obj)} to not be tainted" }
20
+ refute obj.tainted?, msg
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-taint
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tim Pope
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: &84535670 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *84535670
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack
27
+ requirement: &84535450 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *84535450
36
+ description:
37
+ email:
38
+ - code@tpope.net
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.markdown
47
+ - Rakefile
48
+ - lib/rack-taint.rb
49
+ - lib/rack/taint.rb
50
+ - lib/rack/taint/railtie.rb
51
+ - lib/rack/taint/readable.rb
52
+ - rack-taint.gemspec
53
+ - test/rack_taint_test.rb
54
+ - test/test_helper.rb
55
+ homepage: https://github.com/tpope/rack-taint
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.11
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Rack middleware to taint headers, parameters, and input
79
+ test_files: []