From 7dfdc7209ec77c8a249d1d014f537bf3ee8d5dc5 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 27 Apr 2024 06:45:16 +0200 Subject: [PATCH] Errors that occurred while adding SNI context to a server are now ignored. --- svr.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/svr.js b/svr.js index 7b28b07..4b84b09 100644 --- a/svr.js +++ b/svr.js @@ -2177,19 +2177,23 @@ if (!cluster.isPrimary) { } } if (secure) { - sniCredentials.forEach(function (sniCredentialsSingle) { - server.addContext(sniCredentialsSingle.name, { - cert: sniCredentialsSingle.cert, - key: sniCredentialsSingle.key + try { + sniCredentials.forEach(function (sniCredentialsSingle) { + server.addContext(sniCredentialsSingle.name, { + cert: sniCredentialsSingle.cert, + key: sniCredentialsSingle.key + }); + try { + var snMatches = sniCredentialsSingle.name.match(/^([^:[]*|\[[^]]*\]?)((?::.*)?)$/); + if (!snMatches[1][0].match(/^\.+$/)) snMatches[1][0] = snMatches[1][0].replace(/\.+$/, ""); + server._contexts[server._contexts.length - 1][0] = new RegExp("^" + snMatches[1].replace(/([.^$+?\-\\[\]{}])/g, "\\$1").replace(/\*/g, "[^.:]*") + ((snMatches[1][0] == "[" || snMatches[1].match(/^(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/)) ? "" : "\.?") + snMatches[2].replace(/([.^$+?\-\\[\]{}])/g, "\\$1").replace(/\*/g, "[^.]*") + "$", "i"); + } catch (ex) { + // Can't replace regex, ignoring... + } }); - try { - var snMatches = sniCredentialsSingle.name.match(/^([^:[]*|\[[^]]*\]?)((?::.*)?)$/); - if (!snMatches[1][0].match(/^\.+$/)) snMatches[1][0] = snMatches[1][0].replace(/\.+$/, ""); - server._contexts[server._contexts.length - 1][0] = new RegExp("^" + snMatches[1].replace(/([.^$+?\-\\[\]{}])/g, "\\$1").replace(/\*/g, "[^.:]*") + ((snMatches[1][0] == "[" || snMatches[1].match(/^(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/)) ? "" : "\.?") + snMatches[2].replace(/([.^$+?\-\\[\]{}])/g, "\\$1").replace(/\*/g, "[^.]*") + "$", "i"); - } catch (ex) { - // Can't replace regex, ignoring... - } - }); + } catch (err) { + // SNI error + } } server.on("request", reqhandler); server.on("checkExpectation", reqhandler);