Skip to content

Commit

Permalink
Add safelist for object unserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Nov 14, 2023
1 parent c5d0663 commit 2ef5a37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,22 @@
|
*/
'cache_default_value' => true,

/*
|--------------------------------------------------------------------------
| Unserialize Safelist
|--------------------------------------------------------------------------
|
| When using the default value serializer class from this package, we
| will only unserialize objects that have their classes whitelisted here.
| Any other objects will be unserialized to something like:
| __PHP_Incomplete_Class(App\Models\User) {...}
|
| To prevent any objects from being unserialized, simply set this to
| an empty array.
*/
'unserialize_safelist' => [
\Carbon\Carbon::class,
\Carbon\CarbonImmutable::class,
],
];
5 changes: 4 additions & 1 deletion src/Support/ValueSerializers/ValueSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rawilk\Settings\Support\ValueSerializers;

use Illuminate\Support\Arr;
use Rawilk\Settings\Contracts\ValueSerializer as ValueSerializerContract;

class ValueSerializer implements ValueSerializerContract
Expand All @@ -15,6 +16,8 @@ public function serialize($value): string

public function unserialize(string $serialized): mixed
{
return unserialize($serialized, ['allowed_classes' => false]);
$safelistedClasses = Arr::wrap(config('settings.unserialize_safelist', []));

return unserialize($serialized, ['allowed_classes' => $safelistedClasses]);
}
}

0 comments on commit 2ef5a37

Please sign in to comment.