Skip to content

Commit

Permalink
Merge pull request #38091 from nextcloud/backport/35092/stable26
Browse files Browse the repository at this point in the history
[stable26] Check return value and improve error handling on certificate manager
  • Loading branch information
blizzz authored May 17, 2023
2 parents 4bd2f33 + 22f02a4 commit 7c6b47d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/private/Security/CertificateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public function createCertificateBundle(): void {
$tmpPath = $certPath . '.tmp' . $this->random->generate(10, ISecureRandom::CHAR_DIGITS);
$fhCerts = $this->view->fopen($tmpPath, 'w');

if (!is_resource($fhCerts)) {
throw new \RuntimeException('Unable to open file handler to create certificate bundle "' . $tmpPath . '".');
}

// Write user certificates
foreach ($certs as $cert) {
$file = $path . '/uploads/' . $cert->getName();
Expand Down Expand Up @@ -238,7 +242,7 @@ public function getCertificateBundle(): string {
*/
public function getAbsoluteBundlePath(): string {
try {
if (!$this->bundlePath) {
if ($this->bundlePath === null) {
if (!$this->hasCertificates()) {
$this->bundlePath = \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
Expand All @@ -247,10 +251,16 @@ public function getAbsoluteBundlePath(): string {
$this->createCertificateBundle();
}

$this->bundlePath = $this->view->getLocalFile($this->getCertificateBundle());
$certificateBundle = $this->getCertificateBundle();
$this->bundlePath = $this->view->getLocalFile($certificateBundle) ?: null;

if ($this->bundlePath === null) {
throw new \RuntimeException('Unable to get certificate bundle "' . $certificateBundle . '".');
}
}
return $this->bundlePath;
} catch (\Exception $e) {
$this->logger->error('Failed to get absolute bundle path. Fallback to default ca-bundle.crt', ['exception' => $e]);
return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
}
Expand Down

0 comments on commit 7c6b47d

Please sign in to comment.