Allow CrowdSec configuration with any capitalizations in the name.

Ylian Saint-Hilaire committed Jul 8, 2022 at 11:28 UTC 3b16b51b08bf97f4868dde894fe34b3e61afee2b
3 files changed +48 -29
crowdsec.js
+33 -24
@@ -12,22 +12,31 @@ module.exports.CreateCrowdSecBouncer = function (parent, config) {
12 // Current captcha state
13 const currentCaptchaIpList = {};
14
15 - // Set the default values
16 - if (typeof config.userAgent != 'string') { config.userAgent = "CrowdSec Express-NodeJS bouncer/v0.0.1"; }
15 + // Set the default values. "config" will come in with lowercase names with everything, so we need to correct some value names.
16 + if (typeof config.useragent != 'string') { config.useragent = 'CrowdSec Express-NodeJS bouncer/v0.0.1'; }
17 if (typeof config.timeout != 'number') { config.timeout = 2000; }
18 - if ((typeof config.fallbackRemediation != 'string') || (["bypass", "captcha", "ban"].indexOf(config.fallbackRemediation) == -1)) { config.fallbackRemediation = BAN_REMEDIATION; }
19 - if (typeof config.maxRemediation != 'number') { config.maxRemediation = BAN_REMEDIATION; }
20 - if (typeof config.captchaGenerationCacheDuration != 'number') { config.captchaGenerationCacheDuration = 60 * 1000; }
21 - if (typeof config.captchaResolutionCacheDuration != 'number') { config.captchaResolutionCacheDuration = 30 * 60 * 1000; }
22 - if (typeof config.captchaTexts != 'object') { config.captchaTexts = {}; }
23 - if (typeof config.banTexts != 'object') { config.banTexts = {}; }
24 - if (typeof config.colors != 'object') { config.colors = {}; }
25 - if (typeof config.hideCrowdsecMentions != 'boolean') { config.hideCrowdsecMentions = false; }
26 - if (typeof config.customCss != 'string') { config.customCss = ''; }
18 + if ((typeof config.fallbackremediation != 'string') || (['bypass', 'captcha', 'ban'].indexOf(config.fallbackremediation) == -1)) { config.fallbackremediation = BAN_REMEDIATION; }
19 + if (typeof config.maxremediation != 'number') { config.maxremediation = BAN_REMEDIATION; }
20 + if (typeof config.captchagenerationcacheduration != 'number') { config.captchagenerationcacheduration = 60 * 1000; } // 60 seconds
21 + if (typeof config.captcharesolutioncacheduration != 'number') { config.captcharesolutioncacheduration = 30 * 60 * 1000; } // 30 minutes
22 + if (typeof config.captchatexts != 'object') { config.captchatexts = {}; } else {
23 + if (typeof config.captchatexts.tabtitle == 'string') { config.captchatexts.tabTitle = config.captchatexts.tabtitle; delete config.captchatexts.tabtitle; } // Fix "tabTitle" capitalization
24 + }
25 + if (typeof config.bantexts != 'object') { config.bantexts = {}; } else {
26 + if (typeof config.bantexts.tabtitle == 'string') { config.bantexts.tabTitle = config.bantexts.tabtitle; delete config.bantexts.tabtitle; } // Fix "tabTitle" capitalization
27 + }
28 + if (typeof config.colors != 'object') { config.colors = {}; } else {
29 + var colors = {};
30 + // All of the values in "text" and "background" sections happen to be lowercase, so, we can use the values as-is.
31 + if (typeof config.colors.text == 'object') { colors.text = config.colors.text; }
32 + if (typeof config.colors.background == 'object') { colors.background = config.colors.background; }
33 + config.colors = colors;
34 + }
35 + if (typeof config.hidecrowdsecmentions != 'boolean') { config.hidecrowdsecmentions = false; }
36 + if (typeof config.customcss != 'string') { delete config.customcss; }
37 if (typeof config.bypass != 'boolean') { config.bypass = false; }
28 - if (typeof config.trustedRangesForIpForwarding != 'object') { config.trustedRangesForIpForwarding = []; }
29 - if (typeof config.customLogger != 'object') { config.customLogger = null; }
30 - if (typeof config.bypassConnectionTest != 'boolean') { config.bypassConnectionTest = false; }
38 + if (typeof config.customlogger != 'object') { delete config.customlogger; }
39 + if (typeof config.bypassconnectiontest != 'boolean') { config.bypassconnectiontest = false; }
40
41 // Setup the logger
42 var logger = config.customLogger ? config.customLogger : getLogger();
@@ -35,16 +44,16 @@ module.exports.CreateCrowdSecBouncer = function (parent, config) {
44 // Configure the bouncer
45 configure({
46 url: config.url,
38 - apiKey: config.apiKey,
39 - userAgent: config.userAgent,
47 + apiKey: config.apikey,
48 + userAgent: config.useragent,
49 timeout: config.timeout,
41 - fallbackRemediation: config.fallbackRemediation,
42 - maxRemediation: config.maxRemediation,
43 - captchaTexts: config.captchaTexts,
44 - banTexts: config.banTexts,
50 + fallbackRemediation: config.fallbackremediation,
51 + maxRemediation: config.maxremediation,
52 + captchaTexts: config.captchatexts,
53 + banTexts: config.bantexts,
54 colors: config.colors,
46 - hideCrowdsecMentions: config.hideCrowdsecMentions,
47 - customCss: config.customCss
55 + hideCrowdsecMentions: config.hidecrowdsecmentions,
56 + customCss: config.customcss
57 });
58
59 // Test connectivity
@@ -53,7 +62,7 @@ module.exports.CreateCrowdSecBouncer = function (parent, config) {
62 // Process a web request
63 obj.process = async function (domain, req, res, next) {
64 try {
56 - var remediation = config.fallbackRemediation;
65 + var remediation = config.fallbackremediation;
66 try { remediation = await getRemediationForIp(req.clientIp); } catch (ex) { }
67 //console.log('CrowdSec', req.clientIp, remediation, req.url);
68 switch (remediation) {
@@ -75,7 +84,7 @@ module.exports.CreateCrowdSecBouncer = function (parent, config) {
84
85 // Process a captcha request
86 obj.applyCaptcha = async function (req, res, next) {
78 - await applyCaptchaEx(req.clientIp, req, res, next, config.captchaGenerationCacheDuration, config.captchaResolutionCacheDuration, logger);
87 + await applyCaptchaEx(req.clientIp, req, res, next, config.captchagenerationcacheduration, config.captcharesolutioncacheduration, logger);
88 }
89
90 // Process a captcha request
meshcentral.js
+3 -3
@@ -698,7 +698,7 @@ function CreateMeshCentralServer(config, args) {
698 obj.args = args = config2.settings;
699
700 // Lower case all keys in the config file
701 - obj.common.objKeysToLower(config2, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders', 'crowdsec']);
701 + obj.common.objKeysToLower(config2, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders']);
702
703 // Grad some of the values from the original config.json file if present.
704 if ((config.settings.vault != null) && (config2.settings != null)) { config2.settings.vault = config.settings.vault; }
@@ -1196,7 +1196,7 @@ function CreateMeshCentralServer(config, args) {
1196 for (i in args) { config2.settings[i] = args[i]; }
1197
1198 // Lower case all keys in the config file
1199 - common.objKeysToLower(config2, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders', 'crowdsec']);
1199 + common.objKeysToLower(config2, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders']);
1200
1201 // Grad some of the values from the original config.json file if present.
1202 config2['mysql'] = config['mysql'];
@@ -3518,7 +3518,7 @@ function getConfig(createSampleConfig) {
3518
3519 // Lower case all keys in the config file
3520 try {
3521 - require('./common.js').objKeysToLower(config, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders', 'crowdsec']);
3521 + require('./common.js').objKeysToLower(config, ['ldapoptions', 'defaultuserwebstate', 'forceduserwebstate', 'httpheaders']);
3522 } catch (ex) {
3523 console.log('CRITICAL ERROR: Unable to access the file \"./common.js\".\r\nCheck folder & file permissions.');
3524 process.exit();
package.json
+12 -2
@@ -37,6 +37,8 @@
37 "sample-config-advanced.json"
38 ],
39 "dependencies": {
40 + "@crowdsec/express-bouncer": "^0.1.0",
41 + "@yetzt/nedb": "^1.8.0",
42 "archiver": "^5.3.1",
43 "body-parser": "^1.19.0",
44 "cbor": "~5.2.0",
@@ -45,13 +47,21 @@
47 "express": "^4.17.0",
48 "express-handlebars": "^5.3.5",
49 "express-ws": "^4.0.0",
50 + "image-size": "^1.0.1",
51 "ipcheck": "^0.1.0",
52 + "loadavg-windows": "^1.1.1",
53 "minimist": "^1.2.5",
54 "multiparty": "^4.2.1",
51 - "@yetzt/nedb": "^1.8.0",
55 "node-forge": "^1.0.0",
56 + "node-windows": "^0.1.4",
57 + "otplib": "^10.2.3",
58 + "pg": "^8.7.1",
59 + "pgtools": "^0.3.2",
60 + "ssh2": "^1.11.0",
61 + "web-push": "^3.5.0",
62 "ws": "^5.2.3",
54 - "yauzl": "^2.10.0"
63 + "yauzl": "^2.10.0",
64 + "yubikeyotp": "^0.2.0"
65 },
66 "engines": {
67 "node": ">=10.0.0"