Skip to content

Commit d3c3ab6

Browse files
SDK-6180 proxyCaCertificate: harden temp trust-anchor file (mkdtemp + 0600 + O_EXCL)
Security review flagged the predictable temp path used for NODE_EXTRA_CA_CERTS when converting a DER cert. Now write into a fresh owner-only dir (fs.mkdtempSync) and open the file with O_EXCL|O_NOFOLLOW at mode 0600, preventing a local pre-plant/symlink-race on the file the process trusts as a CA. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent fb6c617 commit d3c3ab6

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

bin/helpers/caCertHelper.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,17 @@ function setupCaCertificate(bsConfig) {
9292
if (!isPem) {
9393
const os = require('os');
9494
const path = require('path');
95-
nodeExtra = path.join(os.tmpdir(), `browserstack_sdk_ca_${process.pid}.pem`);
96-
fs.writeFileSync(nodeExtra, pemCerts.join(''));
95+
// Fresh owner-only temp dir (random name) + 0600 + O_EXCL/O_NOFOLLOW: a predictable
96+
// path in a world-writable tmpdir would let a local attacker pre-plant or symlink-race
97+
// the file the process is about to TRUST as a CA.
98+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'browserstack_sdk_ca_'));
99+
nodeExtra = path.join(tmpDir, 'ca.pem');
100+
const fd = fs.openSync(nodeExtra, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW || 0), 0o600);
101+
try {
102+
fs.writeFileSync(fd, pemCerts.join(''));
103+
} finally {
104+
fs.closeSync(fd);
105+
}
97106
}
98107
process.env.NODE_EXTRA_CA_CERTS = nodeExtra;
99108
}

0 commit comments

Comments
 (0)