Does build protect against sql injection?
Example:
@post = @user.posts.build(params[:post])
@post.save
Didn’t see build in the rails security docs.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
builditself doesn’t write anything to the database so SQL injection doesn’t apply. When you callsaveit doesn’t matter whether the object was created viabuildor via another mechanism such as passing attributes tonewor using individualattribute=methods, the same code will be used to save your object to the database.From the documentation on build:
The
savemethod will escape any quotes etc in your attribute values using a method appropriate to the database you’re using (e.g. MySQL) so that the resultinginsertorcreatequery is not susceptible to SQL injection. The same applies toupdate_attributesand to any parameterised:conditionsthat you pass tofind. The time when you need to be careful and may need to do some manual escaping is if you are ever passing literal strings to the database connection as queries.