From 91d4fd55c0f21a939d0c265911a85650206d8a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcell=20F=C3=BCl=C3=B6p?= Date: Fri, 24 Jun 2022 19:28:46 +0000 Subject: [PATCH] :adhesive_bandage: Ensure stat finished before startSSLServer() The two chained stat() promises may not have finished by the time `enableSSL` is evaluated in case of a slow file system (e.g. on a Raspberry Pi where the only block device is an SD card). --- services/ssl-server.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/services/ssl-server.js b/services/ssl-server.js index 13695bd6..c63b7db3 100644 --- a/services/ssl-server.js +++ b/services/ssl-server.js @@ -24,21 +24,23 @@ const printSuccess = () => { // Check if the SSL certs are present and SSL should be enabled let enableSSL = false; -stat(httpsCerts.public).then(() => { - stat(httpsCerts.private).then(() => { +const checkCertificateFiles = stat(httpsCerts.public).then(() => { + return stat(httpsCerts.private).then(() => { enableSSL = true; }).catch(() => { printNotSoGood('Private key not present'); }); }).catch(() => { printNotSoGood('Public key not present'); }); const startSSLServer = (app) => { - // If SSL should be enabled, create a secured server and start it - if (enableSSL) { - const httpsServer = https.createServer({ - key: fs.readFileSync(httpsCerts.private), - cert: fs.readFileSync(httpsCerts.public), - }, app); - httpsServer.listen(SSLPort, () => { printSuccess(); }); - } + checkCertificateFiles.then(() => { + // If SSL should be enabled, create a secured server and start it + if (enableSSL) { + const httpsServer = https.createServer({ + key: fs.readFileSync(httpsCerts.private), + cert: fs.readFileSync(httpsCerts.public), + }, app); + httpsServer.listen(SSLPort, () => { printSuccess(); }); + } + }); }; const middleware = (req, res, next) => {