2013-02-09 34 views
1

最近我完成了關於使用CakePHP創建簡單博客的教程 - 這裏是鏈接:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html 創建驗證表單非常簡單快捷,但我注意到了一個問題。如何讓CakePHP驗證更安全?

命名post.ctp

文件包含:

echo $this->Form->create('Post'); 
    echo $this->Form->input('title'); 

而且它產生的形式最終用戶與該輸入:誰在使用Firefox的螢火蟲可以更改HTML代碼提交表單之前

<input id="PostTitle" type="text" required="required" maxlength="50" name="data[Post][title]"> 

有人從:name="data[Post][title]"到:name="data[Post][author]"。這樣做的結果將更新名爲「作者」的列,而不是「標題」,並允許使用「title」的空數據更新數據庫。

在名爲post.php中「模式」的有效性規則的文件夾不防止:

class Post extends AppModel { 

public $validate = array(
    'title' => array(
     'rule' => 'notEmpty' 
    ), 
    'body' => array(
     'rule' => 'notEmpty' 
    ) 
); 

} 

如何保護我的應用程序,而不是讓別人在更新數據庫中其他列?

回答

3

蛋糕的security component包括form tampering保護。您需要在控制器中添加安全組件:

public $components = array('Security'); 
+0

謝謝。它工作完美。我只是將組件中的行更新爲'public $ components = array('Session','Security');'而沒有更多配置。它在開始時起作用。當我試圖做同樣的事情時,我得到了「請求已被黑洞」的消息。完善。他們應該在第一個教程中加入,因爲這很重要。 – Lucas 2013-02-10 09:16:17

2

看看Model documentation。至少有兩種方式來處理這個問題。

  1. 您可以通過fieldlist作爲第三個參數調用save()時。
  2. 在致電save()之前,您可以設置型號的whitelist屬性。

我還沒有使用過,但Security Component是另一種選擇。

+0

絕對使用安全組件。閱讀鏈接手冊頁面以獲取更多信息。 – ADmad 2013-02-09 20:02:27