Skip to content

Commit 3bb4357

Browse files
Merge pull request #299 from 07souravkunda/redact_keys_stats_table
Redact auth keys on cli stats table
2 parents 7140631 + 5047bf3 commit 3bb4357

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

bin/helpers/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ const LATEST_VERSION_SYNTAX_REGEX = /\d*.latest(.\d*)?/gm
231231

232232
const AUTH_REGEX = /"auth" *: *{[\s\S]*?}/g
233233

234+
const CLI_ARGS_REGEX = /(?<=("u"|"username"|"k"|"key") *: *)"[^,}]*/g
235+
236+
const RAW_ARGS_REGEX = /(?<=("-u"|"-username"|"-k"|"-key") *, *)"[^,\]]*/g
237+
234238
const ERROR_EXIT_CODE = 1;
235239

236240
const BUILD_FAILED_EXIT_CODE = 3;
@@ -260,7 +264,10 @@ module.exports = Object.freeze({
260264
LATEST_VERSION_SYNTAX_REGEX,
261265
ERROR_EXIT_CODE,
262266
AUTH_REGEX,
267+
CLI_ARGS_REGEX,
268+
RAW_ARGS_REGEX,
263269
REDACTED_AUTH,
270+
REDACTED,
264271
BUILD_FAILED_EXIT_CODE,
265272
SPEC_TIMEOUT_LIMIT
266273
});

bin/helpers/usageReporting.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const config = require('./config'),
99
fileLogger = require('./logger').fileLogger,
1010
utils = require('./utils');
1111

12-
const { AUTH_REGEX, REDACTED_AUTH } = require("./constants");
12+
const { AUTH_REGEX, REDACTED_AUTH, REDACTED, CLI_ARGS_REGEX, RAW_ARGS_REGEX } = require("./constants");
1313

