Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - is this safe? taking a URL param to query the DB?

Does Rails automatically protect against vulnerabilities of doing something like:

Given a URL: http://a.com/?id=3131313131313

then in the rails controller

@comment = Comment.find(params[:id])

Does Rails auto protect that, or do I need to do some type of validation to protect the app from hackers?

Thanks

like image 898
AnApprentice Avatar asked Feb 27 '11 20:02

AnApprentice


People also ask

What are rails parameters and why are they useful?

Let’s talk about Rails parameters! Why are they useful? Users can send data to your web application in three different ways. How do you access this data from Rails? With params. Inside your controller action’s you can call params to access form & URL query data. What is params, exactly?

How to use REST-style parameters in rails?

Besides query parameters, you can also enable REST-style parameters. In Rails, we call this is a “dynamic segment”. Let’s see an example. Then you can access this 1, which is the id in books/:id. With params. This will help you find the specific resource the user is looking for.

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.

Are all values inside params strings in rails?

All values inside params are strings. Even if they’ve been submitted as integers Let’s look a little deeper into Rails params because if you don’t understand them well they can be a source of confusion & strange problems! How do form fields & URL parameters map to params keys?


1 Answers

ActiveRecord find will always use .to_i to prevent all SQL injection magic.

Rails will also auto-escape stuff in queries like this:

Comment.where(["id = ?", params[:id]])

But not in

Comment.where("id = #{params[:id]}")
like image 153
Marcel Jackwerth Avatar answered Sep 29 '22 03:09

Marcel Jackwerth