9

I'm working on a legacy rails application and the controllers have many instances of params.permit!. When running a Brakeman scan on it, params.permit! opens up the application to mass assignment vulnerabilities.

My question is- what is the most effective way to get around this params.permit! vulnerability and replace it?

zasman
  • 446
  • 1
  • 8
  • 28

3 Answers3

18

params.permit! whitelists all attributes leading to the vulnerabilities of mass assignment. The best way to get around this is by whitelisting only the necessary attributes like so

params.permit(:attr1,:attr2..)

Even better, use require with permit

Allows you to choose which attributes should be whitelisted for mass updating and thus prevent accidentally exposing that which shouldn't be exposed. Provides two methods for this purpose: require and permit. The former is used to mark parameters as required. The latter is used to set the parameter as permitted and limit which attributes should be allowed for mass updating.

params.require(:key).permit(:attr1, :attr2..)
Pavan
  • 33,316
  • 7
  • 50
  • 76
3

I assume that someone added the params.permit! after a rails upgrade to avoid looking into "strong parameters" and setting it up correctly.

The correct way to fix this is by going through every controller and reviewing what params you need and want to permit for every action, and then using params.permit (without the exclamation mark) to set up the whitelist for permitted paramters:

https://apidock.com/rails/ActionController/Parameters/permit

trueunlessfalse
  • 1,163
  • 10
  • 16
  • That sounds like something that may have happened. So, params.permit(:attribute1, :attribute2) before each method? – zasman Sep 17 '18 at 20:12
  • First, Pavans answer is more complete and quicker then mine. I would recommend to select it as the correct answer. Second: In most cases a crud-controller has only two actions using the params: update and create. Often (not always!) both actions are permitting the same parameters, so in a controller for a 'person' defining 'person_params' once should be enough. – trueunlessfalse Sep 18 '18 at 07:50
  • I would recomend you to read the rails guide for strong parameters: https://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters – trueunlessfalse Sep 18 '18 at 07:51
1

I also found that using the to_unsafe_hash method on individual param calls will work and get around the Brakeman warning. Some info on the method here: https://apidock.com/rails/v4.2.7/ActionController/Parameters/to_unsafe_hash

zasman
  • 446
  • 1
  • 8
  • 28