parameter_cleaner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Based on https://github.com/madebymany/parameter_cleaner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ ParameterCleaner
2
+ ================
3
+
4
+ Strips angle brackets from user input on the way into the application,
5
+ providing an extra level of security against XSS attacks even when
6
+ someone forgets an `h()` in a template.
7
+
8
+ __This is not a replacement for proper escaping!__
9
+
10
+ Exclusions
11
+ ----------
12
+
13
+ Password fields (anything matching `/password/`) are not stripped. For one
14
+ thing, users should be allowed to make strong passwords; for another, you’re
15
+ never going to display them in the application. Right?
16
+
17
+ For fields where you want to allow angle brackets, you can disable it on a
18
+ parameter-by-parameter basis:
19
+
20
+ class SomeController < ApplicationController
21
+ do_not_clean_param [:thing, :html_description]
22
+ end
23
+
24
+ The array corresponds to the hash keys used to get to the parameter; there is
25
+ no distinction between string parameters and array parameters.
26
+
27
+ Form parameter | do_not_clean_param
28
+ ----------------+--------------------
29
+ foo | [:foo] or :foo
30
+ foo[bar] | [:foo, :bar]
31
+ foo[bar][] | [:foo, :bar]
32
+
33
+ You can specify multiple parameters in one line:
34
+
35
+ do_not_clean_param :foo, :bar, [:nested, :baz]
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the parameter_cleaner plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the parameter_cleaner plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ParameterCleaner'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "parameter_cleaner"
@@ -0,0 +1,44 @@
1
+ require "action_controller"
2
+
3
+ class ActionController::Base
4
+ before_filter :pc_remove_angle_brackets_from_params
5
+
6
+ class <<self
7
+ def do_not_clean_param(*names)
8
+ names.each do |name|
9
+ pc_uncleaned_params.push([*name].map{ |s| s.to_s })
10
+ end
11
+ end
12
+
13
+ def pc_uncleaned_params
14
+ @pc_uncleaned_params ||= []
15
+ end
16
+ end
17
+
18
+ private
19
+ def pc_remove_angle_brackets_from_params
20
+ pc_remove_angle_brackets_from_hash(params)
21
+ pc_remove_angle_brackets_from_hash(cookies)
22
+ end
23
+
24
+ def pc_remove_angle_brackets_from_hash(hash, hierarchy=[])
25
+ hash.each do |key, value|
26
+ h = hierarchy + [key]
27
+ case value
28
+ when Hash, HashWithIndifferentAccess
29
+ pc_remove_angle_brackets_from_hash(value, h)
30
+ when Array
31
+ value.map!{ |v| pc_remove_angle_brackets_from_value(v, h) }
32
+ else
33
+ hash[key] = pc_remove_angle_brackets_from_value(value, h) if value.respond_to?('include?'.to_sym)&&['<', '>'].any?{|c| value.include?(c)}
34
+ end
35
+ end
36
+ end
37
+
38
+ def pc_remove_angle_brackets_from_value(value, hierarchy)
39
+ return value if hierarchy.any?{ |k| k =~ /password/ } ||
40
+ self.class.pc_uncleaned_params.include?(hierarchy) ||
41
+ !value.respond_to?(:gsub)
42
+ value.gsub(/[<>]/, "")
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{parameter_cleaner}
3
+ s.version = "0.0.1"
4
+
5
+ s.authors = ["Wayne Deng"]
6
+ s.date = %q{2013-11-12}
7
+ s.platform = Gem::Platform::RUBY
8
+ s.summary = "Clean all the angle brackets from user input params and cookies!"
9
+ s.description = "Clean all the angle brackets from user input params and cookies! Based on https://github.com/madebymany/parameter_cleaner. Thanks to threedaymonk!"
10
+ s.email = %q{wayne.deng.cn@gmail.com}
11
+ s.files = ["init.rb", "MIT-LICENSE", "Rakefile", "README.md", "test/parameter_cleaner_test.rb", "test/test_helper.rb", "lib/parameter_cleaner.rb", "parameter_cleaner.gemspec"]
12
+ s.require_paths = ["lib"]
13
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "ParameterCleaner", "--main", "README"]
14
+
15
+ end
@@ -0,0 +1,74 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+ require "parameter_cleaner"
3
+
4
+ class ParameterCleaningTest < ActionController::TestCase
5
+ class TestController < ActionController::Base
6
+ do_not_clean_param :uncleaned
7
+ do_not_clean_param [:nested, :uncleaned]
8
+ layout nil
9
+
10
+ def index
11
+ render :nothing => true
12
+ end
13
+ end
14
+
15
+ def params
16
+ @request.params
17
+ end
18
+
19
+ tests TestController
20
+
21
+ setup do
22
+ ActionController::Routing::Routes.draw do |map|
23
+ map.test_action "/test-action", :controller => "parameter_cleaning_test/test", :action => "index"
24
+ end
25
+ end
26
+
27
+ should "remove XSS attack vectors" do
28
+ get :index, :field => "blah '';!--\"<XSS>=&{()} blah"
29
+ assert_equal "blah '';!--\"XSS=&{()} blah", params[:field]
30
+ end
31
+
32
+
33
+ should "remove <> from fields" do
34
+ get :index, :field => "blah <foo> blah"
35
+ assert_equal "blah foo blah", params[:field]
36
+ end
37
+
38
+ should "remove <> from nested fields" do
39
+ get :index, :nested => { :field => "blah <bar> blah" }
40
+ assert_equal "blah bar blah", params[:nested][:field]
41
+ end
42
+
43
+ should "remove <> from array fields" do
44
+ get :index, :array => ["blah <> blah"]
45
+ assert_equal ["blah blah"], params[:array]
46
+ end
47
+
48
+ should "not remove <> from password fields" do
49
+ get :index, :nested => {:password => "<><>", :password_confirmation => "<><>"}
50
+ assert_equal "<><>", params[:nested][:password]
51
+ assert_equal "<><>", params[:nested][:password_confirmation]
52
+ end
53
+
54
+ should "not remove <> from whitelisted field" do
55
+ get :index, :uncleaned => "<><>"
56
+ assert_equal "<><>", params[:uncleaned]
57
+ end
58
+
59
+ should "not remove <> from whitelisted nested field" do
60
+ get :index, :nested => {:uncleaned => "<><>"}
61
+ assert_equal "<><>", params[:nested][:uncleaned]
62
+ end
63
+
64
+ should "not remove <> from whitelisted array field" do
65
+ get :index, :uncleaned => ["<><>"]
66
+ assert_equal ["<><>"], params[:uncleaned]
67
+ end
68
+
69
+ should "not try to clean uploaded files" do
70
+ io = StringIO.new("<><>")
71
+ get :index, :upload => io
72
+ assert_equal "<><>", params[:upload].read
73
+ end
74
+ end
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.expand_path("../../lib", __FILE__))
2
+ require 'rubygems'
3
+ require "active_support"
4
+ require "active_support/test_case"
5
+ require "shoulda"
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parameter_cleaner
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Wayne Deng
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-11-12 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Clean all the angle brackets from user input params and cookies! Based on https://github.com/madebymany/parameter_cleaner. Thanks to threedaymonk!
23
+ email: wayne.deng.cn@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - init.rb
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README.md
35
+ - test/parameter_cleaner_test.rb
36
+ - test/test_helper.rb
37
+ - lib/parameter_cleaner.rb
38
+ - parameter_cleaner.gemspec
39
+ has_rdoc: true
40
+ homepage:
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --line-numbers
46
+ - --inline-source
47
+ - --title
48
+ - ParameterCleaner
49
+ - --main
50
+ - README
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 3
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.5.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Clean all the angle brackets from user input params and cookies!
78
+ test_files: []
79
+