Deserializing a PHP serialized string in node js

社会主义新天地 提交于 2020-04-30 06:38:06

问题


I have a PHP serialized string which I was unserializing using php-serialize or 'locutus/php/var/unserialize' in Node js.

"a:2:{s:3:\"$or\";a:1:{i:0;a:1:{s:4:\"$and\";a:1:{i:0;a:1:{s:20:\"attributes.FIRSTNAME\";C:18:\"MongoDB\\BSON\\Regex\":49:{a:2:{s:7:\"pattern\";s:2:\"^a\";s:5:\"flags\";s:1:\"i\";}}}}}}s:4:\"$and\";a:1:{i:0;a:1:{s:3:\"$or\";a:2:{i:0;a:1:{s:8:\"batch_id\";a:1:{s:7:\"$exists\";b:1;}}i:1;a:1:{s:2:\"sc\";a:1:{s:3:\"$ne\";i:-2;}}}}}}"

I am getting error when I try to unserialize it stating:

SyntaxError: Unknown / Unhandled data type(s): c

I am unserializing like :

unserialize(<serialized_string>);

Update:

I am trying the below code when using php-serialize library:

const PhpSerialize = require("php-serialize");
const serialised_str = 'a:2:{s:3:"$or";a:1:{i:0;a:1:{s:4:"$and";a:1:{i:0;a:1:{s:20:"attributes.FIRSTNAME";C:18:"MongoDB\BSON\Regex":49:{a:2:{s:7:"pattern";s:2:"^a";s:5:"flags";s:1:"i";}}}}}}s:4:"$and";a:1:{i:0;a:1:{s:3:"$or";a:2:{i:0;a:1:{s:8:"batch_id";a:1:{s:7:"$exists";b:1;}}i:1;a:1:{s:2:"sc";a:1:{s:3:"$ne";i:-2;}}}}}}';
console.log(PhpSerialize.unserialize(serialised_str));

And getting this error:

Error: Expected '"' at index 106 while unserializing payload

If I try to unserialize the string with no classes, it works. But if the string is having somePHP class, it stops working.

Please someone help me in solving this. Thanks in advance.


回答1:


"a:2:{s:3:\"$or\";a:1:{i:0;a:1:{s:4:\"$and\";a:1:{i:0;a:1:{s:20:\"attributes.FIRSTNAME\";C:18:\"MongoDB\\BSON\\Regex\":49:{a:2:{s:7:\"pattern\";s:2:\"^a\";s:5:\"flags\";s:1:\"i\";}}}}}}s:4:\"$and\";a:1:{i:0;a:1:{s:3:\"$or\";a:2:{i:0;a:1:{s:8:\"batch_id\";a:1:{s:7:\"$exists\";b:1;}}i:1;a:1:{s:2:\"sc\";a:1:{s:3:\"$ne\";i:-2;}}}}}}"

This is not BSON. BSON is binary and you wouldn't be able to read any of it like text. The above looks like it could be PHP-serialized PHP data structure.

It sounds like you should clarify what the different pieces you are working with are (first of all for yourself, since you seem to be confusing the various serialization formats) and then revise your question.

Sometimes when people say the phrase "BSON document" they don't actually mean BSON at all - they simply are referring to a map of keys to values, potentially with certain properties like the keys are all strings.



来源:https://stackoverflow.com/questions/61347446/deserializing-a-php-serialized-string-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!