Skip to content

Commit

Permalink
Add masking on HTTP request/response logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yuqu committed Jan 15, 2020
1 parent 0770692 commit f0aec66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"http-status-codes": "^1.4.0",
"inquirer": "^7.0.0",
"jsonwebtoken": "^8.5.1",
"mask-json": "^1.0.3",
"strip-ansi": "^6.0.0",
"ts-dedent": "^1.1.0",
"url-join": "^4.0.1",
Expand Down
14 changes: 9 additions & 5 deletions src/api/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { AxiosRequestConfig, AxiosResponse } from "axios";
import maskJson from "mask-json";
import logger from "../util/logger";

const blacklist = ["password", "Zeplin-Access-Token", "Zeplin-Token"];

const mask = maskJson(blacklist);

const requestLogger = (request: AxiosRequestConfig): AxiosRequestConfig => {
const { url, method, data, headers } = request;
let httpLog = `HTTP Request: ${method} ${url}`;

if (headers) {
httpLog = httpLog.concat(`, Headers: ${JSON.stringify(headers)}`);
httpLog = httpLog.concat(`, Headers: ${JSON.stringify(mask(headers))}`);
}
if (data) {
httpLog = httpLog.concat(`, Body: ${JSON.stringify(data)}`);
httpLog = httpLog.concat(`, Body: ${JSON.stringify(mask(data))}`);
}

logger.http(httpLog);
Expand All @@ -30,11 +35,10 @@ const responseLogger = (response: AxiosResponse): AxiosResponse => {
.concat(`, Status: ${status}-${statusText}`);

if (headers) {
httpLog = httpLog.concat(`, Headers: ${JSON.stringify(headers)}`);
httpLog = httpLog.concat(`, Headers: ${JSON.stringify(mask(headers))}`);
}

if (data) {
httpLog = httpLog.concat(`, Body: ${JSON.stringify(data)}`);
httpLog = httpLog.concat(`, Body: ${JSON.stringify(mask(data))}`);
}

logger.http(httpLog);
Expand Down
2 changes: 1 addition & 1 deletion src/errors/APIError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class APIError extends CLIError {
message = "Looks like your session has expired, please login again.";
break;
case BAD_REQUEST:
message = "There is a problem with the request payload.";
message = "There is a problem with the API request.";
break;
default:
message = `${response.data.title} - ${response.data.message}`;
Expand Down
10 changes: 10 additions & 0 deletions src/types/mask-json.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type FunctionReturnsSameType = <T>(object: T) => T;

declare module "mask-json" {
function maskJson(
collection: Array<string>,
opts?: { ignoreCase: boolean; replacement: string }
): FunctionReturnsSameType;

export = maskJson;
}

0 comments on commit f0aec66

Please sign in to comment.