merb-param-protection 1.1.0.pre → 1.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/lib/merb-param-protection.rb +122 -122
- data/lib/merb-param-protection/version.rb +2 -2
- metadata +33 -14
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ begin
|
|
14
14
|
|
15
15
|
Jeweler::Tasks.new do |gemspec|
|
16
16
|
|
17
|
-
gemspec.version = Merb::ParamProtection::VERSION
|
17
|
+
gemspec.version = Merb::ParamProtection::VERSION.dup
|
18
18
|
|
19
19
|
gemspec.name = "merb-param-protection"
|
20
20
|
gemspec.description = "Merb plugin that helps protecting sensible parameters"
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require "merb-core"
|
2
|
+
|
1
3
|
# This plugin exposes two new controller methods which allow us to simply and flexibly filter the parameters available within the controller.
|
2
4
|
|
3
5
|
# Setup:
|
@@ -22,158 +24,156 @@
|
|
22
24
|
|
23
25
|
# We also see that params_protected removes ONLY those parameters explicitly specified.
|
24
26
|
|
25
|
-
if defined?(Merb::Plugins)
|
26
|
-
|
27
|
-
# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
|
28
|
-
#Merb::Plugins.config[:merb_param_protection] = {
|
29
|
-
#:chickens => false
|
30
|
-
#}
|
31
|
-
|
32
|
-
#Merb::Plugins.add_rakefiles "merb_param_protection/merbtasks"
|
33
|
-
|
34
|
-
module Merb
|
35
|
-
module ParamsFilter
|
36
|
-
module ControllerMixin
|
37
|
-
def self.included(base)
|
38
|
-
base.send(:extend, ClassMethods)
|
39
|
-
base.send(:include, InstanceMethods)
|
40
|
-
base.send(:class_inheritable_accessor, :accessible_params_args)
|
41
|
-
base.send(:class_inheritable_accessor, :protected_params_args)
|
42
|
-
base.send(:class_inheritable_accessor, :log_params_args)
|
43
|
-
# Don't expose these as public methods - otherwise they'll become controller actions
|
44
|
-
base.send(:protected, :accessible_params_args, :protected_params_args, :log_params_args)
|
45
|
-
base.send(:protected, :accessible_params_args=, :protected_params_args=, :log_params_args=)
|
46
|
-
|
47
|
-
base.send(:before, :initialize_params_filter)
|
48
|
-
end
|
49
27
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
#
|
55
|
-
def params_accessible(args = {})
|
56
|
-
assign_filtered_params(:accessible_params_args, args)
|
57
|
-
end
|
28
|
+
# Merb gives you a Merb::Plugins.config hash...feel free to put your stuff in your piece of it
|
29
|
+
#Merb::Plugins.config[:merb_param_protection] = {
|
30
|
+
#:chickens => false
|
31
|
+
#}
|
58
32
|
|
59
|
-
|
60
|
-
#
|
61
|
-
# params_protected :post => [:status, :author_id]
|
62
|
-
#
|
63
|
-
def params_protected(args = {})
|
64
|
-
assign_filtered_params(:protected_params_args, args)
|
65
|
-
end
|
33
|
+
#Merb::Plugins.add_rakefiles "merb_param_protection/merbtasks"
|
66
34
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
35
|
+
module Merb
|
36
|
+
module ParamsFilter
|
37
|
+
module ControllerMixin
|
38
|
+
def self.included(base)
|
39
|
+
base.send(:extend, ClassMethods)
|
40
|
+
base.send(:include, InstanceMethods)
|
41
|
+
base.send(:class_inheritable_accessor, :accessible_params_args)
|
42
|
+
base.send(:class_inheritable_accessor, :protected_params_args)
|
43
|
+
base.send(:class_inheritable_accessor, :log_params_args)
|
44
|
+
# Don't expose these as public methods - otherwise they'll become controller actions
|
45
|
+
base.send(:protected, :accessible_params_args, :protected_params_args, :log_params_args)
|
46
|
+
base.send(:protected, :accessible_params_args=, :protected_params_args=, :log_params_args=)
|
76
47
|
|
77
|
-
|
48
|
+
base.send(:before, :initialize_params_filter)
|
49
|
+
end
|
78
50
|
|
79
|
-
|
80
|
-
|
51
|
+
module ClassMethods
|
52
|
+
# Ensures these parameters are sent for the object
|
53
|
+
#
|
54
|
+
# params_accessible :post => [:title, :body]
|
55
|
+
#
|
56
|
+
def params_accessible(args = {})
|
57
|
+
assign_filtered_params(:accessible_params_args, args)
|
58
|
+
end
|
81
59
|
|
82
|
-
|
83
|
-
|
84
|
-
|
60
|
+
# Protects parameters of an object
|
61
|
+
#
|
62
|
+
# params_protected :post => [:status, :author_id]
|
63
|
+
#
|
64
|
+
def params_protected(args = {})
|
65
|
+
assign_filtered_params(:protected_params_args, args)
|
66
|
+
end
|
67
|
+
|
68
|
+
# Filters parameters out from the default log string
|
69
|
+
# Params will still be passed to the controller properly, they will
|
70
|
+
# show up as [FILTERED] in the merb logs.
|
71
|
+
#
|
72
|
+
# log_params_filtered :password, 'token'
|
73
|
+
#
|
74
|
+
def log_params_filtered(*args)
|
75
|
+
self.log_params_args = args.collect { |arg| arg.to_sym }
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def assign_filtered_params(method, args)
|
81
|
+
validate_filtered_params(method, args)
|
82
|
+
|
83
|
+
# If the method is nil, set to initial hash, otherwise merge
|
84
|
+
self.send(method).nil? ? self.send(method.to_s + '=', args) : self.send(method).merge!(args)
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
87
|
+
def validate_filtered_params(method, args)
|
88
|
+
# Reversing methods
|
89
|
+
params_methods = [:accessible_params_args, :protected_params_args]
|
90
|
+
params_methods.delete(method)
|
91
|
+
params_method = params_methods.first
|
92
|
+
|
93
|
+
# Make sure the opposite method is not nil
|
94
|
+
unless self.send(params_method).nil?
|
95
|
+
# Loop through arg's keys
|
96
|
+
args.keys.each do |key|
|
97
|
+
# If the key exists on the opposite method, raise exception
|
98
|
+
if self.send(params_method).include?(key)
|
99
|
+
case method
|
100
|
+
when :accessible_params_args then raise "Cannot make accessible a controller (#{self}) that is already protected"
|
101
|
+
when :protected_params_args then raise "Cannot protect controller (#{self}) that is already accessible"
|
102
102
|
end
|
103
103
|
end
|
104
104
|
end
|
105
105
|
end
|
106
106
|
end
|
107
|
+
end
|
107
108
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
109
|
+
module InstanceMethods
|
110
|
+
def initialize_params_filter
|
111
|
+
if accessible_params_args.is_a?(Hash)
|
112
|
+
accessible_params_args.keys.each do |obj|
|
113
|
+
self.request.restrict_params(obj, accessible_params_args[obj])
|
114
114
|
end
|
115
|
+
end
|
115
116
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
end
|
117
|
+
if protected_params_args.is_a?(Hash)
|
118
|
+
protected_params_args.keys.each do |obj|
|
119
|
+
self.request.remove_params_from_object(obj, protected_params_args[obj])
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
123
|
-
|
124
123
|
end
|
125
124
|
|
126
|
-
|
127
|
-
attr_accessor :trashed_params
|
125
|
+
end
|
128
126
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
127
|
+
module RequestMixin
|
128
|
+
attr_accessor :trashed_params
|
129
|
+
|
130
|
+
# Removes specified parameters of an object
|
131
|
+
#
|
132
|
+
# remove_params_from_object(:post, [:status, :author_id])
|
133
|
+
#
|
134
|
+
def remove_params_from_object(obj, attrs = [])
|
135
|
+
unless params[obj].nil?
|
136
|
+
filtered = params
|
137
|
+
attrs.each {|a| filtered[obj].delete(a)}
|
138
|
+
@params = filtered
|
139
139
|
end
|
140
|
+
end
|
140
141
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
end
|
157
|
-
|
158
|
-
remove_params_from_object(obj, trashed_params_keys)
|
142
|
+
# Restricts parameters of an object
|
143
|
+
#
|
144
|
+
# restrict_params(:post, [:title, :body])
|
145
|
+
#
|
146
|
+
def restrict_params(obj, attrs = [])
|
147
|
+
# Make sure the params for the object exists
|
148
|
+
unless params[obj].nil?
|
149
|
+
attrs = attrs.collect {|a| a.to_s}
|
150
|
+
trashed_params_keys = params[obj].keys - attrs
|
151
|
+
|
152
|
+
# Store a hash of the key/value pairs we are going
|
153
|
+
# to remove in case we need them later. Lighthouse Bug # 105
|
154
|
+
@trashed_params = {}
|
155
|
+
trashed_params_keys.each do |key|
|
156
|
+
@trashed_params.merge!({key => params[obj][key]})
|
159
157
|
end
|
160
|
-
end
|
161
158
|
|
159
|
+
remove_params_from_object(obj, trashed_params_keys)
|
160
|
+
end
|
162
161
|
end
|
162
|
+
|
163
163
|
end
|
164
164
|
end
|
165
|
+
end
|
165
166
|
|
166
|
-
|
167
|
-
|
167
|
+
Merb::Controller.send(:include, Merb::ParamsFilter::ControllerMixin)
|
168
|
+
Merb::Request.send(:include, Merb::ParamsFilter::RequestMixin)
|
168
169
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
end
|
176
|
-
result
|
170
|
+
class Merb::Controller
|
171
|
+
def self._filter_params(params)
|
172
|
+
return params if self.log_params_args.nil?
|
173
|
+
result = { }
|
174
|
+
params.each do |k,v|
|
175
|
+
result[k] = (self.log_params_args.include?(k.to_sym) ? '[FILTERED]' : v)
|
177
176
|
end
|
177
|
+
result
|
178
178
|
end
|
179
179
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb-param-protection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- rc1
|
10
|
+
version: 1.1.0.rc1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Lance Carlson
|
@@ -9,29 +15,38 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date: 2010-
|
18
|
+
date: 2010-03-14 00:00:00 +00:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: merb-core
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
25
|
requirements:
|
21
26
|
- - ~>
|
22
27
|
- !ruby/object:Gem::Version
|
23
|
-
|
24
|
-
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
- rc1
|
33
|
+
version: 1.1.0.rc1
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
25
36
|
- !ruby/object:Gem::Dependency
|
26
37
|
name: rspec
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
40
|
requirements:
|
31
41
|
- - ">="
|
32
42
|
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 2
|
46
|
+
- 9
|
33
47
|
version: 1.2.9
|
34
|
-
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
35
50
|
description: Merb plugin that helps protecting sensible parameters
|
36
51
|
email: lancecarlson@gmail.com
|
37
52
|
executables: []
|
@@ -65,18 +80,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
80
|
requirements:
|
66
81
|
- - ">="
|
67
82
|
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
68
85
|
version: "0"
|
69
|
-
version:
|
70
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
87
|
requirements:
|
72
88
|
- - ">"
|
73
89
|
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 1
|
92
|
+
- 3
|
93
|
+
- 1
|
74
94
|
version: 1.3.1
|
75
|
-
version:
|
76
95
|
requirements: []
|
77
96
|
|
78
97
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.3.
|
98
|
+
rubygems_version: 1.3.6
|
80
99
|
signing_key:
|
81
100
|
specification_version: 3
|
82
101
|
summary: Merb plugin that provides params_accessible and params_protected class methods
|