2017-06-21 52 views
0

這可能是個愚蠢的問題,但請耐心等待。Rails控制檯是否繞過質量分配保護?

我一直在玩正在使用的rails應用程序,並且我在控制檯(即rails c)中,我決定嘗試通過控制檯將用戶添加到數據庫。

User模型具有role屬性,它是從UsersController強則params的列表中排除。但是,當我使用控制檯時,我可以通過執行update_attribute來編輯新用戶角色的值。這關係到我。這是否意味着我沒有正確執行強參數,並且不知何故不能保護我的用戶模型免遭羣發任務?或者軌道控制檯故意繞過質量分配?這裏有沒有安全漏洞?

這裏是控制檯輸入/輸出:

2.3.1 :004 > user.update_attribute("role", "admin") 
(0.1ms) begin transaction 
SQL (0.7ms) UPDATE "users" SET "updated_at" = ?, "role" = ? WHERE "users"."id" = ? [["updated_at", "2017-06-21 10:25:34.134203"], ["role", "admin"], ["id", 4]] 
(92.1ms) commit transaction 
=> true 

這裏是UsersController相關部分:

def create 
    sleep(rand(5)) # random delay; mitigates Brute-Force attacks 
    @user = User.new(user_params) 

    if @user.save #&& verify_recaptcha(model: @user) 
     if @user.update_attribute('role', 'user') 
      @user.send_activation_email 
      flash[:info] = "Please check your email to activate your account." 
      redirect_to root_url 

     else 
      render 'new' 
     end 

    else 
     render 'new' #Reset the signup page 
    end 
end 

#... 

#Defines which fields are permitted/required when making a new user. 
def user_params 
    params.require(:user).permit(:name, :email, :password, :password_confirmation) 
end 

預先感謝您。

回答

2

user.update_attribute如您在控制檯中哪些更新看到,只是產生一個SQL查詢

它什麼都沒有做具有很強的參數(「角色」,「管理員」).. 記錄。

強參數用於限制來自視圖/客戶端的未經許可的參數並修改您的記錄。

在你的情況下,

user_params不包括role,因爲你是你自己指定。如果你還沒有做到這一點,並在請求體中我曾派人role: 'admin'

User.new(params) 

會使得用戶admin,如果verify_recaptcha(model: @user)條件不滿足..

+0

啊,OK。那麼我在控制檯上做的事情不是惡意用戶真正能夠複製的東西嗎?除非可能存在SQL注入漏洞或這種性質。 – Makcheese

+0

是的..現在你知道了 –

+0

謝謝!這讓我的心靈放鬆:) – Makcheese

相關問題