-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsanitizeUtils.ts
More file actions
61 lines (54 loc) · 1.56 KB
/
sanitizeUtils.ts
File metadata and controls
61 lines (54 loc) · 1.56 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
import { CENSORED_TEXT } from '../common';
import { hasProperty } from './dataUtils';
/**
* Sanitize an auth header value.
* @param authHeaderValue Sanitize secret portion of known auth headers. If
* unrecognized, replace the whole thing.
* @returns The sanitized auth header value.
*/
export function sanitizeAuthHeaderValue(authHeaderValue: string): string {
if (
authHeaderValue.startsWith('io.deephaven.proto.auth.Token') ||
authHeaderValue.startsWith('Bearer')
) {
return authHeaderValue.replace(/ \S+$/, ` ${CENSORED_TEXT}`);
}
return CENSORED_TEXT;
}
/**
* Sanitize an auth header.
* @param authorization The authorization header value. This can be a string
* or an array of strings. Any other data types will be treated as an empty
* string.
* @returns The sanitized auth header value.
*/
export function sanitizeAuthHeader(authorization: unknown): string | string[] {
if (typeof authorization === 'string') {
return sanitizeAuthHeaderValue(authorization);
}
if (Array.isArray(authorization)) {
return authorization.map(sanitizeAuthHeaderValue);
}
return CENSORED_TEXT;
}
/**
* Sanitize auth headers in a gRPC log message.
* @param args The arguments to sanitize.
* @returns The sanitized arguments.
*/
export function sanitizeGRPCLogMessageArgs([
arg0,
arg1,
...args
]: unknown[]): unknown[] {
if (arg0 === 'start' && hasProperty(arg1, 'authorization')) {
return [
arg0,
{
...arg1,
authorization: sanitizeAuthHeader(arg1.authorization),
},
];
}
return [arg0, arg1, ...args];
}