8000 fix(exec): lockdown file permissions · shelljs/shelljs@118539f · GitHub
[go: up one dir, main page]

Skip to content

Commit 118539f

Browse files
committed
fix(exec): lockdown file permissions
This locks down file permissions used by the internal implementation of `shell.exec()`. Issue #1058 Tested manually using the documented scenarios
1 parent d0a4516 commit 118539f

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/exec.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,24 @@ function execSync(cmd, opts, pipe) {
5858
stderrFile: stderrFile,
5959
};
6060

61-
fs.writeFileSync(paramsFile, JSON.stringify(paramsToSerialize), 'utf8');
61+
// Create the files and ensure these are locked down (for read and write) to
62+
// the current user. The main concerns here are:
63+
//
64+
// * If we execute a command which prints sensitive output, then
65+
// stdoutFile/stderrFile must not be readable by other users.
66+
// * paramsFile must not be readable by other users, or else they can read it
67+
// to figure out the path for stdoutFile/stderrFile and create these first
68+
// (locked down to their own access), which will crash exec() when it tries
69+
// to write to the files.
70+
function writeFileLockedDown(filePath, data) {
71+
fs.writeFileSync(filePath, data, {
72+
encoding: 'utf8',
73+
mode: parseInt('600', 8),
74+
});
75+
}
76+
writeFileLockedDown(stdoutFile, '');
77+
writeFileLockedDown(stderrFile, '');
78+
writeFileLockedDown(paramsFile, JSON.stringify(paramsToSerialize));
6279

6380
var execArgs = [
6481
path.join(__dirname, 'exec-child.js'),
@@ -101,6 +118,7 @@ function execSync(cmd, opts, pipe) {
101118
}
102119

103120
// No biggie if we can't erase the files now -- they're in a temp dir anyway
121+
// and we locked down permissions (see the note above).
104122
try { common.unlinkSync(paramsFile); } catch (e) {}
105123
try { common.unlinkSync(stderrFile); } catch (e) {}
106124
try { common.unlinkSync(stdoutFile); } catch (e) {}

0 commit comments

Comments
 (0)
0