params_cleaner 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/params_cleaner.rb +25 -41
- data/lib/params_cleaner/whitelist.rb +59 -0
- metadata +7 -6
data/lib/params_cleaner.rb
CHANGED
@@ -1,70 +1,54 @@
|
|
1
|
+
require "ostruct"
|
1
2
|
require "active_support/concern"
|
3
|
+
require "active_support/core_ext/hash/deep_merge"
|
2
4
|
require "active_support/core_ext/hash/slice"
|
3
5
|
require "active_support/hash_with_indifferent_access"
|
6
|
+
require "./lib/params_cleaner/whitelist"
|
4
7
|
|
5
8
|
module ParamsCleaner
|
6
9
|
extend ActiveSupport::Concern
|
7
10
|
|
8
|
-
VERSION = "0.
|
11
|
+
VERSION = "0.4.0"
|
9
12
|
|
10
|
-
def clean_params
|
11
|
-
|
12
|
-
|
13
|
-
_clean_hash(key, value)
|
14
|
-
elsif value.kind_of?(Array)
|
15
|
-
_clean_array(key, value)
|
16
|
-
else
|
17
|
-
_clean_value(key, value, top_level)
|
18
|
-
end
|
13
|
+
def clean_params
|
14
|
+
sanitized_params = _applicable_whitelists.map do |whitelist|
|
15
|
+
whitelist.sanitize(params)
|
19
16
|
end
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
def _clean_array(key, value)
|
26
|
-
cleaned_values = value.map do |sub_value|
|
27
|
-
_clean_hash(key, sub_value).last
|
18
|
+
sanitized_params.inject(HashWithIndifferentAccess.new) do |new_params, sanitized|
|
19
|
+
new_params.deep_merge(sanitized)
|
28
20
|
end
|
29
|
-
[key, cleaned_values]
|
30
21
|
end
|
31
22
|
|
32
|
-
def
|
33
|
-
|
34
|
-
clean_values = clean_params(allowed_keys, false)
|
35
|
-
[key, clean_values]
|
23
|
+
def _action_whitelists
|
24
|
+
self.class._action_whitelists
|
36
25
|
end
|
37
26
|
|
38
|
-
def
|
39
|
-
|
27
|
+
def _applicable_whitelists
|
28
|
+
[_action_whitelists[:_all_], _action_whitelists[_current_action_name]].compact
|
29
|
+
end
|
40
30
|
|
41
|
-
|
42
|
-
|
31
|
+
def _current_action_name
|
32
|
+
if respond_to?(:action_name)
|
33
|
+
action_name.to_sym
|
43
34
|
else
|
44
|
-
|
35
|
+
nil
|
45
36
|
end
|
46
37
|
end
|
47
38
|
|
48
39
|
module ClassMethods
|
49
40
|
def allowed_params(*params_groups)
|
50
|
-
@
|
51
|
-
@
|
52
|
-
|
53
|
-
params_groups.each do |params_group|
|
54
|
-
if params_group.is_a?(Hash)
|
55
|
-
@allowed_nested = params_group
|
56
|
-
else
|
57
|
-
@allowed_top_level << params_group
|
58
|
-
end
|
59
|
-
end
|
41
|
+
@action_whitelists ||= {}
|
42
|
+
@action_whitelists[:_all_] = Whitelist.new(params_groups)
|
60
43
|
end
|
61
44
|
|
62
|
-
def
|
63
|
-
@
|
45
|
+
def allowed_params_for(action, *params_groups)
|
46
|
+
@action_whitelists ||= {}
|
47
|
+
@action_whitelists[action] = Whitelist.new(params_groups)
|
64
48
|
end
|
65
49
|
|
66
|
-
def
|
67
|
-
@
|
50
|
+
def _action_whitelists
|
51
|
+
@action_whitelists
|
68
52
|
end
|
69
53
|
end
|
70
54
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module ParamsCleaner
|
2
|
+
class Whitelist
|
3
|
+
def initialize(whitelist)
|
4
|
+
@whitelist = whitelist
|
5
|
+
end
|
6
|
+
|
7
|
+
def sanitize(params, top_level = true)
|
8
|
+
cleaned_params = params.map do |key, value|
|
9
|
+
if value.kind_of?(Hash)
|
10
|
+
_clean_hash(key, value)
|
11
|
+
elsif value.kind_of?(Array)
|
12
|
+
_clean_array(key, value)
|
13
|
+
else
|
14
|
+
_clean_value(key, value, top_level)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
cleaned_params_hash = Hash[cleaned_params]
|
19
|
+
HashWithIndifferentAccess.new(cleaned_params_hash)
|
20
|
+
end
|
21
|
+
|
22
|
+
def _allowed_nested
|
23
|
+
@whitelist.detect { |params_group| params_group.is_a?(Hash) } || {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def _allowed_top_level
|
27
|
+
params_groups = []
|
28
|
+
@whitelist.each do |params_group|
|
29
|
+
unless params_group.is_a?(Hash)
|
30
|
+
params_groups << params_group
|
31
|
+
end
|
32
|
+
end
|
33
|
+
params_groups
|
34
|
+
end
|
35
|
+
|
36
|
+
def _clean_array(key, value)
|
37
|
+
cleaned_values = value.map do |sub_value|
|
38
|
+
_clean_hash(key, sub_value).last
|
39
|
+
end
|
40
|
+
[key, cleaned_values]
|
41
|
+
end
|
42
|
+
|
43
|
+
def _clean_hash(key, value)
|
44
|
+
allowed_keys = value.slice(*_allowed_nested[key.to_sym])
|
45
|
+
clean_values = sanitize(allowed_keys, false)
|
46
|
+
[key, clean_values]
|
47
|
+
end
|
48
|
+
|
49
|
+
def _clean_value(key, value, top_level)
|
50
|
+
return [key, value] unless top_level
|
51
|
+
|
52
|
+
if _allowed_top_level.include?(key.to_sym)
|
53
|
+
[key, value]
|
54
|
+
else
|
55
|
+
[]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: params_cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70327972553720 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70327972553720
|
25
25
|
description: Rails mass assignment protection in the controller
|
26
26
|
email:
|
27
27
|
- drew@drewolson.org
|
@@ -29,6 +29,7 @@ executables: []
|
|
29
29
|
extensions: []
|
30
30
|
extra_rdoc_files: []
|
31
31
|
files:
|
32
|
+
- lib/params_cleaner/whitelist.rb
|
32
33
|
- lib/params_cleaner.rb
|
33
34
|
homepage: https://github.com/drewolson/params_cleaner
|
34
35
|
licenses: []
|
@@ -44,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
44
45
|
version: '0'
|
45
46
|
segments:
|
46
47
|
- 0
|
47
|
-
hash: -
|
48
|
+
hash: -3481307884209043138
|
48
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
50
|
none: false
|
50
51
|
requirements:
|
@@ -53,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
version: '0'
|
54
55
|
segments:
|
55
56
|
- 0
|
56
|
-
hash: -
|
57
|
+
hash: -3481307884209043138
|
57
58
|
requirements: []
|
58
59
|
rubyforge_project:
|
59
60
|
rubygems_version: 1.8.15
|