bumbummen99 / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
501 stars 228 forks source link

Use base64 to avoid truncation with PostgreSQL #167

Closed mauriciv closed 2 years ago

mauriciv commented 2 years ago

When using a PostgreSQL database, serializing the cart's content and storing them in a text column doesn't work. because PostegreSQL drops everything after a zero byte in text columns. And seeing as PHP's serialize adds zero bytes to private and protected members image this results as a truncated string being inserted in the content column and being unable to unserialize it later. This behaviour can be reproduced in the test suite by using a PostegreSQL database instead of a sqlite one.

Using base64, we can sidestep this issue, the zero byte will only "live" in PHP, and never be inserted in PostgreSQL.

Unfotunately, using base64 will end up being a breaking change for those that are currently using a PostgreSQL database, although I would imagine that already implemented some form of workaround because they would have been impacted by this bug already.

bumbummen99 commented 2 years ago

Good work, LGTM 👍.

I am in general not a big fan of the fact that the package is serializing the cart in order to store it, it is just not atomic and comes with disatvantages for example if you have massive carts or want to work without session. That is also why the refactoring is currently blocked as i am looking for a good way to store each cart item seperately.