From 5dd11b85dfca59f7abe4dc81aa0888f1f70d95cb Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 8 Apr 2026 17:05:28 +0100 Subject: [PATCH 1/2] Fix form submissions with non-UTF-8 data crashing the CP listing --- src/Stache/Stores/FormSubmissionsStore.php | 17 +++++++++++++++++ tests/Stache/Stores/FormSubmissionStoreTest.php | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Stache/Stores/FormSubmissionsStore.php b/src/Stache/Stores/FormSubmissionsStore.php index 41b82471bcf..9501732a79f 100644 --- a/src/Stache/Stores/FormSubmissionsStore.php +++ b/src/Stache/Stores/FormSubmissionsStore.php @@ -44,6 +44,8 @@ public function makeItemFromFile($path, $contents) Log::warning('Could not parse form submission file: '.$path); } + $data = $this->sanitizeData($data); + $form = pathinfo($path, PATHINFO_DIRNAME); $form = Str::after($form, $this->parent->directory()); @@ -56,4 +58,19 @@ public function makeItemFromFile($path, $contents) return $submission; } + + private function sanitizeData(array $data): array + { + return collect($data)->map(function ($value) { + if (is_array($value)) { + return $this->sanitizeData($value); + } + + if (is_string($value) && ! mb_check_encoding($value, 'UTF-8')) { + return mb_convert_encoding($value, 'UTF-8', 'UTF-8'); + } + + return $value; + })->all(); + } } diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php index 5dd883dfd0c..0c2c276d528 100644 --- a/tests/Stache/Stores/FormSubmissionStoreTest.php +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -44,6 +44,19 @@ public function it_makes_entry_instances_from_files() $this->assertTrue(Carbon::createFromFormat('Y-m-d H:i:s', '2021-09-08 06:46:31')->eq($item->date()->startOfSecond())); } + #[Test] + public function it_sanitizes_non_utf8_data() + { + $item = $this->parent->store('contact_form')->makeItemFromFile( + Path::tidy($this->directory).'/contact_form/1631083591.2832.yaml', + "name: 'Test User'\nmessage: !!binary dGVzdCBtZXNzYWdlIHdpdGggYmFkIGJ5dGVzOiDtoL3tsYk=" + ); + + $this->assertInstanceOf(Submission::class, $item); + $this->assertTrue(mb_check_encoding($item->get('message'), 'UTF-8')); + $this->assertNotNull(json_encode($item->data()->all())); + } + #[Test] public function it_saves_to_disk() { From a25ab2e65fece4040d4f18a43033eb453f60e296 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 8 Apr 2026 13:29:20 -0400 Subject: [PATCH 2/2] Add assertions for preserved content in non-UTF-8 sanitization test Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/Stache/Stores/FormSubmissionStoreTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Stache/Stores/FormSubmissionStoreTest.php b/tests/Stache/Stores/FormSubmissionStoreTest.php index 0c2c276d528..669e4d3175c 100644 --- a/tests/Stache/Stores/FormSubmissionStoreTest.php +++ b/tests/Stache/Stores/FormSubmissionStoreTest.php @@ -53,6 +53,8 @@ public function it_sanitizes_non_utf8_data() ); $this->assertInstanceOf(Submission::class, $item); + $this->assertEquals('Test User', $item->get('name')); + $this->assertStringContainsString('test message with bad bytes:', $item->get('message')); $this->assertTrue(mb_check_encoding($item->get('message'), 'UTF-8')); $this->assertNotNull(json_encode($item->data()->all())); }