Created
March 12, 2012 03:56
-
-
Save hiromi2424/2019649 to your computer and use it in GitHub Desktop.
CakePHP Mass Assignment Vulnerability - common process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// モデルの場合。 | |
// 以下と相似なヘルパーメソッドを作れば、いちいち同じようなメソッドを大量生産しなくて済むよ! | |
class Post extends AppModel { | |
public function edit($id, $data) { | |
$this->create(false); | |
$this->set($data); | |
$this->set('id', $id); | |
return $this->save(null, true, array('name')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PostsController extends AppController { | |
public function edit($id = null) { | |
if ($this->RequestHandler->is('post')) { | |
// 更新なのでcreate()にfalseを指定する | |
$this->Post->create(false); | |
// データをセットする | |
$this->Post->set($this->request->data); | |
// IDだけはpostデータでセットされると好き勝手やられる可能性があるので、pass(アクションの引数)で渡ってきたものを使う | |
// isAuthorized()でpassの一番目をチェックすることで、記事の所有者であるかを確かめることができる(存在しないレコードとかも) | |
$this->Post->set('id', $id); | |
// 保存するフィールドの指定は必ずする。 | |
if ($this->Post->save(null, true, array('name'))) { | |
// success/redirect | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment