From 586668c3199640c36aad6f39aa4d4f59e65020fd Mon Sep 17 00:00:00 2001 From: b-ma Date: Mon, 19 Feb 2024 17:03:31 +0100 Subject: [PATCH] fix: allow { cert: null, key: null } for self-signed https config, fix #84 --- src/server/Server.js | 18 +++++++++++------- tests/essentials/Server.spec.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/server/Server.js b/src/server/Server.js index 4711d59d..21e58916 100644 --- a/src/server/Server.js +++ b/src/server/Server.js @@ -9,6 +9,7 @@ import { isPlainObject, idGenerator, getTime } from '@ircam/sc-utils'; import chalk from 'chalk'; import compression from 'compression'; import express from 'express'; +import equal from 'fast-deep-equal'; import Keyv from 'keyv'; import { KeyvFile } from 'keyv-file'; import merge from 'lodash/merge.js'; @@ -214,11 +215,11 @@ class Server { throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, should contain both "cert" and "key" entries`); } // @todo - move that to constructor - if (!fs.existsSync(httpsInfos.cert)) { + if (httpsInfos.cert !== null && !fs.existsSync(httpsInfos.cert)) { throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, "cert" file not found`); } - if (!fs.existsSync(httpsInfos.key)) { + if (httpsInfos.key !== null && !fs.existsSync(httpsInfos.key)) { throw new Error(`[soundworks:Server] Invalid "env.httpsInfos" config, "key" file not found`); } } @@ -444,19 +445,22 @@ class Server { this.router.use(soundworksAuth); } - // start http server - const useHttps = this.config.env.useHttps || false; - // ------------------------------------------------------------ // create HTTP(S) SERVER // ------------------------------------------------------------ + const useHttps = this.config.env.useHttps || false; + if (!useHttps) { this.httpServer = http.createServer(this.router); } else { const httpsInfos = this.config.env.httpsInfos; + let useSelfSigned = false; + + if (!httpsInfos || equal(httpsInfos, { cert: null, key: null })) { + useSelfSigned = true; + } - // if certs have been given in config - if (httpsInfos !== null) { + if (!useSelfSigned) { try { // existance of file is checked in contructor let cert = fs.readFileSync(httpsInfos.cert); diff --git a/tests/essentials/Server.spec.js b/tests/essentials/Server.spec.js index 6d50524f..2f8e5d17 100644 --- a/tests/essentials/Server.spec.js +++ b/tests/essentials/Server.spec.js @@ -210,6 +210,18 @@ describe('# server::Server', () => { if (!errored) { assert.fail('should have thrown'); } }); + it(`should use self-signed certificates if both cert and key file are null`, async () => { + const selfSignedConfig = merge({}, config); + selfSignedConfig.env.useHttps = true; + selfSignedConfig.env.httpsInfos = { + cert: null, + key: null, // this is invalid + }; + + const server = new Server(selfSignedConfig); + await server.init(); + }); + it(`should store self-signed certificated in db`, async () => { // these test crash the CI for some reason,just ignore them in CI too const envPathname = path.join(__dirname, '.env'); @@ -421,6 +433,7 @@ describe('# server::Server', () => { assert.isOk('server and process should stop'); }); + // this will stop after a timeout it('should stop the server even if a client is connected', async() => { const server = new Server(config);