Skip to content

Commit 8aef754

Browse files
fix: replace shell exec with execFile to prevent command injection in macOS app termination
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 60870b7 commit 8aef754

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/common/node/childProcess.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,37 @@ export class ChildProcess {
8888
return stdout.toString();
8989
}
9090

91+
public async execFileToString(
92+
command: string,
93+
args: string[] = [],
94+
options: IExecOptions = {},
95+
): Promise<string> {
96+
return new Promise<string>((resolve, reject) => {
97+
this.childProcess.execFile(
98+
command,
99+
args,
100+
options,
101+
(
102+
error: nodeChildProcess.ExecException | null,
103+
stdout: string | Buffer,
104+
stderr: string | Buffer,
105+
) => {
106+
if (error) {
107+
reject(
108+
ErrorHelper.getNestedError(
109+
error,
110+
InternalErrorCode.CommandFailed,
111+
command,
112+
),
113+
);
114+
} else {
115+
resolve(stdout.toString());
116+
}
117+
},
118+
);
119+
});
120+
}
121+
91122
public execFileSync(
92123
command: string,
93124
args: string[] = [],

src/extension/macos/macOSPlatform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class MacOSPlatform extends GeneralPlatform {
208208
// 40959 ?? 0:10.36 /Users/user/.nvm/versions/node/v10.19.0/bin/node /Users/user/Documents/rn_for_mac_proj/node_modules/metro/node_modules/jest-worker/build/workers/processChild.js
209209
// 41004 ?? 0:21.34 /Users/user/Library/Developer/Xcode/DerivedData/rn_for_mac_proj-ghuavabiztosiqfqkrityjoxqfmv/Build/Products/Debug/rn_for_mac_proj.app/Contents/MacOS/rn_for_mac_proj
210210
// 75514 ttys007 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rn_for_mac_proj
211-
const searchResults = await childProcess.execToString(`ps -ax | grep ${appName}`);
211+
const searchResults = await childProcess.execFileToString("ps", ["-ax"]);
212212
if (searchResults) {
213213
const processIdRgx = /(^\d*)\s\?\?/g;
214214
// We are looking for a process whose path contains the "appName.app" part
@@ -217,7 +217,7 @@ export class MacOSPlatform extends GeneralPlatform {
217217
if (processData) {
218218
const match = processIdRgx.exec(processData.trim());
219219
if (match && match[1]) {
220-
await childProcess.execToString(`kill ${match[1]}`);
220+
await childProcess.execFileToString("kill", [match[1]]);
221221
}
222222
}
223223
}

0 commit comments

Comments
 (0)