Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable3.7] refactor: handle temporary files not being created gracefully #10793

Merged
merged 1 commit into from
Mar 5, 2025
Merged
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
36 changes: 25 additions & 11 deletions lib/Service/SmimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function verifyMessage(string $message): bool {
// smime/pkcs7 module. Unfortunately, it is only supported since php 8.
// Ref https://www.php.net/manual/en/function.openssl-cms-verify.php

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

$verifiedContentTemp = $this->tempManager->getTemporaryFile();
$messageTemp = $this->tempManager->getTemporaryFile();
$verifiedContentTemp = $this->getTemporaryFileOrThrow();
$messageTemp = $this->getTemporaryFileOrThrow();
$messageTempHandle = fopen($messageTemp, 'wb');
fwrite($messageTempHandle, $message);
fclose($messageTempHandle);
Expand Down Expand Up @@ -182,7 +182,7 @@ public function parseCertificate(string $certificate): SmimeCertificateInfo {
}
}

$decryptedCertificateFile = $this->tempManager->getTemporaryFile();
$decryptedCertificateFile = $this->getTemporaryFileOrThrow();
file_put_contents($decryptedCertificateFile, $certificate);

$caBundle = [$this->certificateManager->getAbsoluteBundlePath()];
Expand Down Expand Up @@ -379,11 +379,11 @@ public function signMimePart(Horde_Mime_Part $part,
);
}

$decryptedCertificateFile = $this->tempManager->getTemporaryFile();
$decryptedCertificateFile = $this->getTemporaryFileOrThrow();
file_put_contents($decryptedCertificateFile, $decryptedCertificate);

$inPath = $this->tempManager->getTemporaryFile();
$outPath = $this->tempManager->getTemporaryFile();
$inPath = $this->getTemporaryFileOrThrow();
$outPath = $this->getTemporaryFileOrThrow();
file_put_contents($inPath, $part->toString([
'canonical' => true,
'headers' => true,
Expand Down Expand Up @@ -441,8 +441,8 @@ public function decryptMimePartText(string $mimePartText,
);
}

$inPath = $this->tempManager->getTemporaryFile();
$outPath = $this->tempManager->getTemporaryFile();
$inPath = $this->getTemporaryFileOrThrow();
$outPath = $this->getTemporaryFileOrThrow();
file_put_contents($inPath, $mimePartText);
if (!openssl_pkcs7_decrypt($inPath, $outPath, $decryptedCertificate, $decryptedKey)) {
throw new SmimeDecryptException('Failed to decrypt MIME part text');
Expand Down Expand Up @@ -597,8 +597,8 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Ho
throw new ServiceException('Failed to decrypt certificate: ' . $e->getMessage(), 0, $e);
}

$inPath = $this->tempManager->getTemporaryFile();
$outPath = $this->tempManager->getTemporaryFile();
$inPath = $this->getTemporaryFileOrThrow();
$outPath = $this->getTemporaryFileOrThrow();
file_put_contents($inPath, $part->toString([
'canonical' => true,
'headers' => true,
Expand Down Expand Up @@ -626,4 +626,18 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Ho

return $parsedPart;
}

/**
* Create a temporary file and return the path or throw if it could not be created.
*
* @throws ServiceException If the temporary file could not be created
*/
private function getTemporaryFileOrThrow(): string {
$file = $this->tempManager->getTemporaryFile();
if ($file === false) {
throw new ServiceException('Failed to create temporary file');
}

return $file;
}
}