Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Stache/Stores/FormSubmissionsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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();
}
}
15 changes: 15 additions & 0 deletions tests/Stache/Stores/FormSubmissionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ 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->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()));
}

#[Test]
public function it_saves_to_disk()
{
Expand Down
Loading