Skip to content

Commit 2c8b3ec

Browse files
committed
refactor: handle temporary files not being created gracefully
Signed-off-by: Richard Steinmetz <[email protected]>
1 parent a325a66 commit 2c8b3ec

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

lib/Service/SmimeService.php

+25-11
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function verifyMessage(string $message): bool {
6868
// smime/pkcs7 module. Unfortunately, it is only supported since php 8.
6969
// Ref https://www.php.net/manual/en/function.openssl-cms-verify.php
7070

71-
$messageTemp = $this->tempManager->getTemporaryFile();
71+
$messageTemp = $this->getTemporaryFileOrThrow();
7272
$messageTempHandle = fopen($messageTemp, 'wb');
7373
fwrite($messageTempHandle, $message);
7474
fclose($messageTempHandle);
@@ -106,8 +106,8 @@ public function extractSignedContent(string $message): string {
106106
// smime/pkcs7 module. Unfortunately, it is only supported since php 8.
107107
// Ref https://www.php.net/manual/en/function.openssl-cms-verify.php
108108

109-
$verifiedContentTemp = $this->tempManager->getTemporaryFile();
110-
$messageTemp = $this->tempManager->getTemporaryFile();
109+
$verifiedContentTemp = $this->getTemporaryFileOrThrow();
110+
$messageTemp = $this->getTemporaryFileOrThrow();
111111
$messageTempHandle = fopen($messageTemp, 'wb');
112112
fwrite($messageTempHandle, $message);
113113
fclose($messageTempHandle);
@@ -166,7 +166,7 @@ public function parseCertificate(string $certificate): SmimeCertificateInfo {
166166
}
167167
}
168168

169-
$decryptedCertificateFile = $this->tempManager->getTemporaryFile();
169+
$decryptedCertificateFile = $this->getTemporaryFileOrThrow();
170170
file_put_contents($decryptedCertificateFile, $certificate);
171171

172172
$caBundle = [$this->certificateManager->getAbsoluteBundlePath()];
@@ -363,11 +363,11 @@ public function signMimePart(Horde_Mime_Part $part,
363363
);
364364
}
365365

366-
$decryptedCertificateFile = $this->tempManager->getTemporaryFile();
366+
$decryptedCertificateFile = $this->getTemporaryFileOrThrow();
367367
file_put_contents($decryptedCertificateFile, $decryptedCertificate);
368368

369-
$inPath = $this->tempManager->getTemporaryFile();
370-
$outPath = $this->tempManager->getTemporaryFile();
369+
$inPath = $this->getTemporaryFileOrThrow();
370+
$outPath = $this->getTemporaryFileOrThrow();
371371
file_put_contents($inPath, $part->toString([
372372
'canonical' => true,
373373
'headers' => true,
@@ -425,8 +425,8 @@ public function decryptMimePartText(string $mimePartText,
425425
);
426426
}
427427

428-
$inPath = $this->tempManager->getTemporaryFile();
429-
$outPath = $this->tempManager->getTemporaryFile();
428+
$inPath = $this->getTemporaryFileOrThrow();
429+
$outPath = $this->getTemporaryFileOrThrow();
430430
file_put_contents($inPath, $mimePartText);
431431
if (!openssl_pkcs7_decrypt($inPath, $outPath, $decryptedCertificate, $decryptedKey)) {
432432
throw new SmimeDecryptException('Failed to decrypt MIME part text');
@@ -581,8 +581,8 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Hor
581581
throw new ServiceException('Failed to decrypt certificate: ' . $e->getMessage(), 0, $e);
582582
}
583583

584-
$inPath = $this->tempManager->getTemporaryFile();
585-
$outPath = $this->tempManager->getTemporaryFile();
584+
$inPath = $this->getTemporaryFileOrThrow();
585+
$outPath = $this->getTemporaryFileOrThrow();
586586
file_put_contents($inPath, $part->toString([
587587
'canonical' => true,
588588
'headers' => true,
@@ -610,4 +610,18 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Hor
610610

611611
return $parsedPart;
612612
}
613+
614+
/**
615+
* Create a temporary file and return the path or throw if it could not be created.
616+
*
617+
* @throws ServiceException If the temporary file could not be created
618+
*/
619+
private function getTemporaryFileOrThrow(): string {
620+
$file = $this->tempManager->getTemporaryFile();
621+
if ($file === false) {
622+
throw new ServiceException('Failed to create temporary file');
623+
}
624+
625+
return $file;
626+
}
613627
}

0 commit comments

Comments
 (0)