Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP Security - Prevent Form Injection
    primarykey
    data
    text
    <p>I currently have 1 table, Users which looks like this</p> <pre><code>|**id**|**username**|**password**|**role**|**email**| </code></pre> <p>I'm using CakePHP's form helper to automatically fill in editable form fields. I'm creating an edit page in which users can change there username/password/email, but should NOT be able to change their role. I'm currently checking to make sure the user hasn't injected a role POST field into the request and was wondering if there is any better way to do this? It's trivial in this scenario with such a small table, but I can see this becoming tiresome on fields/tables with a large amount of columns. My current edit action looks like this.</p> <pre><code>public function edit($id = null) { $this-&gt;User-&gt;id = $id; if(!$this-&gt;User-&gt;exists()) { throw new NotFoundException('Invalid user'); } $userToEdit = $this-&gt;User-&gt;findById($id); if(!$userToEdit) { throw new NotFoundException('Invalid user'); } if($this-&gt;getUserRole() != 'admin' &amp;&amp; $userToEdit['User']['owner'] != $this-&gt;Auth-&gt;user('id')) { throw new ForbiddenException("You do not have permission to edit this user"); } if($this-&gt;request-&gt;is('post') || $this-&gt;request-&gt;is('put')) { //Do not reset password if empty if(empty($this-&gt;request-&gt;data['User']['password'])) unset($this-&gt;request-&gt;data['User']['password']); if(isset($this-&gt;request-&gt;data['User']['role'])) unset($this-&gt;request-&gt;data['User']['role']); if($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;set('success', true); } else $this-&gt;set('success', false); } else { $this-&gt;request-&gt;data = $this-&gt;User-&gt;read(); //Prevent formhelper from displaying hashed password. unset($this-&gt;request-&gt;data['User']['password']); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload