-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathcondition-evaluator-internal.ts
More file actions
359 lines (314 loc) · 12.4 KB
/
condition-evaluator-internal.ts
File metadata and controls
359 lines (314 loc) · 12.4 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*!
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import {
AndCondition,
OneOfCondition,
EvaluationContext,
NamedCondition,
OrCondition,
PercentCondition,
PercentConditionOperator,
CustomSignalCondition,
CustomSignalOperator,
} from './remote-config-api';
import { createHash } from 'crypto';
import * as vm from 'vm';
/**
* Encapsulates condition evaluation logic to simplify organization and
* facilitate testing.
*
* @internal
*/
export class ConditionEvaluator {
private static MAX_CONDITION_RECURSION_DEPTH = 10;
public evaluateConditions(
namedConditions: NamedCondition[],
context: EvaluationContext): Map<string, boolean> {
// The order of the conditions is significant.
// A JS Map preserves the order of insertion ("Iteration happens in insertion order"
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#description).
const evaluatedConditions = new Map();
for (const namedCondition of namedConditions) {
evaluatedConditions.set(
namedCondition.name,
this.evaluateCondition(namedCondition.condition, context));
}
return evaluatedConditions;
}
private evaluateCondition(
condition: OneOfCondition,
context: EvaluationContext,
nestingLevel = 0): boolean {
if (nestingLevel >= ConditionEvaluator.MAX_CONDITION_RECURSION_DEPTH) {
// TODO: add logging once we have a wrapped logger.
return false;
}
if (condition.orCondition) {
return this.evaluateOrCondition(condition.orCondition, context, nestingLevel + 1)
}
if (condition.andCondition) {
return this.evaluateAndCondition(condition.andCondition, context, nestingLevel + 1)
}
if (condition.true) {
return true;
}
if (condition.false) {
return false;
}
if (condition.percent) {
return this.evaluatePercentCondition(condition.percent, context);
}
if (condition.customSignal) {
return this.evaluateCustomSignalCondition(condition.customSignal, context);
}
// TODO: add logging once we have a wrapped logger.
return false;
}
private evaluateOrCondition(
orCondition: OrCondition,
context: EvaluationContext,
nestingLevel: number): boolean {
const subConditions = orCondition.conditions || [];
for (const subCondition of subConditions) {
// Recursive call.
const result = this.evaluateCondition(
subCondition, context, nestingLevel + 1);
// Short-circuit the evaluation result for true.
if (result) {
return result;
}
}
return false;
}
private evaluateAndCondition(
andCondition: AndCondition,
context: EvaluationContext,
nestingLevel: number): boolean {
const subConditions = andCondition.conditions || [];
for (const subCondition of subConditions) {
// Recursive call.
const result = this.evaluateCondition(
subCondition, context, nestingLevel + 1);
// Short-circuit the evaluation result for false.
if (!result) {
return result;
}
}
return true;
}
private evaluatePercentCondition(
percentCondition: PercentCondition,
context: EvaluationContext
): boolean {
if (!context.randomizationId) {
// TODO: add logging once we have a wrapped logger.
return false;
}
// This is the entry point for processing percent condition data from the response.
// We're not using a proto library, so we can't assume undefined fields have
// default values.
const { seed, percentOperator, microPercent, microPercentRange } = percentCondition;
if (!percentOperator) {
// TODO: add logging once we have a wrapped logger.
return false;
}
const normalizedMicroPercent = microPercent || 0;
const normalizedMicroPercentUpperBound = microPercentRange?.microPercentUpperBound || 0;
const normalizedMicroPercentLowerBound = microPercentRange?.microPercentLowerBound || 0;
const seedPrefix = seed && seed.length > 0 ? `${seed}.` : '';
const stringToHash = `${seedPrefix}${context.randomizationId}`;
const hash = ConditionEvaluator.hashSeededRandomizationId(stringToHash)
const instanceMicroPercentile = hash % BigInt(100 * 1_000_000);
switch (percentOperator) {
case PercentConditionOperator.LESS_OR_EQUAL:
return instanceMicroPercentile <= normalizedMicroPercent;
case PercentConditionOperator.GREATER_THAN:
return instanceMicroPercentile > normalizedMicroPercent;
case PercentConditionOperator.BETWEEN:
return instanceMicroPercentile > normalizedMicroPercentLowerBound
&& instanceMicroPercentile <= normalizedMicroPercentUpperBound;
case PercentConditionOperator.UNKNOWN:
default:
break;
}
// TODO: add logging once we have a wrapped logger.
return false;
}
static hashSeededRandomizationId(seededRandomizationId: string): bigint {
const hex = createHash('sha256').update(seededRandomizationId).digest('hex');
return BigInt(`0x${hex}`);
}
private evaluateCustomSignalCondition(
customSignalCondition: CustomSignalCondition,
context: EvaluationContext
): boolean {
const {
customSignalOperator,
customSignalKey,
targetCustomSignalValues,
} = customSignalCondition;
if (!customSignalOperator || !customSignalKey || !targetCustomSignalValues) {
// TODO: add logging once we have a wrapped logger.
return false;
}
if (!targetCustomSignalValues.length) {
return false;
}
// Extract the value of the signal from the evaluation context.
const actualCustomSignalValue = context[customSignalKey];
if (actualCustomSignalValue == undefined) {
return false
}
switch (customSignalOperator) {
case CustomSignalOperator.STRING_CONTAINS:
return compareStrings(
targetCustomSignalValues,
actualCustomSignalValue,
(target, actual) => actual.includes(target),
);
case CustomSignalOperator.STRING_DOES_NOT_CONTAIN:
return !compareStrings(
targetCustomSignalValues,
actualCustomSignalValue,
(target, actual) => actual.includes(target),
);
case CustomSignalOperator.STRING_EXACTLY_MATCHES:
return compareStrings(
targetCustomSignalValues,
actualCustomSignalValue,
(target, actual) => actual.trim() === target.trim(),
);
case CustomSignalOperator.STRING_CONTAINS_REGEX:
return compareStrings(
targetCustomSignalValues,
actualCustomSignalValue,
(target, actual) => safeRegexTest(target, actual),
);
// For numeric operators only one target value is allowed.
case CustomSignalOperator.NUMERIC_LESS_THAN:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r < 0);
case CustomSignalOperator.NUMERIC_LESS_EQUAL:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r <= 0);
case CustomSignalOperator.NUMERIC_EQUAL:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r === 0);
case CustomSignalOperator.NUMERIC_NOT_EQUAL:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r !== 0);
case CustomSignalOperator.NUMERIC_GREATER_THAN:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r > 0);
case CustomSignalOperator.NUMERIC_GREATER_EQUAL:
return compareNumbers(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r >= 0);
// For semantic operators only one target value is allowed.
case CustomSignalOperator.SEMANTIC_VERSION_LESS_THAN:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r < 0);
case CustomSignalOperator.SEMANTIC_VERSION_LESS_EQUAL:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r <= 0);
case CustomSignalOperator.SEMANTIC_VERSION_EQUAL:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r === 0);
case CustomSignalOperator.SEMANTIC_VERSION_NOT_EQUAL:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r !== 0);
case CustomSignalOperator.SEMANTIC_VERSION_GREATER_THAN:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r > 0);
case CustomSignalOperator.SEMANTIC_VERSION_GREATER_EQUAL:
return compareSemanticVersions(actualCustomSignalValue, targetCustomSignalValues[0], (r) => r >= 0);
}
// TODO: add logging once we have a wrapped logger.
return false;
}
}
// Compares the actual string value of a signal against a list of target
// values. If any of the target values are a match, returns true.
function compareStrings(
targetValues: Array<string>,
actualValue: string|number,
predicateFn: (target: string, actual: string) => boolean
): boolean {
const actual = String(actualValue);
return targetValues.some((target) => predicateFn(target, actual));
}
// Pre-compile a single V8 context to avoid the high performance penalty of
// creating a new context for every regex evaluation.
const sharedRegexSandbox = {
pattern: '',
actual: '',
result: false,
};
const sharedRegexContext = vm.createContext(sharedRegexSandbox);
const sharedRegexScript = new vm.Script(`
try {
result = new RegExp(pattern).test(actual);
} catch (e) {
result = false;
}
`);
// Compares a regex against an actual string safely with a timeout to mitigate ReDoS.
function safeRegexTest(pattern: string, actual: string): boolean {
try {
sharedRegexSandbox.pattern = pattern;
sharedRegexSandbox.actual = actual;
sharedRegexSandbox.result = false;
// Timeout is set to 100ms.
sharedRegexScript.runInContext(sharedRegexContext, { timeout: 100 });
return sharedRegexSandbox.result;
} catch (e) {
// Return false if the regex evaluation times out or throws any other error.
return false;
}
}
// Compares two numbers against each other.
// Calls the predicate function with -1, 0, 1 if actual is less than, equal to, or greater than target.
function compareNumbers(
actualValue: string|number,
targetValue: string,
predicateFn: (result: number) => boolean
): boolean {
const target = Number(targetValue);
const actual = Number(actualValue);
if (isNaN(target) || isNaN(actual)) {
return false;
}
return predicateFn(actual < target ? -1 : actual > target ? 1 : 0);
}
// Max number of segments a numeric version can have. This is enforced by the server as well.
const MAX_LENGTH = 5;
// Compares semantic version strings against each other.
// Calls the predicate function with -1, 0, 1 if actual is less than, equal to, or greater than target.
function compareSemanticVersions(
actualValue: string|number,
targetValue: string,
predicateFn: (result: number) => boolean
): boolean {
const version1 = String(actualValue).split('.').map(Number);
const version2 = targetValue.split('.').map(Number);
if (version1.length > MAX_LENGTH || version2.length > MAX_LENGTH) {
return false;
}
for (let i = 0; i < MAX_LENGTH; i++) {
// Check to see if segments are present. Note that these may be present and be NaN.
const version1HasSegment = version1[i] !== undefined;
const version2HasSegment = version2[i] !== undefined;
// Insert zeros if undefined for easier comparison.
if (!version1HasSegment) version1[i] = 0;
if (!version2HasSegment) version2[i] = 0;
// At this point, if either segment is NaN, we return false directly.
if (isNaN(version1[i]) || isNaN(version2[i])) return false;
// Check if we have a difference in segments. Otherwise continue to next segment.
if (version1[i] < version2[i]) return predicateFn(-1);
if (version1[i] > version2[i]) return predicateFn(1);
}
// If this point is reached, the semantic versions are equal.
return predicateFn(0);
}