From 4d14f2a1f6c9faa0bd7540eb66d1ef5f5a4e59fc Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 16 Jan 2025 09:52:34 -0500 Subject: [PATCH] chore: Replaced backtracking regex with new algorithm --- lib/header-attributes.js | 8 ++++---- test/unit/header-attributes.test.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/header-attributes.js b/lib/header-attributes.js index 9a93a57908..9a76d3c362 100644 --- a/lib/header-attributes.js +++ b/lib/header-attributes.js @@ -95,10 +95,10 @@ function _headerToCamelCase(header) { const newHeader = header.charAt(0).toLowerCase() + header.slice(1) // Converts headers in the form 'header-name' to be in the form 'headerName' - // eslint-disable-next-line sonarjs/slow-regex - return newHeader.replace(/[\W_]+(\w)/g, function capitalize(m, $1) { - return $1.toUpperCase() - }) + return newHeader.split(/[\W_]/).map((ele, i) => { + if (i === 0) return ele + return ele.slice(0, 1).toUpperCase() + ele.slice(1) + }).join('') } function _collectHeaders(headers, nameMap, prefix, transaction) { diff --git a/test/unit/header-attributes.test.js b/test/unit/header-attributes.test.js index 3d383b672f..04c5016057 100644 --- a/test/unit/header-attributes.test.js +++ b/test/unit/header-attributes.test.js @@ -77,6 +77,23 @@ test('#collectRequestHeaders', async (t) => { }) }) + await t.test('should replace repeating non-word characters', (t, end) => { + const { agent } = t.nr + agent.config.allow_all_headers = true + const headers = { + 'foo-bar--baz': 'valid-type' + } + + helper.runInTransaction(agent, (transaction) => { + headerAttributes.collectRequestHeaders(headers, transaction) + + const attributes = transaction.trace.attributes.get(DESTINATIONS.TRANS_COMMON) + assert.equal(attributes['request.headers.fooBarBaz'], 'valid-type') + assert.equal(attributes['foo-bar--baz'], undefined) + end() + }) + }) + await t.test('should lowercase first letter in headers', (t, end) => { const { agent } = t.nr const headers = {