diff --git a/mailpoet/lib/Automation/Engine/Data/WorkflowRunLog.php b/mailpoet/lib/Automation/Engine/Data/WorkflowRunLog.php index 93f35b717f..7a3d680171 100644 --- a/mailpoet/lib/Automation/Engine/Data/WorkflowRunLog.php +++ b/mailpoet/lib/Automation/Engine/Data/WorkflowRunLog.php @@ -3,6 +3,9 @@ namespace MailPoet\Automation\Engine\Data; use DateTimeImmutable; +use InvalidArgumentException; +use MailPoet\Automation\Engine\Exceptions\InvalidStateException; +use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; use MailPoet\Automation\Engine\Utils\Json; use Throwable; @@ -102,6 +105,17 @@ class WorkflowRunLog { * @return void */ public function setData(string $key, $value): void { + try { + $newData = $this->getData(); + $newData[$key] = $value; + $encoded = Json::encode($newData); + $decoded = Json::decode($encoded); + if ($decoded !== $newData) { + throw new InvalidArgumentException('$value must be serializable'); + } + } catch (InvalidStateException | InvalidArgumentException | UnexpectedValueException $e) { + throw new InvalidArgumentException("Invalid data provided for key $key."); + } $this->data[$key] = $value; } diff --git a/mailpoet/tests/integration/Automation/Engine/Data/WorkflowRunLogTest.php b/mailpoet/tests/integration/Automation/Engine/Data/WorkflowRunLogTest.php index 7adbf6333f..5f80b7d9ee 100644 --- a/mailpoet/tests/integration/Automation/Engine/Data/WorkflowRunLogTest.php +++ b/mailpoet/tests/integration/Automation/Engine/Data/WorkflowRunLogTest.php @@ -58,6 +58,16 @@ class WorkflowRunLogTest extends \MailPoetTest { $this->assertSame('value', $data['key']); } + public function testItDoesNotAllowSettingDataThatCannotBeSaved(): void { + $log = new WorkflowRunLog(1, 'step-id', []); + $badData = [ + function() { echo 'closures cannot be serialized'; } + ]; + $this->expectException(\InvalidArgumentException::class); + $log->setData('badData', $badData); + expect($log->getData())->count(0); + } + public function testItGetsExposedViaAction(): void { $this->wp->addAction(Hooks::WORKFLOW_RUN_LOG_AFTER_STEP_RUN, function(WorkflowRunLog $log) { $log->setData('test', 'value');