1414
function get_version(package_name) {
1515
try {
@@ -172,6 +172,10 @@ function isUsageReportingEnabled() {
172172
return process.env.DISABLE_USAGE_REPORTING;
173173
}
174174

175+
function redactKeys(str, regex, redact) {
176+
return str.replace(regex, redact);
177+
}
178+
175179
function send(args) {
176180
if (isUsageReportingEnabled() === "true") return;
177181

@@ -185,10 +189,12 @@ function send(args) {
185189
runSettings = bsConfig.run_settings;
186190
data.cypress_version = bsConfig.run_settings.cypress_version;
187191
}
188-
189-
sanitizedbsConfig = `${(typeof bsConfig === 'string') ? bsConfig :
190-
JSON.stringify(bsConfig)}`.replace(AUTH_REGEX, REDACTED_AUTH);
191-
192+
193+
sanitizedbsConfig = redactKeys(`${(typeof bsConfig === 'string') ? bsConfig :
194+
JSON.stringify(bsConfig)}`, AUTH_REGEX, REDACTED_AUTH);
195+
args.cli_args = args.cli_args && redactKeys(JSON.stringify(args.cli_args), CLI_ARGS_REGEX, REDACTED);
196+
args.raw_args = args.raw_args && redactKeys(JSON.stringify(args.raw_args), RAW_ARGS_REGEX, REDACTED);
197+
192198
delete args.bstack_config;
193199

194200
let zipUploadDetails = {

test/unit/bin/helpers/usageReporting.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const cp = require("child_process"),
22
fs = require("fs");
3+
const { CLI_ARGS_REGEX, REDACTED } = require("../../../../bin/helpers/constants");
34

45
const chai = require("chai"),
56
expect = chai.expect,
@@ -9,7 +10,8 @@ const chai = require("chai"),
910
rewire = require("rewire");
1011

1112
const logger = require("../../../../bin/helpers/logger").winstonLogger,
12-
testObjects = require("../../support/fixtures/testObjects");
13+
testObjects = require("../../support/fixtures/testObjects"),
14+
constant = require('../../../../bin/helpers/constants');
1315

1416
const usageReporting = rewire("../../../../bin/helpers/usageReporting");
1517

@@ -27,6 +29,7 @@ bstack_json_found_in_pwd = usageReporting.__get__("bstack_json_found_in_pwd");
2729
cypress_json_found_in_pwd = usageReporting.__get__("cypress_json_found_in_pwd");
2830
npm_global_path = usageReporting.__get__("npm_global_path");
2931
cli_version_and_path = usageReporting.__get__("cli_version_and_path");
32+
redactKeys = usageReporting.__get__("redactKeys");
3033

3134
describe("usageReporting", () => {
3235
describe("_os", () => {
@@ -408,4 +411,77 @@ describe("usageReporting", () => {
408411
expect(ci_environment()).to.be.null;
409412
});
410413
});
414+
415+
describe("redactKeys", () => {
416+
it("filters username and access_key from bstack_config", () => {
417+
const bstack_config = { auth: { username: "test_123", access_key: "test_key" } };
418+
const sanitizedbsConfig = redactKeys(JSON.stringify(bstack_config), constant.AUTH_REGEX, constant.REDACTED_AUTH);
419+
expect(sanitizedbsConfig.includes("[REDACTED]")).to.be.true;
420+
expect(sanitizedbsConfig.includes("test_123")).to.be.false;
421+
expect(sanitizedbsConfig.includes("test_key")).to.be.false;
422+
});
423+
424+
it("filters keys from cli_args", () => {
425+
const cli_args = {
426+
_: [ 'generate-report', 'ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf' ],
427+
u: 'test_123',
428+
username: 'test_123',
429+
k: 'test_key',
430+
key: 'test_key',
431+
cf: 'browserstack.json',
432+
'config-file': 'browserstack.json',
433+
configFile: 'browserstack.json',
434+
'$0': 'browserstack-cypress'
435+
}
436+
const sanitizedCliArgs = redactKeys(JSON.stringify(cli_args), constant.CLI_ARGS_REGEX, constant.REDACTED);
437+
expect(sanitizedCliArgs.includes("[REDACTED]")).to.be.true;
438+
expect(sanitizedCliArgs.includes("test_123")).to.be.false;
439+
expect(sanitizedCliArgs.includes("test_key")).to.be.false;
440+
expect(sanitizedCliArgs).to.be.equal("{\"_\":[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\"],\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED],\"cf\":\"browserstack.json\",\"config-file\":\"browserstack.json\",\"configFile\":\"browserstack.json\",\"$0\":\"browserstack-cypress\"}");
441+
expect(redactKeys(JSON.stringify({
442+
u: 'test_123',
443+
username: 'test_123',
444+
k: 'test_key',
445+
key: 'test_key',
446+
cf: 'browserstack.json',
447+
'config-file': 'browserstack.json',
448+
configFile: 'browserstack.json',
449+
'$0': 'browserstack-cypress'
450+
}), CLI_ARGS_REGEX, REDACTED)).to.be.equal("{\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED],\"cf\":\"browserstack.json\",\"config-file\":\"browserstack.json\",\"configFile\":\"browserstack.json\",\"$0\":\"browserstack-cypress\"}");
451+
expect(redactKeys(JSON.stringify({
452+
u: 'test_123',
453+
username: 'test_123',
454+
k: 'test_key',
455+
key: 'test_key'
456+
}), CLI_ARGS_REGEX, REDACTED)).to.be.equal("{\"u\":[REDACTED],\"username\":[REDACTED],\"k\":[REDACTED],\"key\":[REDACTED]}");
457+
});
458+
459+
it("filters keys from raw_args", () => {
460+
const raw_args = [
461+
'generate-report',
462+
'ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf',
463+
'-u',
464+
'test_123',
465+
'-k',
466+
'test_key'
467+
]
468+
let sanitizedRawArgs = redactKeys(JSON.stringify(raw_args), constant.RAW_ARGS_REGEX, constant.REDACTED);
469+
expect(sanitizedRawArgs.includes("[REDACTED]")).to.be.true;
470+
expect(sanitizedRawArgs.includes("test_123")).to.be.false;
471+
expect(sanitizedRawArgs.includes("test_key")).to.be.false;
472+
expect(sanitizedRawArgs).to.be.equal("[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\",\"-u\",[REDACTED],\"-k\",[REDACTED]]");
473+
raw_args.push('-files', "test.txt");
474+
sanitizedRawArgs = redactKeys(JSON.stringify(raw_args), constant.RAW_ARGS_REGEX, constant.REDACTED);
475+
expect(sanitizedRawArgs.includes("[REDACTED]")).to.be.true;
476+
expect(sanitizedRawArgs.includes("test_123")).to.be.false;
477+
expect(sanitizedRawArgs.includes("test_key")).to.be.false;
478+
expect(sanitizedRawArgs).to.be.equal("[\"generate-report\",\"ceb31f07eb386706ae7ab52ebe5d9b2ebf2fdebf\",\"-u\",[REDACTED],\"-k\",[REDACTED],\"-files\",\"test.txt\"]");
479+
raw_args.shift(); raw_args.shift();
480+
sanitizedRawArgs = redactKeys(JSON.stringify(raw_args), constant.RAW_ARGS_REGEX, constant.REDACTED);
481+
expect(sanitizedRawArgs.includes("[REDACTED]")).to.be.true;
482+
expect(sanitizedRawArgs.includes("test_123")).to.be.false;
483+
expect(sanitizedRawArgs.includes("test_key")).to.be.false;
484+
expect(sanitizedRawArgs).to.be.equal("[\"-u\",[REDACTED],\"-k\",[REDACTED],\"-files\",\"test.txt\"]");
485+
});
486+
})
411487
});

0 commit comments

Comments
 (0)