Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Decrypt: ErrorException unserialize(): Error at offset 0 of 2 bytes

Tags:

laravel

I wrote a package for subdomain localization. But out of nowhere decrypting cookies is not working anymore.

My locale test website was working, but I needed to unsecure it to use Laravel valet share. Now when I changed it to valet secure again, my website does not work anymore. I get the ErrorException:

unserialize(): Error at offset 0 of 2 bytes

in

//Illuminate/Encryption/Encrypter.php
Row 149.  return $unserialize ? unserialize($decrypted) : $decrypted;

I don't know why. I did not change my app_key, I deleted all cookies for my page but still, it does not work (and it worked perfectly for the last 3 weeks).

If you have a look here or the shortened code:

 return decrypt(request()->cookie('tongue-locale'));

You see that I'm decrypting my content of the cookie. If you are curious why I need to decrypt my cookie, that is because the middleware "\App\Http\Middleware\EncryptCookies::class," is kicking later in.

Does anyone know why it is not working?

like image 679
Philipp Mochine Avatar asked Aug 09 '18 09:08

Philipp Mochine


2 Answers

I don't know what happened, but I needed to change my code into this:

app('encrypter')->decrypt(request()->cookie(self::COOKIE), false);

With "false" the unserialize is ignored. But still, I would love to know why it is not working.

If you translate this part

return $unserialize ? unserialize($decrypted) : $decrypted;

with variables $unserialize = true and $decrypted = "de" it's clear that it fails, but I wonder why it did not before..

like image 97
Philipp Mochine Avatar answered Oct 14 '22 05:10

Philipp Mochine


I fixed the issue using this solutions: https://laravel.com/docs/5.6/upgrade#upgrade-5.6.30

Add this line: protected static $serialize = true;

To app\Http\Middleware\EncryptCookies.php

like image 31
arhakim Avatar answered Oct 14 '22 04:10

arhakim