Skip to content

Commit 46462d0

Browse files
authored
chore: Replaced backtracking regex with new algorithm (#2887)
1 parent acdc034 commit 46462d0

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/header-attributes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ function _headerToCamelCase(header) {
9595
const newHeader = header.charAt(0).toLowerCase() + header.slice(1)
9696

9797
// Converts headers in the form 'header-name' to be in the form 'headerName'
98-
// eslint-disable-next-line sonarjs/slow-regex
99-
return newHeader.replace(/[\W_]+(\w)/g, function capitalize(m, $1) {
100-
return $1.toUpperCase()
101-
})
98+
return newHeader.split(/[\W_]/).map((ele, i) => {
99+
if (i === 0) return ele
100+
return ele.slice(0, 1).toUpperCase() + ele.slice(1)
101+
}).join('')
102102
}
103103

104104
function _collectHeaders(headers, nameMap, prefix, transaction) {

test/unit/header-attributes.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,23 @@ test('#collectRequestHeaders', async (t) => {
7777
})
7878
})
7979

80+
await t.test('should replace repeating non-word characters', (t, end) => {
81+
const { agent } = t.nr
82+
agent.config.allow_all_headers = true
83+
const headers = {
84+
'foo-bar--baz': 'valid-type'
85+
}
86+
87+
helper.runInTransaction(agent, (transaction) => {
88+
headerAttributes.collectRequestHeaders(headers, transaction)
89+
90+
const attributes = transaction.trace.attributes.get(DESTINATIONS.TRANS_COMMON)
91+
assert.equal(attributes['request.headers.fooBarBaz'], 'valid-type')
92+
assert.equal(attributes['foo-bar--baz'], undefined)
93+
end()
94+
})
95+
})
96+
8097
await t.test('should lowercase first letter in headers', (t, end) => {
8198
const { agent } = t.nr
8299
const headers = {

0 commit comments

Comments
 (0)