Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - When to use params.permit! and how to replace it

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?

like image 473
zasman Avatar asked Sep 17 '18 19:09

zasman


People also ask

What does params permit do in Rails?

The permit method returns a copy of the parameters object, returning only the permitted keys and values.

How do params work in Ruby on Rails?

As you might have guessed, params is an alias for the parameters method. params comes from ActionController::Base, which is accessed by your application via ApplicationController. Specifically, params refers to the parameters being passed to the controller via a GET or POST request.

Why we use strong params in Rails?

Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.

Should you permit all the parameters of your rails model?

Sometimes, you are looking to permit all the parameters of your Rails model in controller strong parameters. Instead of specifying them explicitly, one by one, you can permit all of the model attributes. However, this is not a good practice for a production application.

What are strong parameters in Ruby on rails?

Rails introduced the “strong parameters” system, back in Rails 4 as a security feature. It forces you to whitelist the attributes that can be saved. This prevents an issue known as “mass assignment”, which allows malicious users to set admin = true, or set other fields that normally they wouldn’t have access to.

What is the use of permit method in Ruby?

The permit method is a list of allowed (but optional) attributes. As a result, you get back a new params hash with these attributes, but now they’re clear to be saved to the database. Notice that a regular Ruby hash will bypass this security system.

How to change params with and without paramswrapper in rails?

Let’s create a simple Rails app and review how params changes with and without ParamsWrapper. Create a new Rails app on the command line. Add a controller called /controllers/meetups_controller.rb, and paste in the following. Edit /config/routes.rb. And make a request from the command line. The Rails server logs will now display below.


Video Answer


2 Answers

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..)
like image 66
Pavan Avatar answered Oct 21 '22 05:10

Pavan


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

like image 29
trueunlessfalse Avatar answered Oct 21 '22 03:10

trueunlessfalse