-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfinalize-request-log.js
118 lines (96 loc) · 3.61 KB
/
finalize-request-log.js
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
const stringify = require('json-stringify-safe');
/**
* Finalize Request Log
*
* @function sails.helpers.finalizeRequestLog
* @param {Object} req The current `req` object.
* @param {Object} res The current `res` object.
* @param {Object} body The final body of the response given to the end-user.
*
* @returns null
*/
module.exports = {
friendlyName: 'Finalize Request Log',
description: 'Used by response handlers to log final responses to requests.',
inputs: {
req: {
type: 'ref',
description: 'The current incoming request (req).',
required: true
},
res: {
type: 'ref',
description: 'The current outgoing response (res).',
required: true
},
body: {
type: 'ref',
description: 'The body of the response.',
required: true
}
},
exits: {
success: {}
},
fn: async (inputs, exits) => {
if (inputs.req.id && inputs.req.id !== 'IGNORE' && sails.config.log.captureRequests === true) {
const bleep = '*******';
let out = _.merge({}, inputs.body),
headers = _.merge({}, inputs.res.getHeaders()); // copy the object
// Regardless of what our configuration option is set to, NEVER log sensitive info on PRODUCTION!
if (sails.config.security.requestLogger.logSensitiveData !== true || process.env.NODE_ENV.toUpperCase() === 'PROD' || process.env.NODE_ENV.toUpperCase() === 'PRODUCTION') {
if (out._csrf) {
out._csrf = bleep;
}
if (out.token) {
out.token = bleep;
// If we have a token, and a "header" in our out response, hide the header, it contains the token too.
if (out.header) {
out.header = bleep;
}
}
if (out.access_token) {
// eslint-disable-next-line camelcase
out.access_token = bleep;
}
if (out.refresh_token) {
// eslint-disable-next-line camelcase
out.refresh_token = bleep;
}
if (out.secret) {
out.secret = bleep;
// It has a secret, and an image? The image is a QR containing the secret, so hide it.
if (out.image) {
out.image = bleep;
}
}
if (out.backupTokens) {
out.backupTokens = bleep;
}
if (headers['set-cookie']) {
headers['set-cookie'] = bleep;
}
}
if (_.isObject(out)) {
out = stringify(out);
}
const time = Number(process.hrtime.bigint() - inputs.req._requestStartTime) / 1000000; // convert the bigint nanoseconds into milliseconds
const totalTime = time.toFixed(4) + 'ms';
let log = {
responseCode: inputs.res.statusCode,
responseBody: out,
responseHeaders: stringify(headers),
responseTime: totalTime
};
sails.models.requestlog.update({id: inputs.req.id}).set(log).exec((err) => {
/* istanbul ignore if */
if (err) {
console.error(err);
}
return exits.success();
});
} else {
return exits.success();
}
}
};