From 0138d780cb6ba829ab6e312b593a31d1d091736c Mon Sep 17 00:00:00 2001 From: Caleb White Date: Wed, 7 Aug 2024 16:07:46 -0500 Subject: [PATCH 1/3] fix: prevent casting empty string to array from triggering json error (#52415) --- .../Eloquent/Concerns/HasAttributes.php | 6 +++- .../DatabaseConcernsHasAttributesTest.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php index 991f06434a5b..c2c478d1cb59 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php @@ -1271,7 +1271,11 @@ protected function asJson($value) */ public function fromJson($value, $asObject = false) { - return Json::decode($value ?? '', ! $asObject); + if ($value === null || $value === '') { + return null; + } + + return Json::decode($value, ! $asObject); } /** diff --git a/tests/Database/DatabaseConcernsHasAttributesTest.php b/tests/Database/DatabaseConcernsHasAttributesTest.php index 2e2159c4c102..f4e31d4122a4 100644 --- a/tests/Database/DatabaseConcernsHasAttributesTest.php +++ b/tests/Database/DatabaseConcernsHasAttributesTest.php @@ -21,6 +21,14 @@ public function testWithConstructorArguments() $attributes = $instance->getMutatedAttributes(); $this->assertEquals(['some_attribute'], $attributes); } + + public function testCastingEmptyStringToArrayDoesNotError() + { + $instance = new HasAttributesWithArrayCast(); + $this->assertEquals(['foo' => null], $instance->attributesToArray()); + + $this->assertTrue(json_last_error() === JSON_ERROR_NONE); + } } class HasAttributesWithoutConstructor @@ -40,3 +48,23 @@ public function __construct($someValue) { } } + +class HasAttributesWithArrayCast +{ + use HasAttributes; + + public function getArrayableAttributes(): array + { + return ['foo' => '']; + } + + public function getCasts(): array + { + return ['foo' => 'array']; + } + + public function usesTimestamps(): bool + { + return false; + } +} From be2be342d4c74db6a8d2bd18469cd6d488ab9c98 Mon Sep 17 00:00:00 2001 From: driesvints Date: Fri, 9 Aug 2024 07:55:45 +0000 Subject: [PATCH 2/3] Update version to v10.48.20 --- src/Illuminate/Foundation/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index c2e22a0a7743..b9bdd713422a 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -40,7 +40,7 @@ class Application extends Container implements ApplicationContract, CachesConfig * * @var string */ - const VERSION = '10.48.19'; + const VERSION = '10.48.20'; /** * The base path for the Laravel installation. From 05a9554ac0c7361504a54e350787aba84d028ce7 Mon Sep 17 00:00:00 2001 From: driesvints Date: Fri, 9 Aug 2024 07:57:21 +0000 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69942080c4ee..c1721828ee90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Release Notes for 10.x -## [Unreleased](https://github.com/laravel/framework/compare/v10.48.19...10.x) +## [Unreleased](https://github.com/laravel/framework/compare/v10.48.20...10.x) + +## [v10.48.20](https://github.com/laravel/framework/compare/v10.48.19...v10.48.20) - 2024-08-09 + +* [10.x] fix: prevent casting empty string to array from triggering json error by [@calebdw](https://github.com/calebdw) in https://github.com/laravel/framework/pull/52415 ## [v10.48.19](https://github.com/laravel/framework/compare/v10.48.18...v10.48.19) - 2024-08-06