-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathcondition.js
More file actions
291 lines (262 loc) · 10.9 KB
/
Copy pathcondition.js
File metadata and controls
291 lines (262 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict'
module.exports = {
compile,
compileSegments,
templateRequiresEvaluation,
}
const identifierRegex = /^(@[\w$]+|[a-zA-Z_$][\w$]*)$/
// The following identifiers have purposefully not been included in this list:
// - The reserved words `this` and `super` as they can have valid use cases as `ref` values
// - The literals `undefined` and `Infinity` as they can be useful as `ref` values, especially to check if a
// variable is `undefined`.
// - The following future reserved words in older standards, as they can now be used safely:
// `abstract`, `boolean`, `byte`, `char`, `double`, `final`, `float`, `goto`, `int`, `long`, `native`, `short`,
// `synchronized`, `throws`, `transient`, `volatile`.
const reservedWords = new Set([
// Reserved words
'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'else', 'export',
'extends', 'false', 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'new', 'null', 'return',
'switch', 'throw', 'true', 'try', 'typeof', 'var', 'void', 'while', 'with',
// Reserved in strict mode
'let', 'static', 'yield',
// Reserved in module code or async function bodies:
'await',
// Future reserved words
'enum',
// Future reserved words in strict mode
'implements', 'interface', 'package', 'private', 'protected', 'public',
// Literals
'NaN',
])
const PRIMITIVE_TYPES = new Set(['string', 'number', 'bigint', 'boolean', 'undefined', 'symbol', 'null'])
function templateRequiresEvaluation (segments) {
if (segments === undefined) return false // There should always be segments, but just in case
for (const { str } of segments) {
if (str === undefined) return true
}
return false
}
function compileSegments (segments) {
let result = '['
for (let i = 0; i < segments.length; i++) {
const { str, dsl, json } = segments[i]
result += str === undefined
? `(() => {
try {
const result = ${compile(json)}
return typeof result === 'string' ? result : $dd_inspectSegment(result)
} catch (e) {
return { expr: ${JSON.stringify(dsl)}, message: \`\${e.name}: \${e.message}\` }
}
})()`
: JSON.stringify(str)
if (i !== segments.length - 1) {
result += ','
}
}
return `${result}]`
}
// TODO: Consider storing some of these functions that doesn't require closure access to the current scope on `process`
// so they can be reused across probes
function compile (node) {
if (node === null || typeof node === 'number' || typeof node === 'boolean') {
return node
} else if (typeof node === 'string') {
return JSON.stringify(node)
}
const [type, value] = Object.entries(node)[0]
if (type === 'not') {
return `!(${compile(value)})`
} else if (type === 'len' || type === 'count') {
return getSize(compile(value))
} else if (type === 'isEmpty') {
return `${getSize(compile(value))} === 0`
} else if (type === 'isDefined') {
return `(() => {
try {
${compile(value)}
return true
} catch {
return false
}
})()`
} else if (type === 'instanceof') {
return isPrimitiveType(value[1])
? `(typeof ${compile(value[0])} === '${value[1]}')` // TODO: Is parenthesizing necessary?
: `Function.prototype[Symbol.hasInstance].call(${assertIdentifier(value[1])}, ${compile(value[0])})`
} else if (type === 'ref') {
const refValue = assertIdentifier(value)
if (refValue.startsWith('@')) {
return `$dd_${refValue.slice(1)}`
}
return refValue
} else if (Array.isArray(value)) {
const args = value.map(compile)
switch (type) {
case 'eq': return `(${args[0]}) === (${args[1]})`
case 'ne': return `(${args[0]}) !== (${args[1]})`
case 'gt': return `${guardAgainstCoercionSideEffects(args[0])} > ${guardAgainstCoercionSideEffects(args[1])}`
case 'ge': return `${guardAgainstCoercionSideEffects(args[0])} >= ${guardAgainstCoercionSideEffects(args[1])}`
case 'lt': return `${guardAgainstCoercionSideEffects(args[0])} < ${guardAgainstCoercionSideEffects(args[1])}`
case 'le': return `${guardAgainstCoercionSideEffects(args[0])} <= ${guardAgainstCoercionSideEffects(args[1])}`
case 'any': return iterateOn('some', ...args)
case 'all': return iterateOn('every', ...args)
case 'and': return `(${args.join(') && (')})`
case 'or': return `(${args.join(') || (')})`
case 'startsWith': return `String.prototype.startsWith.call(${assertString(args[0])}, ${assertString(args[1])})`
case 'endsWith': return `String.prototype.endsWith.call(${assertString(args[0])}, ${assertString(args[1])})`
case 'contains': return `((obj, elm) => {
if (${isString('obj')}) {
return String.prototype.includes.call(obj, elm)
} else if (Array.isArray(obj)) {
return Array.prototype.includes.call(obj, elm)
} else if (${isTypedArray('obj')}) {
return Object.getPrototypeOf(Int8Array.prototype).includes.call(obj, elm)
} else if (${isInstanceOfCoreType('Set', 'obj')}) {
return Set.prototype.has.call(obj, elm)
} else if (${isInstanceOfCoreType('WeakSet', 'obj')}) {
return WeakSet.prototype.has.call(obj, elm)
} else if (${isInstanceOfCoreType('Map', 'obj')}) {
return Map.prototype.has.call(obj, elm)
} else if (${isInstanceOfCoreType('WeakMap', 'obj')}) {
return WeakMap.prototype.has.call(obj, elm)
} else {
throw new TypeError('Variable does not support contains')
}
})(${args[0]}, ${args[1]})`
case 'matches': return `((str, regex) => {
if (${isString('str')}) {
const regexIsString = ${isString('regex')}
if (regexIsString || Object.getPrototypeOf(regex) === RegExp.prototype) {
return RegExp.prototype.test.call(regexIsString ? new RegExp(regex) : regex, str)
} else {
throw new TypeError('Regular expression must be either a string or an instance of RegExp')
}
} else {
throw new TypeError('Variable is not a string')
}
})(${args[0]}, ${args[1]})`
case 'filter': return `(($dd_var) => {
return ${isIterableCollection('$dd_var')}
? Array.from($dd_var).filter(($dd_it) => ${args[1]})
: Object.entries($dd_var).reduce((acc, [$dd_key, $dd_value]) => {
if (${args[1]}) acc[$dd_key] = $dd_value
return acc
}, {})
})(${args[0]})`
case 'substring': return `((str) => {
if (${isString('str')}) {
return String.prototype.substring.call(str, ${args[1]}, ${args[2]})
} else {
throw new TypeError('Variable is not a string')
}
})(${args[0]})`
case 'getmember': return accessProperty(args[0], args[1], false)
case 'index': return accessProperty(args[0], args[1], true)
}
}
throw new TypeError(`Unknown AST node type: ${type}`)
}
function iterateOn (fnName, variable, callbackCode) {
return `(($dd_val) => {
return ${isIterableCollection('$dd_val')}
? Array.from($dd_val).${fnName}(($dd_it) => ${callbackCode})
: Object.entries($dd_val).${fnName}(([$dd_key, $dd_value]) => ${callbackCode})
})(${variable})`
}
function isString (variable) {
return `(typeof ${variable} === 'string' || ${variable} instanceof String)`
}
function isPrimitiveType (type) {
return PRIMITIVE_TYPES.has(type)
}
function isIterableCollection (variable) {
return `(${isArrayOrTypedArray(variable)} || ${isInstanceOfCoreType('Set', variable)} || ` +
`${isInstanceOfCoreType('WeakSet', variable)})`
}
function isArrayOrTypedArray (variable) {
return `(Array.isArray(${variable}) || ${isTypedArray(variable)})`
}
function isTypedArray (variable) {
return isInstanceOfCoreType('TypedArray', variable, `${variable} instanceof Object.getPrototypeOf(Int8Array)`)
}
function isInstanceOfCoreType (type, variable, fallback = `${variable} instanceof ${type}`) {
return `(globalThis[Symbol.for('dd-trace')].utilTypes?.is${type}?.(${variable}) ?? ${fallback})`
}
function getSize (variable) {
return `((val) => {
if (${isString('val')} || ${isArrayOrTypedArray('val')}) {
return ${guardAgainstPropertyAccessSideEffects('val', '"length"')}
} else if (${isInstanceOfCoreType('Set', 'val')} || ${isInstanceOfCoreType('Map', 'val')}) {
return ${guardAgainstPropertyAccessSideEffects('val', '"size"')}
} else if (${isInstanceOfCoreType('WeakSet', 'val')} || ${isInstanceOfCoreType('WeakMap', 'val')}) {
throw new TypeError('Cannot get size of WeakSet or WeakMap')
} else if (typeof val === 'object' && val !== null) {
return Object.keys(val).length
} else {
throw new TypeError('Cannot get length of variable')
}
})(${variable})`
}
function accessProperty (variable, keyOrIndex, allowMapAccess) {
return `((val, key) => {
if (${isInstanceOfCoreType('Map', 'val')}) {
${allowMapAccess
? 'return Map.prototype.get.call(val, key)'
: 'throw new Error(\'Accessing a Map is not allowed\')'}
} else if (${isInstanceOfCoreType('WeakMap', 'val')}) {
${allowMapAccess
? 'return WeakMap.prototype.get.call(val, key)'
: 'throw new Error(\'Accessing a WeakMap is not allowed\')'}
} else if (${isInstanceOfCoreType('Set', 'val')} || ${isInstanceOfCoreType('WeakSet', 'val')}) {
throw new Error('Accessing a Set or WeakSet is not allowed')
} else {
return ${guardAgainstPropertyAccessSideEffects('val', 'key')}
}
})(${variable}, ${keyOrIndex})`
}
function guardAgainstPropertyAccessSideEffects (variable, propertyName) {
return `((val, key) => {
if (
${isInstanceOfCoreType('Proxy', 'val', 'true')} ||
Object.getOwnPropertyDescriptor(val, key)?.get !== undefined
) {
throw new Error('Possibility of side effect')
} else {
return val[key]
}
})(${variable}, ${propertyName})`
}
function guardAgainstCoercionSideEffects (variable) {
// shortcut if we're comparing number literals
if (typeof variable === 'number') return variable
return `((val) => {
if (
typeof val === 'object' && val !== null && (
${isInstanceOfCoreType('Proxy', 'val', 'true')} ||
val[Symbol.toPrimitive] !== undefined ||
val.valueOf !== Object.prototype.valueOf ||
val.toString !== Object.prototype.toString
)
) {
throw new Error('Possibility of side effect due to coercion methods')
} else {
return val
}
})(${variable})`
}
function assertString (variable) {
return `((val) => {
if (${isString('val')}) {
return val
} else {
throw new TypeError('Variable is not a string')
}
})(${variable})`
}
function assertIdentifier (value) {
if (!identifierRegex.test(value) || reservedWords.has(value)) {
throw new SyntaxError(`Illegal identifier: ${value}`)
}
return value
}