Skip to content

Commit

Permalink
chore: Replaced backtracking regex with new algorithm (#2887)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr authored Jan 16, 2025
1 parent acdc034 commit 46462d0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/header-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions test/unit/header-attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 46462d0

Please sign in to comment.