-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
common.ts
65 lines (52 loc) · 2.21 KB
/
common.ts
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
import {Str} from 'expensify-common';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Session} from '@src/types/onyx';
const MASKING_PATTERN = '***';
const maskSessionDetails = (data: Record<string, unknown>): Record<string, unknown> => {
const session = data.session as Session;
const maskedData: Record<string, unknown> = {};
Object.keys(session).forEach((key) => {
if (key !== 'authToken' && key !== 'encryptedAuthToken') {
maskedData[key] = session[key as keyof Session];
return;
}
maskedData[key] = MASKING_PATTERN;
});
return {
...data,
session: maskedData,
};
};
const maskFragileData = (data: Record<string, unknown> | unknown[], parentKey?: string): Record<string, unknown> | unknown[] => {
if (Array.isArray(data)) {
return data.map((item): unknown => (typeof item === 'object' ? maskFragileData(item as Record<string, unknown>, parentKey) : item));
}
const maskedData: Record<string, unknown> = {};
Object.keys(data).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(data, key)) {
return;
}
const value = data[key];
if (typeof value === 'string' && Str.isValidEmail(value)) {
maskedData[key] = MASKING_PATTERN;
} else if (parentKey && parentKey.includes(ONYXKEYS.COLLECTION.REPORT_ACTIONS) && (key === 'text' || key === 'html')) {
maskedData[key] = MASKING_PATTERN;
} else if (typeof value === 'object') {
maskedData[key] = maskFragileData(value as Record<string, unknown>, key.includes(ONYXKEYS.COLLECTION.REPORT_ACTIONS) ? key : parentKey);
} else {
maskedData[key] = value;
}
});
return maskedData;
};
const maskOnyxState = (data: Record<string, unknown>, isMaskingFragileDataEnabled?: boolean) => {
let onyxState = data;
// Mask session details by default
onyxState = maskSessionDetails(onyxState);
// Mask fragile data other than session details if the user has enabled the option
if (isMaskingFragileDataEnabled) {
onyxState = maskFragileData(onyxState) as Record<string, unknown>;
}
return onyxState;
};
export default {maskOnyxState};