1- import { getConfigurationValue } from '../config/init'
21import { isFileProtocol } from '../url/protocol'
32import { warn } from './console'
43
@@ -21,18 +20,15 @@ import { warn } from './console'
2120 */
2221
2322export class Obfuscator {
24- /**
25- * @type {ObfuscationRuleValidation[] }
26- */
27- #ruleValidationCache
28-
29- constructor ( agentIdentifier ) {
30- this . #ruleValidationCache = Obfuscator . getRuleValidationCache ( agentIdentifier )
31- Obfuscator . logObfuscationRuleErrors ( this . #ruleValidationCache)
23+ constructor ( agentRef ) {
24+ this . agentRef = agentRef
25+ this . warnedRegexMissing = false
26+ this . warnedInvalidRegex = false
27+ this . warnedInvalidReplacement = false
3228 }
3329
34- get ruleValidationCache ( ) {
35- return this . #ruleValidationCache
30+ get obfuscateConfigRules ( ) {
31+ return this . agentRef . init . obfuscate || [ ]
3632 }
3733
3834 /**
@@ -44,44 +40,44 @@ export class Obfuscator {
4440 // if input is not of type string or is an empty string, short-circuit
4541 if ( typeof input !== 'string' || input . trim ( ) . length === 0 ) return input
4642
47- return this . #ruleValidationCache
48- . filter ( ruleValidation => ruleValidation . isValid )
49- . reduce ( ( input , ruleValidation ) => {
50- const { rule } = ruleValidation
51- return input . replace ( rule . regex , rule . replacement || '*' )
52- } , input )
53- }
54-
55- /**
56- * Returns an array of obfuscation rules to be applied to harvested payloads
57- * @param {string } agentIdentifier The agent identifier to get rules for
58- * @returns {ObfuscationRuleValidation[] } The array of rules or validation states
59- */
60- static getRuleValidationCache ( agentIdentifier ) {
61- /**
62- * @type {ObfuscationRule[] }
63- */
64- let rules = getConfigurationValue ( agentIdentifier , 'obfuscate' ) || [ ]
43+ const rules = ( this . obfuscateConfigRules ) . map ( rule => this . validateObfuscationRule ( rule ) )
6544 if ( isFileProtocol ( ) ) {
6645 rules . push ( {
6746 regex : / ^ f i l e : \/ \/ ( .* ) / ,
6847 replacement : atob ( 'ZmlsZTovL09CRlVTQ0FURUQ=' )
6948 } )
7049 }
7150
72- return rules . map ( rule => Obfuscator . validateObfuscationRule ( rule ) )
51+ return rules
52+ . filter ( ruleValidation => ruleValidation . isValid )
53+ . reduce ( ( input , ruleValidation ) => {
54+ const { rule } = ruleValidation
55+ return input . replace ( rule . regex , rule . replacement || '*' )
56+ } , input )
7357 }
7458
7559 /**
7660 * Validates an obfuscation rule and provides errors if any are found.
7761 * @param {ObfuscationRule } rule The rule to validate
7862 * @returns {ObfuscationRuleValidation } The validation state of the rule
7963 */
80- static validateObfuscationRule ( rule ) {
64+ validateObfuscationRule ( rule ) {
8165 const regexMissingDetected = Boolean ( rule . regex === undefined )
8266 const invalidRegexDetected = Boolean ( rule . regex !== undefined && typeof rule . regex !== 'string' && ! ( rule . regex instanceof RegExp ) )
8367 const invalidReplacementDetected = Boolean ( rule . replacement && typeof rule . replacement !== 'string' )
8468
69+ if ( regexMissingDetected && ! this . warnedRegexMissing ) {
70+ warn ( 12 , rule )
71+ this . warnedRegexMissing = true
72+ } else if ( invalidRegexDetected && ! this . warnedInvalidRegex ) {
73+ warn ( 13 , rule )
74+ this . warnedInvalidRegex = true
75+ }
76+ if ( invalidReplacementDetected && ! this . warnedInvalidReplacement ) {
77+ warn ( 14 , rule )
78+ this . warnedInvalidReplacement = true
79+ }
80+
8581 return {
8682 rule,
8783 isValid : ! regexMissingDetected && ! invalidRegexDetected && ! invalidReplacementDetected ,
@@ -92,20 +88,4 @@ export class Obfuscator {
9288 }
9389 }
9490 }
95-
96- /**
97- * Logs any obfuscation rule errors to the console. This is called when an obfuscator
98- * instance is created.
99- * @param {ObfuscationRuleValidation[] } ruleValidationCache The cache of rule validation states
100- */
101- static logObfuscationRuleErrors ( ruleValidationCache ) {
102- for ( const ruleValidation of ruleValidationCache ) {
103- const { rule, isValid, errors } = ruleValidation
104- if ( isValid ) continue
105-
106- if ( errors . regexMissingDetected ) warn ( 12 , rule )
107- else if ( errors . invalidRegexDetected ) warn ( 13 , rule )
108- if ( errors . invalidReplacementDetected ) warn ( 14 , rule )
109- }
110- }
11191}
0 commit comments