Changeset 1251
- Timestamp:
- 01/09/08 20:07:10 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/merb_param_protection/README
r1227 r1251 2 2 ================= 3 3 4 A plugin for the Merb framework that provides .... 4 ** Not fully functional ** 5 6 This plugin exposes two new controller methods which allow us to simply and flexibly filter the parameters available within the controller. 7 8 Setup: 9 The request sets: 10 11 params => { :post => { :title => "ello", :body => "Want it", :status => "green", :author_id => 3, :rank => 4 } } 12 13 Example 1: params_accessable 14 MyController < Application 15 params_accessible :post => [:title, :body] 16 end 17 18 params.inspect # => { :post => { :title => "ello", :body => "Want it" } } 19 20 So we see that params_accessible removes everything except what is explictly specified. 21 22 Example 2: params_protected 23 MyOtherController < Application 24 params_protected :post => [:status, :author_id] 25 end 26 27 params.inspect # => { :post => { :title => "ello", :body => "Want it", :rank => 4 } } 28 29 We also see that params_protected removes ONLY those parameters explicitly specified. plugins/merb_param_protection/lib/merb_param_protection.rb
r1227 r1251 1 # make sure we're running inside Merb 1 # This plugin exposes two new controller methods which allow us to simply and flexibly filter the parameters available within the controller. 2 3 # Setup: 4 # The request sets: 5 # params => { :post => { :title => "ello", :body => "Want it", :status => "green", :author_id => 3, :rank => 4 } } 6 # 7 # Example 1: params_accessable 8 # MyController < Application 9 # params_accessible :post => [:title, :body] 10 # end 11 12 # params.inspect # => { :post => { :title => "ello", :body => "Want it" } } 13 14 # So we see that params_accessible removes everything except what is explictly specified. 15 16 # Example 2: params_protected 17 # MyOtherController < Application 18 # params_protected :post => [:status, :author_id] 19 # end 20 21 # params.inspect # => { :post => { :title => "ello", :body => "Want it", :rank => 4 } } 22 23 # We also see that params_protected removes ONLY those parameters explicitly specified. 24 2 25 if defined?(Merb::Plugins) 3 26 … … 73 96 end 74 97 end 98 75 99 module InstanceMethods 76 100 def initialize_params_filter 77 puts accessible_params_args.inspect 101 if accessible_params_args.is_a?(Hash) 102 puts accessible_params_args.inspect 103 accessible_params_args.keys.each do |obj| 104 puts obj.inspect 105 puts accessible_params_args[obj].inspect 106 self.request.restrict_params(obj, accessible_params_args[obj]) 107 end 108 end 78 109 end 79 110 end plugins/merb_param_protection/spec/merb_param_protection_spec.rb
r1227 r1251 2 2 3 3 describe "merb_param_protection" do 4 describe "Controller", "parameter filtering" do 5 before(:each) do 6 @request = fake_request 7 end 8 4 describe "Controller", "parameter filtering" do 9 5 describe "accessible parameters" do 10 6 class ParamsAccessibleController < Merb::Controller … … 18 14 def index; end 19 15 end 20 21 before(:each) do 22 @params_accessible_controller = ParamsAccessibleController.build(@request) 23 @params_accessible_controller.dispatch('index') 24 end 16 25 17 26 18 it "should store the accessible parameters for that controller" do 19 @params_accessible_controller = ParamsAccessibleController.build(fake_request) 20 @params_accessible_controller.stub!(:initialize_params_filter) 21 @params_accessible_controller.dispatch('index') 27 22 @params_accessible_controller.accessible_params_args.should == { 28 23 :address=> [:street, :zip], :post=> [:title, :body], :customer=> [:name, :phone, :email] 29 24 } 25 end 26 27 it "should remove the parameters from the request that are not accessible" do 28 @request.post_body = "post[title]=hello&post[body]=something&post[status]=published" 29 @params_accessible_controller = ParamsAccessibleController.build(fake_request) 30 @params_accessible_controller.dispatch('index') 30 31 end 31 32 end … … 33 34 describe "protected parameters" do 34 35 before(:each) do 35 @params_protected_controller = ParamsProtectedController.build( @request)36 @params_protected_controller = ParamsProtectedController.build(fake_request) 36 37 @params_protected_controller.dispatch('index') 37 38 end
