Skip to content

Commit

Permalink
Sanitize user attrs on model level
Browse files Browse the repository at this point in the history
  • Loading branch information
dzaporozhets committed Jul 10, 2013
1 parent 3dcc441 commit 223d264
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ gem "d3_rails", "~> 3.1.4"
# underscore-rails
gem "underscore-rails", "~> 1.4.4"

# Sanitize user input
gem "sanitize"

group :assets do
gem "sass-rails"
gem "coffee-rails"
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ DEPENDENCIES
redcarpet (~> 2.2.2)
redis-rails
rspec-rails
sanitize
sass-rails
sdoc
seed-fu
Expand Down
15 changes: 1 addition & 14 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def account
end

def update
if @user.update_attributes(user_attributes)
if @user.update_attributes(params[:user])
flash[:notice] = "Profile was successfully updated"
else
flash[:alert] = "Failed to update profile"
Expand Down Expand Up @@ -69,19 +69,6 @@ def user
@user = current_user
end

def user_attributes
user_attributes = params[:user]

# Sanitize user input because we dont have strict
# validation for this fields
%w(name skype linkedin twitter bio).each do |attr|
value = user_attributes[attr]
user_attributes[attr] = sanitize(strip_tags(value)) if value.present?
end

user_attributes
end

def authorize_change_password!
return render_404 if @user.ldap_user?
end
Expand Down
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class User < ActiveRecord::Base
validate :namespace_uniq, if: ->(user) { user.username_changed? }

before_validation :generate_password, on: :create
before_validation :sanitize_attrs

before_save :ensure_authentication_token

alias_attribute :private_token, :authentication_token

delegate :path, to: :namespace, allow_nil: true, prefix: true
Expand Down Expand Up @@ -356,4 +359,11 @@ def accessible_deploy_keys
def created_by
User.find_by_id(created_by_id) if created_by_id
end

def sanitize_attrs
%w(name username skype linkedin twitter bio).each do |attr|
value = self.send(attr)
self.send("#{attr}=", Sanitize.clean(value)) if value.present?
end
end
end

0 comments on commit 223d264

Please sign in to comment.