Skip to content

Commit 5047bf3

Browse files
committed
add test cases for redact
1 parent 6b6b49b commit 5047bf3

File tree

2 files changed

+85
-5
lines changed

2 files changed

+85
-5
lines changed

bin/helpers/usageReporting.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -186,10 +190,10 @@ function send(args) {
186190
data.cypress_version = bsConfig.run_settings.cypress_version;
187191
}
188192

189-
sanitizedbsConfig = `${(typeof bsConfig === 'string') ? bsConfig :
190-
JSON.stringify(bsConfig)}`.replace(AUTH_REGEX, REDACTED_AUTH);
191-
args.cli_args = args.cli_args && JSON.stringify(args.cli_args).replace(CLI_ARGS_REGEX, REDACTED);
192-
args.raw_args = args.raw_args && JSON.stringify(args.raw_args).replace(RAW_ARGS_REGEX, REDACTED);
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);
193197

194198
delete args.bstack_config;
195199

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)