Skip to content

Commit 8d7357a

Browse files
duncanmccleanclaudejasonvarga
authored
[6.x] Fix form submissions with non-UTF-8 data crashing the CP listing (#14461)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent 02c401a commit 8d7357a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Stache/Stores/FormSubmissionsStore.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function makeItemFromFile($path, $contents)
4444
Log::warning('Could not parse form submission file: '.$path);
4545
}
4646

47+
$data = $this->sanitizeData($data);
48+
4749
$form = pathinfo($path, PATHINFO_DIRNAME);
4850
$form = Str::after($form, $this->parent->directory());
4951

@@ -56,4 +58,19 @@ public function makeItemFromFile($path, $contents)
5658

5759
return $submission;
5860
}
61+
62+
private function sanitizeData(array $data): array
63+
{
64+
return collect($data)->map(function ($value) {
65+
if (is_array($value)) {
66+
return $this->sanitizeData($value);
67+
}
68+
69+
if (is_string($value) && ! mb_check_encoding($value, 'UTF-8')) {
70+
return mb_convert_encoding($value, 'UTF-8', 'UTF-8');
71+
}
72+
73+
return $value;
74+
})->all();
75+
}
5976
}

tests/Stache/Stores/FormSubmissionStoreTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public function it_makes_entry_instances_from_files()
4444
$this->assertTrue(Carbon::createFromFormat('Y-m-d H:i:s', '2021-09-08 06:46:31')->eq($item->date()->startOfSecond()));
4545
}
4646

47+
#[Test]
48+
public function it_sanitizes_non_utf8_data()
49+
{
50+
$item = $this->parent->store('contact_form')->makeItemFromFile(
51+
Path::tidy($this->directory).'/contact_form/1631083591.2832.yaml',
52+
"name: 'Test User'\nmessage: !!binary dGVzdCBtZXNzYWdlIHdpdGggYmFkIGJ5dGVzOiDtoL3tsYk="
53+
);
54+
55+
$this->assertInstanceOf(Submission::class, $item);
56+
$this->assertEquals('Test User', $item->get('name'));
57+
$this->assertStringContainsString('test message with bad bytes:', $item->get('message'));
58+
$this->assertTrue(mb_check_encoding($item->get('message'), 'UTF-8'));
59+
$this->assertNotNull(json_encode($item->data()->all()));
60+
}
61+
4762
#[Test]
4863
public function it_saves_to_disk()
4964
{

0 commit comments

Comments
 (0)