-
Notifications
You must be signed in to change notification settings - Fork 160
Added json_encode/json_decode to list of insecure functions #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Why are |
@fooman I think we should avoid using Thanks for your reply! |
@elevinskii thank you for the proposed changes. It's a kind of change we need to discuss before merging. |
As per magento/architecture#153 it was decided to move @elevinskii could you please update your PR addressing this changes. |
I'll continue to use Data is the most valuable thing, and having a guaranteed known serialization format is valuable, because it enables us to create tooling to work with that data not limited to PHP. The That is the reason to avoid using the Magento serializer and use the built in PHP functions instead. Just my .02$ |
In what instance is Also wanted to add that And lastly merging this means that code targeting 2.1 is going to trigger this as SerializerInterface is only 2.2+. |
I agree with you, Vinai, that having it as an implementable interface leaves room for some risk, but if you are adamant about ensuring that your tooling won't break, is there a reason that you couldn't use the It's pretty straight-forward <?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Serialize;
/**
* This class was introduced only for usage in the \Magento\Framework\DataObject::toJson method.
* It should not be used in other cases and instead \Magento\Framework\Serialize\Serializer\Json::serialize
* should be used.
*/
class JsonConverter
{
/**
* This method should only be used by \Magento\Framework\DataObject::toJson
* All other cases should use \Magento\Framework\Serialize\Serializer\Json::serialize directly
*
* @param string|int|float|bool|array|null $data
* @return bool|string
* @throws \InvalidArgumentException
*/
public static function convert($data)
{
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
return $serializer->serialize($data);
}
} |
@df2k2 I believe @Vinai was referring to tooling that might not be in php (I know he uses Closure a lot...). Also the code you referenced illustrates one of the points I tried to explain above. The code specifically states it shouldn't be used in the comments (not sure why as it doesn't include any reason). So now we need to expand even more mental capacity to work out what to do when we have json encoded data. We are currently making something that should be straight forward - harder. |
I do not know enough about the tooling Vinai refers to and uses, nor do I know anything about clojure, so I can't say anything against his case for using json_encode/json_decode. Sorry for the confusion about the comments, I meant to highlight the part in there that says: Is that straight-forward? |
I've used bash (with I also agree with @fooman that using the classes feels very clunky, and they also make it harder to support older Magento versions with the same module code. Using |
Serialize should stay as insecure not discouraged. It poses a significant risk of enabling remote code exec in a way that is hard to predict. The only advantage is speed so it should not be used outside very limited cases. Json functions ok to use directly. I think the advantage of class is just if there is a faster format included as a library in future php, it could be used instead. |
Putting json_* functions into discouraged might result in worse impact on security, as people would not have and option to ouput their array structures within init scripts in templates. Serilizer, IMO, should be used only for storage related code, where Magento has complete control over. |
I 100% recommend fellow employees to use json_encode in templates for JavaScript initialization using a PHP array instead of hand coding/escaping JavaScript directly It seems overkill to use a ViewModel for that instead |
@elevinskii thanks for your input, however after the architecture discussion magento/architecture#174 we decided not to enforce developers to avoid using native We are working on contract for JSON magento/architecture#161 and will keep you updated about further recommendations. |
Increased version to 13
I've added json_encode and json_decode to list of insecure functions, now it throws warnings if these functions are encountered.