Skip to content

Commit e7d01ac

Browse files
manish-bothraclaude
andcommitted
Parse the collector override as a URL and reject a query or fragment
Request urls are built as `${API_URL}/${path}`, so an override carrying a query or fragment would swallow the endpoint path and the upload would hit / instead. Parse with URL, require an https protocol and a hostname, reject search and hash, and trim a trailing slash so paths are not doubled. Jira: A11Y-13909 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0166a03 commit e7d01ac

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎bin/testObservability/helper/constants.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ exports.BATCH_INTERVAL = 2000;
66
const DEFAULT_API_URL = 'https://collector-observability.browserstack.com';
77

88
// 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.
9+
// on the wire in cleartext. Request urls are also built as `${base}/${path}`, so a query or fragment
10+
// would swallow the endpoint. Reject such values instead of quietly using the default collector.
1011
const httpsOverride = (name) => {
1112
const value = process.env[name];
1213
if (!value) return undefined;
13-
if (!value.startsWith('https://')) {
14+
let parsed;
15+
try {
16+
parsed = new URL(value);
17+
} catch (err) {
18+
throw new Error(`${name} must be a valid https:// URL, got "${value}"`);
19+
}
20+
if (parsed.protocol !== 'https:' || !parsed.hostname) {
1421
throw new Error(`${name} must be an https:// URL, got "${value}"`);
1522
}
16-
return value;
23+
if (parsed.search || parsed.hash) {
24+
throw new Error(`${name} must not contain a query or fragment, got "${value}"`);
25+
}
26+
return value.replace(/\/+$/, '');
1727
};
1828

1929
// Overridable so the remote reporter (own install of this package) can target the launching cli's collector

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,23 @@ describe('utils', () => {
10211021

10221022
expect(() => utils.setSystemEnvs(bsConfig)).to.throw(/must be an https:\/\/ URL/);
10231023
});
1024+
1025+
it('rejects a collector url carrying a query or fragment', () => {
1026+
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com/#staging';
1027+
let bsConfig = { run_settings: { env: null } };
1028+
1029+
expect(() => utils.setSystemEnvs(bsConfig)).to.throw(/must not contain a query or fragment/);
1030+
});
1031+
1032+
it('trims a trailing slash so endpoint paths are not doubled', () => {
1033+
process.env.BROWSERSTACK_TEST_OBSERVABILITY_REMOTE_API_URL = 'https://collector-public.bsstag.com/';
1034+
let bsConfig = { run_settings: { env: null } };
1035+
1036+
utils.setSystemEnvs(bsConfig);
1037+
expect(bsConfig.run_settings.system_env_vars)
1038+
.to.be.an('array')
1039+
.that.includes('BROWSERSTACK_TEST_OBSERVABILITY_API_URL=https://collector-public.bsstag.com');
1040+
});
10241041
});
10251042

10261043
describe('fixCommaSeparatedString', () => {

0 commit comments

Comments
 (0)