Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created March 12, 2012 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromi2424/2019649 to your computer and use it in GitHub Desktop.
Save hiromi2424/2019649 to your computer and use it in GitHub Desktop.
CakePHP Mass Assignment Vulnerability - common process
<?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'));
}
}
<?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