Skip to content

Commit 8ca6e3d

Browse files
committed
fix(logs): redact tokens/passwords/env vars from logs
1 parent 72a3179 commit 8ca6e3d

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@types/node": "^11.13.2",
5858
"@types/recursive-readdir": "^2.2.0",
5959
"debug": "^4.1.1",
60+
"fast-redact": "^1.5.0",
6061
"file-type": "^10.10.0",
6162
"got": "^9.6.0",
6263
"mime-types": "^2.1.22",

Diff for: src/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ export class TwilioServerlessApiClient extends events.EventEmitter {
460460
message: 'Gathering Functions and Assets to deploy',
461461
});
462462

463-
log('Deploy config %O', deployConfig);
463+
log('Deploy config %P', deployConfig);
464464

465465
const searchConfig: SearchConfig = {};
466466
if (deployConfig.functionsFolderName) {

Diff for: src/external-types.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'fast-redact' {
2+
function createRedactor(options: {}): <T>(val: T) => string;
3+
4+
export = createRedactor;
5+
}

Diff for: src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*
55
* Main entry point of the project
66
*/
7+
8+
import './utils/debug';
9+
710
export * from './api';
811
export * from './client';
912
export * from './types/index';

Diff for: src/utils/debug.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import debug from 'debug';
2+
import fastRedact from 'fast-redact';
3+
4+
type RedactorFunction<T extends object> = (val: T) => T;
5+
6+
function prefixAllEntriesWithWildcard(values: string[]): string[] {
7+
const result = [];
8+
9+
for (let val of values) {
10+
result.push(val);
11+
result.push(`*.${val}`);
12+
}
13+
14+
return result;
15+
}
16+
17+
export const generalRedactor = fastRedact({
18+
paths: [
19+
'env.*',
20+
'pkgJson.*',
21+
...prefixAllEntriesWithWildcard([
22+
'authToken',
23+
'apiSecret',
24+
'username',
25+
'password',
26+
'cookies',
27+
'AUTH_TOKEN',
28+
'API_SECRET',
29+
'TWILIO_AUTH_TOKEN',
30+
'TWILIO_API_SECRET',
31+
]),
32+
],
33+
});
34+
35+
export const allPropertiesRedactor = fastRedact({
36+
paths: ['*'],
37+
});
38+
39+
debug.formatters.P = function protectedFormatterMultiline(v: any): string {
40+
if (typeof v === 'object') {
41+
v = JSON.parse(generalRedactor(v));
42+
}
43+
44+
return debug.formatters.O.bind(debug)(v);
45+
};
46+
47+
debug.formatters.p = function protectedFormatterSameline(v: any): string {
48+
if (typeof v === 'object') {
49+
v = JSON.parse(generalRedactor(v));
50+
}
51+
52+
return debug.formatters.o.bind(debug)(v);
53+
};
54+
55+
debug.formatters.R = function redactedFormatterMultiline(v: any): string {
56+
if (typeof v === 'object') {
57+
v = JSON.parse(allPropertiesRedactor(v));
58+
}
59+
return debug.formatters.O.bind(debug)(v);
60+
};
61+
62+
debug.formatters.r = function redactedFormatterSameline(v: any): string {
63+
if (typeof v === 'object') {
64+
v = JSON.parse(allPropertiesRedactor(v));
65+
}
66+
return debug.formatters.o.bind(debug)(v);
67+
};

0 commit comments

Comments
 (0)