2012-09-10 1 views
1

악의적 인 사용자가 내 사이트에 등록 할 때 추가 매개 변수를 추가 할 가능성이 있습니다. 나는 Rails 3.2.8과 Devise 2.1.2를 사용하고있다. 나는 admin 속성을 가진 클래스 User보안 - Devise로 사용자를 생성 할 때 필터 속성

user.rb

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, :recoverable, :trackable, 
    :validatable, :token_authenticatable, :lockable 
end 

schema.rb

create_table "users", :force => true do |t| 
    t.boolean "admin" 
    t.string "email",         :null => false 
    t.string "encrypted_password",  :default => "", :null => false 
    # other devise columns are not relevant for the question... 
end 

나는 사용자의 등록을 위해 제공 Devise::RegistrationsController을 사용하고 있습니다 , 사용자 정의보기는 emailpassword :

입니다. 16,
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="form_line"> 
    <%= f.label :email, 'Email' %> 
    <%= f.email_field :email %> 
    </div> 
    <div class="form_line"> 
    <%= f.label :password, 'Password' %> 
    <%= f.password_field :password %> 
    </div> 
    <div class="form_line"> 
    <%= f.label :password_confirmation, 'Password confirmation' %> 
    <%= f.password_field :password_confirmation %> 
    </div> 

    <div class="actions"> 
    <%= f.submit "Register", class: 'button' %> 
    </div> 
<% end %> 

내가 완벽하게 작동하지만, 악의적 인 사용자가 POST 요청의 값 true와 매개 변수 admin을 추가하는 경우는, 사용자는 관리자 권한으로 작성됩니다.

Started POST "/users" for 127.0.0.1 at 2012-09-10 18:46:15 +0200 
Processing by RegistrationsController#create as HTML 
    Parameters: { "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "admin"=>"true" } 

내 사이트의 보안 취약점입니다. 사용자가 등록 (또는 프로필을 업데이트 할 때 동일한 문제가 발생할 것임) 할 때 추가 매개 변수를 무시하도록 Devise를 구성 할 수 있습니까 (이메일과 비밀번호 만 필요함)? 가능하지 않다면 다른 해결책이 있습니까?

답변

3

관리 필드를 대량 할당에서 보호해야합니다. 당신은 당신의 모델이 추가해야합니다 : 자세한 내용은 여기를 attr_protected :admin

읽기 : http://apidock.com/rails/v3.2.8/ActiveModel/MassAssignmentSecurity/ClassMethods/attr_protected

+2

고맙습니다. 작동합니다. 나는'attr_accessible'을 사용하고 Ruby on Rails 보안 가이드에서 제안한'config.active_record.whitelist_attributes = true' 옵션을 사용하기로했습니다. http://guides.rubyonrails.org/security.html#mass-assignment – Baldrick

0

유증 내가 문제를 제거하는 더 나은 방법이라고 생각 인증을위한 여러 모델을 구성 할 수있는 가능성을 제공합니다.

"Devise를 사용하면 원하는만큼 많은 역할을 설정할 수 있습니다. 예를 들어 사용자 모델이있을 수 있으며 인증 및 시간 초과 가능 기능이있는 관리 모델이 필요할 수 있습니다."

# Create a migration with the required fields 
create_table :admins do |t| 
    t.string :email 
    t.string :encrypted_password 
    t.timestamps 
end 

# Inside your Admin model 
devise :database_authenticatable, :timeoutable 

# Inside your routes 
devise_for :admins 

# Inside your protected controller 
before_filter :authenticate_admin! 

# Inside your controllers and views 
admin_signed_in? 
current_admin 
admin_session 

비슷한 관심사를 가진 사람이라면 시간을내어 기기 설명서를 확인하고 마음에 드는지보십시오. devise documentation

관련 문제