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

[stable4.2] refactor: handle temporary files not being created gracefully #10792

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 @@ -68,7 +68,7 @@
// 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 @@ -106,8 +106,8 @@
// 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 @@ -166,7 +166,7 @@
}
}

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

$caBundle = [$this->certificateManager->getAbsoluteBundlePath()];
Expand Down Expand Up @@ -363,11 +363,11 @@
);
}

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

Check warning on line 366 in lib/Service/SmimeService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SmimeService.php#L366

Added line #L366 was not covered by tests
file_put_contents($decryptedCertificateFile, $decryptedCertificate);

$inPath = $this->tempManager->getTemporaryFile();
$outPath = $this->tempManager->getTemporaryFile();
$inPath = $this->getTemporaryFileOrThrow();
$outPath = $this->getTemporaryFileOrThrow();

Check warning on line 370 in lib/Service/SmimeService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SmimeService.php#L369-L370

Added lines #L369 - L370 were not covered by tests
file_put_contents($inPath, $part->toString([
'canonical' => true,
'headers' => true,
Expand Down Expand Up @@ -425,8 +425,8 @@
);
}

$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 @@ -581,8 +581,8 @@
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 @@ -610,4 +610,18 @@

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');

Check warning on line 622 in lib/Service/SmimeService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/SmimeService.php#L622

Added line #L622 was not covered by tests
}

return $file;
}
}