-
Notifications
You must be signed in to change notification settings - Fork 0
/
mask.ts
57 lines (51 loc) · 1.69 KB
/
mask.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
import traverse from 'traverse'
import { AxiosHeaders } from 'axios'
import { plainClone } from './plain-clone.js'
/**
* List of property names that we want to mask.
* Names are not case sensitive.
*/
export const SENSITIVE_PROPERTY_NAMES = ['password', 'refresh_token']
/**
* List of HTTP header names that we want to mask.
* Names are not case sensitive.
*/
export const SENSITIVE_HEADER_NAMES = ['authorization']
/**
* The string mask to use if none explicitly provided.
*/
export const DEFAULT_MASKING_STRING = '********'
/**
* Mask all properties matching those in {@see SENSITIVE_PROPERTY_NAMES}
*/
export function maskSensitiveInput(data: unknown): any {
return maskSensitiveData(data, SENSITIVE_PROPERTY_NAMES)
}
/**
* Mask all properties matching those in {@see SENSITIVE_HEADER_NAMES}
*/
export function maskSensitiveHeaders(data: unknown): any {
return maskSensitiveData(data, SENSITIVE_HEADER_NAMES)
}
/**
* Mask all properties defined by {@see propertyNames} in the given {@see data} object
* with the mask string defined by the {@see mask} parameter.
*/
export function maskSensitiveData(data: any, propertyNames: string[], mask = DEFAULT_MASKING_STRING): any {
if (typeof data === 'object' && data !== null && data !== undefined) {
let mutatedData: Record<string, any> = {}
if (data instanceof AxiosHeaders) {
mutatedData = plainClone(data.toJSON()) as Record<string, any>
} else {
mutatedData = plainClone(data) as Record<string, any>
}
traverse(mutatedData).forEach(function (this: any) {
if (propertyNames.includes(this.key?.toLowerCase() ?? '')) {
this.update(mask)
}
})
return mutatedData
} else {
return data
}
}