php - CakePhp: Avoid XSS attack keeping the ease of use of cake -


one of things cakephp, can have generated edited form allows save.

e.g. in controller:

function add() {         if (!empty($this->data)) {             $this->post->create();             if ($this->post->save($this->data)) {                 $this->session->setflash(__('the post has been saved', true));                 $this->redirect(array('action' => 'index'));             } else {                 $this->session->setflash(__('the post not saved. please, try again.', true));             }         }         $users = $this->post->user->find('list');         $this->set(compact('users'));     } 

the problem our fields vulnerable xss (cross site scripting). i'm aware of "sanitize::clean" way, i've problem that: it's mean have on fields before save object. , if once add 1 field? should go on our code check sanitize it?? there way "sanitize object before save it", without specifing fields?

thank you!

you can @ beforesave() method models

http://book.cakephp.org/view/1052/beforesave

the data submitted available in $this->data[$this->alias] array, could

foreach($this->data[$this->alias] $k => $v) {    $this->data[$this->alias][$k] = sanitize::clean($v); } 

usually want store whatever submitted user in database , sanitize when need display it, way still preserve original html content (if indeed intended html input (for instance: blog post)).

if want sanitize before displaying, using afterfind() don't have call sanitize everytime.

http://book.cakephp.org/view/1050/afterfind

function afterfind($results, $primary) {    $tosanitize = array('field1', 'field2', 'field4');    if(!empty($results[0])) {       foreach($results $i => $res) {          foreach($tosanitize $ts) {             if(!empty($res[$this->alias][$ts]))                 $results[$i][$this->alias][$ts] = sanitize::clean($res[$this->alias][$ts]);             }          }       }    } else {       foreach($tosanitize $ts) {         if(!empty($results[$ts]))             $results[$ts] = sanitize::clean($results[$ts]);         }      }    }     return $results; } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -