Skip to content

feat: mask the payload with sensitive info in console logs #2786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/helpers/REST.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ Sends PATCH request to API.

```js
I.sendPatchRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPatchRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters
Expand All @@ -115,6 +118,9 @@ Sends POST request to API.

```js
I.sendPostRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPostRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters
Expand All @@ -129,6 +135,9 @@ Sends PUT request to API.

```js
I.sendPutRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPutRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters
Expand Down
21 changes: 20 additions & 1 deletion lib/helper/REST.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const axios = require('axios').default;
const Secret = require('../secret');

const Helper = require('../helper');

Expand Down Expand Up @@ -75,12 +76,18 @@ class REST extends Helper {
* @param {*} request
*/
async _executeRequest(request) {
const _debugRequest = { ...request };
axios.defaults.timeout = request.timeout || this.options.timeout;

if (this.headers && this.headers.auth) {
request.auth = this.headers.auth;
}

if (request.data instanceof Secret) {
_debugRequest.data = '*****';
request.data = typeof request.data === 'object' ? { ...request.data.toString() } : request.data.toString();
}

if ((typeof request.data) === 'string') {
if (!request.headers || !request.headers['Content-Type']) {
request.headers = { ...request.headers, ...{ 'Content-Type': 'application/x-www-form-urlencoded' } };
Expand All @@ -91,7 +98,7 @@ class REST extends Helper {
await this.config.onRequest(request);
}

this.debugSection('Request', JSON.stringify(request));
this.debugSection('Request', JSON.stringify(_debugRequest));

let response;
try {
Expand Down Expand Up @@ -149,6 +156,10 @@ class REST extends Helper {
*
* ```js
* I.sendPostRequest('/api/users.json', { "email": "user@user.com" });
*
* // To mask the payload in logs
* I.sendPostRequest('/api/users.json', secret({ "email": "user@user.com" }));
*
* ```
*
* @param {*} url
Expand Down Expand Up @@ -176,6 +187,10 @@ class REST extends Helper {
*
* ```js
* I.sendPatchRequest('/api/users.json', { "email": "user@user.com" });
*
* // To mask the payload in logs
* I.sendPatchRequest('/api/users.json', secret({ "email": "user@user.com" }));
*
* ```
*
* @param {string} url
Expand Down Expand Up @@ -203,6 +218,10 @@ class REST extends Helper {
*
* ```js
* I.sendPutRequest('/api/users.json', { "email": "user@user.com" });
*
* // To mask the payload in logs
* I.sendPutRequest('/api/users.json', secret({ "email": "user@user.com" }));
*
* ```
*
* @param {string} url
Expand Down