|
6 | 6 | // components that external developers may be modifying.
|
7 | 7 |
|
8 | 8 | import express from 'express';
|
| 9 | +import url from 'url'; |
9 | 10 | import log from './logger';
|
10 | 11 |
|
11 | 12 | export default class PromiseRouter {
|
@@ -154,8 +155,8 @@ export default class PromiseRouter {
|
154 | 155 | function makeExpressHandler(promiseHandler) {
|
155 | 156 | return function(req, res, next) {
|
156 | 157 | try {
|
157 |
| - log.verbose(req.method, req.originalUrl, req.headers, |
158 |
| - JSON.stringify(req.body, null, 2)); |
| 158 | + log.verbose(req.method, maskSensitiveUrl(req), req.headers, |
| 159 | + JSON.stringify(maskSensitiveBody(req), null, 2)); |
159 | 160 | promiseHandler(req).then((result) => {
|
160 | 161 | if (!result.response && !result.location && !result.text) {
|
161 | 162 | log.error('the handler did not include a "response" or a "location" field');
|
@@ -194,3 +195,34 @@ function makeExpressHandler(promiseHandler) {
|
194 | 195 | }
|
195 | 196 | }
|
196 | 197 | }
|
| 198 | + |
| 199 | +function maskSensitiveBody(req) { |
| 200 | + let maskBody = Object.assign({}, req.body); |
| 201 | + let shouldMaskBody = (req.method === 'POST' && req.originalUrl.endsWith('/users') |
| 202 | + && !req.originalUrl.includes('classes')) || |
| 203 | + (req.method === 'PUT' && /users\/\w+$/.test(req.originalUrl) |
| 204 | + && !req.originalUrl.includes('classes')) || |
| 205 | + (req.originalUrl.includes('classes/_User')); |
| 206 | + if (shouldMaskBody) { |
| 207 | + for (let key of Object.keys(maskBody)) { |
| 208 | + if (key == 'password') { |
| 209 | + maskBody[key] = '********'; |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + return maskBody; |
| 215 | +} |
| 216 | + |
| 217 | +function maskSensitiveUrl(req) { |
| 218 | + let maskUrl = req.originalUrl.toString(); |
| 219 | + let shouldMaskUrl = req.method === 'GET' && req.originalUrl.includes('/login') |
| 220 | + && !req.originalUrl.includes('classes'); |
| 221 | + if (shouldMaskUrl) { |
| 222 | + let password = url.parse(req.originalUrl, true).query.password; |
| 223 | + if (password) { |
| 224 | + maskUrl = maskUrl.replace('password=' + password, 'password=********') |
| 225 | + } |
| 226 | + } |
| 227 | + return maskUrl; |
| 228 | +} |
0 commit comments