![]() |
Model::update() is dangerous - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31) +--- Thread: Model::update() is dangerous (/showthread.php?tid=84833) |
RE: Model::update() is dangerous - ikesela - 11-16-2022 maybe can fix this, byass id checking if it is NULL , there should be no if($id), no checking need so WhereIn to be work Code: protected function doUpdate($id = null, $data = null): bool RE: Model::update() is dangerous - michalsn - 11-16-2022 There is no good solution for this for now. Any change would be a BC break. Developers may use composite keys with the model. PHP Code: $model->where(['key1' => 'value1', 'key2' => 'value2'])->update(null, $data); I believe this was a design choice. Whether we like the current model behavior or not, the code presented here and in the repo lacks any basic checks in update() / postUpdate() method:
Meanwhile, the code responsible for showing the view: getEdit() has these checks. There should be no difference when validating the $id for getEdit() and postUpdate(). I get your reasoning with update(null, $data), but the real danger here is poorly-written code, not the framework. In this case, simple value validation for $id would solve all the problems. RE: Model::update() is dangerous - kenjis - 11-16-2022 Certainly, the current API cannot be changed without a BC break. However, an API that is not well designed should be changed to something more secure. All inputs should be validated. Yes, that is correct. But have you guys ever forgotten to validate input values? With the current API, developers create a vulnerability that if they forget to validate $id even once, all records will be updated. I can't think of a use case like this sample where you want to update all records at the same time when updating a single record. UPDATE table and UPDATE table WHERE id = ? are fundamentally different use cases. There is no need to provide both in the same method. RE: Model::update() is dangerous - michalsn - 11-17-2022 kenjis Wrote:But have you guys ever forgotten to validate input values? Ok, this is hard to argue. This probably happened to everyone. kenjis Wrote:I can't think of a use case like this sample where you want to update all records at the same time when updating a single record. As I said before. We have to think of it from the point of view of the entire API that comes with the Model. Developers may write queries that don't necessarily update all the records when the null is set, like: PHP Code: $model->where(...)->set(...)->update(); So, if we suddenly start to require an $id, this will break people's code. Not sure what is more damaging. The only workaround I see for this is checking if the where part was set before or not. Something like: PHP Code: protected function doUpdate($id = null, $data = null, bool $allowNullId = false): bool It would still be a BC break, but the overall functionality would remain. Still, I'm not sure this is the best thing to do. RE: Model::update() is dangerous - kenjis - 11-17-2022 I created another controller. I added the validation for id. But it is still vulnerable. https://github.com/kenjis/ci4-model-update-danger/blob/78b08c5bb1e7259c3f4863af63df86920fdac9f8/app/Controllers/News3.php#L101-L123 PHP Code: public function update() RE: Model::update() is dangerous - kenjis - 11-17-2022 (11-17-2022, 09:12 AM)michalsn Wrote: The only workaround I see for this is checking if the where part was set before or not. Something like: It seems no problem. It prevents unexpected all record updates. What's your concern? RE: Model::update() is dangerous - michalsn - 11-18-2022 (11-17-2022, 05:26 PM)kenjis Wrote: I created another controller. I added the validation for id. Ok, you got me here. How this is still a vulnerability? kenjis Wrote:It seems no problem. It prevents unexpected all record updates. Not a big fan of the API I came up with. RE: Model::update() is dangerous - kenjis - 11-19-2022 (11-15-2022, 09:12 PM)iRedds Wrote: And what solution do you propose? See https://github.com/codeigniter4/CodeIgniter4/pull/6883 RE: Model::update() is dangerous - InsiteFX - 11-19-2022 Nver ever trust user input data! If it's a bad vulnerability then we need to fix it before it gets out of control. RE: Model::update() is dangerous - kenjis - 11-28-2022 I would like to change the API:
|