Skip to content

Commit 0166a03

Browse files
manish-bothraclaude
andcommitted
Require https for the collector overrides and isolate the env vars in tests
Review feedback on #1210. Collector requests carry BS_TESTOPS_JWT, so an http:// override would send it in cleartext: reject such a value instead of falling back to the default collector. The assignment moves out of the try block in setSystemEnvs because that block swallows errors, which would have turned the rejection into a silent fallback. Tests now clear both URL variables before each case and restore the originals after. Jira: A11Y-13909 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent bf87243 commit 0166a03

3 files changed

Lines changed: 57 additions & 22 deletions

File tree

‎bin/helpers/utils.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,12 +610,14 @@ exports.setSystemEnvs = (bsConfig) => {
610610
logger.error(`Error in adding accessibility configs ${error}`)
611611
}
612612

613+
// Remote reporter must post to this build's collector, not its package default. Kept out of the
614+
// try below, which swallows errors: a rejected override must fail rather than fall back silently.
615+
envKeys['BROWSERSTACK_TEST_OBSERVABILITY_API_URL'] = remoteReporterApiUrl();
616+
613617
try {
614618
OBSERVABILITY_ENV_VARS.forEach(key => {
615619
envKeys[key] = process.env[key];
616620
});
617-
// Remote reporter must post to this build's collector, not its package default
618-
envKeys['BROWSERSTACK_TEST_OBSERVABILITY_API_URL'] = remoteReporterApiUrl();
619621

620622
let gitConfigPath = o11yHelpers.findGitConfig(process.cwd());
621623
if(!o11yHelpers.isBrowserstackInfra()) process.env.OBSERVABILITY_GIT_CONFIG_PATH_LOCAL = gitConfigPath;

‎bin/testObservability/helper/constants.js‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ exports.consoleHolder = Object.assign({},console);
44
exports.BATCH_SIZE = 1000;
55
exports.BATCH_INTERVAL = 2000;
66
const DEFAULT_API_URL = 'https://collector-observability.browserstack.com';
7+
8+
// Requests to the collector carry BS_TESTOPS_JWT, so an http:// override would put the build's token
9+
// on the wire in cleartext. Reject it instead of quietly using the default collector.
10+
const httpsOverride = (name) => {
11+
const value = process.env[name];
12+
if (!value) return undefined;
13+
if (!value.startsWith('https://')) {
14+
throw new Error(`${name} must be an https:// URL, got "${value}"`);
15+
}
16+
return value;
17+
};
18+
719
// Overridable so the remote reporter (own install of this package) can target the launching cli's collector
8-
exports.API_URL = process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL || DEFAULT_API_URL;
20+
exports.API_URL = httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_API_URL') || DEFAULT_API_URL;
921
// Collector handed to the reporter on the remote machine; separate override for when it cannot reach API_URL
10-
exports.remoteReporterApiUrl = () => process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL
11-
|| process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL || DEFAULT_API_URL;
22+
exports.remoteReporterApiUrl = () => httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL')
23+
|| httpsOverride('BROWSERSTACK_TEST_OBSERVABILITY_API_URL') || DEFAULT_API_URL;
1224

1325
exports.IPC_EVENTS = {
1426
LOG: 'testObservability:cypressLog',

‎test/unit/bin/helpers/utils.js‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,29 @@ describe('utils', () => {
962962
delete process.env.cypress_test_2;
963963
});
964964

965+
const URL_ENV_VARS = [
966+
'BROWSERSTACK_TEST_OBSERVABILITY_API_URL',
967+
'BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL',
968+
];
969+
let savedUrlEnvs = {};
970+
971+
beforeEach(() => {
972+
URL_ENV_VARS.forEach((key) => {
973+
savedUrlEnvs[key] = process.env[key];
974+
delete process.env[key];
975+
});
976+
});
977+
978+
afterEach(() => {
979+
URL_ENV_VARS.forEach((key) => {
980+
if (savedUrlEnvs[key] === undefined) {
981+
delete process.env[key];
982+
} else {
983+
process.env[key] = savedUrlEnvs[key];
984+
}
985+
});
986+
});
987+
965988
it('forwards the observability collector url for the remote reporter', () => {
966989
let bsConfig = { run_settings: { env: null } };
967990

@@ -975,30 +998,28 @@ describe('utils', () => {
975998
process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL = 'https://collector-example.bsstag.com';
976999
let bsConfig = { run_settings: { env: null } };
9771000

978-
try {
979-
utils.setSystemEnvs(bsConfig);
980-
expect(bsConfig.run_settings.system_env_vars)
981-
.to.be.an('array')
982-
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-example.bsstag.com');
983-
} finally {
984-
delete process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL;
985-
}
1001+
utils.setSystemEnvs(bsConfig);
1002+
expect(bsConfig.run_settings.system_env_vars)
1003+
.to.be.an('array')
1004+
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-example.bsstag.com');
9861005
});
9871006

9881007
it('prefers a separate remote collector url for the remote reporter', () => {
9891008
process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL = 'https://collector-private.bsstag.com';
9901009
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com';
9911010
let bsConfig = { run_settings: { env: null } };
9921011

993-
try {
994-
utils.setSystemEnvs(bsConfig);
995-
expect(bsConfig.run_settings.system_env_vars)
996-
.to.be.an('array')
997-
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-public.bsstag.com');
998-
} finally {
999-
delete process.env.BROWSERSTACK_TEST_OBSERVABILITY_API_URL;
1000-
delete process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL;
1001-
}
1012+
utils.setSystemEnvs(bsConfig);
1013+
expect(bsConfig.run_settings.system_env_vars)
1014+
.to.be.an('array')
1015+
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-public.bsstag.com');
1016+
});
1017+
1018+
it('rejects a collector url that is not https', () => {
1019+
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'http://collector-public.bsstag.com';
1020+
let bsConfig = { run_settings: { env: null } };
1021+
1022+
expect(() => utils.setSystemEnvs(bsConfig)).to.throw(/must be an https:\/\/ URL/);
10021023
});
10031024
});
10041025

0 commit comments

Comments
 (0)