Brakeman is an open source static analysis tool which checks Ruby on Rails applications for security vulnerabilities.
[my-rails-app] $ gem install brakeman
[my-rails-app] $ brakeman
[my-rails-app] $ brakeman -o output_file
# Instead of this
user = User.new
user.name = params[:user][:name]
user.email = params[:user][:email]
# I can do this
User.new(params[:user])
Parameters: {
user: {
name: 'Athur Dent',
email: 'dent@arthur.com'
}
}
User.new(params[:user])
Parameters: {
user: {
name: 'Athur Dent',
email: 'dent@arthur.com',
admin: true
}
}
class UsersController < ApplicationController
def create
@user = User.new(user_params)
# ...
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
Parameters: {
"user" => {
"name" => "Arthur Dent",
"email" => "dent@arthurdent.com",
"admin"=>"1"
},
"commit"=>"Create User"
}
Unpermitted parameter: :admin
class User < ActiveRecord::Base
attr_accessible :name, :email
end
irb(main):001:0> User.new admin: true
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: admin
Marks a string as trusted safe. It will be inserted into HTML with no additional escaping performed. It is your responsibilty to ensure that the string contains no malicious content. It should never be called on user input.
# GOOD
<%= raw glyphicon('search') %>
# VERY BAD
<%= raw user.name %>
# BAD, VULNERABLE CODE
User.where("name LIKE '%#{params[:name]}%'")
# GOOD
User.where("name LIKE '%?%'", params[:name])
# BETTER
User.where("name LIKE '%:name%'", name: params[:name])
rails-sqli.org
`rm -rf /my/awesome/dir/#{file_path}`
file_path = "../*" #=> PWNED
file_path = "* && my-evil-command" #=> PWNED
command = "rm -rf /my/awesome/dir/my-cool-file.txt"
system command