From ea9bd29f25ce2ec2fb69a125588a043d1cc14daf Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Mon, 13 Jan 2025 16:43:14 +0530 Subject: [PATCH 1/3] fix: sonar issues in dynamic config --- src/util/dynamicConfig.js | 8 ++++---- src/v0/destinations/af/deleteUsers.js | 2 +- src/v0/destinations/airship/transform.js | 2 +- src/v0/destinations/clickup/util.js | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/util/dynamicConfig.js b/src/util/dynamicConfig.js index bdfebc87e87..76349e9794b 100644 --- a/src/util/dynamicConfig.js +++ b/src/util/dynamicConfig.js @@ -3,11 +3,10 @@ const get = require('get-value'); const unset = require('unset-value'); function getDynamicConfigValue(event, value) { - // this regex checks for pattern "only spaces {{ path || defaultvalue }} only spaces" . - // " {{message.traits.key || \"email\" }} " - // " {{ message.traits.key || 1233 }} " + // Improved regex for safe and efficient pattern matching const defFormat = - /^\s*{{\s*(?[A-Z_a-z]\w*(?:\.[A-Z_a-z]\w*)+)\s*\|\|\s*(?.*)\s*}}\s*$/; + /^\s*{{\s*(?[A-Z_a-z]\w*(?:\.[A-Z_a-z]\w*)+)\s*\|\|\s*(?[^{}]+?)\s*}}\s*$/; + const matResult = value.match(defFormat); if (matResult) { // Support "event.." alias for "message.." @@ -21,6 +20,7 @@ function getDynamicConfigValue(event, value) { } return value; } + /** var format2 = //; matResult = value.match(format2); if (matResult) { diff --git a/src/v0/destinations/af/deleteUsers.js b/src/v0/destinations/af/deleteUsers.js index e0da3699edd..ca956dc86fb 100644 --- a/src/v0/destinations/af/deleteUsers.js +++ b/src/v0/destinations/af/deleteUsers.js @@ -77,7 +77,7 @@ const userDeletionHandler = async (userAttributes, config) => { if (config.statusCallbackUrls) { const statusCallbackUrlsArray = config.statusCallbackUrls.split(','); const filteredStatusCallbackUrlsArray = statusCallbackUrlsArray.filter((statusCallbackUrl) => { - const URLRegex = /^(https?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w!#$&'()*+,/:;=?@[\]~-]+$/; + const URLRegex = /^(https?:\/\/)?[\w.-]+(\.[A-Za-z]{2,})+([#/?]\S*)?$/; return statusCallbackUrl.match(URLRegex); }); if (filteredStatusCallbackUrlsArray.length > 3) { diff --git a/src/v0/destinations/airship/transform.js b/src/v0/destinations/airship/transform.js index 18b7a87547f..50b5f309553 100644 --- a/src/v0/destinations/airship/transform.js +++ b/src/v0/destinations/airship/transform.js @@ -76,7 +76,7 @@ const identifyResponseBuilder = (message, { Config }) => { // attribute if (typeof traits[key] !== 'boolean') { const attribute = { action: 'set' }; - const keyMapped = RESERVED_TRAITS_MAPPING[key.toLowerCase()]; + const keyMapped = RESERVED_TRAITS_MAPPING[key] || RESERVED_TRAITS_MAPPING[key.toLowerCase()]; if (keyMapped) { attribute.key = keyMapped; } else { diff --git a/src/v0/destinations/clickup/util.js b/src/v0/destinations/clickup/util.js index 5da4192b5bb..9930954ecbf 100644 --- a/src/v0/destinations/clickup/util.js +++ b/src/v0/destinations/clickup/util.js @@ -56,7 +56,7 @@ const validateEmail = (email) => { * @param {*} url */ const validateUrl = (url) => { - const regex = /^(https?:\/\/)[\w.-]+(?:\.[\w.-]+)+[\w!#$&'()*+,/:;=?@[\]~-]+$/; + const regex = /^https?:\/\/[\w.-]+(\.[A-Za-z]{2,})+([#/?]\S*)?$/; if (!regex.test(url)) { throw new InstrumentationError('The provided url is invalid'); } From 9716513af4beb3fbce4a844e5783f7e54e51ea4c Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Mon, 13 Jan 2025 21:04:18 +0530 Subject: [PATCH 2/3] fix: sonar issues in dynamic config --- src/util/dynamicConfig.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/dynamicConfig.js b/src/util/dynamicConfig.js index 76349e9794b..ffc61d76ad3 100644 --- a/src/util/dynamicConfig.js +++ b/src/util/dynamicConfig.js @@ -3,9 +3,9 @@ const get = require('get-value'); const unset = require('unset-value'); function getDynamicConfigValue(event, value) { - // Improved regex for safe and efficient pattern matching + // Refined regex to ensure safety and avoid super-linear runtime const defFormat = - /^\s*{{\s*(?[A-Z_a-z]\w*(?:\.[A-Z_a-z]\w*)+)\s*\|\|\s*(?[^{}]+?)\s*}}\s*$/; + /^\s*{{\s*(?[A-Z_a-z]\w*(?:\.[A-Z_a-z]\w*)*)\s*\|\|\s*(?[^{|}]+)\s*}}\s*$/; const matResult = value.match(defFormat); if (matResult) { From 6b9d487377ecb6d91c26bf4e71b6fe14dd9633c4 Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Tue, 14 Jan 2025 16:15:06 +0530 Subject: [PATCH 3/3] fix: sonar issues in dynamic config --- src/util/dynamicConfig.js | 45 +++++++++++++++++++--------------- src/util/dynamicConfig.test.js | 2 +- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/util/dynamicConfig.js b/src/util/dynamicConfig.js index ffc61d76ad3..a189141e1cd 100644 --- a/src/util/dynamicConfig.js +++ b/src/util/dynamicConfig.js @@ -3,30 +3,35 @@ const get = require('get-value'); const unset = require('unset-value'); function getDynamicConfigValue(event, value) { - // Refined regex to ensure safety and avoid super-linear runtime - const defFormat = - /^\s*{{\s*(?[A-Z_a-z]\w*(?:\.[A-Z_a-z]\w*)*)\s*\|\|\s*(?[^{|}]+)\s*}}\s*$/; + // Check if the value contains the "{{ }}" pattern + value = value.trim(); + if (value.startsWith(`{{`) && value.endsWith(`}}`)) { + // Remove the surrounding "{{ }}" and trim spaces + const innerContent = value.slice(2, -2).trim(); - const matResult = value.match(defFormat); - if (matResult) { - // Support "event.." alias for "message.." - const fieldPath = matResult.groups.path.replace(/^event\.(.*)$/, 'message.$1'); - const pathVal = get(event, fieldPath); - if (pathVal) { - value = pathVal; - unset(event, fieldPath); - } else { - value = matResult.groups.defaultVal.replace(/"/g, '').trim(); + // Split the content by "||" to separate path and default value + const parts = innerContent.split('||').map((part) => part.trim()); + + // Ensure there are exactly two parts: path and default value + if (parts.length === 2) { + const [path, defaultVal] = parts; + + // Replace "event.." with "message.." + const fieldPath = path.startsWith('event.') ? path.replace(/^event\./, 'message.') : path; + + // Retrieve the value from the event object + const pathVal = get(event, fieldPath); + + // Use the path value if available, otherwise use the default value + if (pathVal) { + unset(event, fieldPath); // Remove the used path from the event object + return pathVal; + } + return defaultVal.replace(/"/g, '').trim(); // Clean up and use default value } - return value; } - /** var format2 = //; - matResult = value.match(format2); - if (matResult) { - - return value - } */ + // Return the value unchanged if no "{{ }}" pattern or invalid format return value; } diff --git a/src/util/dynamicConfig.test.js b/src/util/dynamicConfig.test.js index 523f31ed10c..c500fa213a4 100644 --- a/src/util/dynamicConfig.test.js +++ b/src/util/dynamicConfig.test.js @@ -6,7 +6,7 @@ const reqType = 'processor'; describe(`${funcName} Tests`, () => { const funcTestData = getFuncTestData(__dirname, `./testdata/${funcName}.json`); - test.each(funcTestData)('$description', async ({ description, input, output }) => { + test.each(funcTestData)('$description', async ({ input, output }) => { let result; if (Array.isArray(input)) { result = processDynamicConfig(...input);