You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Create obfuscator for harvest metadata (referrer URL, etc.)
26
+
// No event type specified - applies to all harvests regardless of feature
27
+
this.obfuscator=newObfuscator(agentRef)
28
+
24
29
subscribeToEOL(()=>{// do one last harvest round or check
25
30
this.initializedAggregates.forEach(aggregateInst=>{// let all features wrap up things needed to do before ANY harvest in case there's last minute cross-feature data dependencies
Copy file name to clipboardExpand all lines: src/common/util/obfuscate.js
+154-3Lines changed: 154 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/**
2
-
* Copyright 2020-2025 New Relic, Inc. All rights reserved.
2
+
* Copyright 2020-2026 New Relic, Inc. All rights reserved.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
import{isFileProtocol}from'../url/protocol'
@@ -10,6 +10,7 @@ import { warn } from './console'
10
10
* @typedef {object} ObfuscationRule
11
11
* @property {string|RegExp} regex The regular expression to match against in the payload
12
12
* @property {string} [replacement] The string to replace the matched regex with
13
+
* @property {string[]} [eventFilter] An optional list of event types to which this rule should be applied. If not provided, or an empty array, the rule will be applied to all events.
13
14
*/
14
15
15
16
/**
@@ -24,15 +25,34 @@ import { warn } from './console'
24
25
*/
25
26
26
27
exportclassObfuscator{
27
-
constructor(agentRef){
28
+
/**
29
+
* @param {Object} agentRef - Reference to the agent instance
30
+
* @param {string} [eventType] - Optional event type this obfuscator instance handles.
31
+
* If provided, only rules matching this event type (or rules with no eventFilter) will be applied.
32
+
*/
33
+
constructor(agentRef,eventType){
28
34
this.agentRef=agentRef
35
+
this.eventType=eventType
29
36
this.warnedRegexMissing=false
30
37
this.warnedInvalidRegex=false
31
38
this.warnedInvalidReplacement=false
32
39
}
33
40
34
41
getobfuscateConfigRules(){
35
-
returnthis.agentRef.init.obfuscate||[]
42
+
constallRules=this.agentRef.init.obfuscate||[]
43
+
44
+
// If this instance has no specific event type, return all rules
45
+
if(!this.eventType)returnallRules
46
+
47
+
// Filter rules to only those that apply to this instance's event type
48
+
returnallRules.filter(rule=>{
49
+
// If rule has no eventFilter, it applies to all events
50
+
if(!this.#hasValidEventFilter(rule)){
51
+
returntrue
52
+
}
53
+
// Otherwise, check if this instance's event type matches the rule's filter
54
+
returnrule.eventFilter.includes(this.eventType)
55
+
})
36
56
}
37
57
38
58
/**
@@ -60,6 +80,137 @@ export class Obfuscator {
60
80
},input)
61
81
}
62
82
83
+
/**
84
+
* Traverses an object and obfuscates all string properties.
85
+
* This instance will only apply rules that match its configured event type (if any).
86
+
* For features with mixed event types in their payloads (like generic_events), this will
87
+
* traverse the object and check each object's eventType property against the rules.
88
+
* @param {Object|Array} obj - The object or array to traverse
89
+
* @returns {Object|Array} The modified object
90
+
*/
91
+
traverseAndObfuscateEvents(obj){
92
+
if(!obj||typeofobj!=='object')returnobj
93
+
94
+
// If this instance was configured with a specific event type, obfuscate everything
95
+
// (rules are already filtered in obfuscateConfigRules getter)
* Recursively applies a function to all string properties in an object.
132
+
* @param {Object|Array} obj - The object or array to traverse
133
+
* @param {Function} fn - The function to apply to string properties
134
+
* @private
135
+
*/
136
+
#applyFnToAllStrings (obj,fn){
137
+
if(!obj||typeofobj!=='object')return
138
+
139
+
Object.keys(obj).forEach(property=>{
140
+
constvalue=obj[property]
141
+
142
+
if(typeofvalue==='object'&&value!==null){
143
+
this.#applyFnToAllStrings(value,fn)
144
+
}elseif(typeofvalue==='string'){
145
+
obj[property]=fn(value)
146
+
}
147
+
})
148
+
}
149
+
150
+
/**
151
+
* Recursively applies a function to string properties based on eventType filtering.
152
+
* @param {Object|Array} obj - The object or array to traverse
153
+
* @param {Function} fn - The function to apply to string properties (applies all rules)
154
+
* @param {string[]} eventTypes - Array of event types to apply event-specific obfuscation
155
+
* @param {Array} globalRules - Rules without eventFilter that apply to all events
156
+
* @param {boolean|null} shouldObfuscate - Track obfuscation state: null = not determined yet, true = obfuscate with all rules, false = obfuscate with global rules only
// if the agent is tracking ITSELF, it can spawn endless ajax requests early if they are large from custom attributes, so we just disable early harvest for ajax in this case.
28
33
super.canHarvestEarly=false
@@ -131,7 +136,7 @@ export class Aggregate extends AggregateBase {
0 commit comments