Skip to content

Instantly share code, notes, and snippets.

View iHiD's full-sized avatar
💙

Jeremy Walker iHiD

💙
View GitHub Profile
@iHiD
iHiD / user_creation.rb
Created June 10, 2012 13:51
Security Article Part 2 - 6
User.create!
# -> #<User id: 2, can_do_dangerous_things: false, ...>
User.create!(:can_do_dangerous_things => true)
# -> ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: can_do_dangerous_things
User.create!(:permissions => {:can_do_dangerous_things => true})
# -> #<User id: 2, can_do_dangerous_things: true, ...>
@iHiD
iHiD / application.rb
Created June 10, 2012 13:55
Security Article Part 2 - 6
config.active_record.whitelist_attributes = true
@iHiD
iHiD / User.rb
Created June 10, 2012 13:56
Security Article Part 2 - 6
class User < ActiveRecord::Base
# Has attributes: [:username, :hashed_password, :is_admin]
attr_accessible :username
end
@iHiD
iHiD / User.rb
Created June 10, 2012 14:01
Security Article Part 2 - 7
class User < ActiveRecord::Base
# Has attributes: [:username, :hashed_password, :is_admin]
attr_accessible :username
attr_accessible :username, :is_admin, :as => :internal
end
@iHiD
iHiD / user_updating.rb
Created June 10, 2012 14:02
Security Article Part 2 - 8
User.update_attributes(:username => "iHiD")
User.update_attributes({:username => "iHiD", :is_admin => true}, :as => :internal)
@iHiD
iHiD / ProjectsController.rb
Created June 10, 2012 14:30
Security Article Part 2 - 8
class ProjectsController < ApplicationController
def index
@projects = Project.where(
"user_id = #{current_user.id} AND name LIKE '#{params[:name]}%'"
)
#...
end
end
@iHiD
iHiD / vulnerable.sql
Created June 10, 2012 14:34
Security Article Part 2 - 9
SELECT * FROM "projects"
WHERE user_id = 1
AND name LIKE '' OR created_at LIKE '%'
@iHiD
iHiD / safe_sql.rb
Created June 10, 2012 14:39
Security Article Part 2 - 10
@projects = Project.where(:user_id => current_user.id).
where('name LIKE ?', "#{params[:name]}%")
@iHiD
iHiD / injection_opts.rb
Created June 10, 2012 14:47
Security Article Part 2 - 11
# Use a hash
Project.where(:user_id => current_user.id)
# Use placeholders
Project.where("user_id = ?", current_user.id)
# Use bind variables
Project.where("user_id = :user_id", {:user_id => current_user.id})
@iHiD
iHiD / ProjectsController.rb
Created June 10, 2012 14:51
Security Article Part 2 - 12
class ProjectsController < ApplicationController
def index
@project = current_user.projects.where('name LIKE ?', "#{params[:name]}%")
#...
end
end