Skip to content

Commit 1adf9ac

Browse files
Uses spawn instead of exec for opening browser
Replaces `exec` with `spawn` for opening URLs in the default browser to avoid potential command injection vulnerabilities and improve reliability. The previous implementation used `exec`, which could be vulnerable if the URL contained malicious characters. `spawn` offers better control over the process and avoids shell interpretation.
1 parent ead324b commit 1adf9ac

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/aps-auth.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function escapeHtml(s: string): string {
1919

2020
import { createServer } from "node:http";
2121
import type { IncomingMessage, ServerResponse } from "node:http";
22-
import { exec } from "node:child_process";
22+
import { spawn } from "node:child_process";
2323
import { homedir } from "node:os";
2424
import { join } from "node:path";
2525
import {
@@ -227,13 +227,23 @@ function deleteCacheFile(): void {
227227

228228
/** Open a URL in the user's default browser (cross‑platform). */
229229
function openBrowser(url: string): void {
230-
const cmd =
231-
process.platform === "win32"
232-
? `start "" "${url}"`
233-
: process.platform === "darwin"
234-
? `open "${url}"`
235-
: `xdg-open "${url}"`;
236-
exec(cmd);
230+
let program: string;
231+
let args: string[];
232+
233+
if (process.platform === "win32") {
234+
program = "cmd";
235+
args = ["/c", "start", "", url];
236+
} else if (process.platform === "darwin") {
237+
program = "open";
238+
args = [url];
239+
} else {
240+
program = "xdg-open";
241+
args = [url];
242+
}
243+
244+
const child = spawn(program, args, { stdio: "ignore" });
245+
child.on("error", () => { /* ignore – best‑effort */ });
246+
child.unref();
237247
}
238248

239249
/** In‑memory cache so we don't re‑read the file every call. */

0 commit comments

Comments
 (0)