开发者

PHP/deserialize - is it safe to deserialize $_GET value?

开发者 https://www.devze.com 2023-02-07 05:08 出处:网络
I\'m passing urlencode()d serialize()d arrays around my webpages, via $_GET[]. I开发者_高级运维s it safe to deserialize() a value from $_GET? The deserialized array will sometimes be shown to the use

I'm passing urlencode()d serialize()d arrays around my webpages, via $_GET[].

I开发者_高级运维s it safe to deserialize() a value from $_GET? The deserialized array will sometimes be shown to the user. Would it be possible for a user to expose/reference variables or functions etc within my code? In other words, when deserializing the value, does PHP treat it as data or code?

Update:

I see the documentation says: "Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost. "

So that means i'm safe? :-)


Absolutely, positively, no.

You shouldn't blindly trust anything from the client side, however there is a way you can give yourself more confidence.

I'm assuming that if you've got PHP serialized data coming from the client side, that client obtained that from a server at some point? If that's the case, and the client doesn't modify the data, you could include a hash along with the data to verify it hasn't been tampered with.

The other alternative would be to unserialize the object, but regard it as 'tainted', then copy and re-verify the unserialized data into a 'clean' object.


This method is as "safe "as any other kind of incoming GET or POST data - you will always need to sanitize the data before working with it! But there are additional issues with unserializing user data.

When unserializing an object, PHP will look whether the class has a __wakeup magic method. That method will get executed if present.

Now this is not a massive security hole in itself, because the class definition is never transmitted in the serialized data. Any malicious code would have to be present in the system already. However, there are conceivable scenarios where this could be a problem (e.g. a plug-in system that can install third party code) and I would be very wary with this.

Also, theoretically, this allows an attacker to create an object of any class inside your script. While not a security problem straight away, it is surely not good practice to do.

JSON encoding would be a more safe way, because it can contain only "dumb" data.


You are serializing only data-part of objects/arrays/variables, the actual executable code is not serialized- there is no point in doing that - serialization helps to transfer your data between two different worlds- executed code can be same or different there - for data it does not matter.

Though possible hacks would be possible - but only based on data - classes and types and values might differ - it's up the code how can it cope with errors during deserialization.


Yes, its safe. You are asking is it safe to serialize the value of the $_GET array. Yes, it is safe. Nothing gets executed during the serialization of array. Since $_GET array does not contain any objects, only the parameters from query string, it cannot do any harm during serialization/unserialization.

You mentioned something you saw on documentation about circular references. Don't worry about that, it does not apply in your case because there are no objects inside the $_GET array.

As far as using the actual data from the $_GET array, that's a different question and the answer would be no, it's not safe to use data from the $_GET array without applying some type of filter or validation first

0

精彩评论

暂无评论...
验证码 换一张
取 消