Skip to content

Commit 7ec09e7

Browse files
TophrC-ddBridgeAR
andauthored
fix: AWS Payload Tagging no longer breaks with bad JSONPath (#6358)
Co-authored-by: Ruben Bridgewater <ruben.bridgewater@datadoghq.com>
1 parent a5fcf17 commit 7ec09e7

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

packages/dd-trace/src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function validateNamingVersion (versionString) {
187187
* @param {string | string[]} input
188188
*/
189189
function splitJSONPathRules (input) {
190-
if (!input) return
190+
if (!input || input === '$') return
191191
if (Array.isArray(input)) return input
192192
if (input === 'all') return []
193193
return input.split(',')

packages/dd-trace/src/payload-tagging/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ function maybeJSONParseValue (value) {
3838
function expand (object, expansionRules) {
3939
for (const rule of expansionRules) {
4040
jsonpath(rule, object, (value, _type, desc) => {
41-
desc.parent[desc.parentProperty] = maybeJSONParseValue(value)
41+
if (desc.parent && desc.parentProperty) {
42+
desc.parent[desc.parentProperty] = maybeJSONParseValue(value)
43+
}
4244
})
4345
}
4446
}
@@ -52,7 +54,9 @@ function expand (object, expansionRules) {
5254
function redact (object, redactionRules) {
5355
for (const rule of redactionRules) {
5456
jsonpath(rule, object, (_value, _type, desc) => {
55-
desc.parent[desc.parentProperty] = 'redacted'
57+
if (desc.parent && desc.parentProperty) {
58+
desc.parent[desc.parentProperty] = 'redacted'
59+
}
5660
})
5761
}
5862
}

packages/dd-trace/test/payload-tagging/index.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,40 @@ describe('Tagging orchestration', () => {
199199
assert.strictEqual(tags[`${PAYLOAD_TAG_RESPONSE_PREFIX}.request`], 'foo')
200200
})
201201

202+
it('should not fail if the response config contains invalid config', () => {
203+
const config = {
204+
request: ['invalid,request'],
205+
response: ['invalid,$.foo,$.response'],
206+
expand: []
207+
}
208+
const input = {
209+
request: 'foo',
210+
response: 'bar',
211+
}
212+
const tags = computeTags(config, input, { maxDepth: 10, prefix: PAYLOAD_TAG_RESPONSE_PREFIX })
213+
assert.strictEqual(tags[`${PAYLOAD_TAG_RESPONSE_PREFIX}.response`], 'redacted')
214+
assert.strictEqual(tags[`${PAYLOAD_TAG_RESPONSE_PREFIX}.request`], 'foo')
215+
})
216+
217+
it('should apply expansion rules with dollar identical to an empty input', () => {
218+
const config = {
219+
request: ['$'],
220+
response: ['$'],
221+
expand: ['$.request', '$.response', '$.invalid'],
222+
}
223+
const input = {
224+
request: '{ "foo": "bar" }',
225+
response: '{ "baz": "quux" }',
226+
invalid: '{ invalid JSON }',
227+
untargeted: '{ "foo": "bar" }',
228+
}
229+
const tags = computeTags(config, input, { maxDepth: 10, prefix: 'foo' })
230+
assert.strictEqual(tags['foo.request.foo'], 'bar')
231+
assert.strictEqual(tags['foo.response.baz'], 'quux')
232+
assert.strictEqual(tags['foo.invalid'], '{ invalid JSON }')
233+
assert.strictEqual(tags['foo.untargeted'], '{ "foo": "bar" }')
234+
})
235+
202236
it('should apply expansion rules', () => {
203237
const config = {
204238
request: [],

0 commit comments

Comments
 (0)