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
7 changes: 7 additions & 0 deletions src/Laravel/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ public function decryptMessage(string $secureMessageId, string $verificationCode
$record->save();
}

// Wipe the keys before the secure message is handed to event listeners. Most failure paths
// already wipe the keys (the DecryptException constructor does so when it is given the secure
// message), but the paths that throw without it - a missing key file, a missing file blob or
// malformed stored ciphertext - would otherwise expose the decrypted keys on this instance to
// listeners (and to anything they serialize the event to, such as a queue).
$secureMessage->wipeKeysFromMemory();

// Dispatch events.
match ($exception::class) {
HitPointLimitReachedException::class => $this->event->dispatch(new HitPointLimitReached($secureMessage)),
Expand Down
14 changes: 14 additions & 0 deletions tests/Laravel/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ public function testDecryptMessageStorageKeyNotFound(): void
$secureMessageFactoryMock->shouldReceive('setMetaKey')->withArgs(['metaKey'])->once()->andReturnSelf();

$eventMock->shouldReceive('dispatch')->withArgs([\Mockery::on(function ($event) {
// The event must not expose any key material to listeners, even though this failure path
// throws without a secure message (so the DecryptException constructor never wiped it).
$this->assertNull($event->secureMessage->getDatabaseKey());
$this->assertNull($event->secureMessage->getStorageKey());
$this->assertNull($event->secureMessage->getMetaKey());
$this->assertNull($event->secureMessage->getVerificationCode());

return $event::class === DecryptionFailed::class;
})])->once();

Expand Down Expand Up @@ -489,6 +496,13 @@ public function testDecryptFileMessageBlobMissing(): void
$secureMessageFactoryMock->shouldReceive('setMetaKey')->withArgs(['metaKey'])->once()->andReturnSelf();

$eventMock->shouldReceive('dispatch')->withArgs([\Mockery::on(function ($event) {
// The event must not expose any key material to listeners, even though this failure path
// throws without a secure message (so the DecryptException constructor never wiped it).
$this->assertNull($event->secureMessage->getDatabaseKey());
$this->assertNull($event->secureMessage->getStorageKey());
$this->assertNull($event->secureMessage->getMetaKey());
$this->assertNull($event->secureMessage->getVerificationCode());

return $event::class === DecryptionFailed::class;
})])->once();

Expand Down